fix: handle non-git worktree paths (#1805)

This commit is contained in:
Jinjing 2026-05-13 22:22:51 -07:00 committed by GitHub
parent d08a1b9525
commit 17aef50c82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 0 deletions

View File

@ -362,6 +362,25 @@ describe('listWorktrees', () => {
warnSpy.mockRestore()
})
it('returns no worktrees when the path exists but is not a git repo', async () => {
const warnSpy = vi.spyOn(console, 'warn')
gitExecFileAsyncMock.mockRejectedValueOnce(
Object.assign(new Error('Command failed: git worktree list --porcelain'), {
code: 128,
stdout: '',
stderr: 'fatal: not a git repository (or any of the parent directories): .git\n'
})
)
await expect(listWorktrees('/private/tmp/orca-issue-1582-test/my-repo')).resolves.toEqual([])
expect(gitExecFileAsyncMock).toHaveBeenCalledWith(['worktree', 'list', '--porcelain'], {
cwd: '/private/tmp/orca-issue-1582-test/my-repo'
})
expect(warnSpy).not.toHaveBeenCalled()
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') {

View File

@ -14,6 +14,24 @@ function getErrorCode(error: unknown): string | undefined {
: undefined
}
function getErrorText(error: unknown): string {
if (typeof error === 'object' && error !== null) {
const parts: string[] = []
if ('message' in error && typeof error.message === 'string') {
parts.push(error.message)
}
if ('stderr' in error && typeof error.stderr === 'string') {
parts.push(error.stderr)
}
return parts.join('\n')
}
return String(error)
}
function isNotGitRepositoryError(error: unknown): boolean {
return /not a git repository/i.test(getErrorText(error))
}
function normalizeLocalBranchRef(branch: string): string {
return branch.replace(/^refs\/heads\//, '')
}
@ -122,6 +140,9 @@ export async function listWorktrees(repoPath: string): Promise<GitWorktreeInfo[]
}
}
}
if (isNotGitRepositoryError(err)) {
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.