_git.cjs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getDefaultRevisionId = exports.getGitInfo = void 0;
  4. async function importChildProcess() {
  5. const { exec } = await import("child_process");
  6. return { exec };
  7. }
  8. const execGit = (command, exec) => {
  9. return new Promise((resolve) => {
  10. exec(`git ${command.join(" ")}`, (error, stdout) => {
  11. if (error) {
  12. resolve(null);
  13. }
  14. else {
  15. resolve(stdout.trim());
  16. }
  17. });
  18. });
  19. };
  20. const getGitInfo = async (remote = "origin") => {
  21. let exec;
  22. try {
  23. const execImport = await importChildProcess();
  24. exec = execImport.exec;
  25. }
  26. catch (e) {
  27. // no-op
  28. return null;
  29. }
  30. const isInsideWorkTree = await execGit(["rev-parse", "--is-inside-work-tree"], exec);
  31. if (!isInsideWorkTree) {
  32. return null;
  33. }
  34. const [remoteUrl, commit, commitTime, branch, tags, dirty, authorName, authorEmail,] = await Promise.all([
  35. execGit(["remote", "get-url", remote], exec),
  36. execGit(["rev-parse", "HEAD"], exec),
  37. execGit(["log", "-1", "--format=%ct"], exec),
  38. execGit(["rev-parse", "--abbrev-ref", "HEAD"], exec),
  39. execGit(["describe", "--tags", "--exact-match", "--always", "--dirty"], exec),
  40. execGit(["status", "--porcelain"], exec).then((output) => output !== ""),
  41. execGit(["log", "-1", "--format=%an"], exec),
  42. execGit(["log", "-1", "--format=%ae"], exec),
  43. ]);
  44. return {
  45. remoteUrl,
  46. commit,
  47. commitTime,
  48. branch,
  49. tags,
  50. dirty,
  51. authorName,
  52. authorEmail,
  53. };
  54. };
  55. exports.getGitInfo = getGitInfo;
  56. const getDefaultRevisionId = async () => {
  57. let exec;
  58. try {
  59. const execImport = await importChildProcess();
  60. exec = execImport.exec;
  61. }
  62. catch (e) {
  63. // no-op
  64. return null;
  65. }
  66. const commit = await execGit(["rev-parse", "HEAD"], exec);
  67. if (!commit) {
  68. return null;
  69. }
  70. return commit;
  71. };
  72. exports.getDefaultRevisionId = getDefaultRevisionId;