Skip to content

Commit 16efd74

Browse files
authored
feat: BASE to undefined if HEAD~1 does not exist (#167)
1 parent 5ba8bac commit 16efd74

3 files changed

Lines changed: 49 additions & 30 deletions

File tree

dist/index.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37872,6 +37872,7 @@ const defaultWorkingDirectory = '.';
3787237872
const ProxifiedClient = action_1.Octokit.plugin(proxyPlugin);
3787337873
let BASE_SHA;
3787437874
(() => __awaiter(void 0, void 0, void 0, function* () {
37875+
var _a;
3787537876
if (workingDirectory !== defaultWorkingDirectory) {
3787637877
if ((0, fs_1.existsSync)(workingDirectory)) {
3787737878
process.chdir(workingDirectory);
@@ -37919,17 +37920,27 @@ let BASE_SHA;
3791937920
process.stdout.write(`Using provided fallback SHA: ${fallbackSHA}\n`);
3792037921
}
3792137922
else {
37922-
process.stdout.write(`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`);
37923-
process.stdout.write('\n');
37924-
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`);
37925-
process.stdout.write('\n');
37926-
const commitCountOutput = (0, child_process_1.spawnSync)('git', ['rev-list', '--count', `origin/${mainBranchName}`], { encoding: 'utf-8' }).stdout;
37927-
const commitCount = parseInt(stripNewLineEndings(commitCountOutput), 10);
37928-
const LAST_COMMIT_CMD = `origin/${mainBranchName}${commitCount > 1 ? '~1' : ''}`;
37923+
// Check if HEAD~1 exists, and if not, set BASE_SHA to the empty tree hash
37924+
const LAST_COMMIT_CMD = `origin/${mainBranchName}~1`;
3792937925
const baseRes = (0, child_process_1.spawnSync)('git', ['rev-parse', LAST_COMMIT_CMD], {
3793037926
encoding: 'utf-8',
3793137927
});
37932-
BASE_SHA = baseRes.stdout;
37928+
if (baseRes.status !== 0 || !baseRes.stdout) {
37929+
const emptyTreeRes = (0, child_process_1.spawnSync)('git', ['hash-object', '-t', 'tree', '/dev/null'], {
37930+
encoding: 'utf-8',
37931+
});
37932+
// 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is the expected result of hashing the empty tree
37933+
BASE_SHA =
37934+
(_a = emptyTreeRes.stdout) !== null && _a !== void 0 ? _a : `4b825dc642cb6eb9a060e54bf8d69288fbee4904`;
37935+
process.stdout.write(`HEAD~1 does not exist. We are therefore defaulting to use the empty git tree hash as BASE.\n`);
37936+
}
37937+
else {
37938+
process.stdout.write(`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`);
37939+
BASE_SHA = baseRes.stdout;
37940+
}
37941+
process.stdout.write('\n');
37942+
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`);
37943+
process.stdout.write('\n');
3793337944
}
3793437945
core.setOutput('noPreviousBuild', 'true');
3793537946
}

find-successful-workflow.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,40 @@ let BASE_SHA: string;
8585
BASE_SHA = fallbackSHA;
8686
process.stdout.write(`Using provided fallback SHA: ${fallbackSHA}\n`);
8787
} else {
88-
process.stdout.write(
89-
`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`,
90-
);
88+
// Check if HEAD~1 exists, and if not, set BASE_SHA to the empty tree hash
89+
const LAST_COMMIT_CMD = `origin/${mainBranchName}~1`;
90+
91+
const baseRes = spawnSync('git', ['rev-parse', LAST_COMMIT_CMD], {
92+
encoding: 'utf-8',
93+
});
94+
95+
if (baseRes.status !== 0 || !baseRes.stdout) {
96+
const emptyTreeRes = spawnSync(
97+
'git',
98+
['hash-object', '-t', 'tree', '/dev/null'],
99+
{
100+
encoding: 'utf-8',
101+
},
102+
);
103+
// 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is the expected result of hashing the empty tree
104+
BASE_SHA =
105+
emptyTreeRes.stdout ?? `4b825dc642cb6eb9a060e54bf8d69288fbee4904`;
106+
process.stdout.write(
107+
`HEAD~1 does not exist. We are therefore defaulting to use the empty git tree hash as BASE.\n`,
108+
);
109+
} else {
110+
process.stdout.write(
111+
`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`,
112+
);
113+
114+
BASE_SHA = baseRes.stdout;
115+
}
116+
91117
process.stdout.write('\n');
92118
process.stdout.write(
93119
`NOTE: You can instead make this a hard error by setting 'error-on-no-successful-workflow' on the action in your workflow.\n`,
94120
);
95121
process.stdout.write('\n');
96-
97-
const commitCountOutput = spawnSync(
98-
'git',
99-
['rev-list', '--count', `origin/${mainBranchName}`],
100-
{ encoding: 'utf-8' },
101-
).stdout;
102-
const commitCount = parseInt(
103-
stripNewLineEndings(commitCountOutput),
104-
10,
105-
);
106-
107-
const LAST_COMMIT_CMD = `origin/${mainBranchName}${
108-
commitCount > 1 ? '~1' : ''
109-
}`;
110-
const baseRes = spawnSync('git', ['rev-parse', LAST_COMMIT_CMD], {
111-
encoding: 'utf-8',
112-
});
113-
BASE_SHA = baseRes.stdout;
114122
}
115123
core.setOutput('noPreviousBuild', 'true');
116124
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"private": true,
3-
"version": "4.0.6",
3+
"version": "4.1.0",
44
"license": "MIT",
55
"description": "This package.json is here purely to control the version of the Action, in combination with https://github.com/JamesHenry/publish-shell-action",
66
"scripts": {

0 commit comments

Comments
 (0)