@lhci/utils
Advanced tools
Comparing version 0.2.1-alpha.2 to 0.2.1-alpha.3
{ | ||
"name": "@lhci/utils", | ||
"version": "0.2.1-alpha.2", | ||
"version": "0.2.1-alpha.3", | ||
"license": "Apache-2.0", | ||
@@ -15,3 +15,3 @@ "repository": { | ||
}, | ||
"gitHead": "0c1c082f26abb93e0701f9fea3362434bb76a35a" | ||
"gitHead": "b014cbaefe989e45a883d4ba5c9e2d6174daf5c7" | ||
} |
@@ -11,2 +11,19 @@ /** | ||
/** | ||
* @param {Array<[string, ReadonlyArray<string>]>} commands | ||
* @return {import('child_process').SpawnSyncReturns<string>} | ||
*/ | ||
function runCommandsUntilFirstSuccess(commands) { | ||
/** @type {import('child_process').SpawnSyncReturns<string>|undefined} */ | ||
let result; | ||
for (const [command, args] of commands) { | ||
result = childProcess.spawnSync(command, args, {encoding: 'utf8'}); | ||
if (result.status === 0) break; | ||
} | ||
if (!result) throw new Error('Must specify at least one command'); | ||
return result; | ||
} | ||
const envVars = process.env; | ||
@@ -50,4 +67,7 @@ | ||
function getCurrentBranch() { | ||
// Use Travis CI vars if available. | ||
if (envVars.TRAVIS_PULL_REQUEST_BRANCH) return envVars.TRAVIS_PULL_REQUEST_BRANCH.slice(0, 40); | ||
if (envVars.TRAVIS_BRANCH) return envVars.TRAVIS_BRANCH.slice(0, 40); | ||
// Use GitHub Actions vars if available. See https://github.com/GoogleChrome/lighthouse-ci/issues/43#issuecomment-551174778 | ||
if (envVars.GITHUB_HEAD_REF) return envVars.GITHUB_HEAD_REF.slice(0, 40); | ||
@@ -57,7 +77,9 @@ const result = childProcess.spawnSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { | ||
}); | ||
if (result.status !== 0) { | ||
const branch = result.stdout.trim().slice(0, 40); | ||
if (result.status !== 0 || branch === 'HEAD') { | ||
throw new Error('Unable to determine current branch with `git rev-parse --abbrev-ref HEAD`'); | ||
} | ||
return result.stdout.trim().slice(0, 40); | ||
return branch; | ||
} | ||
@@ -138,5 +160,6 @@ | ||
function getAncestorHashForBranch(hash = 'HEAD') { | ||
const result = childProcess.spawnSync('git', ['merge-base', hash, 'master'], { | ||
encoding: 'utf8', | ||
}); | ||
const result = runCommandsUntilFirstSuccess([ | ||
['git', ['merge-base', hash, 'origin/master']], | ||
['git', ['merge-base', hash, 'master']], | ||
]); | ||
@@ -143,0 +166,0 @@ // Ancestor hash is optional, so do not throw if it can't be computed. |
365616
8363