_git.js 1.9 KB

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