perf(worktrees): avoid redundant fetch during deletion (#11918)

This commit is contained in:
Neil
2026-08-01 21:59:41 -07:00
committed by GitHub
parent 4be4d10ae0
commit 6e2a88c091
8 changed files with 223 additions and 30 deletions
+11 -5
View File
@@ -574,6 +574,9 @@ branch refs/heads/main
'git rev-parse --verify --quiet refs/remotes/origin/main^{commit}': {
stdout: 'base123\n'
},
'git rev-parse --verify --quiet HEAD^{commit}': {
stdout: 'base123\n'
},
'git merge-tree --write-tree base123 refs/heads/feature/test': {
stdout: 'tree123\n'
},
@@ -589,6 +592,7 @@ branch refs/heads/main
expect(calls).toContain('git merge-tree --write-tree base123 refs/heads/feature/test')
expect(calls).toContain('git update-ref -d refs/heads/feature/test def456')
expect(calls).toContain('git config --remove-section branch.feature/test')
expect(calls).not.toContain('git remote')
})
it('deletes a squash-merged branch with branch-only merge commits via expected head', async () => {
@@ -732,13 +736,15 @@ branch refs/heads/main
await expect(removeWorktree('/repo', '/repo-feature')).resolves.toEqual({})
const calls = getGitCalls()
const mergeTreeCall = 'git merge-tree --write-tree base123 refs/heads/feature/test'
const mergeTreeIndexes = calls.flatMap((call, index) => (call === mergeTreeCall ? [index] : []))
const fetchIndex = calls.indexOf('git fetch --prune origin')
const updateRefIndex = calls.indexOf('git update-ref -d refs/heads/feature/test def456')
expect(calls).toContain('git fetch --prune origin')
expect(calls).toContain('git update-ref -d refs/heads/feature/test def456')
expectGitCallOrder(
calls,
'git fetch --prune origin',
'git merge-tree --write-tree base123 refs/heads/feature/test'
)
expect(mergeTreeIndexes).toHaveLength(1)
expect(fetchIndex).toBeLessThan(mergeTreeIndexes[0])
expect(mergeTreeIndexes[0]).toBeLessThan(updateRefIndex)
expectGitCallOrder(
calls,
'git fetch --prune origin',
+3 -5
View File
@@ -2,9 +2,8 @@
import { readFile, stat } from 'node:fs/promises'
import { isAbsolute, join, posix, resolve, win32 } from 'node:path'
import {
branchHasNoUnmergedChangesOnAnyTarget,
getBranchCleanupTargetRefs,
refreshBranchCleanupTargetRefs
branchHasNoUnmergedChangesWithLazyTargetRefresh,
getBranchCleanupTargetRefs
} from '../../shared/git-branch-cleanup'
import { resolveWorktreeAddBaseRef } from '../../shared/worktree-base-ref'
import { withSpan } from '../observability/tracer'
@@ -1267,10 +1266,9 @@ async function deleteAlreadyMergedBranchAfterSafeDeleteFailure(
...(execOptions?.stdin !== undefined ? { stdin: execOptions.stdin } : {})
})
const targetRefs = await getBranchCleanupTargetRefs(runGit, branchName)
await refreshBranchCleanupTargetRefs(runGit, targetRefs)
// Why: squash merges rewrite commit IDs, so `branch -d` rejects already-merged branches; delete only when Git proves no unmerged tree changes.
if (
!(await branchHasNoUnmergedChangesOnAnyTarget(
!(await branchHasNoUnmergedChangesWithLazyTargetRefresh(
runGit,
branchName,
targetRefs,