var path = require('path') , fs = require('fs') , spawn = require('child_process').spawn , mkdirp = require('mkdirp') , utils = require('./lib') function safespawn() { var c try { c = spawn.apply(null, arguments) } catch (e) { throw new Error('Failed to start command: ' + JSON.stringify([].slice.call(arguments))) } c.on('error', function (err) { // suppress node errors }) return c } function httpCloneCmd(config, branch) { var urls = utils.httpUrl(config) , screen = 'git clone --recursive ' + urls[1] + ' .' , args = ['clone', '--recursive', urls[0], '.'] if (branch) { args = args.concat(['-b', branch]) screen += ' -b ' + branch } return { command: 'git', args: args, screen: screen } } function pull(dest, config, context, done) { context.cmd({ cmd: 'git reset --hard', cwd: dest }, function (exitCode) { utils.gitCmd('git pull', dest, config.auth, context, done) }) } function gitVersion(next) { var child = safespawn('git', ['--version']) , out = '' child.stdout.setEncoding('utf8') child.stderr.setEncoding('utf8') child.stdout.on('data', function (data) { out += data }) child.stderr.on('data', function (data) { out += data }) child.on('close', function (code) { if (code) return next(new Error('Failed to get git version: ' + out)) next(null, out) }) child.on('error', function () {}) } function clone(dest, config, ref, context, done) { if (config.auth.type === 'ssh') { var cmd = 'git clone --recursive ' + utils.sshUrl(config)[0] + ' .' if (ref.branch) { cmd += ' -b ' + ref.branch // this /only/ gets the one branch; so only use if we won't be caching if (!config.cache) cmd += ' --single-branch' } return utils.gitaneCmd(cmd, dest, config.auth.privkey, context, done) } context.cmd({ cmd: httpCloneCmd(config), cwd: dest }, done) } function badCode(name, code) { var e = new Error(name + ' failed with code ' + code) e.code = code e.exitCode = code return e } module.exports = { init: function (dirs, config, job, done) { return done(null, { config: config, fetch: function (context, done) { module.exports.fetch(dirs.data, config, job, context, done) } }) }, fetch: fetch } function getMasterPrivKey(branches) { for (var i=0; i