From af2564d3e0fec2bf67a690e21c0fc115ce085bcd Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Mon, 14 Jun 2021 10:20:58 +0200 Subject: [PATCH] Fix #90 getLocalRef() returns wrong ref git show-ref will return all branches where end segment matches the input. This cause issues when there are both 'someBranch' and 'somePrefix/someBranch' branches. This fix ensures the correct ref is returned by explicitly matching segments after common parts (e.g. refs/heads). --- dist/index.js | 5 +++-- src/git.ts | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 14e671c..7f134f1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4015,8 +4015,9 @@ async function getLocalRef(shortName) { const output = (await exec_1.default('git', ['show-ref', shortName], { ignoreReturnCode: true })).stdout; const refs = output .split(/\r?\n/g) - .map(l => { var _a, _b; return (_b = (_a = l.match(/refs\/.*$/)) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : ''; }) - .filter(l => l !== ''); + .map(l => l.match(/refs\/(?:(?:heads)|(?:tags)|(?:remotes\/origin))\/(.*)$/)) + .filter(match => match !== null && match[1] === shortName) + .map(match => { var _a; return (_a = match === null || match === void 0 ? void 0 : match[0]) !== null && _a !== void 0 ? _a : ''; }); // match can't be null here but compiler doesn't understand that if (refs.length === 0) { return undefined; } diff --git a/src/git.ts b/src/git.ts index f08d2e5..3d6d3be 100644 --- a/src/git.ts +++ b/src/git.ts @@ -215,8 +215,9 @@ async function getLocalRef(shortName: string): Promise { const output = (await exec('git', ['show-ref', shortName], {ignoreReturnCode: true})).stdout const refs = output .split(/\r?\n/g) - .map(l => l.match(/refs\/.*$/)?.[0] ?? '') - .filter(l => l !== '') + .map(l => l.match(/refs\/(?:(?:heads)|(?:tags)|(?:remotes\/origin))\/(.*)$/)) + .filter(match => match !== null && match[1] === shortName) + .map(match => match?.[0] ?? '') // match can't be null here but compiler doesn't understand that if (refs.length === 0) { return undefined