checkout.js 810 B

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