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

gitPatchPath.ts

118 lines3.1 KB

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();
31const fromUtf8 = new TextDecoder();
32​
33/**
34 * A header side's `a/` or `b/` belongs inside the quoting, so pass it in along with the name:
35 * what git quotes is the whole token a reader takes off the line, side letter and all.
36 */
37export function quoteGitPatchPath(path: string): string {
38 let body = "";
39 let quoting = false;
40 for (const character of path) {
41 const escape = ESCAPE_BY_CHARACTER.get(character);
42 if (escape !== undefined) {
43 body += escape;
44 quoting = true;
45 continue;
46 }
47 const code = character.codePointAt(0) ?? 0;
48 if (code < LOWEST_PRINTABLE || code === DELETE_CHARACTER) {
49 body += `\\${code.toString(8).padStart(3, "0")}`;
50 quoting = true;
51 continue;
52 }
53 body += character;
54 }
55 return quoting ? `${QUOTE}${body}${QUOTE}` : path;
57​
58/**
59 * The escapes are per byte, so a name in another alphabet arrives as a run of octal and
60 * only reads back as itself once those bytes are rejoined and decoded together.
61 */
62function unescapeBody(body: string): string {
63 if (!body.includes("\\")) return body;
64 const bytes: Array<number> = [];
65 // Anything left as itself is encoded a run at a time rather than a unit at a time, so a
66 // character outside the basic plane keeps its pair together rather than coming back as halves.
67 let literal = "";
68 const flush = () => {
69 if (literal.length === 0) return;
70 bytes.push(...utf8.encode(literal));
71 literal = "";
72 };
73 let at = 0;
74 while (at < body.length) {
75 const character = body.charAt(at);
76 if (character !== "\\") {
77 literal += character;
78 at += 1;
79 continue;
80 }
81 const escaped = body.charAt(at + 1);
82 if (escaped === "") {
83 flush();
84 bytes.push(BACKSLASH);
85 break;
86 }
87 const named = CHARACTER_BY_ESCAPE[escaped];
88 if (named !== undefined) {
89 flush();
90 bytes.push(named);
91 at += 2;
92 continue;
93 }
94 const octal = body.slice(at + 1, at + 4);
95 if (/^[0-7]{3}$/.test(octal)) {
96 flush();
97 bytes.push(Number.parseInt(octal, 8));
98 at += 4;
99 continue;
100 }
101 literal += escaped;
102 at += 2;
103 }
104 flush();
105 return fromUtf8.decode(new Uint8Array(bytes));
107​
108/**
109 * The escapes are undone whether the quotes are still there or not, because patch parsers disagree
110 * about how much of the quoting they hand back: the one the clients read diffs with takes the
111 * quotes off the `diff --git` line's names and leaves them on a rename's.
112 */
113export function unquoteGitPatchPath(token: string): string {
114 if (token.length >= 2 && token.startsWith(QUOTE) && token.endsWith(QUOTE)) {
115 return unescapeBody(token.slice(1, -1));
116 }
117 return unescapeBody(token);