fetch.js 950 B

1234567891011121314151617181920212223242526272829303132333435
  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 (remote, 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 (!branch) branch = '';
  13. if (!opt) opt = {};
  14. if (!opt.cwd) opt.cwd = process.cwd();
  15. if (!opt.args) opt.args = ' ';
  16. if (!remote && opt.args.indexOf('--all') === -1) remote = 'origin';
  17. var maxBuffer = opt.maxBuffer || 200 * 1024;
  18. var cmd = 'git fetch ' + opt.args;
  19. var args = [];
  20. if (remote)
  21. args.push(remote);
  22. if (branch)
  23. args = args.concat(branch);
  24. if (args.length > 0)
  25. cmd += escape(args);
  26. return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) {
  27. if (err) return cb(err);
  28. if (!opt.quiet) log(stdout, stderr);
  29. cb();
  30. });
  31. };