@@ -3866,15 +3866,26 @@ async function getChangesOnHead() {
3866
3866
}
3867
3867
exports.getChangesOnHead = getChangesOnHead;
3868
3868
async function getChangesSinceMergeBase(base, ref, initialFetchDepth) {
3869
- const baseRef = `remotes/origin/${base}` ;
3869
+ let baseRef;
3870
3870
async function hasMergeBase() {
3871
- return (await exec_1.default('git', ['merge-base', baseRef, ref], { ignoreReturnCode: true })).code === 0;
3871
+ return (baseRef !== undefined && ( await exec_1.default('git', ['merge-base', baseRef, ref], { ignoreReturnCode: true })).code === 0) ;
3872
3872
}
3873
3873
let noMergeBase = false;
3874
- core.startGroup(`Searching for merge-base ${baseRef }...${ref}`);
3874
+ core.startGroup(`Searching for merge-base ${base }...${ref}`);
3875
3875
try {
3876
+ baseRef = await getFullRef(base);
3876
3877
if (!(await hasMergeBase())) {
3877
- await exec_1.default('git', ['fetch', `--depth=${initialFetchDepth}`, 'origin', base, ref]);
3878
+ await exec_1.default('git', ['fetch', '--no-tags', `--depth=${initialFetchDepth}`, 'origin', base, ref]);
3879
+ if (baseRef === undefined) {
3880
+ baseRef = await getFullRef(base);
3881
+ if (baseRef === undefined) {
3882
+ await exec_1.default('git', ['fetch', '--tags', `--depth=1`, 'origin', base, ref]);
3883
+ baseRef = await getFullRef(base);
3884
+ if (baseRef === undefined) {
3885
+ throw new Error(`Could not determine what is ${base} - fetch works but it's not a branch or tag`);
3886
+ }
3887
+ }
3888
+ }
3878
3889
let depth = initialFetchDepth;
3879
3890
let lastCommitCount = await getCommitCount();
3880
3891
while (!(await hasMergeBase())) {
@@ -3897,16 +3908,17 @@ async function getChangesSinceMergeBase(base, ref, initialFetchDepth) {
3897
3908
finally {
3898
3909
core.endGroup();
3899
3910
}
3911
+ let diffArg = `${baseRef}...${ref}`;
3900
3912
if (noMergeBase) {
3901
- core.warning('No merge base found - all files will be listed as added ');
3902
- return await listAllFilesAsAdded() ;
3913
+ core.warning('No merge base found - change detection will use direct <commit>..<commit> comparison ');
3914
+ diffArg = `${baseRef}..${ref}` ;
3903
3915
}
3904
- // Get changes introduced on HEAD compared to ref
3905
- core.startGroup(`Change detection ${baseRef}...${ref }`);
3916
+ // Get changes introduced on ref compared to base
3917
+ core.startGroup(`Change detection ${diffArg }`);
3906
3918
let output = '';
3907
3919
try {
3908
3920
// Three dots '...' change detection - finds merge-base and compares against it
3909
- output = (await exec_1.default('git', ['diff', '--no-renames', '--name-status', '-z', `${baseRef}...${ref}` ])).stdout;
3921
+ output = (await exec_1.default('git', ['diff', '--no-renames', '--name-status', '-z', diffArg ])).stdout;
3910
3922
}
3911
3923
finally {
3912
3924
fixStdOutNullTermination();
@@ -3994,6 +4006,24 @@ async function getCommitCount() {
3994
4006
const count = parseInt(output);
3995
4007
return isNaN(count) ? 0 : count;
3996
4008
}
4009
+ async function getFullRef(shortName) {
4010
+ if (isGitSha(shortName)) {
4011
+ return shortName;
4012
+ }
4013
+ const output = (await exec_1.default('git', ['show-ref', shortName], { ignoreReturnCode: true })).stdout;
4014
+ const refs = output
4015
+ .split(/\r?\n/g)
4016
+ .map(l => { var _a, _b; return (_b = (_a = l.match(/refs\/.*$/)) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : ''; })
4017
+ .filter(l => l !== '');
4018
+ if (refs.length === 0) {
4019
+ return undefined;
4020
+ }
4021
+ const remoteRef = refs.find(ref => ref.startsWith('refs/remotes/origin/'));
4022
+ if (remoteRef) {
4023
+ return remoteRef;
4024
+ }
4025
+ return refs[0];
4026
+ }
3997
4027
function fixStdOutNullTermination() {
3998
4028
// Previous command uses NULL as delimiters and output is printed to stdout.
3999
4029
// We have to make sure next thing written to stdout will start on new line.
@@ -4743,7 +4773,7 @@ async function getChangedFilesFromGit(base, initialFetchDepth) {
4743
4773
return await git.getChanges(baseSha);
4744
4774
}
4745
4775
// Changes introduced by current branch against the base branch
4746
- core.info(`Changes will be detected against the branch ${baseRef}`);
4776
+ core.info(`Changes will be detected against ${baseRef}`);
4747
4777
return await git.getChangesSinceMergeBase(baseRef, ref, initialFetchDepth);
4748
4778
}
4749
4779
// Uses github REST api to get list of files changed in PR
0 commit comments