From a6989ad59299fce51b6ce7850e7eaebdfce5ee59 Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Fri, 26 Mar 2021 00:05:48 +0100 Subject: [PATCH] Get full ref name without multiple invocations to git show-ref --- dist/index.js | 25 +++++++++++-------------- src/git.ts | 26 +++++++++++++------------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/dist/index.js b/dist/index.js index 7ac8911..e516c4f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4010,22 +4010,19 @@ async function getFullRef(shortName) { if (isGitSha(shortName)) { return shortName; } - const remoteRef = `refs/remotes/origin/${shortName}`; - const tagRef = `refs/tags/${shortName}`; - const headRef = `refs/heads/${shortName}`; - if (await verifyRef(remoteRef)) { + 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 !== ''); + if (refs.length === 0) { + return undefined; + } + const remoteRef = refs.find(ref => ref.startsWith('refs/remotes/origin/')); + if (remoteRef) { return remoteRef; } - else if (await verifyRef(tagRef)) { - return tagRef; - } - else if (await verifyRef(headRef)) { - return headRef; - } - return undefined; -} -async function verifyRef(ref) { - return (await exec_1.default('git', ['show-ref', '--verify', ref], { ignoreReturnCode: true })).code === 0; + return refs[0]; } function fixStdOutNullTermination() { // Previous command uses NULL as delimiters and output is printed to stdout. diff --git a/src/git.ts b/src/git.ts index 4831e83..97a4021 100644 --- a/src/git.ts +++ b/src/git.ts @@ -207,22 +207,22 @@ async function getFullRef(shortName: string): Promise { return shortName } - const remoteRef = `refs/remotes/origin/${shortName}` - const tagRef = `refs/tags/${shortName}` - const headRef = `refs/heads/${shortName}` - if (await verifyRef(remoteRef)) { - return remoteRef - } else if (await verifyRef(tagRef)) { - return tagRef - } else if (await verifyRef(headRef)) { - return headRef + 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 !== '') + + if (refs.length === 0) { + return undefined } - return undefined -} + const remoteRef = refs.find(ref => ref.startsWith('refs/remotes/origin/')) + if (remoteRef) { + return remoteRef + } -async function verifyRef(ref: string): Promise { - return (await exec('git', ['show-ref', '--verify', ref], {ignoreReturnCode: true})).code === 0 + return refs[0] } function fixStdOutNullTermination(): void {