Open History to see the last commit
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 /** Past the first hunk header every line is content, and content can start like a header. */
12 inBody: boolean;
13}
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;
25}
26
27/**
28 * Git ends the name with a tab when it holds a space, and a name with a tab of its own
29 * arrives quoted with that tab escaped, so the first literal tab is never part of what the
30 * file is called.
31 */
32function sidePath(rest: string, prefix: string): string | null {
33 const tab = rest.indexOf("\t");
34 const token = tab === -1 ? rest : rest.slice(0, tab);
35 if (token === "/dev/null") return null;
36 const path = unquoteGitPatchPath(token);
37 return path.startsWith(prefix) ? path.slice(prefix.length) : path;
38}
39
40function headerSide(token: string, prefix: string): string | null {
41 const path = unquoteGitPatchPath(token);
42 return path.startsWith(prefix) ? path.slice(prefix.length) : null;
43}
44
45/**
46 * `a/one two b/one two` can split in more than one place, so the split leaving both sides
47 * equal wins; a rename (the only case where sides differ) states its names on separate lines
48 * instead.
49 */
50function headerPaths(rest: string): readonly [string | null, string | null] {
51 if (rest.startsWith(QUOTE)) {
52 const end = quotedEnd(rest);
53 if (end === -1 || rest.charAt(end + 1) !== " ") return [null, null];
54 return [headerSide(rest.slice(0, end + 1), "a/"), headerSide(rest.slice(end + 2), "b/")];
55 }
56 if (rest.endsWith(QUOTE)) {
57 const opens = rest.indexOf(QUOTE);
58 if (opens < 1 || rest.charAt(opens - 1) !== " ") return [null, null];
59 return [headerSide(rest.slice(0, opens - 1), "a/"), headerSide(rest.slice(opens), "b/")];
60 }
61 if (!rest.startsWith("a/")) return [null, null];
62 const splits: Array<number> = [];
63 for (let at = rest.indexOf(" b/"); at !== -1; at = rest.indexOf(" b/", at + 1)) splits.push(at);
64 const chosen =
65 splits.find((at) => rest.slice(2, at) === rest.slice(at + 3)) ??
66 (splits.length === 1 ? splits[0] : undefined);
67 return chosen === undefined ? [null, null] : [rest.slice(2, chosen), rest.slice(chosen + 3)];
68}
69
70function headRevision(rest: string): string | null {
71 const gap = rest.indexOf("..");
72 if (gap === -1) return null;
73 const after = rest.slice(gap + 2);
74 const end = after.indexOf(" ");
75 const head = end === -1 ? after : after.slice(0, end);
76 return head.length === 0 ? null : head;
77}
78
79function parseEntries(patch: string): ReadonlyArray<Omit<Entry, "inBody">> {
80 const entries: Array<Omit<Entry, "inBody">> = [];
81 let entry: Entry | null = null;
82
83 const close = () => {
84 if (entry === null) return;
85 const { inBody: _inBody, ...closed } = entry;
86 entries.push(closed);
87 entry = null;
88 };
89
90 for (const line of patch.split("\n")) {
91 if (line.startsWith(ENTRY)) {
92 close();
93 const [oldPath, newPath] = headerPaths(line.slice(ENTRY.length));
94 entry = { oldPath, newPath, deleted: false, revision: null, inBody: false };
95 continue;
96 }
97 if (entry === null || entry.inBody) continue;
98 if (line.startsWith("@@")) {
99 entry.inBody = true;
100 } else if (line.startsWith("index ")) {
101 entry.revision = headRevision(line.slice("index ".length));
102 } else if (line.startsWith("deleted file mode")) {
103 entry.deleted = true;
104 } else if (line.startsWith("rename from ")) {
105 entry.oldPath = unquoteGitPatchPath(line.slice("rename from ".length));
106 } else if (line.startsWith("rename to ")) {
107 entry.newPath = unquoteGitPatchPath(line.slice("rename to ".length));
108 } else if (line.startsWith("--- ")) {
109 entry.oldPath = sidePath(line.slice(4), "a/");
110 } else if (line.startsWith("+++ ")) {
111 const side = sidePath(line.slice(4), "b/");
112 entry.newPath = side;
113 if (side === null) entry.deleted = true;
114 }
115 }
116 close();
117 return entries;
118}
119
120/**
121 * What the head has of each file, as the blob ids from a unified patch's `index <before>..<after>`
122 * line: Bitbucket exposes no blob id for a file anywhere else. Keyed by the head's name, except
123 * for a deletion where only the old name exists. An entry with no `index` line (most often one
124 * Bitbucket excluded by pattern) is left out.
125 */
126export function parseDiffFileRevisions(patch: string): ReadonlyMap<string, string> {
127 const revisions = new Map<string, string>();
128 for (const entry of parseEntries(patch)) {
129 const path = entry.deleted ? entry.oldPath : (entry.newPath ?? entry.oldPath);
130 if (path !== null && path.length > 0 && entry.revision !== null) {
131 revisions.set(path, entry.revision);
132 }
133 }
134 return revisions;
135}
136
137/**
138 * The files a unified patch changes, as the name each side has. A side with no file (`/dev/null`
139 * for an added or a deleted file) is null; a rename is the only entry whose two names differ.
140 */
141export function parseDiffFilePaths(
142 patch: string,
143): ReadonlyArray<{ readonly oldPath: string | null; readonly newPath: string | null }> {
144 return parseEntries(patch).flatMap(({ oldPath, newPath }) =>
145 (oldPath ?? newPath) === null ? [] : [{ oldPath, newPath }],
146 );
147}