Skip to content

Commit 2bfc468

Browse files
committed
Add decodeGitFilePath()
1 parent 63eb7bb commit 2bfc468

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/actions-util.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,41 @@ test("determineBaseBranchHeadCommitOid other error", async (t) => {
324324

325325
infoStub.restore();
326326
});
327+
328+
test("decodeGitFilePath unquoted strings", async (t) => {
329+
t.deepEqual(actionsUtil.decodeGitFilePath("foo"), "foo");
330+
t.deepEqual(actionsUtil.decodeGitFilePath("foo bar"), "foo bar");
331+
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\\\bar"), "foo\\\\bar");
332+
t.deepEqual(actionsUtil.decodeGitFilePath('foo\\"bar'), 'foo\\"bar');
333+
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\001bar"), "foo\\001bar");
334+
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\abar"), "foo\\abar");
335+
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\bbar"), "foo\\bbar");
336+
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\fbar"), "foo\\fbar");
337+
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\nbar"), "foo\\nbar");
338+
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\rbar"), "foo\\rbar");
339+
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\tbar"), "foo\\tbar");
340+
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\vbar"), "foo\\vbar");
341+
t.deepEqual(
342+
actionsUtil.decodeGitFilePath("\\a\\b\\f\\n\\r\\t\\v"),
343+
"\\a\\b\\f\\n\\r\\t\\v",
344+
);
345+
});
346+
347+
test("decodeGitFilePath quoted strings", async (t) => {
348+
t.deepEqual(actionsUtil.decodeGitFilePath('"foo"'), "foo");
349+
t.deepEqual(actionsUtil.decodeGitFilePath('"foo bar"'), "foo bar");
350+
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\\\bar"'), "foo\\bar");
351+
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\"bar"'), 'foo"bar');
352+
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\001bar"'), "foo\x01bar");
353+
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\abar"'), "foo\x07bar");
354+
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\bbar"'), "foo\bbar");
355+
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\fbar"'), "foo\fbar");
356+
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\nbar"'), "foo\nbar");
357+
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\rbar"'), "foo\rbar");
358+
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\tbar"'), "foo\tbar");
359+
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\vbar"'), "foo\vbar");
360+
t.deepEqual(
361+
actionsUtil.decodeGitFilePath('"\\a\\b\\f\\n\\r\\t\\v"'),
362+
"\x07\b\f\n\r\t\v",
363+
);
364+
});

src/actions-util.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,54 @@ export const determineBaseBranchHeadCommitOid = async function (
161161
}
162162
};
163163

164+
/**
165+
* Decode, if necessary, a file path produced by Git. See
166+
* https://git-scm.com/docs/git-config#Documentation/git-config.txt-corequotePath
167+
* for details on how Git encodes file paths with special characters.
168+
*
169+
* This function works only for Git output with `core.quotePath=false`.
170+
*/
171+
export const decodeGitFilePath = function (filePath: string): string {
172+
if (filePath.startsWith('"') && filePath.endsWith('"')) {
173+
filePath = filePath.substring(1, filePath.length - 1);
174+
return filePath.replace(
175+
/\\([abfnrtv\\"]|[0-7]{1,3})/g,
176+
(_match, seq: string) => {
177+
switch (seq[0]) {
178+
case "a":
179+
return "\x07";
180+
case "b":
181+
return "\b";
182+
case "f":
183+
return "\f";
184+
case "n":
185+
return "\n";
186+
case "r":
187+
return "\r";
188+
case "t":
189+
return "\t";
190+
case "v":
191+
return "\v";
192+
case "\\":
193+
return "\\";
194+
case '"':
195+
return '"';
196+
default:
197+
// Both String.fromCharCode() and String.fromCodePoint() works only
198+
// for constructing an entire character at once. If a Unicode
199+
// character is encoded as a sequence of escaped bytes, calling these
200+
// methods sequentially on the individual byte values would *not*
201+
// produce the original multi-byte Unicode character. As a result,
202+
// this implementation works only with the Git option core.quotePath
203+
// set to false.
204+
return String.fromCharCode(parseInt(seq, 8));
205+
}
206+
},
207+
);
208+
}
209+
return filePath;
210+
};
211+
164212
/**
165213
* Get the ref currently being analyzed.
166214
*/

0 commit comments

Comments
 (0)