chunk-node-git.a44b4872.mjs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { a as resolve } from './chunk-constants.71e8a211.mjs';
  2. import { e as execa } from './vendor-index.fbec8a81.mjs';
  3. import 'tty';
  4. import 'url';
  5. import 'path';
  6. import 'buffer';
  7. import 'child_process';
  8. import 'process';
  9. import './vendor-index.2ae8040a.mjs';
  10. import './vendor-_commonjsHelpers.4da45ef5.mjs';
  11. import 'fs';
  12. import 'stream';
  13. import 'util';
  14. import 'os';
  15. import './vendor-index.29636037.mjs';
  16. import 'assert';
  17. import 'events';
  18. class VitestGit {
  19. constructor(cwd) {
  20. this.cwd = cwd;
  21. }
  22. async resolveFilesWithGitCommand(args) {
  23. let result;
  24. try {
  25. result = await execa("git", args, { cwd: this.root });
  26. } catch (e) {
  27. e.message = e.stderr;
  28. throw e;
  29. }
  30. return result.stdout.split("\n").filter((s) => s !== "").map((changedPath) => resolve(this.root, changedPath));
  31. }
  32. async findChangedFiles(options) {
  33. const root = await this.getRoot(this.cwd);
  34. if (!root)
  35. return null;
  36. this.root = root;
  37. const changedSince = options.changedSince;
  38. if (typeof changedSince === "string") {
  39. const [committed, staged2, unstaged2] = await Promise.all([
  40. this.getFilesSince(changedSince),
  41. this.getStagedFiles(),
  42. this.getUnstagedFiles()
  43. ]);
  44. return [...committed, ...staged2, ...unstaged2];
  45. }
  46. const [staged, unstaged] = await Promise.all([
  47. this.getStagedFiles(),
  48. this.getUnstagedFiles()
  49. ]);
  50. return [...staged, ...unstaged];
  51. }
  52. getFilesSince(hash) {
  53. return this.resolveFilesWithGitCommand(
  54. ["diff", "--name-only", `${hash}...HEAD`]
  55. );
  56. }
  57. getStagedFiles() {
  58. return this.resolveFilesWithGitCommand(
  59. ["diff", "--cached", "--name-only"]
  60. );
  61. }
  62. getUnstagedFiles() {
  63. return this.resolveFilesWithGitCommand(
  64. [
  65. "ls-files",
  66. "--other",
  67. "--modified",
  68. "--exclude-standard"
  69. ]
  70. );
  71. }
  72. async getRoot(cwd) {
  73. const options = ["rev-parse", "--show-cdup"];
  74. try {
  75. const result = await execa("git", options, { cwd });
  76. return resolve(cwd, result.stdout);
  77. } catch {
  78. return null;
  79. }
  80. }
  81. }
  82. export { VitestGit };