Skip to content

Commit 3cbd577

Browse files
committed
fix(git): strip GIT environs when running git
When running an npm command from within a git environment, such as installing or testing during a git rebase or bisect, these environment variables will be passed to the child process, causing it to fetch/checkout/etc in the root project instead of doing what the user intends. Strip them out so that they are not passed to the child process. Also, remove git environs from the test environment, so that spawning git in a test to set up a dummy repo doesn't mess with the main project's git repository. This enables adding `exec npm test` in a `git rebase -i` list to run tests between commits.
1 parent c1522be commit 3cbd577

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/utils/git.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ function execGit (args, options, cb) {
2828

2929
function spawnGit (args, options) {
3030
log.info('git', args)
31+
// If we're already in a git command (eg, running test as an exec
32+
// line in an interactive rebase) then these environment variables
33+
// will force git to operate on the current project, instead of
34+
// checking out/fetching/etc. whatever the user actually intends.
35+
options.env = options.env || Object.keys(process.env)
36+
.filter(k => !/^GIT/.test(k))
37+
.reduce((set, k) => {
38+
set[k] = process.env[k]
39+
return set
40+
}, {})
3141
return spawn(git, prefixGitArgs().concat(args || []), options)
3242
}
3343

test/common-tap.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ var readCmdShim = require('read-cmd-shim')
77
var isWindows = require('../lib/utils/is-windows.js')
88
var Bluebird = require('bluebird')
99

10+
// remove any git envs so that we don't mess with the main repo
11+
// when running git subprocesses in tests
12+
Object.keys(process.env).filter(k => /^GIT/.test(k)).forEach(
13+
k => delete process.env[k]
14+
)
15+
1016
// cheesy hackaround for test deps (read: nock) that rely on setImmediate
1117
if (!global.setImmediate || !require('timers').setImmediate) {
1218
require('timers').setImmediate = global.setImmediate = function () {

0 commit comments

Comments
 (0)