Skip to content

Commit

Permalink
Fetch base and search merge-base without creating local branch
Browse files Browse the repository at this point in the history
  • Loading branch information
dorny committed Mar 9, 2021
1 parent 0aa1597 commit 0c0d1a8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
21 changes: 11 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3865,29 +3865,30 @@ async function getChangesOnHead() {
return parseGitDiffOutput(output);
}
exports.getChangesOnHead = getChangesOnHead;
async function getChangesSinceMergeBase(baseRef, ref, initialFetchDepth) {
async function getChangesSinceMergeBase(base, initialFetchDepth) {
const baseRef = `remotes/origin/${base}`;
async function hasMergeBase() {
return (await exec_1.default('git', ['merge-base', baseRef, ref], { ignoreReturnCode: true })).code === 0;
return (await exec_1.default('git', ['merge-base', baseRef, exports.HEAD], { ignoreReturnCode: true })).code === 0;
}
let noMergeBase = false;
core.startGroup(`Searching for merge-base ${baseRef}...${ref}`);
core.startGroup(`Searching for merge-base ${baseRef}...${exports.HEAD}`);
try {
let init = true;
let lastCommitCount = await getCommitCount();
let depth = Math.max(lastCommitCount * 2, initialFetchDepth);
while (!(await hasMergeBase())) {
if (init) {
await exec_1.default('git', ['fetch', `--depth=${depth}`, 'origin', `${baseRef}:${baseRef}`, `${ref}`]);
await exec_1.default('git', ['fetch', `--depth=${depth}`, 'origin', base, exports.HEAD]);
init = false;
}
else {
await exec_1.default('git', ['fetch', `--deepen=${depth}`, 'origin', baseRef, ref]);
await exec_1.default('git', ['fetch', `--deepen=${depth}`, 'origin', base, exports.HEAD]);
}
const commitCount = await getCommitCount();
if (commitCount === lastCommitCount) {
core.info('No more commits were fetched');
core.info('Last attempt will be to fetch full history');
await exec_1.default('git', ['fetch', '--unshallow']);
await exec_1.default('git', ['fetch']);
if (!(await hasMergeBase())) {
noMergeBase = true;
}
Expand All @@ -3905,11 +3906,11 @@ async function getChangesSinceMergeBase(baseRef, ref, initialFetchDepth) {
return await listAllFilesAsAdded();
}
// Get changes introduced on HEAD compared to ref
core.startGroup(`Change detection ${baseRef}...${ref}`);
core.startGroup(`Change detection ${baseRef}...${exports.HEAD}`);
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}...${ref}`])).stdout;
output = (await exec_1.default('git', ['diff', '--no-renames', '--name-status', '-z', `${baseRef}...${exports.HEAD}`])).stdout;
}
finally {
fixStdOutNullTermination();
Expand Down Expand Up @@ -4735,7 +4736,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, ref, initialFetchDepth);
return await git.getChangesSinceMergeBase(defaultRef, initialFetchDepth);
}
else {
core.info('Initial push detected - all files will be listed as added');
Expand All @@ -4747,7 +4748,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, ref, initialFetchDepth);
return await git.getChangesSinceMergeBase(baseRef, initialFetchDepth);
}
// Uses github REST api to get list of files changed in PR
async function getChangedFilesFromApi(token, pullRequest) {
Expand Down
22 changes: 10 additions & 12 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,31 @@ export async function getChangesOnHead(): Promise<File[]> {
return parseGitDiffOutput(output)
}

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

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

let noMergeBase = false
core.startGroup(`Searching for merge-base ${baseRef}...${ref}`)
core.startGroup(`Searching for merge-base ${baseRef}...${HEAD}`)
try {
let init = true
let lastCommitCount = await getCommitCount()
let depth = Math.max(lastCommitCount * 2, initialFetchDepth)
while (!(await hasMergeBase())) {
if (init) {
await exec('git', ['fetch', `--depth=${depth}`, 'origin', `${baseRef}:${baseRef}`, `${ref}`])
await exec('git', ['fetch', `--depth=${depth}`, 'origin', base, HEAD])
init = false
} else {
await exec('git', ['fetch', `--deepen=${depth}`, 'origin', baseRef, ref])
await exec('git', ['fetch', `--deepen=${depth}`, 'origin', base, HEAD])
}
const commitCount = await getCommitCount()
if (commitCount === lastCommitCount) {
core.info('No more commits were fetched')
core.info('Last attempt will be to fetch full history')
await exec('git', ['fetch', '--unshallow'])
await exec('git', ['fetch'])
if (!(await hasMergeBase())) {
noMergeBase = true
}
Expand All @@ -99,11 +97,11 @@ export async function getChangesSinceMergeBase(
}

// Get changes introduced on HEAD compared to ref
core.startGroup(`Change detection ${baseRef}...${ref}`)
core.startGroup(`Change detection ${baseRef}...${HEAD}`)
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}...${ref}`])).stdout
output = (await exec('git', ['diff', '--no-renames', '--name-status', '-z', `${baseRef}...${HEAD}`])).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, ref, initialFetchDepth)
return await git.getChangesSinceMergeBase(defaultRef, 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, ref, initialFetchDepth)
return await git.getChangesSinceMergeBase(baseRef, initialFetchDepth)
}

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

0 comments on commit 0c0d1a8

Please sign in to comment.