feat: add revert/discard support for untracked files in source control (#140)
- Enhance discardChanges to handle untracked files by deleting them instead of git checkout - Add path traversal protection to prevent discarding files outside the worktree - Extract source control action logic into getSourceControlActions helper - Show revert button for untracked files with confirmation dialog - Add tests for both backend and frontend logic Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
00332fbd3e
commit
cff5b034e4
@@ -0,0 +1,78 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const { execFileAsyncMock, readFileMock, rmMock } = vi.hoisted(() => ({
|
||||
execFileAsyncMock: vi.fn(),
|
||||
readFileMock: vi.fn(),
|
||||
rmMock: vi.fn()
|
||||
}))
|
||||
|
||||
vi.mock('util', async () => {
|
||||
const actual = await vi.importActual('util')
|
||||
return {
|
||||
...actual,
|
||||
promisify: vi.fn(() => execFileAsyncMock)
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('fs/promises', () => ({
|
||||
readFile: readFileMock,
|
||||
rm: rmMock
|
||||
}))
|
||||
|
||||
import { discardChanges } from './status'
|
||||
|
||||
describe('discardChanges', () => {
|
||||
beforeEach(() => {
|
||||
execFileAsyncMock.mockReset()
|
||||
readFileMock.mockReset()
|
||||
rmMock.mockReset()
|
||||
})
|
||||
|
||||
it('restores tracked files from HEAD', async () => {
|
||||
execFileAsyncMock.mockResolvedValueOnce({ stdout: 'src/file.ts\n' })
|
||||
execFileAsyncMock.mockResolvedValueOnce({ stdout: '' })
|
||||
|
||||
await discardChanges('/repo', 'src/file.ts')
|
||||
|
||||
expect(execFileAsyncMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'git',
|
||||
['ls-files', '--error-unmatch', '--', 'src/file.ts'],
|
||||
{
|
||||
cwd: '/repo',
|
||||
encoding: 'utf-8'
|
||||
}
|
||||
)
|
||||
expect(execFileAsyncMock).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'git',
|
||||
['restore', '--worktree', '--source=HEAD', '--', 'src/file.ts'],
|
||||
{
|
||||
cwd: '/repo',
|
||||
encoding: 'utf-8'
|
||||
}
|
||||
)
|
||||
expect(rmMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('removes untracked files from disk', async () => {
|
||||
execFileAsyncMock.mockRejectedValueOnce(new Error('not tracked'))
|
||||
|
||||
await discardChanges('/repo', 'src/new-file.ts')
|
||||
|
||||
expect(execFileAsyncMock).toHaveBeenCalledTimes(1)
|
||||
expect(rmMock).toHaveBeenCalledWith('/repo/src/new-file.ts', {
|
||||
force: true,
|
||||
recursive: true
|
||||
})
|
||||
})
|
||||
|
||||
it('rejects paths that traverse outside the worktree', async () => {
|
||||
await expect(discardChanges('/repo', '../../etc/passwd')).rejects.toThrow(
|
||||
'resolves outside the worktree'
|
||||
)
|
||||
|
||||
expect(execFileAsyncMock).not.toHaveBeenCalled()
|
||||
expect(rmMock).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
+25
-6
@@ -1,7 +1,7 @@
|
||||
import { execFile } from 'child_process'
|
||||
import { readFile } from 'fs/promises'
|
||||
import { readFile, rm } from 'fs/promises'
|
||||
import { promisify } from 'util'
|
||||
import { join } from 'path'
|
||||
import { join, resolve } from 'path'
|
||||
import type { GitStatusEntry, GitFileStatus, GitDiffResult } from '../../shared/types'
|
||||
|
||||
const execFileAsync = promisify(execFile)
|
||||
@@ -163,8 +163,27 @@ export async function unstageFile(worktreePath: string, filePath: string): Promi
|
||||
* Discard working tree changes for a file.
|
||||
*/
|
||||
export async function discardChanges(worktreePath: string, filePath: string): Promise<void> {
|
||||
await execFileAsync('git', ['checkout', '--', filePath], {
|
||||
cwd: worktreePath,
|
||||
encoding: 'utf-8'
|
||||
})
|
||||
const resolvedWorktree = resolve(worktreePath)
|
||||
const resolvedTarget = resolve(worktreePath, filePath)
|
||||
if (!resolvedTarget.startsWith(`${resolvedWorktree}/`)) {
|
||||
throw new Error(`Path "${filePath}" resolves outside the worktree`)
|
||||
}
|
||||
|
||||
let tracked = false
|
||||
try {
|
||||
await execFileAsync('git', ['ls-files', '--error-unmatch', '--', filePath], {
|
||||
cwd: worktreePath,
|
||||
encoding: 'utf-8'
|
||||
})
|
||||
tracked = true
|
||||
} catch {
|
||||
// File is not tracked by git
|
||||
}
|
||||
|
||||
await (tracked
|
||||
? execFileAsync('git', ['restore', '--worktree', '--source=HEAD', '--', filePath], {
|
||||
cwd: worktreePath,
|
||||
encoding: 'utf-8'
|
||||
})
|
||||
: rm(resolvedTarget, { force: true, recursive: true }))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user