Skip to content

Commit a980804

Browse files
committed
Show working diffs with context like Zed
1 parent feb3e3f commit a980804

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

packages/desktop/src/features/git/DiffManager.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export type WorkingTreeGroups = {
3737
const MAX_UNTRACKED_FILE_BYTES = 1024 * 1024; // 1MB
3838

3939
export class GitDiffManager {
40+
private readonly WORKING_DIFF_CONTEXT_LINES = 3; // Zed/Git default context
4041
constructor(
4142
private gitExecutor: GitExecutor,
4243
private logger?: Logger,
@@ -190,7 +191,7 @@ export class GitDiffManager {
190191
const { stdout: trackedDiff } = await this.runGit({
191192
sessionId,
192193
cwd: worktreePath,
193-
argv: ['git', 'diff', '--color=never', '--unified=0', '--src-prefix=a/', '--dst-prefix=b/', 'HEAD'],
194+
argv: ['git', 'diff', '--color=never', `--unified=${this.WORKING_DIFF_CONTEXT_LINES}`, '--src-prefix=a/', '--dst-prefix=b/', 'HEAD'],
194195
timeoutMs: 120_000,
195196
meta: { source: 'gitDiff', operation: 'diff-working' },
196197
});
@@ -238,7 +239,7 @@ export class GitDiffManager {
238239
const { stdout: diff } = await this.runGit({
239240
sessionId,
240241
cwd: worktreePath,
241-
argv: ['git', 'diff', '--cached', '--color=never', '--unified=0', '--src-prefix=a/', '--dst-prefix=b/', 'HEAD'],
242+
argv: ['git', 'diff', '--cached', '--color=never', `--unified=${this.WORKING_DIFF_CONTEXT_LINES}`, '--src-prefix=a/', '--dst-prefix=b/', 'HEAD'],
242243
timeoutMs: 120_000,
243244
meta: { source: 'gitDiff', operation: 'diff-working-staged' },
244245
});
@@ -270,7 +271,7 @@ export class GitDiffManager {
270271
const { stdout: diff } = await this.runGit({
271272
sessionId,
272273
cwd: worktreePath,
273-
argv: ['git', 'diff', '--color=never', '--unified=0', '--src-prefix=a/', '--dst-prefix=b/'],
274+
argv: ['git', 'diff', '--color=never', `--unified=${this.WORKING_DIFF_CONTEXT_LINES}`, '--src-prefix=a/', '--dst-prefix=b/'],
274275
timeoutMs: 120_000,
275276
meta: { source: 'gitDiff', operation: 'diff-working-unstaged' },
276277
});

packages/desktop/src/features/git/StagingManager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,11 @@ export class GitStagingManager {
310310
scope: 'staged' | 'unstaged',
311311
sessionId: string
312312
): Promise<string> {
313+
const unified = '--unified=3';
313314
const argv =
314315
scope === 'staged'
315-
? ['git', 'diff', '--cached', '--color=never', '--unified=0', '--src-prefix=a/', '--dst-prefix=b/', 'HEAD', '--', filePath]
316-
: ['git', 'diff', '--color=never', '--unified=0', '--src-prefix=a/', '--dst-prefix=b/', '--', filePath];
316+
? ['git', 'diff', '--cached', '--color=never', unified, '--src-prefix=a/', '--dst-prefix=b/', 'HEAD', '--', filePath]
317+
: ['git', 'diff', '--color=never', unified, '--src-prefix=a/', '--dst-prefix=b/', '--', filePath];
317318

318319
const result = await this.gitExecutor.run({
319320
sessionId,

packages/desktop/src/features/git/__tests__/DiffManager.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { GitDiffManager } from '../DiffManager';
33
import type { GitExecutor } from '../../../executors/git';
44

55
describe('GitDiffManager', () => {
6-
it('uses --unified=0 for staged working tree diff', async () => {
6+
it('uses --unified=3 for staged working tree diff', async () => {
77
const mockGitExecutor: GitExecutor = {
88
run: vi.fn(async ({ argv }) => {
99
if (argv[1] === 'rev-parse') {
@@ -34,7 +34,7 @@ describe('GitDiffManager', () => {
3434
.mock.calls.find((c) => (c[0] as any).argv?.[1] === 'diff' && (c[0] as any).argv?.includes('--cached') && !(c[0] as any).argv?.includes('--name-only') && !(c[0] as any).argv?.includes('--shortstat'));
3535

3636
expect(diffCall).toBeTruthy();
37-
expect((diffCall?.[0] as any).argv).toEqual(expect.arrayContaining(['--unified=0']));
37+
expect((diffCall?.[0] as any).argv).toEqual(expect.arrayContaining(['--unified=3']));
3838
});
3939

4040
it('uses --unified=0 for commit diff (git show)', async () => {
@@ -64,4 +64,3 @@ describe('GitDiffManager', () => {
6464
expect((showCall?.[0] as any).argv).toEqual(expect.arrayContaining(['--unified=0']));
6565
});
6666
});
67-

packages/desktop/src/features/git/__tests__/StagingManager.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ describe('GitStagingManager', () => {
334334

335335
// Verify git diff was called
336336
expect(mockGitExecutor.run).toHaveBeenNthCalledWith(1, expect.objectContaining({
337-
argv: expect.arrayContaining(['git', 'diff', '--unified=0']),
337+
argv: expect.arrayContaining(['git', 'diff', '--unified=3']),
338338
}));
339339

340340
// Verify git apply was called with --cached
@@ -683,7 +683,7 @@ Binary files differ`;
683683
expect(mockGitExecutor.run).toHaveBeenCalledTimes(2);
684684

685685
expect(mockGitExecutor.run).toHaveBeenNthCalledWith(1, expect.objectContaining({
686-
argv: expect.arrayContaining(['git', 'diff', '--cached', '--unified=0']),
686+
argv: expect.arrayContaining(['git', 'diff', '--cached', '--unified=3']),
687687
}));
688688

689689
expect(mockGitExecutor.run).toHaveBeenNthCalledWith(2, expect.objectContaining({
@@ -728,7 +728,7 @@ Binary files differ`;
728728
expect(result.success).toBe(true);
729729
expect(mockGitExecutor.run).toHaveBeenCalledTimes(2);
730730
expect(mockGitExecutor.run).toHaveBeenNthCalledWith(1, expect.objectContaining({
731-
argv: expect.arrayContaining(['git', 'diff', '--unified=0']),
731+
argv: expect.arrayContaining(['git', 'diff', '--unified=3']),
732732
}));
733733
expect(mockGitExecutor.run).toHaveBeenNthCalledWith(2, expect.objectContaining({
734734
argv: expect.arrayContaining(['git', 'apply', '-R']),
@@ -779,7 +779,7 @@ Binary files differ`;
779779
expect(result.success).toBe(true);
780780
expect(mockGitExecutor.run).toHaveBeenCalledTimes(3);
781781
expect(mockGitExecutor.run).toHaveBeenNthCalledWith(1, expect.objectContaining({
782-
argv: expect.arrayContaining(['git', 'diff', '--cached', '--unified=0']),
782+
argv: expect.arrayContaining(['git', 'diff', '--cached', '--unified=3']),
783783
}));
784784
expect(mockGitExecutor.run).toHaveBeenNthCalledWith(2, expect.objectContaining({
785785
argv: expect.arrayContaining(['git', 'apply', '--cached', '-R']),

0 commit comments

Comments
 (0)