branch.js 915 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var log = require('fancy-log');
  3. var exec = require('child_process').exec;
  4. var escape = require('any-shell-escape');
  5. // want to get the current branch instead?
  6. // git.revParse({args:'--abbrev-ref HEAD'})
  7. module.exports = function (branch, opt, cb) {
  8. if (!cb && typeof opt === 'function') {
  9. // optional options
  10. cb = opt;
  11. opt = {};
  12. }
  13. if (!cb || typeof cb !== 'function') cb = function () {};
  14. if (!opt) opt = {};
  15. if (!branch) return cb(new Error('gulp-git: Branch name is required git.branch("name")'));
  16. if (!opt.cwd) opt.cwd = process.cwd();
  17. if (!opt.args) opt.args = ' ';
  18. var maxBuffer = opt.maxBuffer || 200 * 1024;
  19. var cmd = 'git branch ' + opt.args + ' ' + escape([branch]);
  20. return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) {
  21. if (err) return cb(err);
  22. if (!opt.quiet) log(stdout, stderr);
  23. cb(null, stdout);
  24. });
  25. };