get-last-commit.js 905 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. const log = require("npmlog");
  3. const childProcess = require("@lerna/child-process");
  4. module.exports.getLastCommit = getLastCommit;
  5. /**
  6. * @param {import("@lerna/child-process").ExecOpts} execOpts
  7. */
  8. function getLastCommit(execOpts) {
  9. if (hasTags(execOpts)) {
  10. log.silly("getLastTagInBranch");
  11. return childProcess.execSync("git", ["describe", "--tags", "--abbrev=0"], execOpts);
  12. }
  13. log.silly("getFirstCommit");
  14. return childProcess.execSync("git", ["rev-list", "--max-parents=0", "HEAD"], execOpts);
  15. }
  16. /**
  17. * @param {import("@lerna/child-process").ExecOpts} opts
  18. */
  19. function hasTags(opts) {
  20. let result = false;
  21. try {
  22. result = !!childProcess.execSync("git", ["tag"], opts);
  23. } catch (err) {
  24. log.warn("ENOTAGS", "No git tags were reachable from this branch!");
  25. log.verbose("hasTags error", err);
  26. }
  27. log.verbose("hasTags", result);
  28. return result;
  29. }