Skip to content
Source
Sign inSign up
Open History to see the last commit

diffFileRevisions.ts

130 lines4.6 KB

1import { unquoteGitPatchPath } from "@t3tools/shared/gitPatchPath";
2​
3const ENTRY = "diff --git ";
4const QUOTE = '"';
5​
6interface Entry {
7 oldPath: string | null;
8 newPath: string | null;
9 deleted: boolean;
10 revision: string | null;
11 /** After the first hunk, content can resemble file headers. */
12 inBody: boolean;
14​
15function quotedEnd(rest: string): number {
16 for (let at = 1; at < rest.length; at += 1) {
17 const char = rest.charAt(at);
18 if (char === "\\") {
19 at += 1;
20 continue;
21 }
22 if (char === QUOTE) return at;
23 }
24 return -1;
26​
27/** Git terminates names with a literal tab and escapes tabs within names. */
28function sidePath(rest: string, prefix: string): string | null {
29 const tab = rest.indexOf("\t");
30 const token = tab === -1 ? rest : rest.slice(0, tab);
31 if (token === "/dev/null") return null;
32 const path = unquoteGitPatchPath(token);
33 return path.startsWith(prefix) ? path.slice(prefix.length) : path;
35​
36function headerSide(token: string, prefix: string): string | null {
37 const path = unquoteGitPatchPath(token);
38 return path.startsWith(prefix) ? path.slice(prefix.length) : null;
40​
41/** Match equal sides for ambiguous unquoted paths; renames state paths separately. */
42function headerPaths(rest: string): readonly [string | null, string | null] {
43 if (rest.startsWith(QUOTE)) {
44 const end = quotedEnd(rest);
45 if (end === -1 || rest.charAt(end + 1) !== " ") return [null, null];
46 return [headerSide(rest.slice(0, end + 1), "a/"), headerSide(rest.slice(end + 2), "b/")];
47 }
48 if (rest.endsWith(QUOTE)) {
49 const opens = rest.indexOf(QUOTE);
50 if (opens < 1 || rest.charAt(opens - 1) !== " ") return [null, null];
51 return [headerSide(rest.slice(0, opens - 1), "a/"), headerSide(rest.slice(opens), "b/")];
52 }
53 if (!rest.startsWith("a/")) return [null, null];
54 const splits: Array<number> = [];
55 for (let at = rest.indexOf(" b/"); at !== -1; at = rest.indexOf(" b/", at + 1)) splits.push(at);
56 const chosen =
57 splits.find((at) => rest.slice(2, at) === rest.slice(at + 3)) ??
58 (splits.length === 1 ? splits[0] : undefined);
59 return chosen === undefined ? [null, null] : [rest.slice(2, chosen), rest.slice(chosen + 3)];
61​
62function headRevision(rest: string): string | null {
63 const gap = rest.indexOf("..");
64 if (gap === -1) return null;
65 const after = rest.slice(gap + 2);
66 const end = after.indexOf(" ");
67 const head = end === -1 ? after : after.slice(0, end);
68 return head.length === 0 ? null : head;
70​
71function parseEntries(patch: string): ReadonlyArray<Omit<Entry, "inBody">> {
72 const entries: Array<Omit<Entry, "inBody">> = [];
73 let entry: Entry | null = null;
74​
75 const close = () => {
76 if (entry === null) return;
77 const { inBody: _inBody, ...closed } = entry;
78 entries.push(closed);
79 entry = null;
80 };
81​
82 for (const line of patch.split("\n")) {
83 if (line.startsWith(ENTRY)) {
84 close();
85 const [oldPath, newPath] = headerPaths(line.slice(ENTRY.length));
86 entry = { oldPath, newPath, deleted: false, revision: null, inBody: false };
87 continue;
88 }
89 if (entry === null || entry.inBody) continue;
90 if (line.startsWith("@@")) {
91 entry.inBody = true;
92 } else if (line.startsWith("index ")) {
93 entry.revision = headRevision(line.slice("index ".length));
94 } else if (line.startsWith("deleted file mode")) {
95 entry.deleted = true;
96 } else if (line.startsWith("rename from ")) {
97 entry.oldPath = unquoteGitPatchPath(line.slice("rename from ".length));
98 } else if (line.startsWith("rename to ")) {
99 entry.newPath = unquoteGitPatchPath(line.slice("rename to ".length));
100 } else if (line.startsWith("--- ")) {
101 entry.oldPath = sidePath(line.slice(4), "a/");
102 } else if (line.startsWith("+++ ")) {
103 const side = sidePath(line.slice(4), "b/");
104 entry.newPath = side;
105 if (side === null) entry.deleted = true;
106 }
107 }
108 close();
109 return entries;
111​
112/** Head blob IDs by head path (old path for deletions); omit files without index lines. */
113export function parseDiffFileRevisions(patch: string): ReadonlyMap<string, string> {
114 const revisions = new Map<string, string>();
115 for (const entry of parseEntries(patch)) {
116 const path = entry.deleted ? entry.oldPath : (entry.newPath ?? entry.oldPath);
117 if (path !== null && path.length > 0 && entry.revision !== null) {
118 revisions.set(path, entry.revision);
119 }
120 }
121 return revisions;
123​
124export function parseDiffFilePaths(
125 patch: string,
126): ReadonlyArray<{ readonly oldPath: string | null; readonly newPath: string | null }> {
127 return parseEntries(patch).flatMap(({ oldPath, newPath }) =>
128 (oldPath ?? newPath) === null ? [] : [{ oldPath, newPath }],
129 );