async function getLocalRef()

in paths-filter/src/git.ts [211:233]


async function getLocalRef(shortName: string): Promise<string | undefined> {
  if (isGitSha(shortName)) {
    return (await hasCommit(shortName)) ? shortName : undefined
  }

  const output = (await getExecOutput('git', ['show-ref', shortName], {ignoreReturnCode: true})).stdout
  const refs = output
    .split(/\r?\n/g)
    .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
  }

  const remoteRef = refs.find(ref => ref.startsWith('refs/remotes/origin/'))
  if (remoteRef) {
    return remoteRef
  }

  return refs[0]
}