push.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. var log = require('fancy-log');
  3. var exec = require('child_process').exec;
  4. var escape = require('any-shell-escape');
  5. var revParse = require('./revParse');
  6. module.exports = function (remote, branch, opt, cb) {
  7. if (!remote) remote = 'origin';
  8. function setBranch(cb2) {
  9. if (branch && typeof branch === 'function') {
  10. cb = branch;
  11. branch = null;
  12. }
  13. if (branch && Object.prototype.toString.call(branch) === '[object Object]') {
  14. opt = branch;
  15. branch = null;
  16. }
  17. if (!branch) {
  18. revParse({ args: '--abbrev-ref HEAD' },
  19. function callback(err, out) {
  20. if (err) return cb2(err);
  21. branch = out;
  22. cb2();
  23. });
  24. } else {
  25. cb2();
  26. }
  27. }
  28. function flush(err) {
  29. if (err) return cb(err);
  30. if (!cb && typeof opt === 'function') {
  31. cb = opt;
  32. opt = {};
  33. }
  34. if (!cb || typeof cb !== 'function') cb = function() {};
  35. if (!opt) opt = {};
  36. if (!opt.cwd) opt.cwd = process.cwd();
  37. if (!opt.args) opt.args = ' ';
  38. var cmd = 'git push ' + escape([].concat(remote, branch)) + ' ' + opt.args;
  39. var maxBuffer = opt.maxBuffer || 200 * 1024;
  40. return exec(cmd, {
  41. cwd: opt.cwd,
  42. maxBuffer: maxBuffer
  43. }, function(err, stdout, stderr) {
  44. if (err) return cb(err);
  45. if (!opt.quiet) log(stdout, stderr);
  46. cb();
  47. });
  48. }
  49. return setBranch(flush);
  50. };