Skip to content

Commit

Permalink
Use ref instead of HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
dorny committed Mar 9, 2021
1 parent a301a0a commit 138368f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
18 changes: 9 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3865,21 +3865,21 @@ async function getChangesOnHead() {
return parseGitDiffOutput(output);
}
exports.getChangesOnHead = getChangesOnHead;
async function getChangesSinceMergeBase(base, initialFetchDepth) {
async function getChangesSinceMergeBase(base, ref, initialFetchDepth) {
const baseRef = `remotes/origin/${base}`;
async function hasMergeBase() {
return (await exec_1.default('git', ['merge-base', baseRef, exports.HEAD], { ignoreReturnCode: true })).code === 0;
return (await exec_1.default('git', ['merge-base', baseRef, ref], { ignoreReturnCode: true })).code === 0;
}
let noMergeBase = false;
core.startGroup(`Searching for merge-base ${baseRef}...${exports.HEAD}`);
core.startGroup(`Searching for merge-base ${baseRef}...${ref}`);
try {
if (!(await hasMergeBase())) {
await exec_1.default('git', ['fetch', `--depth=${initialFetchDepth}`, 'origin', base, exports.HEAD]);
await exec_1.default('git', ['fetch', `--depth=${initialFetchDepth}`, 'origin', base, ref]);
let depth = initialFetchDepth;
let lastCommitCount = await getCommitCount();
while (!(await hasMergeBase())) {
depth = Math.min(depth * 2, Number.MAX_SAFE_INTEGER);
await exec_1.default('git', ['fetch', `--deepen=${depth}`, 'origin', base, exports.HEAD]);
await exec_1.default('git', ['fetch', `--deepen=${depth}`, 'origin', base, ref]);
const commitCount = await getCommitCount();
if (commitCount === lastCommitCount) {
core.info('No more commits were fetched');
Expand All @@ -3902,11 +3902,11 @@ async function getChangesSinceMergeBase(base, initialFetchDepth) {
return await listAllFilesAsAdded();
}
// Get changes introduced on HEAD compared to ref
core.startGroup(`Change detection ${baseRef}...${exports.HEAD}`);
core.startGroup(`Change detection ${baseRef}...${ref}`);
let output = '';
try {
// Three dots '...' change detection - finds merge-base and compares against it
output = (await exec_1.default('git', ['diff', '--no-renames', '--name-status', '-z', `${baseRef}...${exports.HEAD}`])).stdout;
output = (await exec_1.default('git', ['diff', '--no-renames', '--name-status', '-z', `${baseRef}...${ref}`])).stdout;
}
finally {
fixStdOutNullTermination();
Expand Down Expand Up @@ -4732,7 +4732,7 @@ async function getChangedFilesFromGit(base, initialFetchDepth) {
if (baseSha === git.NULL_SHA) {
if (defaultRef && baseRef !== defaultRef) {
core.info(`First push of a branch detected - changes will be detected against the default branch ${defaultRef}`);
return await git.getChangesSinceMergeBase(defaultRef, initialFetchDepth);
return await git.getChangesSinceMergeBase(defaultRef, ref, initialFetchDepth);
}
else {
core.info('Initial push detected - all files will be listed as added');
Expand All @@ -4744,7 +4744,7 @@ async function getChangedFilesFromGit(base, initialFetchDepth) {
}
// Changes introduced by current branch against the base branch
core.info(`Changes will be detected against the branch ${baseRef}`);
return await git.getChangesSinceMergeBase(baseRef, initialFetchDepth);
return await git.getChangesSinceMergeBase(baseRef, ref, initialFetchDepth);
}
// Uses github REST api to get list of files changed in PR
async function getChangedFilesFromApi(token, pullRequest) {
Expand Down
14 changes: 7 additions & 7 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ export async function getChangesOnHead(): Promise<File[]> {
return parseGitDiffOutput(output)
}

export async function getChangesSinceMergeBase(base: string, initialFetchDepth: number): Promise<File[]> {
export async function getChangesSinceMergeBase(base: string, ref: string, initialFetchDepth: number): Promise<File[]> {
const baseRef = `remotes/origin/${base}`

async function hasMergeBase(): Promise<boolean> {
return (await exec('git', ['merge-base', baseRef, HEAD], {ignoreReturnCode: true})).code === 0
return (await exec('git', ['merge-base', baseRef, ref], {ignoreReturnCode: true})).code === 0
}

let noMergeBase = false
core.startGroup(`Searching for merge-base ${baseRef}...${HEAD}`)
core.startGroup(`Searching for merge-base ${baseRef}...${ref}`)
try {
if (!(await hasMergeBase())) {
await exec('git', ['fetch', `--depth=${initialFetchDepth}`, 'origin', base, HEAD])
await exec('git', ['fetch', `--depth=${initialFetchDepth}`, 'origin', base, ref])
let depth = initialFetchDepth
let lastCommitCount = await getCommitCount()
while (!(await hasMergeBase())) {
depth = Math.min(depth * 2, Number.MAX_SAFE_INTEGER)
await exec('git', ['fetch', `--deepen=${depth}`, 'origin', base, HEAD])
await exec('git', ['fetch', `--deepen=${depth}`, 'origin', base, ref])
const commitCount = await getCommitCount()
if (commitCount === lastCommitCount) {
core.info('No more commits were fetched')
Expand All @@ -94,11 +94,11 @@ export async function getChangesSinceMergeBase(base: string, initialFetchDepth:
}

// Get changes introduced on HEAD compared to ref
core.startGroup(`Change detection ${baseRef}...${HEAD}`)
core.startGroup(`Change detection ${baseRef}...${ref}`)
let output = ''
try {
// Three dots '...' change detection - finds merge-base and compares against it
output = (await exec('git', ['diff', '--no-renames', '--name-status', '-z', `${baseRef}...${HEAD}`])).stdout
output = (await exec('git', ['diff', '--no-renames', '--name-status', '-z', `${baseRef}...${ref}`])).stdout
} finally {
fixStdOutNullTermination()
core.endGroup()
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async function getChangedFilesFromGit(base: string, initialFetchDepth: number):
if (baseSha === git.NULL_SHA) {
if (defaultRef && baseRef !== defaultRef) {
core.info(`First push of a branch detected - changes will be detected against the default branch ${defaultRef}`)
return await git.getChangesSinceMergeBase(defaultRef, initialFetchDepth)
return await git.getChangesSinceMergeBase(defaultRef, ref, initialFetchDepth)
} else {
core.info('Initial push detected - all files will be listed as added')
return await git.listAllFilesAsAdded()
Expand All @@ -122,7 +122,7 @@ async function getChangedFilesFromGit(base: string, initialFetchDepth: number):

// Changes introduced by current branch against the base branch
core.info(`Changes will be detected against the branch ${baseRef}`)
return await git.getChangesSinceMergeBase(baseRef, initialFetchDepth)
return await git.getChangesSinceMergeBase(baseRef, ref, initialFetchDepth)
}

// Uses github REST api to get list of files changed in PR
Expand Down

0 comments on commit 138368f

Please sign in to comment.