Skip to content

Commit

Permalink
Get full ref name without multiple invocations to git show-ref
Browse files Browse the repository at this point in the history
  • Loading branch information
dorny committed Mar 25, 2021
1 parent 6d81690 commit a6989ad
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
25 changes: 11 additions & 14 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
return remoteRef;
}
else if (await verifyRef(tagRef)) {
return tagRef;
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;
}
else if (await verifyRef(headRef)) {
return headRef;
const remoteRef = refs.find(ref => ref.startsWith('refs/remotes/origin/'));
if (remoteRef) {
return remoteRef;
}
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.
Expand Down
26 changes: 13 additions & 13 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,22 @@ async function getFullRef(shortName: string): Promise<string | undefined> {
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<boolean> {
return (await exec('git', ['show-ref', '--verify', ref], {ignoreReturnCode: true})).code === 0
return refs[0]
}

function fixStdOutNullTermination(): void {
Expand Down

0 comments on commit a6989ad

Please sign in to comment.