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 <eric@collectivegenius.net>
This commit is contained in:
Peter Evans 2024-03-12 23:16:55 +09:00 committed by GitHub
parent 57a101480a
commit 70a41aba78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 35 additions and 12 deletions

View file

@ -140,10 +140,22 @@ describe('create-or-update-branch tests', () => {
}) })
async function beforeTest(): Promise<void> { async function beforeTest(): Promise<void> {
await git.fetch(
[`${DEFAULT_BRANCH}:${DEFAULT_BRANCH}`],
REMOTE_NAME,
['--force', '--update-head-ok'],
true
)
await git.checkout(DEFAULT_BRANCH) await git.checkout(DEFAULT_BRANCH)
} }
async function afterTest(deleteRemote = true): Promise<void> { async function afterTest(deleteRemote = true): Promise<void> {
await git.fetch(
[`${DEFAULT_BRANCH}:${DEFAULT_BRANCH}`],
REMOTE_NAME,
['--force', '--update-head-ok'],
true
)
await git.checkout(DEFAULT_BRANCH) await git.checkout(DEFAULT_BRANCH)
try { try {
// Get the upstream branch if it exists // Get the upstream branch if it exists
@ -1454,8 +1466,7 @@ describe('create-or-update-branch tests', () => {
expect( expect(
await gitLogMatches([ await gitLogMatches([
_commitMessage, _commitMessage,
...commits.commitMsgs, commits.commitMsgs[0] // fetch depth of base is 1
INIT_COMMIT_MESSAGE
]) ])
).toBeTruthy() ).toBeTruthy()
}) })
@ -1590,7 +1601,9 @@ describe('create-or-update-branch tests', () => {
expect(await getFileContent(TRACKED_FILE)).toEqual(_changes.tracked) expect(await getFileContent(TRACKED_FILE)).toEqual(_changes.tracked)
expect(await getFileContent(UNTRACKED_FILE)).toEqual(_changes.untracked) expect(await getFileContent(UNTRACKED_FILE)).toEqual(_changes.untracked)
expect( expect(
await gitLogMatches([...commits.commitMsgs, INIT_COMMIT_MESSAGE]) await gitLogMatches([
commits.commitMsgs[0] // fetch depth of base is 1
])
).toBeTruthy() ).toBeTruthy()
}) })
@ -1668,7 +1681,9 @@ describe('create-or-update-branch tests', () => {
expect(await getFileContent(TRACKED_FILE)).toEqual(_changes.tracked) expect(await getFileContent(TRACKED_FILE)).toEqual(_changes.tracked)
expect(await getFileContent(UNTRACKED_FILE)).toEqual(_changes.untracked) expect(await getFileContent(UNTRACKED_FILE)).toEqual(_changes.untracked)
expect( expect(
await gitLogMatches([...commits.commitMsgs, INIT_COMMIT_MESSAGE]) await gitLogMatches([
commits.commitMsgs[0] // fetch depth of base is 1
])
).toBeTruthy() ).toBeTruthy()
}) })
@ -1951,8 +1966,7 @@ describe('create-or-update-branch tests', () => {
await gitLogMatches([ await gitLogMatches([
_commitMessage, _commitMessage,
..._commits.commitMsgs, ..._commits.commitMsgs,
...commitsOnBase.commitMsgs, commitsOnBase.commitMsgs[0] // fetch depth of base is 1
INIT_COMMIT_MESSAGE
]) ])
).toBeTruthy() ).toBeTruthy()
}) })
@ -2147,8 +2161,7 @@ describe('create-or-update-branch tests', () => {
expect( expect(
await gitLogMatches([ await gitLogMatches([
_commitMessage, _commitMessage,
...commitsOnBase.commitMsgs, commitsOnBase.commitMsgs[0] // fetch depth of base is 1
INIT_COMMIT_MESSAGE
]) ])
).toBeTruthy() ).toBeTruthy()
}) })

9
dist/index.js vendored
View file

@ -183,8 +183,13 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
// This will also be true if the working base type is a commit // This will also be true if the working base type is a commit
if (workingBase != base) { if (workingBase != base) {
core.info(`Rebasing commits made to ${workingBaseType} '${workingBase}' on to base branch '${base}'`); core.info(`Rebasing commits made to ${workingBaseType} '${workingBase}' on to base branch '${base}'`);
const fetchArgs = ['--force'];
if (branchRemoteName != 'fork') {
// If pushing to a fork we cannot shallow fetch otherwise the 'shallow update not allowed' error occurs
fetchArgs.push('--depth=1');
}
// Checkout the actual base // Checkout the actual base
yield git.fetch([`${base}:${base}`], baseRemote, ['--force']); yield git.fetch([`${base}:${base}`], baseRemote, fetchArgs);
yield git.checkout(base); yield git.checkout(base);
// Cherrypick commits from the temporary branch starting from the working base // Cherrypick commits from the temporary branch starting from the working base
const commits = yield git.revList([`${workingBase}..${tempBranch}`, '.'], ['--reverse']); const commits = yield git.revList([`${workingBase}..${tempBranch}`, '.'], ['--reverse']);
@ -197,7 +202,7 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
// Reset the temp branch to the working index // Reset the temp branch to the working index
yield git.checkout(tempBranch, 'HEAD'); yield git.checkout(tempBranch, 'HEAD');
// Reset the base // Reset the base
yield git.fetch([`${base}:${base}`], baseRemote, ['--force']); yield git.fetch([`${base}:${base}`], baseRemote, fetchArgs);
} }
// Try to fetch the pull request branch // Try to fetch the pull request branch
if (!(yield tryFetch(git, branchRemoteName, branch))) { if (!(yield tryFetch(git, branchRemoteName, branch))) {

View file

@ -199,8 +199,13 @@ export async function createOrUpdateBranch(
core.info( core.info(
`Rebasing commits made to ${workingBaseType} '${workingBase}' on to base branch '${base}'` `Rebasing commits made to ${workingBaseType} '${workingBase}' on to base branch '${base}'`
) )
const fetchArgs = ['--force']
if (branchRemoteName != 'fork') {
// If pushing to a fork we cannot shallow fetch otherwise the 'shallow update not allowed' error occurs
fetchArgs.push('--depth=1')
}
// Checkout the actual base // Checkout the actual base
await git.fetch([`${base}:${base}`], baseRemote, ['--force']) await git.fetch([`${base}:${base}`], baseRemote, fetchArgs)
await git.checkout(base) await git.checkout(base)
// Cherrypick commits from the temporary branch starting from the working base // Cherrypick commits from the temporary branch starting from the working base
const commits = await git.revList( const commits = await git.revList(
@ -219,7 +224,7 @@ export async function createOrUpdateBranch(
// Reset the temp branch to the working index // Reset the temp branch to the working index
await git.checkout(tempBranch, 'HEAD') await git.checkout(tempBranch, 'HEAD')
// Reset the base // Reset the base
await git.fetch([`${base}:${base}`], baseRemote, ['--force']) await git.fetch([`${base}:${base}`], baseRemote, fetchArgs)
} }
// Try to fetch the pull request branch // Try to fetch the pull request branch