Elffuss BraidHub

kikocis/continue public

main · 2.1 KiBRawPermalink
1import crypto from "crypto";
2import { IndexTag } from "..";
3
4// Maximum length for table names to stay under OS filename limits
5const MAX_TABLE_NAME_LENGTH = 240;
6
7// Leave room for branch and artifactId
8const MAX_DIR_LENGTH = 200;
9
10/**
11 * Converts an IndexTag to a string representation, safely handling long paths.
12 *
13 * The string is used as a table name and identifier in various places, so it needs
14 * to stay under OS filename length limits (typically 255 chars). This is especially
15 * important for dev containers where the directory path can be very long due to
16 * containing container configuration.
17 *
18 * The format is: "{directory}::{branch}::{artifactId}"
19 *
20 * To handle long paths:
21 * 1. First tries the full string - most backwards compatible
22 * 2. If too long, truncates directory from the beginning and adds a hash prefix
23 * to ensure uniqueness while preserving the more readable end parts
24 * 3. Finally ensures entire string stays under MAX_TABLE_NAME_LENGTH for OS compatibility
25 *
26 * @param tag The tag containing directory, branch, and artifactId
27 * @returns A string representation safe for use as a table name
28 */
29export function tagToString(tag: IndexTag): string {
30 const result = `${tag.directory}::${tag.branch}::${tag.artifactId}`;
31
32 if (result.length <= MAX_TABLE_NAME_LENGTH) {
33 return result;
34 }
35
36 // Create a hash of the full directory path to ensure uniqueness
37 const dirHash = crypto
38 .createHash("md5")
39 .update(tag.directory)
40 .digest("hex")
41 .slice(0, 8);
42
43 // Calculate how much space we have for the directory after accounting for hash, separators, branch, and artifactId
44 const nonDirLength = `${dirHash}_::${tag.branch}::${tag.artifactId}`.length;
45 const maxDirForTruncated = MAX_TABLE_NAME_LENGTH - nonDirLength;
46
47 // Truncate from the beginning of directory path to preserve the more unique end parts
48 const truncatedDir =
49 tag.directory.length > maxDirForTruncated
50 ? tag.directory.slice(tag.directory.length - maxDirForTruncated)
51 : tag.directory;
52
53 return `${dirHash}_${truncatedDir}::${tag.branch}::${tag.artifactId}`;
54}