Open History to see the last commit
1const ESCAPE_BY_CHARACTER = new Map([
2 ['"', '\\"'],
3 ["\\", "\\\\"],
4 ["\u0007", "\\a"],
5 ["\b", "\\b"],
6 ["\t", "\\t"],
7 ["\n", "\\n"],
8 ["\v", "\\v"],
9 ["\f", "\\f"],
10 ["\r", "\\r"],
11]);
12
13const CHARACTER_BY_ESCAPE: Record<string, number> = {
14 '"': 0x22,
15 "\\": 0x5c,
16 a: 0x07,
17 b: 0x08,
18 f: 0x0c,
19 n: 0x0a,
20 r: 0x0d,
21 t: 0x09,
22 v: 0x0b,
23};
24
25const QUOTE = '"';
26const DELETE_CHARACTER = 0x7f;
27const LOWEST_PRINTABLE = 0x20;
28const BACKSLASH = 0x5c;
29
30const utf8 = new TextEncoder();
31// A leading U+FEFF is part of the filename, not an encoding marker.
32const fromUtf8 = new TextDecoder("utf-8", { ignoreBOM: true });
33
34/**
35 * A header side's `a/` or `b/` belongs inside the quoting, so pass it in along with the name:
36 * what git quotes is the whole token a reader takes off the line, side letter and all.
37 */
38export function quoteGitPatchPath(path: string): string {
39 let body = "";
40 let quoting = false;
41 for (const character of path) {
42 const escape = ESCAPE_BY_CHARACTER.get(character);
43 if (escape !== undefined) {
44 body += escape;
45 quoting = true;
46 continue;
47 }
48 const code = character.codePointAt(0) ?? 0;
49 if (code < LOWEST_PRINTABLE || code === DELETE_CHARACTER) {
50 body += `\\${code.toString(8).padStart(3, "0")}`;
51 quoting = true;
52 continue;
53 }
54 body += character;
55 }
56 return quoting ? `${QUOTE}${body}${QUOTE}` : path;
57}
58
59/**
60 * The escapes are per byte, so a name in another alphabet arrives as a run of octal and
61 * only reads back as itself once those bytes are rejoined and decoded together.
62 */
63function unescapeBody(body: string): string {
64 if (!body.includes("\\")) return body;
65 const bytes: Array<number> = [];
66 // Anything left as itself is encoded a run at a time rather than a unit at a time, so a
67 // character outside the basic plane keeps its pair together rather than coming back as halves.
68 let literal = "";
69 const flush = () => {
70 if (literal.length === 0) return;
71 bytes.push(...utf8.encode(literal));
72 literal = "";
73 };
74 let at = 0;
75 while (at < body.length) {
76 const character = body.charAt(at);
77 if (character !== "\\") {
78 literal += character;
79 at += 1;
80 continue;
81 }
82 const escaped = body.charAt(at + 1);
83 if (escaped === "") {
84 flush();
85 bytes.push(BACKSLASH);
86 break;
87 }
88 const named = CHARACTER_BY_ESCAPE[escaped];
89 if (named !== undefined) {
90 flush();
91 bytes.push(named);
92 at += 2;
93 continue;
94 }
95 const octal = body.slice(at + 1, at + 4);
96 if (/^[0-7]{3}$/.test(octal)) {
97 flush();
98 bytes.push(Number.parseInt(octal, 8));
99 at += 4;
100 continue;
101 }
102 literal += escaped;
103 at += 2;
104 }
105 flush();
106 return fromUtf8.decode(new Uint8Array(bytes));
107}
108
109/**
110 * The escapes are undone whether the quotes are still there or not, because patch parsers disagree
111 * about how much of the quoting they hand back: the one the clients read diffs with takes the
112 * quotes off the `diff --git` line's names and leaves them on a rename's.
113 */
114export function unquoteGitPatchPath(token: string): string {
115 if (token.length >= 2 && token.startsWith(QUOTE) && token.endsWith(QUOTE)) {
116 return unescapeBody(token.slice(1, -1));
117 }
118 return unescapeBody(token);
119}