fix(worktree): drop unsupported -z from git worktree list --porcelain (#1470)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -87,7 +87,7 @@ describe('removeWorktree', () => {
|
||||
|
||||
it('removes the worktree, prunes stale refs, and deletes its local branch', async () => {
|
||||
mockGitCommands({
|
||||
'git worktree list --porcelain -z': {
|
||||
'git worktree list --porcelain': {
|
||||
stdout: `worktree /repo
|
||||
HEAD abc123
|
||||
branch refs/heads/main
|
||||
@@ -97,7 +97,7 @@ HEAD def456
|
||||
branch refs/heads/feature/test
|
||||
`
|
||||
},
|
||||
'git worktree list --porcelain -z#2': {
|
||||
'git worktree list --porcelain#2': {
|
||||
stdout: `worktree /repo
|
||||
HEAD abc123
|
||||
branch refs/heads/main
|
||||
@@ -121,7 +121,7 @@ branch refs/heads/main
|
||||
|
||||
it('skips branch deletion when another worktree still points at the branch', async () => {
|
||||
mockGitCommands({
|
||||
'git worktree list --porcelain -z': {
|
||||
'git worktree list --porcelain': {
|
||||
stdout: `worktree /repo
|
||||
HEAD abc123
|
||||
branch refs/heads/main
|
||||
@@ -135,7 +135,7 @@ HEAD def456
|
||||
branch refs/heads/feature/test
|
||||
`
|
||||
},
|
||||
'git worktree list --porcelain -z#2': {
|
||||
'git worktree list --porcelain#2': {
|
||||
stdout: `worktree /repo
|
||||
HEAD abc123
|
||||
branch refs/heads/main
|
||||
@@ -154,7 +154,7 @@ branch refs/heads/feature/test
|
||||
expect.arrayContaining([
|
||||
'git worktree remove /repo-feature',
|
||||
'git worktree prune',
|
||||
'git worktree list --porcelain -z'
|
||||
'git worktree list --porcelain'
|
||||
])
|
||||
)
|
||||
expect(calls).not.toContain('git branch -D feature/test')
|
||||
@@ -163,7 +163,7 @@ branch refs/heads/feature/test
|
||||
|
||||
it('deletes the branch after prune removes stale sibling worktree entries', async () => {
|
||||
mockGitCommands({
|
||||
'git worktree list --porcelain -z': {
|
||||
'git worktree list --porcelain': {
|
||||
stdout: `worktree /repo
|
||||
HEAD abc123
|
||||
branch refs/heads/main
|
||||
@@ -178,7 +178,7 @@ branch refs/heads/feature/test
|
||||
prunable gitdir file points to non-existent location
|
||||
`
|
||||
},
|
||||
'git worktree list --porcelain -z#2': {
|
||||
'git worktree list --porcelain#2': {
|
||||
stdout: `worktree /repo
|
||||
HEAD abc123
|
||||
branch refs/heads/main
|
||||
@@ -201,7 +201,7 @@ branch refs/heads/main
|
||||
|
||||
it('passes --force before the worktree path when forced removal is requested', async () => {
|
||||
mockGitCommands({
|
||||
'git worktree list --porcelain -z': {
|
||||
'git worktree list --porcelain': {
|
||||
stdout: `worktree /repo
|
||||
HEAD abc123
|
||||
branch refs/heads/main
|
||||
@@ -211,7 +211,7 @@ HEAD def456
|
||||
branch refs/heads/feature/test
|
||||
`
|
||||
},
|
||||
'git worktree list --porcelain -z#2': {
|
||||
'git worktree list --porcelain#2': {
|
||||
stdout: `worktree /repo
|
||||
HEAD abc123
|
||||
branch refs/heads/main
|
||||
@@ -226,7 +226,7 @@ branch refs/heads/main
|
||||
|
||||
it('matches Windows worktree paths before deleting the branch', async () => {
|
||||
mockGitCommands({
|
||||
'git worktree list --porcelain -z': {
|
||||
'git worktree list --porcelain': {
|
||||
stdout: `worktree C:/repo
|
||||
HEAD abc123
|
||||
branch refs/heads/main
|
||||
@@ -236,7 +236,7 @@ HEAD def456
|
||||
branch refs/heads/feature/test
|
||||
`
|
||||
},
|
||||
'git worktree list --porcelain -z#2': {
|
||||
'git worktree list --porcelain#2': {
|
||||
stdout: `worktree C:/repo
|
||||
HEAD abc123
|
||||
branch refs/heads/main
|
||||
@@ -259,7 +259,7 @@ branch refs/heads/main
|
||||
it('keeps removal successful when branch cleanup fails', async () => {
|
||||
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
mockGitCommands({
|
||||
'git worktree list --porcelain -z': {
|
||||
'git worktree list --porcelain': {
|
||||
stdout: `worktree /repo
|
||||
HEAD abc123
|
||||
branch refs/heads/main
|
||||
@@ -269,7 +269,7 @@ HEAD def456
|
||||
branch refs/heads/feature/test
|
||||
`
|
||||
},
|
||||
'git worktree list --porcelain -z#2': {
|
||||
'git worktree list --porcelain#2': {
|
||||
stdout: `worktree /repo
|
||||
HEAD abc123
|
||||
branch refs/heads/main
|
||||
@@ -306,16 +306,15 @@ describe('listWorktrees', () => {
|
||||
resolveGitDirMock.mockImplementation(async (worktreePath: string) => `${worktreePath}/.git`)
|
||||
})
|
||||
|
||||
it('translates parsed path fields from NUL-delimited porcelain output', async () => {
|
||||
it('translates parsed path fields from line-block porcelain output', async () => {
|
||||
gitExecFileAsyncMock.mockResolvedValueOnce({
|
||||
stdout:
|
||||
'worktree /home/me/repo\0HEAD abc123\0branch refs/heads/main\0\0' +
|
||||
'worktree /home/me/repo-feature\0HEAD def456\0branch refs/heads/feature/test\0sparse\0\0'
|
||||
})
|
||||
translateWslOutputPathsMock.mockImplementation((output: string) => {
|
||||
expect(output).not.toContain('\0')
|
||||
return output.replace('/home/me/', '\\\\wsl.localhost\\Ubuntu\\home\\me\\')
|
||||
'worktree /home/me/repo\nHEAD abc123\nbranch refs/heads/main\n\n' +
|
||||
'worktree /home/me/repo-feature\nHEAD def456\nbranch refs/heads/feature/test\nsparse\n\n'
|
||||
})
|
||||
translateWslOutputPathsMock.mockImplementation((output: string) =>
|
||||
output.replace('/home/me/', '\\\\wsl.localhost\\Ubuntu\\home\\me\\')
|
||||
)
|
||||
|
||||
await expect(listWorktrees('\\\\wsl.localhost\\Ubuntu\\home\\me\\repo')).resolves.toEqual([
|
||||
{
|
||||
@@ -337,27 +336,26 @@ describe('listWorktrees', () => {
|
||||
// Why: the non-sparse main worktree gets an fs probe of its sparse config
|
||||
// file; the linked worktree short-circuits on the parsed `sparse` token and
|
||||
// does not. Only one git subprocess runs regardless of worktree count.
|
||||
expect(getGitCalls()).toEqual(['git worktree list --porcelain -z'])
|
||||
expect(getGitCalls()).toEqual(['git worktree list --porcelain'])
|
||||
expect(statMock).toHaveBeenCalledTimes(1)
|
||||
expect(translateWslOutputPathsMock).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('detects sparse checkout after translating paths when porcelain omits sparse token', async () => {
|
||||
gitExecFileAsyncMock.mockImplementation((args: string[]) => {
|
||||
if (args.join(' ') === 'worktree list --porcelain -z') {
|
||||
if (args.join(' ') === 'worktree list --porcelain') {
|
||||
return {
|
||||
stdout:
|
||||
'worktree /home/me/repo\0HEAD abc123\0branch refs/heads/main\0\0' +
|
||||
'worktree /home/me/repo-feature\0HEAD def456\0branch refs/heads/feature/test\0\0',
|
||||
'worktree /home/me/repo\nHEAD abc123\nbranch refs/heads/main\n\n' +
|
||||
'worktree /home/me/repo-feature\nHEAD def456\nbranch refs/heads/feature/test\n\n',
|
||||
stderr: ''
|
||||
}
|
||||
}
|
||||
throw new Error(`Unexpected git call: ${args.join(' ')}`)
|
||||
})
|
||||
translateWslOutputPathsMock.mockImplementation((output: string) => {
|
||||
expect(output).not.toContain('\0')
|
||||
return output.replace('/home/me/', '\\\\wsl.localhost\\Ubuntu\\home\\me\\')
|
||||
})
|
||||
translateWslOutputPathsMock.mockImplementation((output: string) =>
|
||||
output.replace('/home/me/', '\\\\wsl.localhost\\Ubuntu\\home\\me\\')
|
||||
)
|
||||
const featureWorktreePath = '\\\\wsl.localhost\\Ubuntu\\home\\me\\repo-feature'
|
||||
resolveGitDirMock.mockImplementation(async (worktreePath: string) =>
|
||||
worktreePath === featureWorktreePath
|
||||
@@ -394,7 +392,7 @@ describe('listWorktrees', () => {
|
||||
// Why: the detection path must not spawn a git subprocess per worktree —
|
||||
// the perf regression in #1131 came from `git sparse-checkout list` firing
|
||||
// on every poll.
|
||||
expect(getGitCalls()).toEqual(['git worktree list --porcelain -z'])
|
||||
expect(getGitCalls()).toEqual(['git worktree list --porcelain'])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -428,7 +426,7 @@ describe('addSparseWorktree', () => {
|
||||
'git sparse-checkout set -- packages/web': {
|
||||
error: new Error('sparse setup failed')
|
||||
},
|
||||
'git worktree list --porcelain -z': {
|
||||
'git worktree list --porcelain': {
|
||||
stdout: `worktree /repo
|
||||
HEAD abc123
|
||||
branch refs/heads/main
|
||||
@@ -438,7 +436,7 @@ HEAD def456
|
||||
branch refs/heads/feature/test
|
||||
`
|
||||
},
|
||||
'git worktree list --porcelain -z#2': {
|
||||
'git worktree list --porcelain#2': {
|
||||
stdout: `worktree /repo
|
||||
HEAD abc123
|
||||
branch refs/heads/main
|
||||
|
||||
+13
-32
@@ -35,12 +35,10 @@ function looksLikeWindowsPath(pathValue: string): boolean {
|
||||
*/
|
||||
export function parseWorktreeList(output: string): GitWorktreeInfo[] {
|
||||
const worktrees: GitWorktreeInfo[] = []
|
||||
const blocks = output.includes('\0')
|
||||
? parseNullDelimitedWorktreeBlocks(output)
|
||||
: output
|
||||
.trim()
|
||||
.split(/\r?\n\r?\n/)
|
||||
.map((block) => block.trim().split(/\r?\n/))
|
||||
const blocks = output
|
||||
.trim()
|
||||
.split(/\r?\n\r?\n/)
|
||||
.map((block) => block.trim().split(/\r?\n/))
|
||||
|
||||
for (const lines of blocks) {
|
||||
if (lines.length === 0) {
|
||||
@@ -88,11 +86,12 @@ export function parseWorktreeList(output: string): GitWorktreeInfo[] {
|
||||
*/
|
||||
export async function listWorktrees(repoPath: string): Promise<GitWorktreeInfo[]> {
|
||||
try {
|
||||
const { stdout } = await gitExecFileAsync(['worktree', 'list', '--porcelain', '-z'], {
|
||||
// Why: do not pass `-z` here. `-z` requires Git ≥ 2.36; older Git rejects
|
||||
// it, listWorktrees returns [], and every create flow throws "Worktree
|
||||
// created but not found in listing" (issue #1453).
|
||||
const { stdout } = await gitExecFileAsync(['worktree', 'list', '--porcelain'], {
|
||||
cwd: repoPath
|
||||
})
|
||||
// Why: WSL path translation is line-oriented, but `-z` porcelain output is
|
||||
// NUL-delimited. Parse first so only complete path fields are translated.
|
||||
const worktrees = parseWorktreeList(stdout).map((worktree) => {
|
||||
const translatedPath = translateWorktreePath(worktree.path, repoPath)
|
||||
return translatedPath === worktree.path ? worktree : { ...worktree, path: translatedPath }
|
||||
@@ -106,7 +105,11 @@ export async function listWorktrees(repoPath: string): Promise<GitWorktreeInfo[]
|
||||
return isSparse ? { ...worktree, isSparse } : worktree
|
||||
})
|
||||
)
|
||||
} catch {
|
||||
} catch (err) {
|
||||
// Why: a silent catch turned issue #1453's underlying
|
||||
// "git: unknown switch -z" into the opaque "not found in listing" toast.
|
||||
// Surface the cause so future regressions show up immediately.
|
||||
console.warn('[git/worktree] listWorktrees failed:', err)
|
||||
return []
|
||||
}
|
||||
}
|
||||
@@ -273,28 +276,6 @@ export async function removeWorktree(
|
||||
}
|
||||
}
|
||||
|
||||
function parseNullDelimitedWorktreeBlocks(output: string): string[][] {
|
||||
const blocks: string[][] = []
|
||||
let current: string[] = []
|
||||
|
||||
for (const token of output.split('\0')) {
|
||||
if (!token) {
|
||||
if (current.length > 0) {
|
||||
blocks.push(current)
|
||||
current = []
|
||||
}
|
||||
continue
|
||||
}
|
||||
current.push(token)
|
||||
}
|
||||
|
||||
if (current.length > 0) {
|
||||
blocks.push(current)
|
||||
}
|
||||
|
||||
return blocks
|
||||
}
|
||||
|
||||
function translateWorktreePath(worktreePath: string, repoPath: string): string {
|
||||
const prefix = 'worktree '
|
||||
const translated = translateWslOutputPaths(`${prefix}${worktreePath}`, repoPath)
|
||||
|
||||
Reference in New Issue
Block a user