Skip to content

Commit 4536209

Browse files
7487poteat
authored andcommitted
fix(diff): preserve hunks when Git forces colored output
1 parent cbab6f4 commit 4536209

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎mods/diff/hooks/git/argv/diff-leading-args.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const DIFF_LEADING_ARGS = [
1313
'-c',
1414
'core.quotePath=false',
1515
'diff',
16+
'--no-color',
1617
'--no-ext-diff',
1718
'--no-textconv',
1819
'--ignore-submodules=dirty',

‎mods/diff/tests/git/fetch-diff.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,42 @@ describe('fetch-diff', () => {
148148
).toBe(true)
149149
})
150150

151+
test("forced git colors do not hide a changed file's hunks", async () => {
152+
const deps = depsOf({
153+
'HEAD --shortstat': Fixtures.ok(
154+
' 1 file changed, 1 insertion(+), 1 deletion(-)',
155+
),
156+
'HEAD --numstat': Fixtures.ok('1\t1\ta.ts\0'),
157+
'ls-files': Fixtures.ok(),
158+
})
159+
160+
const git = Fixtures.scriptedGitOf({
161+
'--no-color': Fixtures.ok('@@ -1 +1 @@\n-old\n+new\n'),
162+
diff: Fixtures.ok(
163+
'\x1b[36m@@ -1 +1 @@\x1b[m\n\x1b[31m-old\x1b[m\n' +
164+
'\x1b[32m+\x1b[m\x1b[32mnew\x1b[m\n',
165+
),
166+
})
167+
168+
const outcome = await Git.fetchDiff(deps, 'uncommitted')
169+
const data = outcome.kind === 'data' ? outcome.data : null
170+
const [row] = data?.files ?? []
171+
172+
expect(data?.stats).toEqual({
173+
filesCount: 1,
174+
linesAdded: 1,
175+
linesRemoved: 1,
176+
})
177+
178+
expect(
179+
data && row ? await Git.fetchFileHunks(git.run, data, row) : null,
180+
).toEqual({
181+
hunks: [{ oldStart: 1, newStart: 1, lines: ['-old', '+new'] }],
182+
isTruncated: false,
183+
isLarge: false,
184+
})
185+
})
186+
151187
test('uncommitted mode drops pre-session untracked files', async () => {
152188
const outcome = await Git.fetchDiff(
153189
depsOf(

0 commit comments

Comments
 (0)