git-push.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. const log = require("npmlog");
  3. const childProcess = require("@lerna/child-process");
  4. module.exports.gitPush = gitPush;
  5. /**
  6. * @param {string} remote
  7. * @param {string} branch
  8. * @param {import("@lerna/child-process").ExecOpts} opts
  9. */
  10. function gitPush(remote, branch, opts) {
  11. log.silly("gitPush", remote, branch);
  12. return childProcess
  13. .exec("git", ["push", "--follow-tags", "--no-verify", "--atomic", remote, branch], opts)
  14. .catch((error) => {
  15. // @see https://github.com/sindresorhus/execa/blob/v1.0.0/index.js#L159-L179
  16. // the error message _should_ be on stderr except when GIT_REDIRECT_STDERR has been configured to redirect
  17. // to stdout. More details in https://git-scm.com/docs/git#Documentation/git.txt-codeGITREDIRECTSTDERRcode
  18. if (
  19. /atomic/.test(error.stderr) ||
  20. (process.env.GIT_REDIRECT_STDERR === "2>&1" && /atomic/.test(error.stdout))
  21. ) {
  22. // --atomic is only supported in git >=2.4.0, which some crusty CI environments deem unnecessary to upgrade.
  23. // so let's try again without attempting to pass an option that is almost 5 years old as of this writing...
  24. log.warn("gitPush", error.stderr);
  25. log.info("gitPush", "--atomic failed, attempting non-atomic push");
  26. return childProcess.exec("git", ["push", "--follow-tags", "--no-verify", remote, branch], opts);
  27. }
  28. // ensure unexpected errors still break chain
  29. throw error;
  30. });
  31. }