Skip to content

Commit

Permalink
feat: BASE to undefined if HEAD~1 does not exist (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini authored Aug 29, 2024
1 parent 5ba8bac commit 16efd74
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
27 changes: 19 additions & 8 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 @@ -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');
}
Expand Down
50 changes: 29 additions & 21 deletions find-successful-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 16efd74

Please sign in to comment.