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).
This commit is contained in:
Michal Dorner 2021-06-14 10:20:58 +02:00
parent 78ab00f877
commit af2564d3e0
No known key found for this signature in database
GPG key ID: 9EEE04B48DA36786
2 changed files with 6 additions and 4 deletions

View file

@ -215,8 +215,9 @@ async function getLocalRef(shortName: string): Promise<string | undefined> {
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