Skip to content

Commit 86c9ed2

Browse files
authoredJun 17, 2020
fix: run all git commands with submodule.recurse=false (#888)
This explicitly prevents git commands from recursing into git submodules and thus causing issues with commands like stash or reset.
1 parent a1904ec commit 86c9ed2

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed
 

‎lib/execGit.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
const debug = require('debug')('lint-staged:git')
44
const execa = require('execa')
55

6+
/**
7+
* Explicitly never recurse commands into submodules, overriding local/global configuration.
8+
* @see https://git-scm.com/docs/git-config#Documentation/git-config.txt-submodulerecurse
9+
*/
10+
const NO_SUBMODULE_RECURSE = ['-c', 'submodule.recurse=false']
11+
12+
const GIT_GLOBAL_OPTIONS = [...NO_SUBMODULE_RECURSE]
13+
614
module.exports = async function execGit(cmd, options = {}) {
715
debug('Running git command', cmd)
816
try {
9-
const { stdout } = await execa('git', [].concat(cmd), {
17+
const { stdout } = await execa('git', GIT_GLOBAL_OPTIONS.concat(cmd), {
1018
...options,
1119
all: true,
1220
cwd: options.cwd || process.cwd(),
@@ -16,3 +24,6 @@ module.exports = async function execGit(cmd, options = {}) {
1624
throw new Error(all)
1725
}
1826
}
27+
28+
// exported for tests
29+
module.exports.GIT_GLOBAL_OPTIONS = GIT_GLOBAL_OPTIONS

‎test/execGit.spec.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
import path from 'path'
22
import execa from 'execa'
3-
import execGit from '../lib/execGit'
3+
4+
import execGit, { GIT_GLOBAL_OPTIONS } from '../lib/execGit'
5+
6+
test('GIT_GLOBAL_OPTIONS', () => {
7+
expect(GIT_GLOBAL_OPTIONS).toMatchInlineSnapshot(`
8+
Array [
9+
"-c",
10+
"submodule.recurse=false",
11+
]
12+
`)
13+
})
414

515
describe('execGit', () => {
616
it('should execute git in process.cwd if working copy is not specified', async () => {
717
const cwd = process.cwd()
818
await execGit(['init', 'param'])
9-
expect(execa).toHaveBeenCalledWith('git', ['init', 'param'], { all: true, cwd })
19+
expect(execa).toHaveBeenCalledWith('git', [...GIT_GLOBAL_OPTIONS, 'init', 'param'], {
20+
all: true,
21+
cwd,
22+
})
1023
})
1124

1225
it('should execute git in a given working copy', async () => {
1326
const cwd = path.join(process.cwd(), 'test', '__fixtures__')
1427
await execGit(['init', 'param'], { cwd })
15-
expect(execa).toHaveBeenCalledWith('git', ['init', 'param'], { all: true, cwd })
28+
expect(execa).toHaveBeenCalledWith('git', [...GIT_GLOBAL_OPTIONS, 'init', 'param'], {
29+
all: true,
30+
cwd,
31+
})
1632
})
1733
})

0 commit comments

Comments
 (0)
Please sign in to comment.