git emits no ---/+++ for a binary file, a pure rename and a mode-only change, and parseUnifiedDiff only ever read paths from those two lines. The section therefore kept oldPath/newPath === '', so DiffFiles drew a header carrying just the binary badge and no file name (sandwiched between two .md rows, which is exactly how it reads as if those .md files were the binary ones), and a pure rename / mode-only change collapsed to an empty disabled button (no path, and fileTag cannot see a rename). Recover the paths in order of authority: ---/+++ when present, else the section's own `rename from|to`, `Binary files A and B differ`, or the `diff --git` header. Only the mode-only shape needs the last one: it takes the ` b/` whose two sides name the same file, and leaves the path empty rather than guessing. ---/+++ values also lose the TAB git appends to a spaced path, and decodeGitPath undoes git's C-quoting so diff text from a default-quotePath source still renders the real file name. runGit gains `-c core.quotePath=false` for the same reason at the source: git's default turns a space/CJK path into an octal-escaped quoted string, which every diff surface then renders as the file name. The `-z`-framed porcelain callers are unaffected (git never quotes NUL-framed output). Verified against taktop_redo 8b4b933, the commit whose three .jpg rows rendered as bare badges: all three now carry their full path, and real git output for space/CJK paths parses with no escapes and no trailing TAB. New tests: tests/diff-binary-row.spec.tsx (rendered rows) and the path matrix in tests/diff-rows.spec.ts; tests/git-spawn.spec.ts now derives the subcommand from runGit's prefix instead of counting its flags. tests/git.spec.ts had pinned the empty path (`oldPath === ''`) as the known behaviour of a mode/rename-only section; it now asserts the recovered paths instead, and the binary section there asserts its own name too.
103 lines
3.5 KiB
TypeScript
103 lines
3.5 KiB
TypeScript
import { EventEmitter } from 'node:events'
|
|
import { PassThrough } from 'node:stream'
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const { spawnMock } = vi.hoisted(() => ({ spawnMock: vi.fn() }))
|
|
|
|
vi.mock('node:child_process', () => ({ spawn: spawnMock }))
|
|
|
|
import { isGitRepo, worktrees } from '../src/git.ts'
|
|
|
|
afterEach(() => {
|
|
spawnMock.mockReset()
|
|
})
|
|
|
|
describe('git subprocess spawning', () => {
|
|
it('hides spawned git windows', async () => {
|
|
spawnMock.mockImplementation(() => {
|
|
const child = new EventEmitter()
|
|
const stdout = new PassThrough()
|
|
const stderr = new PassThrough()
|
|
Object.assign(child, { stdout, stderr, kill: vi.fn() })
|
|
|
|
queueMicrotask(() => {
|
|
stdout.end('true\n')
|
|
stderr.end()
|
|
child.emit('close', 0)
|
|
})
|
|
|
|
return child
|
|
})
|
|
|
|
await expect(isGitRepo('C:\\repo')).resolves.toBe(true)
|
|
|
|
expect(spawnMock).toHaveBeenCalledTimes(1)
|
|
expect(spawnMock).toHaveBeenCalledWith(
|
|
'git',
|
|
['-C', 'C:\\repo', '--no-pager', '-c', 'color.ui=false', '-c', 'core.quotePath=false', 'rev-parse', '--is-inside-work-tree'],
|
|
expect.objectContaining({ windowsHide: true }),
|
|
)
|
|
})
|
|
|
|
it('falls back when worktree list does not support -z', async () => {
|
|
// The subcommand starts after runGit's own prefix (`-C <cwd>
|
|
// --no-pager` plus any number of `-c key=value` flags), so find it
|
|
// instead of counting: pinning the count breaks on every added `-c`.
|
|
const subcommandOf = (args: string[]): string[] => {
|
|
let at = 0
|
|
while (at < args.length) {
|
|
if (args[at] === '-C' || args[at] === '-c') { at += 2; continue }
|
|
if (args[at] === '--no-pager') { at += 1; continue }
|
|
break
|
|
}
|
|
return args.slice(at)
|
|
}
|
|
|
|
const gitChild = (stdoutText = '', stderrText = '', code = 0): EventEmitter => {
|
|
const child = new EventEmitter()
|
|
const stdout = new PassThrough()
|
|
const stderr = new PassThrough()
|
|
Object.assign(child, { stdout, stderr, kill: vi.fn() })
|
|
queueMicrotask(() => {
|
|
stdout.end(stdoutText)
|
|
stderr.end(stderrText)
|
|
child.emit('close', code)
|
|
})
|
|
return child
|
|
}
|
|
|
|
spawnMock.mockImplementation((_file: string, args: string[]) => {
|
|
const command = subcommandOf(args)
|
|
if (command[0] === 'rev-parse' && command[1] === '--is-inside-work-tree') return gitChild('true\n')
|
|
if (command[0] === 'rev-parse' && command[1] === '--show-toplevel') return gitChild('C:\\repo\n')
|
|
if (command[0] === 'rev-parse' && command[1] === '--abbrev-ref') return gitChild('main\n')
|
|
if (command[0] === 'status') return gitChild('')
|
|
if (command[0] === 'worktree' && command.includes('-z')) {
|
|
return gitChild('', "error: unknown switch `z'\n", 129)
|
|
}
|
|
if (command[0] === 'worktree') {
|
|
return gitChild([
|
|
'worktree C:\\repo',
|
|
'HEAD abc',
|
|
'branch refs/heads/main',
|
|
'',
|
|
].join('\n'))
|
|
}
|
|
throw new Error(`unexpected git command: ${command.join(' ')}`)
|
|
})
|
|
|
|
await expect(worktrees('C:\\repo')).resolves.toEqual([
|
|
{ path: 'C:\\repo', branch: 'main', current: true, changes: 0 },
|
|
])
|
|
await expect(worktrees('C:\\repo')).resolves.toEqual([
|
|
{ path: 'C:\\repo', branch: 'main', current: true, changes: 0 },
|
|
])
|
|
|
|
const nulAttempts = spawnMock.mock.calls.filter((call) => {
|
|
const args = call[1] as string[]
|
|
return args.includes('worktree') && args.includes('-z')
|
|
})
|
|
expect(nulAttempts).toHaveLength(1)
|
|
})
|
|
})
|