node/deps/npm/lib/utils/git.js

56 lines
1.3 KiB
JavaScript
Raw Normal View History

2014-08-01 00:05:30 +08:00
// handle some git configuration for windows
exports.spawn = spawnGit
exports.chainableExec = chainableExec
exports.whichAndExec = whichAndExec
var exec = require("child_process").execFile
, spawn = require("./spawn")
2014-08-01 00:05:30 +08:00
, npm = require("../npm.js")
, which = require("which")
, git = npm.config.get("git")
2014-11-05 07:08:12 +08:00
, assert = require("assert")
, log = require("npmlog")
2014-08-01 00:05:30 +08:00
function prefixGitArgs () {
2014-08-01 00:05:30 +08:00
return process.platform === "win32" ? ["-c", "core.longpaths=true"] : []
}
function execGit (args, options, cb) {
log.info('git', args)
var fullArgs = prefixGitArgs().concat(args || [])
return exec(git, fullArgs, options, function (err) {
if (err) log.error('git', fullArgs.join(' '))
cb.apply(null, arguments)
})
2014-08-01 00:05:30 +08:00
}
function spawnGit (args, options) {
2014-11-05 07:08:12 +08:00
log.info("git", args)
2014-08-01 00:05:30 +08:00
return spawn(git, prefixGitArgs().concat(args || []), options)
}
function chainableExec () {
2014-08-01 00:05:30 +08:00
var args = Array.prototype.slice.call(arguments)
return [execGit].concat(args)
}
function whichGit (cb) {
2014-08-01 00:05:30 +08:00
return which(git, cb)
}
function whichAndExec (args, options, cb) {
2014-11-05 07:08:12 +08:00
assert.equal(typeof cb, "function", "no callback provided")
2014-08-01 00:05:30 +08:00
// check for git
whichGit(function (err) {
if (err) {
err.code = "ENOGIT"
return cb(err)
}
execGit(args, options, cb)
})
}