@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 165 lines 6 KB
Raw
1 import { cutLineNote } from "@t3tools/shared/diffCutLines";
2 import { unquoteGitPatchPath } from "@t3tools/shared/gitPatchPath";
3
4 const ENTRY = "diff --git ";
5 const QUOTE = '"';
6
7 interface Entry {
8 oldPath: string | null;
9 newPath: string | null;
10 deleted: boolean;
11 revision: string | null;
12 /** After the first hunk, content can resemble file headers. */
13 inBody: boolean;
14 }
15
16 function quotedEnd(rest: string): number {
17 for (let at = 1; at < rest.length; at += 1) {
18 const char = rest.charAt(at);
19 if (char === "\\") {
20 at += 1;
21 continue;
22 }
23 if (char === QUOTE) return at;
24 }
25 return -1;
26 }
27
28 /** Git terminates names with a literal tab and escapes tabs within names. */
29 function sidePath(rest: string, prefix: string): string | null {
30 const tab = rest.indexOf("\t");
31 const token = tab === -1 ? rest : rest.slice(0, tab);
32 if (token === "/dev/null") return null;
33 const path = unquoteGitPatchPath(token);
34 return path.startsWith(prefix) ? path.slice(prefix.length) : path;
35 }
36
37 function headerSide(token: string, prefix: string): string | null {
38 const path = unquoteGitPatchPath(token);
39 return path.startsWith(prefix) ? path.slice(prefix.length) : null;
40 }
41
42 /** Match equal sides for ambiguous unquoted paths; renames state paths separately. */
43 function headerPaths(rest: string): readonly [string | null, string | null] {
44 if (rest.startsWith(QUOTE)) {
45 const end = quotedEnd(rest);
46 if (end === -1 || rest.charAt(end + 1) !== " ") return [null, null];
47 return [headerSide(rest.slice(0, end + 1), "a/"), headerSide(rest.slice(end + 2), "b/")];
48 }
49 if (rest.endsWith(QUOTE)) {
50 const opens = rest.indexOf(QUOTE);
51 if (opens < 1 || rest.charAt(opens - 1) !== " ") return [null, null];
52 return [headerSide(rest.slice(0, opens - 1), "a/"), headerSide(rest.slice(opens), "b/")];
53 }
54 if (!rest.startsWith("a/")) return [null, null];
55 const splits: Array<number> = [];
56 for (let at = rest.indexOf(" b/"); at !== -1; at = rest.indexOf(" b/", at + 1)) splits.push(at);
57 const chosen =
58 splits.find((at) => rest.slice(2, at) === rest.slice(at + 3)) ??
59 (splits.length === 1 ? splits[0] : undefined);
60 return chosen === undefined ? [null, null] : [rest.slice(2, chosen), rest.slice(chosen + 3)];
61 }
62
63 function headRevision(rest: string): string | null {
64 const gap = rest.indexOf("..");
65 if (gap === -1) return null;
66 const after = rest.slice(gap + 2);
67 const end = after.indexOf(" ");
68 const head = end === -1 ? after : after.slice(0, end);
69 return head.length === 0 ? null : head;
70 }
71
72 function parseEntries(patch: string): ReadonlyArray<Omit<Entry, "inBody">> {
73 const entries: Array<Omit<Entry, "inBody">> = [];
74 let entry: Entry | null = null;
75
76 const close = () => {
77 if (entry === null) return;
78 const { inBody: _inBody, ...closed } = entry;
79 entries.push(closed);
80 entry = null;
81 };
82
83 for (const line of patch.split("\n")) {
84 if (line.startsWith(ENTRY)) {
85 close();
86 const [oldPath, newPath] = headerPaths(line.slice(ENTRY.length));
87 entry = { oldPath, newPath, deleted: false, revision: null, inBody: false };
88 continue;
89 }
90 if (entry === null || entry.inBody) continue;
91 if (line.startsWith("@@")) {
92 entry.inBody = true;
93 } else if (line.startsWith("index ")) {
94 entry.revision = headRevision(line.slice("index ".length));
95 } else if (line.startsWith("deleted file mode")) {
96 entry.deleted = true;
97 } else if (line.startsWith("rename from ")) {
98 entry.oldPath = unquoteGitPatchPath(line.slice("rename from ".length));
99 } else if (line.startsWith("rename to ")) {
100 entry.newPath = unquoteGitPatchPath(line.slice("rename to ".length));
101 } else if (line.startsWith("--- ")) {
102 entry.oldPath = sidePath(line.slice(4), "a/");
103 } else if (line.startsWith("+++ ")) {
104 const side = sidePath(line.slice(4), "b/");
105 entry.newPath = side;
106 if (side === null) entry.deleted = true;
107 }
108 }
109 close();
110 return entries;
111 }
112
113 /** Head blob IDs by head path (old path for deletions); omit files without index lines. */
114 export function parseDiffFileRevisions(patch: string): ReadonlyMap<string, string> {
115 const revisions = new Map<string, string>();
116 for (const entry of parseEntries(patch)) {
117 const path = entry.deleted ? entry.oldPath : (entry.newPath ?? entry.oldPath);
118 if (path !== null && path.length > 0 && entry.revision !== null) {
119 revisions.set(path, entry.revision);
120 }
121 }
122 return revisions;
123 }
124
125 export function parseDiffFilePaths(
126 patch: string,
127 ): ReadonlyArray<{ readonly oldPath: string | null; readonly newPath: string | null }> {
128 return parseEntries(patch).flatMap(({ oldPath, newPath }) =>
129 (oldPath ?? newPath) === null ? [] : [{ oldPath, newPath }],
130 );
131 }
132
133 function nextEntry(patch: string, from: number): number {
134 const at = patch.indexOf(`\n${ENTRY}`, from);
135 return at === -1 ? -1 : at + 1;
136 }
137
138 /** The one file's section of a multi-file patch, or null when no section names `path`. */
139 export function diffFileSection(patch: string, path: string): string | null {
140 let start = patch.startsWith(ENTRY) ? 0 : nextEntry(patch, 0);
141 while (start !== -1) {
142 const end = nextEntry(patch, start);
143 const section = end === -1 ? patch.slice(start) : patch.slice(start, end);
144 // Only the header names the file; hunks can be megabytes.
145 const hunk = section.indexOf("\n@@");
146 const [entry] = parseEntries(hunk === -1 ? section : section.slice(0, hunk));
147 if (entry !== undefined && (entry.newPath ?? entry.oldPath) === path) return section;
148 start = end;
149 }
150 return null;
151 }
152
153 /** Cuts hunk lines past `maxLength` and says how much was cut; headers stay whole. */
154 export function truncateLongDiffLines(patch: string, maxLength: number): string {
155 let inBody = false;
156 return patch
157 .split("\n")
158 .map((line) => {
159 if (line.startsWith(ENTRY)) inBody = false;
160 else if (line.startsWith("@@")) inBody = true;
161 if (!inBody || line.length <= maxLength + 1) return line;
162 return `${line.slice(0, maxLength + 1)}${cutLineNote(line.length - 1 - maxLength)}`;
163 })
164 .join("\n");
165 }