Skip to content

Commit

Permalink
feat: BASE to undefined if HEAD~1 does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Aug 29, 2024
1 parent 5ba8bac commit 5ae1486
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
17 changes: 13 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -37923,13 +37924,21 @@ let BASE_SHA;
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',
});
BASE_SHA =
(_a = emptyTreeRes.stdout) !== null && _a !== void 0 ? _a : `4b825dc642cb6eb9a060e54bf8d69288fbee4904`;
}
else {
BASE_SHA = baseRes.stdout;
}
}
core.setOutput('noPreviousBuild', 'true');
}
Expand Down
29 changes: 16 additions & 13 deletions find-successful-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,26 @@ let BASE_SHA: string;
);
process.stdout.write('\n');

const commitCountOutput = spawnSync(
'git',
['rev-list', '--count', `origin/${mainBranchName}`],
{ encoding: 'utf-8' },
).stdout;
const commitCount = parseInt(
stripNewLineEndings(commitCountOutput),
10,
);
// Check if HEAD~1 exists, and if not, set BASE_SHA to the empty tree hash
const LAST_COMMIT_CMD = `origin/${mainBranchName}~1`;

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;

if (baseRes.status !== 0 || !baseRes.stdout) {
const emptyTreeRes = spawnSync(
'git',
['hash-object', '-t', 'tree', '/dev/null'],
{
encoding: 'utf-8',
},
);
BASE_SHA =
emptyTreeRes.stdout ?? `4b825dc642cb6eb9a060e54bf8d69288fbee4904`;
} else {
BASE_SHA = baseRes.stdout;
}
}
core.setOutput('noPreviousBuild', 'true');
}
Expand Down

0 comments on commit 5ae1486

Please sign in to comment.