Skip to content

Commit ca8fa40

Browse files
authored
Merge pull request #75 from dorny/fix-searching-for-merge-base
Fetch base and search merge-base without creating local branch
2 parents 0aa1597 + c64be94 commit ca8fa40

File tree

3 files changed

+42
-47
lines changed

3 files changed

+42
-47
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## v2.9.2
4+
- [Fix fetching git history](https://github.com/dorny/paths-filter/pull/75)
5+
36
## v2.9.1
47
- [Fix fetching git history + fallback to unshallow repo](https://github.com/dorny/paths-filter/pull/74)
58

dist/index.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3865,36 +3865,33 @@ async function getChangesOnHead() {
38653865
return parseGitDiffOutput(output);
38663866
}
38673867
exports.getChangesOnHead = getChangesOnHead;
3868-
async function getChangesSinceMergeBase(baseRef, ref, initialFetchDepth) {
3868+
async function getChangesSinceMergeBase(base, ref, initialFetchDepth) {
3869+
const baseRef = `remotes/origin/${base}`;
38693870
async function hasMergeBase() {
38703871
return (await exec_1.default('git', ['merge-base', baseRef, ref], { ignoreReturnCode: true })).code === 0;
38713872
}
38723873
let noMergeBase = false;
38733874
core.startGroup(`Searching for merge-base ${baseRef}...${ref}`);
38743875
try {
3875-
let init = true;
3876-
let lastCommitCount = await getCommitCount();
3877-
let depth = Math.max(lastCommitCount * 2, initialFetchDepth);
3878-
while (!(await hasMergeBase())) {
3879-
if (init) {
3880-
await exec_1.default('git', ['fetch', `--depth=${depth}`, 'origin', `${baseRef}:${baseRef}`, `${ref}`]);
3881-
init = false;
3882-
}
3883-
else {
3884-
await exec_1.default('git', ['fetch', `--deepen=${depth}`, 'origin', baseRef, ref]);
3885-
}
3886-
const commitCount = await getCommitCount();
3887-
if (commitCount === lastCommitCount) {
3888-
core.info('No more commits were fetched');
3889-
core.info('Last attempt will be to fetch full history');
3890-
await exec_1.default('git', ['fetch', '--unshallow']);
3891-
if (!(await hasMergeBase())) {
3892-
noMergeBase = true;
3876+
if (!(await hasMergeBase())) {
3877+
await exec_1.default('git', ['fetch', `--depth=${initialFetchDepth}`, 'origin', base, ref]);
3878+
let depth = initialFetchDepth;
3879+
let lastCommitCount = await getCommitCount();
3880+
while (!(await hasMergeBase())) {
3881+
depth = Math.min(depth * 2, Number.MAX_SAFE_INTEGER);
3882+
await exec_1.default('git', ['fetch', `--deepen=${depth}`, 'origin', base, ref]);
3883+
const commitCount = await getCommitCount();
3884+
if (commitCount === lastCommitCount) {
3885+
core.info('No more commits were fetched');
3886+
core.info('Last attempt will be to fetch full history');
3887+
await exec_1.default('git', ['fetch']);
3888+
if (!(await hasMergeBase())) {
3889+
noMergeBase = true;
3890+
}
3891+
break;
38933892
}
3894-
break;
3893+
lastCommitCount = commitCount;
38953894
}
3896-
depth = Math.min(depth * 2, Number.MAX_SAFE_INTEGER);
3897-
lastCommitCount = commitCount;
38983895
}
38993896
}
39003897
finally {

src/git.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,40 +54,35 @@ export async function getChangesOnHead(): Promise<File[]> {
5454
return parseGitDiffOutput(output)
5555
}
5656

57-
export async function getChangesSinceMergeBase(
58-
baseRef: string,
59-
ref: string,
60-
initialFetchDepth: number
61-
): Promise<File[]> {
57+
export async function getChangesSinceMergeBase(base: string, ref: string, initialFetchDepth: number): Promise<File[]> {
58+
const baseRef = `remotes/origin/${base}`
59+
6260
async function hasMergeBase(): Promise<boolean> {
6361
return (await exec('git', ['merge-base', baseRef, ref], {ignoreReturnCode: true})).code === 0
6462
}
6563

6664
let noMergeBase = false
6765
core.startGroup(`Searching for merge-base ${baseRef}...${ref}`)
6866
try {
69-
let init = true
70-
let lastCommitCount = await getCommitCount()
71-
let depth = Math.max(lastCommitCount * 2, initialFetchDepth)
72-
while (!(await hasMergeBase())) {
73-
if (init) {
74-
await exec('git', ['fetch', `--depth=${depth}`, 'origin', `${baseRef}:${baseRef}`, `${ref}`])
75-
init = false
76-
} else {
77-
await exec('git', ['fetch', `--deepen=${depth}`, 'origin', baseRef, ref])
78-
}
79-
const commitCount = await getCommitCount()
80-
if (commitCount === lastCommitCount) {
81-
core.info('No more commits were fetched')
82-
core.info('Last attempt will be to fetch full history')
83-
await exec('git', ['fetch', '--unshallow'])
84-
if (!(await hasMergeBase())) {
85-
noMergeBase = true
67+
if (!(await hasMergeBase())) {
68+
await exec('git', ['fetch', `--depth=${initialFetchDepth}`, 'origin', base, ref])
69+
let depth = initialFetchDepth
70+
let lastCommitCount = await getCommitCount()
71+
while (!(await hasMergeBase())) {
72+
depth = Math.min(depth * 2, Number.MAX_SAFE_INTEGER)
73+
await exec('git', ['fetch', `--deepen=${depth}`, 'origin', base, ref])
74+
const commitCount = await getCommitCount()
75+
if (commitCount === lastCommitCount) {
76+
core.info('No more commits were fetched')
77+
core.info('Last attempt will be to fetch full history')
78+
await exec('git', ['fetch'])
79+
if (!(await hasMergeBase())) {
80+
noMergeBase = true
81+
}
82+
break
8683
}
87-
break
84+
lastCommitCount = commitCount
8885
}
89-
depth = Math.min(depth * 2, Number.MAX_SAFE_INTEGER)
90-
lastCommitCount = commitCount
9186
}
9287
} finally {
9388
core.endGroup()

0 commit comments

Comments
 (0)