diff --git a/backend/src/services/git-operations.ts b/backend/src/services/git-operations.ts index 9447167c..a0d9b4f2 100644 --- a/backend/src/services/git-operations.ts +++ b/backend/src/services/git-operations.ts @@ -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}` } diff --git a/backend/tests/fixtures/git-helpers.ts b/backend/tests/fixtures/git-helpers.ts new file mode 100644 index 00000000..bd151b49 --- /dev/null +++ b/backend/tests/fixtures/git-helpers.ts @@ -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 { + 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 { + try { + await rm(repoPath, { recursive: true, force: true }) + } catch { + // Ignore + } +} + +export async function createTestFile(repoPath: string, fileName: string, content: string): Promise { + 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 { + await executeCommand(['git', 'add', fileName], { cwd: repoPath }) + await executeCommand(['git', 'commit', '-m', `Add ${fileName}`], { cwd: repoPath }) +} diff --git a/backend/tests/services/git-operations.test.ts b/backend/tests/services/git-operations.test.ts new file mode 100644 index 00000000..6c5ccff2 --- /dev/null +++ b/backend/tests/services/git-operations.test.ts @@ -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) + }) + }) +}) diff --git a/frontend/src/components/file-browser/FileDiffView.tsx b/frontend/src/components/file-browser/FileDiffView.tsx index 60665006..4b8224a8 100644 --- a/frontend/src/components/file-browser/FileDiffView.tsx +++ b/frontend/src/components/file-browser/FileDiffView.tsx @@ -201,7 +201,7 @@ export function FileDiffView({ repoId, filePath, onBack, onOpenFile, isMobile = -{diffData.deletions} )} - {diffData.diff && } + {diffData.diff && }