Skip to content

Commit

Permalink
Merge pull request #29 from npm/nlf/no-shell
Browse files Browse the repository at this point in the history
fix: do not use a shell for git commands
  • Loading branch information
wraithgar authored Apr 15, 2021
2 parents 35f321c + f48dc34 commit 9fab115
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 25 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ jobs:
git config --global user.name "Your Name"
npm test -- --no-coverage --timeout 60
- name: Run linting
run: |
npm run lint
################################################################################
# Run coverage check
#
Expand Down
8 changes: 4 additions & 4 deletions lib/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const { basename, resolve } = require('path')

const revs = require('./revs.js')
const spawn = require('./spawn.js')
const { isWindows, escapePath } = require('./utils.js')
const { isWindows } = require('./utils.js')

const pickManifest = require('npm-pick-manifest')
const fs = require('fs')
Expand Down Expand Up @@ -112,7 +112,7 @@ const branch = (repo, revDoc, target, opts) => {
'-b',
revDoc.ref,
repo,
escapePath(target, opts),
target,
'--recurse-submodules'
]
if (maybeShallow(repo, opts)) { args.push('--depth=1') }
Expand All @@ -125,7 +125,7 @@ const plain = (repo, revDoc, target, opts) => {
const args = [
'clone',
repo,
escapePath(target, opts),
target,
'--recurse-submodules'
]
if (maybeShallow(repo, opts)) { args.push('--depth=1') }
Expand All @@ -151,7 +151,7 @@ const unresolved = (repo, ref, target, opts) => {
// can't do this one shallowly, because the ref isn't advertised
// but we can avoid checking out the working dir twice, at least
const lp = isWindows(opts) ? ['--config', 'core.longpaths=true'] : []
const cloneArgs = ['clone', '--mirror', '-q', repo, escapePath(target + '/.git', opts)]
const cloneArgs = ['clone', '--mirror', '-q', repo, target + '/.git']
const git = (args) => spawn(args, { ...opts, cwd: target })
return mkdirp(target)
.then(() => git(cloneArgs.concat(lp)))
Expand Down
1 change: 1 addition & 0 deletions lib/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ const gitEnv = {
module.exports = (opts = {}) => ({
stdioString: true,
...opts,
shell: false,
env: opts.env || { ...gitEnv, ...process.env }
})
13 changes: 0 additions & 13 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
const { basename } = require('path')

const isWindows = opts => (opts.fakePlatform || process.platform) === 'win32'

// wrap the target in quotes for Windows when using cmd(.exe) as a shell to
// avoid clone failures for paths with spaces
const escapePath = (gitPath, opts) => {
const isCmd = opts.shell && (basename(opts.shell.toLowerCase(), '.exe') === 'cmd')
if (isWindows(opts) && isCmd && !gitPath.startsWith('"')) {
return `"${gitPath}"`
}
return gitPath
}

exports.escapePath = escapePath
exports.isWindows = isWindows
3 changes: 1 addition & 2 deletions lib/which.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { escapePath } = require('./utils.js')
const which = require('which')

let gitPath
Expand All @@ -13,5 +12,5 @@ module.exports = (opts = {}) => {
if (!gitPath || opts.git === false) {
return Object.assign(new Error('No git binary found in $PATH'), { code: 'ENOGIT' })
}
return escapePath(gitPath, opts)
return gitPath
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"prepublishOnly": "git push origin --follow-tags",
"preversion": "npm test",
"snap": "tap",
"test": "tap"
"test": "tap",
"posttest": "npm run lint"
},
"tap": {
"check-coverage": true,
Expand Down
5 changes: 4 additions & 1 deletion test/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,8 @@ if ((process.platform) === 'win32') {
)
} else {
t.test('cloning to folder with spaces with cmd as the shell not on windows', t =>
t.rejects(clone(join(regularRepo, '.git'), 'HEAD', clonedRepoSpaces2, { fakePlatform: 'win32', shell: 'cmd' })))
clone(join(regularRepo, '.git'), 'HEAD', clonedRepoSpaces2, { fakePlatform: 'win32', shell: 'cmd' })
.then(() => revs(regularRepo))
.then((r) => revs(clonedRepoSpaces2).then((r2) => t.same(Object.keys(r.shas), Object.keys(r2.shas))))
)
}
3 changes: 3 additions & 0 deletions test/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const gitEnv = {

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

t.equal(gitOpts().shell, false, 'shell defaults to false')
t.equal(gitOpts({ shell: '/bin/bash' }).shell, false, 'shell cannot be overridden')

t.test('does not override', t => {
const { GIT_ASKPASS, GIT_SSH_COMMAND } = process.env
t.teardown(() => {
Expand Down

0 comments on commit 9fab115

Please sign in to comment.