Skip to content

Commit

Permalink
Merge pull request #75 from dorny/fix-searching-for-merge-base
Browse files Browse the repository at this point in the history
Fetch base and search merge-base without creating local branch
  • Loading branch information
dorny authored Mar 9, 2021
2 parents 0aa1597 + c64be94 commit ca8fa40
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 47 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v2.9.2
- [Fix fetching git history](https://github.com/dorny/paths-filter/pull/75)

## v2.9.1
- [Fix fetching git history + fallback to unshallow repo](https://github.com/dorny/paths-filter/pull/74)

Expand Down
41 changes: 19 additions & 22 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3865,36 +3865,33 @@ async function getChangesOnHead() {
return parseGitDiffOutput(output);
}
exports.getChangesOnHead = getChangesOnHead;
async function getChangesSinceMergeBase(baseRef, ref, initialFetchDepth) {
async function getChangesSinceMergeBase(base, ref, initialFetchDepth) {
const baseRef = `remotes/origin/${base}`;
async function hasMergeBase() {
return (await exec_1.default('git', ['merge-base', baseRef, ref], { ignoreReturnCode: true })).code === 0;
}
let noMergeBase = false;
core.startGroup(`Searching for merge-base ${baseRef}...${ref}`);
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}`]);
init = false;
}
else {
await exec_1.default('git', ['fetch', `--deepen=${depth}`, 'origin', baseRef, ref]);
}
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']);
if (!(await hasMergeBase())) {
noMergeBase = true;
if (!(await hasMergeBase())) {
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, ref]);
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']);
if (!(await hasMergeBase())) {
noMergeBase = true;
}
break;
}
break;
lastCommitCount = commitCount;
}
depth = Math.min(depth * 2, Number.MAX_SAFE_INTEGER);
lastCommitCount = commitCount;
}
}
finally {
Expand Down
45 changes: 20 additions & 25 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,40 +54,35 @@ 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, ref: 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
}

let noMergeBase = false
core.startGroup(`Searching for merge-base ${baseRef}...${ref}`)
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}`])
init = false
} else {
await exec('git', ['fetch', `--deepen=${depth}`, 'origin', baseRef, ref])
}
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'])
if (!(await hasMergeBase())) {
noMergeBase = true
if (!(await hasMergeBase())) {
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, ref])
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'])
if (!(await hasMergeBase())) {
noMergeBase = true
}
break
}
break
lastCommitCount = commitCount
}
depth = Math.min(depth * 2, Number.MAX_SAFE_INTEGER)
lastCommitCount = commitCount
}
} finally {
core.endGroup()
Expand Down

0 comments on commit ca8fa40

Please sign in to comment.