Add decodeGitFilePath()
This commit is contained in:
parent
63eb7bbf1f
commit
2bfc468cd8
2 changed files with 86 additions and 0 deletions
|
|
@ -324,3 +324,41 @@ test("determineBaseBranchHeadCommitOid other error", async (t) => {
|
||||||
|
|
||||||
infoStub.restore();
|
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",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -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.
|
* Get the ref currently being analyzed.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue