Skip to content

Commit 0109e8d

Browse files
committed
fix: strip Git CRLF warning from output
1 parent dfd6a7a commit 0109e8d

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

.changeset/ninety-clubs-heal.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'lint-staged': patch
3+
---
4+
5+
Make sure Git's warning about CRLF line-endings doesn't interfere with creating initial backup stash.

lib/execGit.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { createDebug } from './debug.js'
44

55
const debugLog = createDebug('lint-staged:execGit')
66

7+
/** @example "warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it" */
8+
const GIT_CRLF_WARNING = /^warning.*CRLF.*the next time Git touches it/i
9+
710
/**
811
* Explicitly never recurse commands into submodules, overriding local/global configuration.
912
* @see https://git-scm.com/docs/git-config#Documentation/git-config.txt-submodulerecurse
@@ -25,6 +28,11 @@ export const execGit = async (cmd, options) => {
2528

2629
let output = ''
2730
for await (const line of result) {
31+
if (GIT_CRLF_WARNING.test(line)) {
32+
debugLog('Stripped Git CRLF warning: %s', line)
33+
continue
34+
}
35+
2836
output += line + '\n'
2937
}
3038
output = output.trimEnd()

test/unit/execGit.spec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,28 @@ describe('execGit', () => {
2424

2525
it('should execute git in a given working copy', async ({ expect }) => {
2626
const cwd = path.join(process.cwd(), 'test', '__fixtures__')
27-
await execGit(['init', 'param'], { cwd })
27+
const output = await execGit(['init', 'param'], { cwd })
2828
expect(exec).toHaveBeenCalledExactlyOnceWith('git', [...GIT_GLOBAL_OPTIONS, 'init', 'param'], {
2929
nodeOptions: {
3030
cwd,
3131
stdio: ['ignore'],
3232
},
3333
})
34+
35+
expect(output).toBe('test')
36+
})
37+
38+
it('should strip Git CRLF warning from start of output', async ({ expect }) => {
39+
vi.mocked(exec).mockReturnValueOnce({
40+
async *[Symbol.asyncIterator]() {
41+
yield "warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it"
42+
yield "warning: in the working copy of 'package.json', LF will be replaced by CRLF the next time Git touches it"
43+
yield '09756bc72fde5a4c77ff788ad49fa1c1111a8527'
44+
},
45+
})
46+
47+
const output = await execGit(['stash', 'create'])
48+
49+
expect(output).toBe('09756bc72fde5a4c77ff788ad49fa1c1111a8527')
3450
})
3551
})

0 commit comments

Comments
 (0)