Skip to content

Commit

Permalink
Update repo.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
rindeal authored Sep 4, 2024
1 parent 018256d commit 97550e5
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export interface Ref {
hash: string
}


const gitOidRegExp = /^[0-9a-f]{40}$/
const gitRefRegExp = /^(?:refs\/(?:heads|tags|remotes)\/|HEAD$)/


export class RefDiff {
type: string = 'NO_TYPE'
sourceRepo: Repo
Expand Down Expand Up @@ -110,7 +115,7 @@ export class Repo {
throw new Error(errorMsg)
}
Logger.debug(`Executing \`git ls-remote\` for \`${this.url}\``)
execFile('git', ['ls-remote', '--quiet' , '--exit-code', '--', this.url], {}, (error, stdout/*, stderr*/) => {
execFile('git', ['ls-remote', '--quiet', '--exit-code', '--', this.url], {}, (error, stdout/*, stderr*/) => {
if ( error ) {
Logger.error(`Error fetching refs for \`${this.url}\`: \`${error.message}\``)
reject(error)
Expand All @@ -120,18 +125,35 @@ export class Repo {
}
})
})

const refs = result.split('\n')
.filter(line => line)
.map(line => {
const [hash, name] = line.split('\t')
if ( ! hash || ! name ) {
const errorMsg = `Invalid \`git ls-remote\` output line: \`${line}\``
Logger.error(errorMsg)
throw new Error(errorMsg)
}
if ( ! gitOidRegExp.test(hash) ) {
const errorMsg = `Invalid Git OID hash: \`${hash}\``
Logger.error(errorMsg)
throw new Error(errorMsg)
}
if ( ! gitRefRegExp.test(name) ) {
const errorMsg = `Invalid Git ref name: \`${name}\``
Logger.error(errorMsg)
throw new Error(errorMsg)
}
Logger.silly(`Parsed ref: \`${name}\` with hash: \`${hash}\` from \`${this.url}\``)
return {name, hash}
return { name, hash }
})

this._refs = refs
Logger.debug(`Fetched \`${refs.length}\` refs for \`${this.url}\``)
return refs
}


async _buildRefIndexes() {
Logger.trace(`GitLsRemote.Repo._buildRefIndexes() called for \`${this.url}\``)
if ( ! this._refs ) {
Expand Down

0 comments on commit 97550e5

Please sign in to comment.