Skip to content

Commit 9fab115

Browse files
authored
Merge pull request #29 from npm/nlf/no-shell
fix: do not use a shell for git commands
2 parents 35f321c + f48dc34 commit 9fab115

File tree

8 files changed

+15
-25
lines changed

8 files changed

+15
-25
lines changed

.github/workflows/ci.yml

-4
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ jobs:
6464
git config --global user.name "Your Name"
6565
npm test -- --no-coverage --timeout 60
6666
67-
- name: Run linting
68-
run: |
69-
npm run lint
70-
7167
################################################################################
7268
# Run coverage check
7369
#

lib/clone.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const { basename, resolve } = require('path')
2424

2525
const revs = require('./revs.js')
2626
const spawn = require('./spawn.js')
27-
const { isWindows, escapePath } = require('./utils.js')
27+
const { isWindows } = require('./utils.js')
2828

2929
const pickManifest = require('npm-pick-manifest')
3030
const fs = require('fs')
@@ -112,7 +112,7 @@ const branch = (repo, revDoc, target, opts) => {
112112
'-b',
113113
revDoc.ref,
114114
repo,
115-
escapePath(target, opts),
115+
target,
116116
'--recurse-submodules'
117117
]
118118
if (maybeShallow(repo, opts)) { args.push('--depth=1') }
@@ -125,7 +125,7 @@ const plain = (repo, revDoc, target, opts) => {
125125
const args = [
126126
'clone',
127127
repo,
128-
escapePath(target, opts),
128+
target,
129129
'--recurse-submodules'
130130
]
131131
if (maybeShallow(repo, opts)) { args.push('--depth=1') }
@@ -151,7 +151,7 @@ const unresolved = (repo, ref, target, opts) => {
151151
// can't do this one shallowly, because the ref isn't advertised
152152
// but we can avoid checking out the working dir twice, at least
153153
const lp = isWindows(opts) ? ['--config', 'core.longpaths=true'] : []
154-
const cloneArgs = ['clone', '--mirror', '-q', repo, escapePath(target + '/.git', opts)]
154+
const cloneArgs = ['clone', '--mirror', '-q', repo, target + '/.git']
155155
const git = (args) => spawn(args, { ...opts, cwd: target })
156156
return mkdirp(target)
157157
.then(() => git(cloneArgs.concat(lp)))

lib/opts.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ const gitEnv = {
77
module.exports = (opts = {}) => ({
88
stdioString: true,
99
...opts,
10+
shell: false,
1011
env: opts.env || { ...gitEnv, ...process.env }
1112
})

lib/utils.js

-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
const { basename } = require('path')
2-
31
const isWindows = opts => (opts.fakePlatform || process.platform) === 'win32'
42

5-
// wrap the target in quotes for Windows when using cmd(.exe) as a shell to
6-
// avoid clone failures for paths with spaces
7-
const escapePath = (gitPath, opts) => {
8-
const isCmd = opts.shell && (basename(opts.shell.toLowerCase(), '.exe') === 'cmd')
9-
if (isWindows(opts) && isCmd && !gitPath.startsWith('"')) {
10-
return `"${gitPath}"`
11-
}
12-
return gitPath
13-
}
14-
15-
exports.escapePath = escapePath
163
exports.isWindows = isWindows

lib/which.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const { escapePath } = require('./utils.js')
21
const which = require('which')
32

43
let gitPath
@@ -13,5 +12,5 @@ module.exports = (opts = {}) => {
1312
if (!gitPath || opts.git === false) {
1413
return Object.assign(new Error('No git binary found in $PATH'), { code: 'ENOGIT' })
1514
}
16-
return escapePath(gitPath, opts)
15+
return gitPath
1716
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"prepublishOnly": "git push origin --follow-tags",
2020
"preversion": "npm test",
2121
"snap": "tap",
22-
"test": "tap"
22+
"test": "tap",
23+
"posttest": "npm run lint"
2324
},
2425
"tap": {
2526
"check-coverage": true,

test/clone.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -253,5 +253,8 @@ if ((process.platform) === 'win32') {
253253
)
254254
} else {
255255
t.test('cloning to folder with spaces with cmd as the shell not on windows', t =>
256-
t.rejects(clone(join(regularRepo, '.git'), 'HEAD', clonedRepoSpaces2, { fakePlatform: 'win32', shell: 'cmd' })))
256+
clone(join(regularRepo, '.git'), 'HEAD', clonedRepoSpaces2, { fakePlatform: 'win32', shell: 'cmd' })
257+
.then(() => revs(regularRepo))
258+
.then((r) => revs(clonedRepoSpaces2).then((r2) => t.same(Object.keys(r.shas), Object.keys(r2.shas))))
259+
)
257260
}

test/opts.js

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const gitEnv = {
77

88
t.match(gitOpts().env, gitEnv, 'got the git defaults we want')
99

10+
t.equal(gitOpts().shell, false, 'shell defaults to false')
11+
t.equal(gitOpts({ shell: '/bin/bash' }).shell, false, 'shell cannot be overridden')
12+
1013
t.test('does not override', t => {
1114
const { GIT_ASKPASS, GIT_SSH_COMMAND } = process.env
1215
t.teardown(() => {

0 commit comments

Comments
 (0)