Handle stale SSH repo worktree refreshes (#1508)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinwoo Hong
2026-05-06 13:01:09 -07:00
committed by GitHub
co-authored by Orca
parent 9c24425504
commit 28caefd82c
5 changed files with 327 additions and 44 deletions
+21
View File
@@ -341,6 +341,27 @@ describe('listWorktrees', () => {
expect(translateWslOutputPathsMock).toHaveBeenCalledTimes(2)
})
it('returns no worktrees when the repo path is gone', async () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
gitExecFileAsyncMock.mockRejectedValueOnce(
Object.assign(new Error('spawn git ENOENT'), {
code: 'ENOENT'
})
)
statMock.mockRejectedValueOnce(Object.assign(new Error('ENOENT'), { code: 'ENOENT' }))
await expect(listWorktrees('/workspace/deleted-repo')).resolves.toEqual([])
expect(gitExecFileAsyncMock).toHaveBeenCalledWith(['worktree', 'list', '--porcelain'], {
cwd: '/workspace/deleted-repo'
})
expect(statMock).toHaveBeenCalledWith('/workspace/deleted-repo')
expect(warnSpy).toHaveBeenCalledWith(
'[git/worktree] repo path missing; skipping worktree list: /workspace/deleted-repo'
)
warnSpy.mockRestore()
})
it('detects sparse checkout after translating paths when porcelain omits sparse token', async () => {
gitExecFileAsyncMock.mockImplementation((args: string[]) => {
if (args.join(' ') === 'worktree list --porcelain') {
+17 -1
View File
@@ -8,6 +8,12 @@ type SparseWorktreeCreateError = Error & {
cleanupFailed?: boolean
}
function getErrorCode(error: unknown): string | undefined {
return typeof error === 'object' && error !== null && 'code' in error
? String((error as { code?: unknown }).code)
: undefined
}
function normalizeLocalBranchRef(branch: string): string {
return branch.replace(/^refs\/heads\//, '')
}
@@ -106,10 +112,20 @@ export async function listWorktrees(repoPath: string): Promise<GitWorktreeInfo[]
})
)
} catch (err) {
if (getErrorCode(err) === 'ENOENT') {
try {
await stat(repoPath)
} catch (statErr) {
if (getErrorCode(statErr) === 'ENOENT') {
console.warn(`[git/worktree] repo path missing; skipping worktree list: ${repoPath}`)
return []
}
}
}
// 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)
console.warn(`[git/worktree] listWorktrees failed for ${repoPath}:`, err)
return []
}
}