Skip to content

Commit

Permalink
get git sha on install
Browse files Browse the repository at this point in the history
  • Loading branch information
justinwoo committed Aug 3, 2023
1 parent bd858d0 commit aabcd25
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
34 changes: 34 additions & 0 deletions report.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ function processDependencyTreeOutput (resolve, reject) {
}
}

function processGitRevParseOutput (resolve, reject) {
return function (error, stdout, stderr) {
if (error && !stdout) {
return reject(new Error(`Scarf received an error from git rev-parse: ${error} | ${stderr}`))
}

const output = String(stdout).trim()

if (output.length > 0) {
return resolve(output)
} else {
return reject(new Error('Scarf did not receive usable output from git rev-parse'))
}
}
}

// packageJSONOverride: a test convenience to set a packageJSON explicitly.
// Leave empty to use the actual root package.json.
async function getDependencyInfo (packageJSONOverride) {
Expand Down Expand Up @@ -226,6 +242,18 @@ async function getDependencyInfo (packageJSONOverride) {
})
}

async function getGitSha () {
const promise = new Promise((resolve, reject) => {
exec(`cd ${rootPath} && git rev-parse HEAD`, { timeout: execTimeout, maxBuffer: 1024 * 1024 * 1024 }, processGitRevParseOutput(resolve, reject))
})
try {
return await promise
} catch (e) {
logIfVerbose(e)
return undefined
}
}

async function reportPostInstall () {
const scarfApiToken = process.env.SCARF_API_TOKEN

Expand All @@ -235,6 +263,10 @@ async function reportPostInstall () {
return Promise.reject(new Error('No parent found, nothing to report'))
}

const gitSha = await getGitSha()
logIfVerbose(`Injecting sha to parent: ${gitSha}`)
dependencyInfo.parent.gitSha = gitSha

const rootPackage = dependencyInfo.rootPackage

if (!userHasOptedIn(rootPackage) && isYarn()) {
Expand Down Expand Up @@ -512,8 +544,10 @@ module.exports = {
tmpFileName,
dirName,
processDependencyTreeOutput,
processGitRevParseOutput,
npmExecPath,
getDependencyInfo,
getGitSha,
reportPostInstall,
hashWithDefault,
findScarfInFullDependencyTree
Expand Down
10 changes: 10 additions & 0 deletions test/report.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ describe('Reporting tests', () => {
return report.processDependencyTreeOutput(resolve, reject)(null, JSON.stringify(parsedLsOutput), null)
})).rejects.toEqual(new Error('The package depending on Scarf is the root package being installed, but Scarf is not configured to run in this case. To enable it, set `scarfSettings.allowTopLevel = true` in your package.json'))
})

test('Can parse example git rev-parse HEAD output', async () => {
await expect(new Promise((resolve, reject) => {
return report.processGitRevParseOutput(resolve, reject)(null, '9ace16b9e3833ad4e761a49f17fe607723d5bd5e\n', null)
})).resolves.toEqual('9ace16b9e3833ad4e761a49f17fe607723d5bd5e')
})

test('getGitSha resolves in test run of cloned repository', async () => {
await expect(report.getGitSha()).resolves.toBeTruthy()
})
})

function dependencyTreeScarfEnabled () {
Expand Down

0 comments on commit aabcd25

Please sign in to comment.