Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: shallow fetch the actual base when rebasing from working base #2816

Merged
merged 3 commits into from Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 21 additions & 8 deletions __test__/create-or-update-branch.int.test.ts
Expand Up @@ -140,10 +140,22 @@ describe('create-or-update-branch tests', () => {
})

async function beforeTest(): Promise<void> {
await git.fetch(
[`${DEFAULT_BRANCH}:${DEFAULT_BRANCH}`],
REMOTE_NAME,
['--force', '--update-head-ok'],
true
)
await git.checkout(DEFAULT_BRANCH)
}

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)
try {
// Get the upstream branch if it exists
Expand Down Expand Up @@ -1454,8 +1466,7 @@ describe('create-or-update-branch tests', () => {
expect(
await gitLogMatches([
_commitMessage,
...commits.commitMsgs,
INIT_COMMIT_MESSAGE
commits.commitMsgs[0] // fetch depth of base is 1
])
).toBeTruthy()
})
Expand Down Expand Up @@ -1590,7 +1601,9 @@ describe('create-or-update-branch tests', () => {
expect(await getFileContent(TRACKED_FILE)).toEqual(_changes.tracked)
expect(await getFileContent(UNTRACKED_FILE)).toEqual(_changes.untracked)
expect(
await gitLogMatches([...commits.commitMsgs, INIT_COMMIT_MESSAGE])
await gitLogMatches([
commits.commitMsgs[0] // fetch depth of base is 1
])
).toBeTruthy()
})

Expand Down Expand Up @@ -1668,7 +1681,9 @@ describe('create-or-update-branch tests', () => {
expect(await getFileContent(TRACKED_FILE)).toEqual(_changes.tracked)
expect(await getFileContent(UNTRACKED_FILE)).toEqual(_changes.untracked)
expect(
await gitLogMatches([...commits.commitMsgs, INIT_COMMIT_MESSAGE])
await gitLogMatches([
commits.commitMsgs[0] // fetch depth of base is 1
])
).toBeTruthy()
})

Expand Down Expand Up @@ -1951,8 +1966,7 @@ describe('create-or-update-branch tests', () => {
await gitLogMatches([
_commitMessage,
..._commits.commitMsgs,
...commitsOnBase.commitMsgs,
INIT_COMMIT_MESSAGE
commitsOnBase.commitMsgs[0] // fetch depth of base is 1
])
).toBeTruthy()
})
Expand Down Expand Up @@ -2147,8 +2161,7 @@ describe('create-or-update-branch tests', () => {
expect(
await gitLogMatches([
_commitMessage,
...commitsOnBase.commitMsgs,
INIT_COMMIT_MESSAGE
commitsOnBase.commitMsgs[0] // fetch depth of base is 1
])
).toBeTruthy()
})
Expand Down
9 changes: 7 additions & 2 deletions dist/index.js
Expand Up @@ -183,8 +183,13 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
// This will also be true if the working base type is a commit
if (workingBase != 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
yield git.fetch([`${base}:${base}`], baseRemote, ['--force']);
yield git.fetch([`${base}:${base}`], baseRemote, fetchArgs);
yield git.checkout(base);
// Cherrypick commits from the temporary branch starting from the working base
const commits = yield git.revList([`${workingBase}..${tempBranch}`, '.'], ['--reverse']);
Expand All @@ -197,7 +202,7 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
// Reset the temp branch to the working index
yield git.checkout(tempBranch, 'HEAD');
// Reset the base
yield git.fetch([`${base}:${base}`], baseRemote, ['--force']);
yield git.fetch([`${base}:${base}`], baseRemote, fetchArgs);
}
// Try to fetch the pull request branch
if (!(yield tryFetch(git, branchRemoteName, branch))) {
Expand Down
9 changes: 7 additions & 2 deletions src/create-or-update-branch.ts
Expand Up @@ -199,8 +199,13 @@ export async function createOrUpdateBranch(
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
await git.fetch([`${base}:${base}`], baseRemote, ['--force'])
await git.fetch([`${base}:${base}`], baseRemote, fetchArgs)
await git.checkout(base)
// Cherrypick commits from the temporary branch starting from the working base
const commits = await git.revList(
Expand All @@ -219,7 +224,7 @@ export async function createOrUpdateBranch(
// Reset the temp branch to the working index
await git.checkout(tempBranch, 'HEAD')
// Reset the base
await git.fetch([`${base}:${base}`], baseRemote, ['--force'])
await git.fetch([`${base}:${base}`], baseRemote, fetchArgs)
}

// Try to fetch the pull request branch
Expand Down