fix untracked file diff regex pattern and add tests

This commit is contained in:
Chris Scott committed 2026-01-04 17:23:26 -05:00
1 parent ed4bc17987
commit b9b139ce48
4 files changed
+118 -4

No files matched your search

+3 -3
View File
@@ -232,14 +232,14 @@ export async function getFileDiff(repoPath: string, filePath: string, database?:
if (fileStatus.status === 'untracked') {
try {
const content = await executeCommand(['git', '-C', fullRepoPath, 'diff', '--no-index', '--', '/dev/null', filePath], { env })
const content = await executeCommand(['git', '-C', fullRepoPath, 'diff', '--no-index', '--', '/dev/null', filePath], { env, silent: true })
diff = content
} catch (error: unknown) {
const errorMessage = getErrorMessage(error)
if (errorMessage?.includes('exit code 1') || errorMessage?.includes('Command failed with code 1')) {
const output = errorMessage || ''
const diffMatch = output.match(/diff --git[\s\S]*/)
diff = diffMatch ? diffMatch[0] : `New file: ${filePath}`
const diffMatch = output.match(/diff --git[\s\S]*(?=\n\n No newline|$)/)
diff = diffMatch ? diffMatch[0] : output.substring(output.indexOf('diff --git'))
} else {
diff = `New file: ${filePath}`
}
+46
View File
@@ -0,0 +1,46 @@
import { mkdir, writeFile, rm } from 'fs/promises'
import { join } from 'path'
import { executeCommand } from '../../src/utils/process'
export function gitReuseDir(name: string): string {
const base = process.env.TMPDIR || '/tmp'
return join(base, `opencode-test-${name}`)
}
export async function setupGitTestRepo(repoPath: string): Promise<void> {
try {
await rm(repoPath, { recursive: true, force: true })
} catch {
// Ignore if dir doesn't exist
}
await mkdir(repoPath, { recursive: true })
await executeCommand(['git', 'init'], { cwd: repoPath })
await executeCommand(['git', 'config', 'user.name', 'Test User'], { cwd: repoPath })
await executeCommand(['git', 'config', 'user.email', 'test@example.com'], { cwd: repoPath })
await executeCommand(['git', 'config', 'commit.gpgSign', 'false'], { cwd: repoPath })
}
export async function cleanupTestRepo(repoPath: string): Promise<void> {
try {
await rm(repoPath, { recursive: true, force: true })
} catch {
// Ignore
}
}
export async function createTestFile(repoPath: string, fileName: string, content: string): Promise<void> {
const filePath = join(repoPath, fileName)
const dir = join(repoPath, fileName.split('/').slice(0, -1).join('/'))
if (dir !== repoPath) {
await mkdir(dir, { recursive: true })
}
await writeFile(filePath, content, 'utf-8')
}
export async function stageAndCommitTestFile(repoPath: string, fileName: string): Promise<void> {
await executeCommand(['git', 'add', fileName], { cwd: repoPath })
await executeCommand(['git', 'commit', '-m', `Add ${fileName}`], { cwd: repoPath })
}
@@ -0,0 +1,68 @@
import { describe, it, expect, beforeAll, afterAll } from 'bun:test'
import { gitReuseDir, setupGitTestRepo, cleanupTestRepo, createTestFile, stageAndCommitTestFile } from '../fixtures/git-helpers'
import { getFileDiff } from '../../src/services/git-operations'
const testRepoPath = gitReuseDir('git-operations-test')
describe('getFileDiff', () => {
beforeAll(async () => {
await setupGitTestRepo(testRepoPath)
})
afterAll(async () => {
await cleanupTestRepo(testRepoPath)
})
describe('untracked file diff', () => {
it('should extract diff from git --no-index output when exit code 1', async () => {
const fileName = 'new-file.txt'
const content = 'First line\nSecond line\nThird line\n'
await createTestFile(testRepoPath, fileName, content)
const result = await getFileDiff(testRepoPath, fileName)
expect(result.status).toBe('untracked')
expect(result.diff).toBeTruthy()
expect(result.diff).toContain('diff --git')
expect(result.diff).toContain(`a/${fileName}`)
expect(result.diff).toContain(`b/${fileName}`)
expect(result.additions).toBe(3)
expect(result.deletions).toBe(0)
expect(result.isBinary).toBe(false)
})
it('should handle new file with multiple additions', async () => {
const fileName = 'new-code-file.js'
const content = 'function hello() {\n console.log("Hello world");\n return true;\n}\n'
await createTestFile(testRepoPath, fileName, content)
const result = await getFileDiff(testRepoPath, fileName)
expect(result.status).toBe('untracked')
expect(result.additions).toBeGreaterThan(0)
expect(result.diff).toContain('diff --git')
})
})
describe('tracked file diff', () => {
it('should get diff for modified tracked file', async () => {
const fileName = 'modified-file.txt'
const originalContent = 'Original line 1\nOriginal line 2\n'
await createTestFile(testRepoPath, fileName, originalContent)
await stageAndCommitTestFile(testRepoPath, fileName)
const modifiedContent = 'Original line 1\nModified line 2\nNew line 3\n'
await createTestFile(testRepoPath, fileName, modifiedContent)
const result = await getFileDiff(testRepoPath, fileName)
expect(result.status).toBe('modified')
expect(result.diff).toBeTruthy()
expect(result.diff).toContain('-Original line 2')
expect(result.diff).toContain('+Modified line 2')
expect(result.diff).toContain('+New line 3')
expect(result.additions).toBe(2)
expect(result.deletions).toBe(1)
})
})
})
@@ -201,7 +201,7 @@ export function FileDiffView({ repoId, filePath, onBack, onOpenFile, isMobile =
<span className="text-red-500">-{diffData.deletions}</span>
</>
)}
{diffData.diff && <CopyButton content={diffData.diff} title="Copy diff" iconSize="sm" variant="ghost" />}
{diffData.diff && <CopyButton content={diffData.diff || ''} title="Copy diff" iconSize="sm" variant="ghost" className="flex-shrink-0" />}
</div>
</div>