Skip to content

Commit

Permalink
fix(git): strip GIT environs when running git
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
isaacs committed Jul 3, 2019
1 parent c1522be commit 3cbd577
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/utils/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ function execGit (args, options, cb) {

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

Expand Down
6 changes: 6 additions & 0 deletions test/common-tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ var readCmdShim = require('read-cmd-shim')
var isWindows = require('../lib/utils/is-windows.js')
var Bluebird = require('bluebird')

// remove any git envs so that we don't mess with the main repo
// when running git subprocesses in tests
Object.keys(process.env).filter(k => /^GIT/.test(k)).forEach(
k => delete process.env[k]
)

// cheesy hackaround for test deps (read: nock) that rely on setImmediate
if (!global.setImmediate || !require('timers').setImmediate) {
require('timers').setImmediate = global.setImmediate = function () {
Expand Down

0 comments on commit 3cbd577

Please sign in to comment.