Skip to content

Commit 70a41ab

Browse files
perf: shallow fetch the actual base when rebasing from working base (#2816)
* Update git.fetch calls to use depth=1 (#2810) * When base is set, fetch depth=1 * PR Feedback - remove depth=1 from tryFetch function * push-to-fork fix * test updates to handle shallow fetch of base --------- Co-authored-by: Eric Webb <[email protected]>
1 parent 57a1014 commit 70a41ab

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

__test__/create-or-update-branch.int.test.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,22 @@ describe('create-or-update-branch tests', () => {
140140
})
141141

142142
async function beforeTest(): Promise<void> {
143+
await git.fetch(
144+
[`${DEFAULT_BRANCH}:${DEFAULT_BRANCH}`],
145+
REMOTE_NAME,
146+
['--force', '--update-head-ok'],
147+
true
148+
)
143149
await git.checkout(DEFAULT_BRANCH)
144150
}
145151

146152
async function afterTest(deleteRemote = true): Promise<void> {
153+
await git.fetch(
154+
[`${DEFAULT_BRANCH}:${DEFAULT_BRANCH}`],
155+
REMOTE_NAME,
156+
['--force', '--update-head-ok'],
157+
true
158+
)
147159
await git.checkout(DEFAULT_BRANCH)
148160
try {
149161
// Get the upstream branch if it exists
@@ -1454,8 +1466,7 @@ describe('create-or-update-branch tests', () => {
14541466
expect(
14551467
await gitLogMatches([
14561468
_commitMessage,
1457-
...commits.commitMsgs,
1458-
INIT_COMMIT_MESSAGE
1469+
commits.commitMsgs[0] // fetch depth of base is 1
14591470
])
14601471
).toBeTruthy()
14611472
})
@@ -1590,7 +1601,9 @@ describe('create-or-update-branch tests', () => {
15901601
expect(await getFileContent(TRACKED_FILE)).toEqual(_changes.tracked)
15911602
expect(await getFileContent(UNTRACKED_FILE)).toEqual(_changes.untracked)
15921603
expect(
1593-
await gitLogMatches([...commits.commitMsgs, INIT_COMMIT_MESSAGE])
1604+
await gitLogMatches([
1605+
commits.commitMsgs[0] // fetch depth of base is 1
1606+
])
15941607
).toBeTruthy()
15951608
})
15961609

@@ -1668,7 +1681,9 @@ describe('create-or-update-branch tests', () => {
16681681
expect(await getFileContent(TRACKED_FILE)).toEqual(_changes.tracked)
16691682
expect(await getFileContent(UNTRACKED_FILE)).toEqual(_changes.untracked)
16701683
expect(
1671-
await gitLogMatches([...commits.commitMsgs, INIT_COMMIT_MESSAGE])
1684+
await gitLogMatches([
1685+
commits.commitMsgs[0] // fetch depth of base is 1
1686+
])
16721687
).toBeTruthy()
16731688
})
16741689

@@ -1951,8 +1966,7 @@ describe('create-or-update-branch tests', () => {
19511966
await gitLogMatches([
19521967
_commitMessage,
19531968
..._commits.commitMsgs,
1954-
...commitsOnBase.commitMsgs,
1955-
INIT_COMMIT_MESSAGE
1969+
commitsOnBase.commitMsgs[0] // fetch depth of base is 1
19561970
])
19571971
).toBeTruthy()
19581972
})
@@ -2147,8 +2161,7 @@ describe('create-or-update-branch tests', () => {
21472161
expect(
21482162
await gitLogMatches([
21492163
_commitMessage,
2150-
...commitsOnBase.commitMsgs,
2151-
INIT_COMMIT_MESSAGE
2164+
commitsOnBase.commitMsgs[0] // fetch depth of base is 1
21522165
])
21532166
).toBeTruthy()
21542167
})

dist/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,13 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
183183
// This will also be true if the working base type is a commit
184184
if (workingBase != base) {
185185
core.info(`Rebasing commits made to ${workingBaseType} '${workingBase}' on to base branch '${base}'`);
186+
const fetchArgs = ['--force'];
187+
if (branchRemoteName != 'fork') {
188+
// If pushing to a fork we cannot shallow fetch otherwise the 'shallow update not allowed' error occurs
189+
fetchArgs.push('--depth=1');
190+
}
186191
// Checkout the actual base
187-
yield git.fetch([`${base}:${base}`], baseRemote, ['--force']);
192+
yield git.fetch([`${base}:${base}`], baseRemote, fetchArgs);
188193
yield git.checkout(base);
189194
// Cherrypick commits from the temporary branch starting from the working base
190195
const commits = yield git.revList([`${workingBase}..${tempBranch}`, '.'], ['--reverse']);
@@ -197,7 +202,7 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
197202
// Reset the temp branch to the working index
198203
yield git.checkout(tempBranch, 'HEAD');
199204
// Reset the base
200-
yield git.fetch([`${base}:${base}`], baseRemote, ['--force']);
205+
yield git.fetch([`${base}:${base}`], baseRemote, fetchArgs);
201206
}
202207
// Try to fetch the pull request branch
203208
if (!(yield tryFetch(git, branchRemoteName, branch))) {

src/create-or-update-branch.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,13 @@ export async function createOrUpdateBranch(
199199
core.info(
200200
`Rebasing commits made to ${workingBaseType} '${workingBase}' on to base branch '${base}'`
201201
)
202+
const fetchArgs = ['--force']
203+
if (branchRemoteName != 'fork') {
204+
// If pushing to a fork we cannot shallow fetch otherwise the 'shallow update not allowed' error occurs
205+
fetchArgs.push('--depth=1')
206+
}
202207
// Checkout the actual base
203-
await git.fetch([`${base}:${base}`], baseRemote, ['--force'])
208+
await git.fetch([`${base}:${base}`], baseRemote, fetchArgs)
204209
await git.checkout(base)
205210
// Cherrypick commits from the temporary branch starting from the working base
206211
const commits = await git.revList(
@@ -219,7 +224,7 @@ export async function createOrUpdateBranch(
219224
// Reset the temp branch to the working index
220225
await git.checkout(tempBranch, 'HEAD')
221226
// Reset the base
222-
await git.fetch([`${base}:${base}`], baseRemote, ['--force'])
227+
await git.fetch([`${base}:${base}`], baseRemote, fetchArgs)
223228
}
224229

225230
// Try to fetch the pull request branch

0 commit comments

Comments
 (0)