diff --git a/src/actions-util.test.ts b/src/actions-util.test.ts index 6caec4f30..fb1a1842d 100644 --- a/src/actions-util.test.ts +++ b/src/actions-util.test.ts @@ -324,3 +324,41 @@ test("determineBaseBranchHeadCommitOid other error", async (t) => { infoStub.restore(); }); + +test("decodeGitFilePath unquoted strings", async (t) => { + t.deepEqual(actionsUtil.decodeGitFilePath("foo"), "foo"); + t.deepEqual(actionsUtil.decodeGitFilePath("foo bar"), "foo bar"); + t.deepEqual(actionsUtil.decodeGitFilePath("foo\\\\bar"), "foo\\\\bar"); + t.deepEqual(actionsUtil.decodeGitFilePath('foo\\"bar'), 'foo\\"bar'); + t.deepEqual(actionsUtil.decodeGitFilePath("foo\\001bar"), "foo\\001bar"); + t.deepEqual(actionsUtil.decodeGitFilePath("foo\\abar"), "foo\\abar"); + t.deepEqual(actionsUtil.decodeGitFilePath("foo\\bbar"), "foo\\bbar"); + t.deepEqual(actionsUtil.decodeGitFilePath("foo\\fbar"), "foo\\fbar"); + t.deepEqual(actionsUtil.decodeGitFilePath("foo\\nbar"), "foo\\nbar"); + t.deepEqual(actionsUtil.decodeGitFilePath("foo\\rbar"), "foo\\rbar"); + t.deepEqual(actionsUtil.decodeGitFilePath("foo\\tbar"), "foo\\tbar"); + t.deepEqual(actionsUtil.decodeGitFilePath("foo\\vbar"), "foo\\vbar"); + t.deepEqual( + actionsUtil.decodeGitFilePath("\\a\\b\\f\\n\\r\\t\\v"), + "\\a\\b\\f\\n\\r\\t\\v", + ); +}); + +test("decodeGitFilePath quoted strings", async (t) => { + t.deepEqual(actionsUtil.decodeGitFilePath('"foo"'), "foo"); + t.deepEqual(actionsUtil.decodeGitFilePath('"foo bar"'), "foo bar"); + t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\\\bar"'), "foo\\bar"); + t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\"bar"'), 'foo"bar'); + t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\001bar"'), "foo\x01bar"); + t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\abar"'), "foo\x07bar"); + t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\bbar"'), "foo\bbar"); + t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\fbar"'), "foo\fbar"); + t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\nbar"'), "foo\nbar"); + t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\rbar"'), "foo\rbar"); + t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\tbar"'), "foo\tbar"); + t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\vbar"'), "foo\vbar"); + t.deepEqual( + actionsUtil.decodeGitFilePath('"\\a\\b\\f\\n\\r\\t\\v"'), + "\x07\b\f\n\r\t\v", + ); +}); diff --git a/src/actions-util.ts b/src/actions-util.ts index 8d4453428..8f39e331b 100644 --- a/src/actions-util.ts +++ b/src/actions-util.ts @@ -161,6 +161,54 @@ export const determineBaseBranchHeadCommitOid = async function ( } }; +/** + * Decode, if necessary, a file path produced by Git. See + * https://git-scm.com/docs/git-config#Documentation/git-config.txt-corequotePath + * for details on how Git encodes file paths with special characters. + * + * This function works only for Git output with `core.quotePath=false`. + */ +export const decodeGitFilePath = function (filePath: string): string { + if (filePath.startsWith('"') && filePath.endsWith('"')) { + filePath = filePath.substring(1, filePath.length - 1); + return filePath.replace( + /\\([abfnrtv\\"]|[0-7]{1,3})/g, + (_match, seq: string) => { + switch (seq[0]) { + case "a": + return "\x07"; + case "b": + return "\b"; + case "f": + return "\f"; + case "n": + return "\n"; + case "r": + return "\r"; + case "t": + return "\t"; + case "v": + return "\v"; + case "\\": + return "\\"; + case '"': + return '"'; + default: + // Both String.fromCharCode() and String.fromCodePoint() works only + // for constructing an entire character at once. If a Unicode + // character is encoded as a sequence of escaped bytes, calling these + // methods sequentially on the individual byte values would *not* + // produce the original multi-byte Unicode character. As a result, + // this implementation works only with the Git option core.quotePath + // set to false. + return String.fromCharCode(parseInt(seq, 8)); + } + }, + ); + } + return filePath; +}; + /** * Get the ref currently being analyzed. */