From 16efd743af6fc1421daaaa33b61cec4a907b38ed Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Thu, 29 Aug 2024 13:18:41 +0300 Subject: [PATCH] feat: BASE to undefined if HEAD~1 does not exist (#167) --- dist/index.js | 27 ++++++++++++++------ find-successful-workflow.ts | 50 +++++++++++++++++++++---------------- package.json | 2 +- 3 files changed, 49 insertions(+), 30 deletions(-) diff --git a/dist/index.js b/dist/index.js index 01d3178..221f822 100644 --- a/dist/index.js +++ b/dist/index.js @@ -37872,6 +37872,7 @@ const defaultWorkingDirectory = '.'; const ProxifiedClient = action_1.Octokit.plugin(proxyPlugin); let BASE_SHA; (() => __awaiter(void 0, void 0, void 0, function* () { + var _a; if (workingDirectory !== defaultWorkingDirectory) { if ((0, fs_1.existsSync)(workingDirectory)) { process.chdir(workingDirectory); @@ -37919,17 +37920,27 @@ let BASE_SHA; process.stdout.write(`Using provided fallback SHA: ${fallbackSHA}\n`); } else { - process.stdout.write(`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`); - process.stdout.write('\n'); - process.stdout.write(`NOTE: You can instead make this a hard error by setting 'error-on-no-successful-workflow' on the action in your workflow.\n`); - process.stdout.write('\n'); - const commitCountOutput = (0, child_process_1.spawnSync)('git', ['rev-list', '--count', `origin/${mainBranchName}`], { encoding: 'utf-8' }).stdout; - const commitCount = parseInt(stripNewLineEndings(commitCountOutput), 10); - const LAST_COMMIT_CMD = `origin/${mainBranchName}${commitCount > 1 ? '~1' : ''}`; + // Check if HEAD~1 exists, and if not, set BASE_SHA to the empty tree hash + const LAST_COMMIT_CMD = `origin/${mainBranchName}~1`; const baseRes = (0, child_process_1.spawnSync)('git', ['rev-parse', LAST_COMMIT_CMD], { encoding: 'utf-8', }); - BASE_SHA = baseRes.stdout; + if (baseRes.status !== 0 || !baseRes.stdout) { + const emptyTreeRes = (0, child_process_1.spawnSync)('git', ['hash-object', '-t', 'tree', '/dev/null'], { + encoding: 'utf-8', + }); + // 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is the expected result of hashing the empty tree + BASE_SHA = + (_a = emptyTreeRes.stdout) !== null && _a !== void 0 ? _a : `4b825dc642cb6eb9a060e54bf8d69288fbee4904`; + process.stdout.write(`HEAD~1 does not exist. We are therefore defaulting to use the empty git tree hash as BASE.\n`); + } + else { + process.stdout.write(`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`); + BASE_SHA = baseRes.stdout; + } + process.stdout.write('\n'); + process.stdout.write(`NOTE: You can instead make this a hard error by setting 'error-on-no-successful-workflow' on the action in your workflow.\n`); + process.stdout.write('\n'); } core.setOutput('noPreviousBuild', 'true'); } diff --git a/find-successful-workflow.ts b/find-successful-workflow.ts index c2c1324..1aff4d4 100644 --- a/find-successful-workflow.ts +++ b/find-successful-workflow.ts @@ -85,32 +85,40 @@ let BASE_SHA: string; BASE_SHA = fallbackSHA; process.stdout.write(`Using provided fallback SHA: ${fallbackSHA}\n`); } else { - process.stdout.write( - `We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`, - ); + // Check if HEAD~1 exists, and if not, set BASE_SHA to the empty tree hash + const LAST_COMMIT_CMD = `origin/${mainBranchName}~1`; + + const baseRes = spawnSync('git', ['rev-parse', LAST_COMMIT_CMD], { + encoding: 'utf-8', + }); + + if (baseRes.status !== 0 || !baseRes.stdout) { + const emptyTreeRes = spawnSync( + 'git', + ['hash-object', '-t', 'tree', '/dev/null'], + { + encoding: 'utf-8', + }, + ); + // 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is the expected result of hashing the empty tree + BASE_SHA = + emptyTreeRes.stdout ?? `4b825dc642cb6eb9a060e54bf8d69288fbee4904`; + process.stdout.write( + `HEAD~1 does not exist. We are therefore defaulting to use the empty git tree hash as BASE.\n`, + ); + } else { + process.stdout.write( + `We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`, + ); + + BASE_SHA = baseRes.stdout; + } + process.stdout.write('\n'); process.stdout.write( `NOTE: You can instead make this a hard error by setting 'error-on-no-successful-workflow' on the action in your workflow.\n`, ); process.stdout.write('\n'); - - const commitCountOutput = spawnSync( - 'git', - ['rev-list', '--count', `origin/${mainBranchName}`], - { encoding: 'utf-8' }, - ).stdout; - const commitCount = parseInt( - stripNewLineEndings(commitCountOutput), - 10, - ); - - const LAST_COMMIT_CMD = `origin/${mainBranchName}${ - commitCount > 1 ? '~1' : '' - }`; - const baseRes = spawnSync('git', ['rev-parse', LAST_COMMIT_CMD], { - encoding: 'utf-8', - }); - BASE_SHA = baseRes.stdout; } core.setOutput('noPreviousBuild', 'true'); } diff --git a/package.json b/package.json index 7412322..7e0f422 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "private": true, - "version": "4.0.6", + "version": "4.1.0", "license": "MIT", "description": "This package.json is here purely to control the version of the Action, in combination with https://github.com/JamesHenry/publish-shell-action", "scripts": {