@zukhruf/git-hooks 0.8.20 → 0.8.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dist/build.d.ts +1 -1
  2. package/dist/build.d.ts.map +1 -1
  3. package/dist/build.js +44 -46
  4. package/dist/build.js.map +1 -1
  5. package/dist/codegen.d.ts.map +1 -1
  6. package/dist/codegen.js +41 -8
  7. package/dist/codegen.js.map +1 -1
  8. package/dist/commit.d.ts +30 -2
  9. package/dist/commit.d.ts.map +1 -1
  10. package/dist/commit.js +261 -22
  11. package/dist/commit.js.map +1 -1
  12. package/dist/git.d.ts +3 -0
  13. package/dist/git.d.ts.map +1 -0
  14. package/dist/git.js +20 -0
  15. package/dist/git.js.map +1 -0
  16. package/dist/index.d.ts +2 -0
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js.map +1 -1
  19. package/dist/npm/lockfile.d.ts +14 -0
  20. package/dist/npm/lockfile.d.ts.map +1 -0
  21. package/dist/npm/lockfile.js +77 -0
  22. package/dist/npm/lockfile.js.map +1 -0
  23. package/dist/push.d.ts +14 -7
  24. package/dist/push.d.ts.map +1 -1
  25. package/dist/push.js +26 -82
  26. package/dist/push.js.map +1 -1
  27. package/dist/ref-update.d.ts +1 -1
  28. package/dist/ref-update.d.ts.map +1 -1
  29. package/dist/scripts/commit-msg.d.ts.map +1 -1
  30. package/dist/scripts/commit-msg.js +9 -15
  31. package/dist/scripts/commit-msg.js.map +1 -1
  32. package/dist/scripts/host.d.ts +10 -0
  33. package/dist/scripts/host.d.ts.map +1 -0
  34. package/dist/scripts/host.js +37 -0
  35. package/dist/scripts/host.js.map +1 -0
  36. package/dist/scripts/pre-commit.d.ts.map +1 -1
  37. package/dist/scripts/pre-commit.js +35 -13
  38. package/dist/scripts/pre-commit.js.map +1 -1
  39. package/dist/scripts/pre-push.d.ts.map +1 -1
  40. package/dist/scripts/pre-push.js +24 -24
  41. package/dist/scripts/pre-push.js.map +1 -1
  42. package/dist/snapshot/drift.d.ts +0 -2
  43. package/dist/snapshot/drift.d.ts.map +1 -1
  44. package/dist/snapshot/drift.js +5 -57
  45. package/dist/snapshot/drift.js.map +1 -1
  46. package/dist/snapshot/installs.d.ts +1 -0
  47. package/dist/snapshot/installs.d.ts.map +1 -1
  48. package/dist/snapshot/installs.js +85 -29
  49. package/dist/snapshot/installs.js.map +1 -1
  50. package/dist/snapshot/snapshot.d.ts +21 -0
  51. package/dist/snapshot/snapshot.d.ts.map +1 -0
  52. package/dist/snapshot/snapshot.js +52 -0
  53. package/dist/snapshot/snapshot.js.map +1 -0
  54. package/dist/snapshot/snapshots.d.ts +30 -0
  55. package/dist/snapshot/snapshots.d.ts.map +1 -0
  56. package/dist/snapshot/snapshots.js +181 -0
  57. package/dist/snapshot/snapshots.js.map +1 -0
  58. package/dist/sync.d.ts.map +1 -1
  59. package/dist/sync.js +16 -16
  60. package/dist/sync.js.map +1 -1
  61. package/dist/tree.d.ts +24 -0
  62. package/dist/tree.d.ts.map +1 -0
  63. package/dist/tree.js +96 -0
  64. package/dist/tree.js.map +1 -0
  65. package/package.json +4 -4
package/dist/build.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type Push } from './push.ts';
1
+ import type { Push } from './push.ts';
2
2
  import { type Outcome, Step } from './step.ts';
3
3
  interface BuildOptions {
4
4
  readonly targets?: readonly string[];
@@ -1 +1 @@
1
- {"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":"AAIA,OAAO,EAAwB,KAAK,IAAI,EAAE,MAAM,WAAW,CAAC;AAE5D,OAAO,EAAE,KAAK,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAE/C,UAAU,YAAY;IAEpB,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAErC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAG3B,QAAQ,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CACjD;AAuBD,qBAAa,KAAM,SAAQ,IAAI,CAAC,IAAI,CAAC;;IACnC,QAAQ,CAAC,IAAI,WAAW;gBAKZ,EAAE,OAAmB,EAAE,QAAQ,EAAE,GAAQ,EAAE,GAAE,YAAiB;IAOpE,GAAG,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC;CAgGxC"}
1
+ {"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEtC,OAAO,EAAE,KAAK,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAE/C,UAAU,YAAY;IAEpB,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAErC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAG3B,QAAQ,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CACjD;AAuBD,qBAAa,KAAM,SAAQ,IAAI,CAAC,IAAI,CAAC;;IACnC,QAAQ,CAAC,IAAI,WAAW;gBAKZ,EAAE,OAAmB,EAAE,QAAQ,EAAE,GAAQ,EAAE,GAAE,YAAiB;IAOpE,GAAG,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC;CA+FxC"}
package/dist/build.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import { readFile, rm } from 'node:fs/promises';
2
2
  import { join } from 'node:path';
3
3
  import { stripVTControlCharacters } from 'node:util';
4
- import { MissingLockfileError } from "./push.js";
5
- import { runNx } from "./snapshot/nx.js";
4
+ import { MissingLockfileError } from "./snapshot/snapshots.js";
6
5
  import { Step } from "./step.js";
7
6
  // The lines of error output that a fail shows for each failed task.
8
7
  const shownLines = 20;
@@ -47,50 +46,49 @@ export class Build extends Step {
47
46
  async #build(push, head) {
48
47
  const short = head.slice(0, 7);
49
48
  try {
50
- return await push.inSnapshot(head, async (folder, environment) => {
51
- const cache = environment.NX_CACHE_DIRECTORY ?? '';
52
- // A run that ends before its tasks writes no summary, so the summary
53
- // of an older run must not stay.
54
- await rm(join(cache, 'run.json'), { force: true });
55
- const { code, stdout, stderr } = await runNx(folder, { ...environment, ...this.#env }, [
56
- 'run-many',
57
- `--targets=${this.#targets.join(',')}`,
58
- '--configuration=production',
59
- ...(this.#parallel === undefined
60
- ? []
61
- : [`--parallel=${this.#parallel}`]),
62
- ]);
63
- const tasks = await lastRun(cache);
64
- if (code === 0) {
65
- const projects = new Set(tasks.map((task) => task.projectName));
66
- const cached = tasks.filter((task) => task.cacheStatus !== 'cache-miss');
67
- return {
68
- kind: 'built',
69
- line: `${short}: built ${projects.size} projects, ${cached.length} of ${tasks.length} tasks from cache`,
70
- };
71
- }
72
- // nx exits with 130 when a task fails, so each code other than 0 is a
73
- // fail.
74
- const failed = tasks.filter((task) => task.status !== 0);
75
- if (failed.length === 0)
76
- return {
77
- kind: 'failed',
78
- lines: [
79
- `${short}: nx exited with ${code}`,
80
- ...shown(`${stdout}\n${stderr}`),
81
- ],
82
- };
83
- const lines = [
84
- `${short}: ${failed.length} of ${tasks.length} tasks failed: ${failed.map(taskName).join(', ')}`,
85
- ];
86
- for (const task of failed) {
87
- if (failed.length > 1)
88
- lines.push(` ${taskName(task)}`);
89
- const output = await readFile(join(cache, 'terminalOutputs', task.hash), 'utf8').catch(() => '');
90
- lines.push(...shown(output, true));
91
- }
92
- return { kind: 'failed', lines };
93
- });
49
+ const snapshot = await push.snapshot(head, { packages: true });
50
+ const cache = snapshot.nxCacheDirectory;
51
+ // A run that ends before its tasks writes no summary, so the summary of
52
+ // an older run must not stay.
53
+ await rm(join(cache, 'run.json'), { force: true });
54
+ const { status, stdout, stderr } = await snapshot.bin('nx', [
55
+ 'run-many',
56
+ `--targets=${this.#targets.join(',')}`,
57
+ '--configuration=production',
58
+ ...(this.#parallel === undefined
59
+ ? []
60
+ : [`--parallel=${this.#parallel}`]),
61
+ ], { env: this.#env });
62
+ const tasks = await lastRun(cache);
63
+ if (status === 0) {
64
+ const projects = new Set(tasks.map((task) => task.projectName));
65
+ const cached = tasks.filter((task) => task.cacheStatus !== 'cache-miss');
66
+ return {
67
+ kind: 'built',
68
+ line: `${short}: built ${projects.size} projects, ${cached.length} of ${tasks.length} tasks from cache`,
69
+ };
70
+ }
71
+ // nx exits with 130 when a task fails, so each code other than 0 is a
72
+ // fail.
73
+ const failed = tasks.filter((task) => task.status !== 0);
74
+ if (failed.length === 0)
75
+ return {
76
+ kind: 'failed',
77
+ lines: [
78
+ `${short}: nx exited with ${status}`,
79
+ ...shown(`${stdout}\n${stderr}`),
80
+ ],
81
+ };
82
+ const lines = [
83
+ `${short}: ${failed.length} of ${tasks.length} tasks failed: ${failed.map(taskName).join(', ')}`,
84
+ ];
85
+ for (const task of failed) {
86
+ if (failed.length > 1)
87
+ lines.push(` ${taskName(task)}`);
88
+ const output = await readFile(join(cache, 'terminalOutputs', task.hash), 'utf8').catch(() => '');
89
+ lines.push(...shown(output, true));
90
+ }
91
+ return { kind: 'failed', lines };
94
92
  }
95
93
  catch (error) {
96
94
  if (error instanceof MissingLockfileError)
package/dist/build.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"build.js","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,wBAAwB,EAAE,MAAM,WAAW,CAAC;AAErD,OAAO,EAAE,oBAAoB,EAAa,MAAM,WAAW,CAAC;AAC5D,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAgB,IAAI,EAAE,MAAM,WAAW,CA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AAMD,qBAAa,OAAQ,SAAQ,IAAI,CAAC,MAAM,CAAC;;IACvC,QAAQ,CAAC,IAAI,aAAa;gBAKd,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,cAAc;IAO1C,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CAmB5C"}
1
+ {"version":3,"file":"codegen.d.ts","sourceRoot":"","sources":["../src/codegen.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,KAAK,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAE/C,UAAU,cAAc;IAGtB,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAElC,QAAQ,CAAC,GAAG,EAAE,SAAS,MAAM,EAAE,CAAC;IAEhC,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;CACnC;AAOD,qBAAa,OAAQ,SAAQ,IAAI,CAAC,MAAM,CAAC;;IACvC,QAAQ,CAAC,IAAI,aAAa;gBAKd,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,cAAc;IAO1C,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CA+B5C"}
package/dist/codegen.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { Step } from "./step.js";
2
2
  // Builds generated files, such as an OpenAPI spec and the client made from it,
3
- // and stages them. With `when`, it builds only when nx says that one of those
4
- // projects is affected by the staged files: a hand-kept list of inputs would
5
- // miss a file, and an edit that is not staged is not part of the commit.
3
+ // from the staged commit, and stages them. With `when`, it builds only when nx
4
+ // says that one of those projects is affected by the staged changes: a
5
+ // hand-kept list of inputs would miss a file, and an edit that is not staged
6
+ // is not part of the commit.
6
7
  export class Codegen extends Step {
7
8
  name = 'codegen';
8
9
  #when;
@@ -16,13 +17,22 @@ export class Codegen extends Step {
16
17
  }
17
18
  async run(commit) {
18
19
  if (this.#when !== undefined) {
19
- const affected = new Set(await commit.affected());
20
+ const affected = new Set(await affectedProjects(commit));
20
21
  if (!this.#when.some((project) => affected.has(project)))
21
22
  return this.skip(`none of ${this.#when.join(', ')} is affected`);
22
23
  }
24
+ let generated;
23
25
  try {
24
- for (const target of this.#run)
25
- await commit.nx('run', target);
26
+ generated = await commit.generate(this.#stage, async (snapshot) => {
27
+ for (const target of this.#run) {
28
+ const { status, stdout, stderr } = await snapshot.bin('nx', [
29
+ 'run',
30
+ target,
31
+ ]);
32
+ if (status !== 0)
33
+ throw new Error(`nx run ${target} exited with ${status}:\n${`${stdout}${stderr}`.trimEnd()}`);
34
+ }
35
+ });
26
36
  }
27
37
  catch (error) {
28
38
  // A failed build is the step's outcome: the author reads the reason that
@@ -31,8 +41,31 @@ export class Codegen extends Step {
31
41
  throw error;
32
42
  return this.fail(error.message.trimEnd());
33
43
  }
34
- await commit.stage(this.#stage);
35
- return this.pass(`ran ${this.#run.join(', ')}; staged ${this.#stage.join(', ')}`);
44
+ if (generated.kind === 'not-staged')
45
+ return this.fail(generated.reason);
46
+ return this.pass(`ran ${this.#run.join(', ')}; staged ${generated.entries.length === 0 ? 'nothing new' : generated.entries.map(({ path }) => path).join(', ')}`);
36
47
  }
37
48
  }
49
+ // The nx projects that the staged changes affect, as nx in the snapshot of
50
+ // the staged commit lists them.
51
+ async function affectedProjects(commit) {
52
+ const changes = await commit.changes();
53
+ if (changes.length === 0)
54
+ return [];
55
+ const snapshot = await commit.snapshot({ packages: true });
56
+ const { status, stdout, stderr } = await snapshot.bin('nx', [
57
+ 'show',
58
+ 'projects',
59
+ '--affected',
60
+ `--files=${changes.map(({ path }) => path).join(',')}`,
61
+ '--json',
62
+ ]);
63
+ if (status !== 0)
64
+ throw new Error(`nx show projects exited with ${status}:\n${stderr.trimEnd()}`);
65
+ const projects = JSON.parse(stdout);
66
+ if (!Array.isArray(projects) ||
67
+ !projects.every((project) => typeof project === 'string'))
68
+ throw new TypeError(`nx show projects gave ${stdout.trim()}, not a list of names`);
69
+ return projects;
70
+ }
38
71
  //# sourceMappingURL=codegen.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"codegen.js","sourceRoot":"","sources":["../src/codegen.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,IAAI,EAAE,MAAM,WAAW,CAAC;AAY/C,+EAA+E;AAC/E,8EAA8E;AAC9E,6EAA6E;AAC7E,yEAAyE;AACzE,MAAM,OAAO,OAAQ,SAAQ,IAAY;IAC9B,IAAI,GAAG,SAAS,CAAC;IACjB,KAAK,CAAgC;IACrC,IAAI,CAAoB;IACxB,MAAM,CAAoB;IAEnC,YAAY,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAkB;QAC9C,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAChB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,MAAc;QACtB,IAAI,IAAI,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;YAC7B,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,MAAM,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;YAClD,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;gBACtD,OAAO,IAAI,CAAC,IAAI,CAAC,WAAW,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QACrE,CAAC;QACD,IAAI,CAAC;YACH,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,IAAI;gBAAE,MAAM,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QACjE,CAAC;QAAC,OAAO,KAAK,EAA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Q,CAAC;AAClB,CAAC"}
package/dist/commit.d.ts CHANGED
@@ -1,15 +1,43 @@
1
+ import { PackagedSnapshot, Snapshot } from './snapshot/snapshot.ts';
2
+ import type { Snapshots } from './snapshot/snapshots.ts';
3
+ import { type DiffEntry, Tree } from './tree.ts';
4
+ export type Generated = {
5
+ readonly kind: 'staged';
6
+ readonly entries: readonly DiffEntry[];
7
+ } | {
8
+ readonly kind: 'not-staged';
9
+ readonly entries: readonly DiffEntry[];
10
+ readonly reason: string;
11
+ };
12
+ interface CommitOptions {
13
+ readonly root: string;
14
+ readonly environment: NodeJS.ProcessEnv;
15
+ readonly snapshots?: Snapshots;
16
+ readonly notice?: (message: string) => void;
17
+ readonly generated?: (tree: string) => void;
18
+ }
1
19
  export declare class Commit {
2
20
  #private;
3
21
  readonly root: string;
4
- constructor(root: string, environment: NodeJS.ProcessEnv);
22
+ constructor({ root, environment, snapshots, notice, generated, }: CommitOptions);
23
+ tree(): Promise<Tree>;
24
+ changes(): Promise<readonly DiffEntry[]>;
25
+ touches(globs: readonly string[]): Promise<boolean>;
26
+ snapshot(options?: {
27
+ readonly packages?: false;
28
+ }): Promise<Snapshot>;
29
+ snapshot(options: {
30
+ readonly packages: true;
31
+ }): Promise<PackagedSnapshot>;
32
+ generate(globs: readonly string[], work: (snapshot: PackagedSnapshot) => Promise<void>): Promise<Generated>;
5
33
  staged(): Promise<string[]>;
6
34
  files(): Promise<string[]>;
7
35
  blob(path: string): Promise<string>;
8
- touches(globs: readonly string[]): Promise<boolean>;
9
36
  inSnapshot<T>(work: (folder: string) => Promise<T>): Promise<T>;
10
37
  modified(): Promise<string[]>;
11
38
  stage(paths: readonly string[]): Promise<void>;
12
39
  nx(...args: string[]): Promise<string>;
13
40
  affected(): Promise<string[]>;
14
41
  }
42
+ export {};
15
43
  //# sourceMappingURL=commit.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"commit.d.ts","sourceRoot":"","sources":["../src/commit.ts"],"names":[],"mappings":"AAeA,qB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EAAE,CAAC;IAYhC,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAM7B,UAAU,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAU/D,QAAQ,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAM7B,KAAK,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAS9C,EAAE,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IAoBtC,QAAQ,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;CAkKpC"}
package/dist/commit.js CHANGED
@@ -1,21 +1,117 @@
1
- import { execFile } from 'node:child_process';
2
- import { mkdtempDisposable } from 'node:fs/promises';
1
+ import { execFile, spawn } from 'node:child_process';
2
+ import { chmod, copyFile, lstat, mkdir, mkdtempDisposable, readlink, rm, symlink, } from 'node:fs/promises';
3
3
  import { tmpdir } from 'node:os';
4
- import { join, matchesGlob } from 'node:path';
4
+ import { basename, dirname, join, resolve } from 'node:path';
5
+ import { text } from 'node:stream/consumers';
5
6
  import { promisify } from 'node:util';
7
+ import { gitIn } from "./git.js";
6
8
  import { withoutRepoVariables } from "./repo-variables.js";
9
+ import { PackagedSnapshot, Snapshot } from "./snapshot/snapshot.js";
10
+ import { Tree, changesBetween, matches } from "./tree.js";
7
11
  const execFileAsync = promisify(execFile);
12
+ // The author and the committer of the commit that the hook writes from the
13
+ // staged tree. They do not change, so one tree and one parent give one commit.
14
+ const stagedCommitter = {
15
+ GIT_AUTHOR_NAME: 'git-hooks',
16
+ GIT_AUTHOR_EMAIL: 'git-hooks@localhost',
17
+ GIT_AUTHOR_DATE: '@0 +0000',
18
+ GIT_COMMITTER_NAME: 'git-hooks',
19
+ GIT_COMMITTER_EMAIL: 'git-hooks@localhost',
20
+ GIT_COMMITTER_DATE: '@0 +0000',
21
+ };
8
22
  // The staged commit: the index of the repo at `root`. Each read of a staged
9
23
  // file goes to its staged copy, never to the working tree, because other
10
- // sessions can edit the working tree while the hook runs. The environment is
11
- // the hook's own, so git keeps the index that `git commit` gave it in
12
- // GIT_INDEX_FILE.
24
+ // sessions can edit the working tree while the hook runs. The git commands get
25
+ // the hook environment, so git keeps the index that `git commit` gave the hook
26
+ // in GIT_INDEX_FILE. Each tool gets the tool environment, which the host
27
+ // computes, in the snapshots of the hook. A repo's own test that builds a
28
+ // Commit gives one environment and no snapshots; the tool environment then
29
+ // comes from that environment.
13
30
  export class Commit {
14
31
  root;
15
32
  #environment;
16
- constructor(root, environment) {
33
+ #snapshots;
34
+ #notice;
35
+ #generated;
36
+ #git;
37
+ constructor({ root, environment, snapshots, notice = () => { }, generated = () => { }, }) {
17
38
  this.root = root;
18
39
  this.#environment = environment;
40
+ this.#snapshots = snapshots;
41
+ this.#notice = notice;
42
+ this.#generated = generated;
43
+ this.#git = gitIn(root, environment);
44
+ }
45
+ // The staged tree: the tree that `git write-tree` writes from the index of
46
+ // the hook. Each call writes the tree of the index as it is then.
47
+ async tree() {
48
+ return Tree.list(await this.#stagedTree(), this.#git);
49
+ }
50
+ // The diff entries from the tree of HEAD to the staged tree. A first commit
51
+ // diffs from the empty tree.
52
+ async changes() {
53
+ return changesBetween(this.#git, await this.#revision('HEAD^{tree}'), await this.#stagedTree());
54
+ }
55
+ // Whether a diff entry of the commit matches one of the globs. A deleted
56
+ // path counts, so a commit that only deletes a file that a step watches
57
+ // still starts the step.
58
+ async touches(globs) {
59
+ return (await this.changes()).some(({ path }) => matches(path, globs));
60
+ }
61
+ async snapshot({ packages = false, } = {}) {
62
+ if (this.#snapshots === undefined)
63
+ throw new TypeError('this Commit has no snapshots: preCommit builds the Commit that has them');
64
+ const clone = await this.#snapshots.open(await this.#stagedCommit(), 'the staged commit', packages);
65
+ return packages ? new PackagedSnapshot(clone) : new Snapshot(clone);
66
+ }
67
+ // Runs `work` in a private snapshot of the staged commit, with packages,
68
+ // and stages each file that the work changed there and that matches one of
69
+ // the globs: new, modified and deleted files, links and the executable bit
70
+ // as written. The index of the hook gets them; the working-tree copy of a
71
+ // file changes too, but only when it is still the old staged copy, because
72
+ // another session may be editing it. `work` fails by throwing, and then
73
+ // nothing is staged.
74
+ async generate(globs, work) {
75
+ if (this.#snapshots === undefined)
76
+ throw new TypeError('this Commit has no snapshots: preCommit builds the Commit that has them');
77
+ await using clone = await this.#snapshots.openPrivate(await this.#stagedCommit(), 'the staged commit');
78
+ await work(new PackagedSnapshot(clone));
79
+ const entries = await written(gitIn(clone.folder, clone.environment), clone.folder, globs);
80
+ if (entries.length === 0)
81
+ return { kind: 'staged', entries };
82
+ const lines = [];
83
+ for (const entry of entries) {
84
+ const staged = await this.#revision(`:${entry.path}`);
85
+ const copy = await this.#workingObject(entry.path);
86
+ if (entry.change === 'deleted') {
87
+ lines.push(`0 ${'0'.repeat(this.#nullLength(staged))}\t${entry.path}`);
88
+ if (copy === staged)
89
+ await rm(join(this.root, entry.path), { force: true });
90
+ else
91
+ this.#leftAlone(entry.path);
92
+ continue;
93
+ }
94
+ const source = join(clone.folder, entry.path);
95
+ const { mode, object } = await this.#store(entry.path, source);
96
+ lines.push(`${mode} ${object}\t${entry.path}`);
97
+ if (copy === staged)
98
+ await place(source, join(this.root, entry.path));
99
+ else
100
+ this.#leftAlone(entry.path);
101
+ }
102
+ if (isPartialCommit(this.#environment))
103
+ return {
104
+ kind: 'not-staged',
105
+ entries,
106
+ reason: [
107
+ 'this commit names its paths (git commit --only or git commit <paths>), so the hook cannot stage what it generated:',
108
+ ...entries.map(({ path }) => ` ${path}`),
109
+ `Commit them with your paths: git commit --only <your paths> ${entries.map(({ path }) => path).join(' ')}`,
110
+ ].join('\n'),
111
+ };
112
+ await this.#updateIndex(`${lines.join('\n')}\n`);
113
+ this.#generated(await this.#stagedTree());
114
+ return { kind: 'staged', entries };
19
115
  }
20
116
  // The paths that the commit adds, copies, modifies or renames.
21
117
  async staged() {
@@ -37,11 +133,6 @@ export class Commit {
37
133
  blob(path) {
38
134
  return this.#git('cat-file', 'blob', `:${path}`);
39
135
  }
40
- // Whether the commit stages a path that matches one of the globs.
41
- async touches(globs) {
42
- const staged = await this.staged();
43
- return staged.some((path) => globs.some((glob) => matchesGlob(path, glob)));
44
- }
45
136
  // Runs `work` on a copy of the staged tree in a temporary folder, for a
46
137
  // check that reads many files at once. The folder is removed afterwards.
47
138
  async inSnapshot(work) {
@@ -68,7 +159,8 @@ export class Commit {
68
159
  const { stdout } = await execFileAsync(join(this.root, 'node_modules/.bin/nx'), args, {
69
160
  cwd: this.root,
70
161
  env: {
71
- ...(await withoutRepoVariables(this.#environment)),
162
+ ...(this.#snapshots?.toolEnvironment ??
163
+ (await withoutRepoVariables(this.#environment))),
72
164
  NX_DAEMON: 'false',
73
165
  },
74
166
  encoding: 'utf8',
@@ -89,15 +181,162 @@ export class Commit {
89
181
  throw new TypeError(`nx show projects gave ${output.trim()}, not a list of names`);
90
182
  return projects;
91
183
  }
92
- async #git(...args) {
93
- const { stdout } = await execFileAsync('git', args, {
94
- cwd: this.root,
95
- env: this.#environment,
96
- encoding: 'utf8',
97
- // A staged file of any size, not Node's default of 1 MiB.
98
- maxBuffer: 2 ** 30,
99
- });
100
- return stdout;
184
+ // The object of the working-tree copy of `path` as git would store it, or
185
+ // undefined when there is none.
186
+ async #workingObject(path) {
187
+ const file = join(this.root, path);
188
+ const stats = await lstat(file).catch(() => undefined);
189
+ if (stats === undefined)
190
+ return undefined;
191
+ if (stats.isSymbolicLink())
192
+ return (await gitWithInput(this.root, this.#environment, await readlink(file), [
193
+ 'hash-object',
194
+ '--stdin',
195
+ ])).trim();
196
+ return (await this.#git('hash-object', `--path=${path}`, '--', file)).trim();
197
+ }
198
+ // Writes the file at `source` into the object store of the repo, as the
199
+ // content of `path`, and gives its mode and object.
200
+ async #store(path, source) {
201
+ const stats = await lstat(source);
202
+ if (stats.isSymbolicLink())
203
+ return {
204
+ mode: '120000',
205
+ object: (await gitWithInput(this.root, this.#environment, await readlink(source), ['hash-object', '-w', '--stdin'])).trim(),
206
+ };
207
+ return {
208
+ mode: (stats.mode & 0o111) === 0 ? '100644' : '100755',
209
+ object: (await this.#git('hash-object', '-w', `--path=${path}`, '--', source)).trim(),
210
+ };
211
+ }
212
+ // A null object id is all zeros, as long as the ids of the repo: 40 for
213
+ // SHA-1, 64 for SHA-256.
214
+ #nullLength(staged) {
215
+ return staged?.length ?? 40;
216
+ }
217
+ #leftAlone(path) {
218
+ this.#notice(`left the working-tree copy of ${path}: it has edits that are not staged`);
219
+ }
220
+ // One update of the index of the hook. It takes the lock of the index, so
221
+ // it fails while a peer's git command holds it.
222
+ async #updateIndex(info) {
223
+ try {
224
+ await gitWithInput(this.root, this.#environment, info, [
225
+ 'update-index',
226
+ '--index-info',
227
+ ]);
228
+ }
229
+ catch (error) {
230
+ if (Error.isError(error) && /index\.lock/.test(error.message))
231
+ throw new Error('another git process holds the lock of the index, so the hook could not stage what it generated; commit again', { cause: error });
232
+ throw error;
233
+ }
234
+ }
235
+ // `git write-tree` takes the lock of the index, to keep the trees that it
236
+ // wrote, and fails while another process holds it: a peer's `git add`, or
237
+ // the `git status` of an editor. A plain `git commit` does not lock the
238
+ // index while the hook runs. So the tree is written from a private copy of
239
+ // the index of the hook. No index file is an empty index.
240
+ async #stagedTree() {
241
+ await using copy = await mkdtempDisposable(join(tmpdir(), 'git-hooks-index-'));
242
+ const index = join(copy.path, 'index');
243
+ try {
244
+ await copyFile(resolve(this.root, (await this.#git('rev-parse', '--git-path', 'index')).trim()), index);
245
+ }
246
+ catch (error) {
247
+ if (!Error.isError(error) ||
248
+ !('code' in error) ||
249
+ error.code !== 'ENOENT')
250
+ throw error;
251
+ }
252
+ return (await gitIn(this.root, { ...this.#environment, GIT_INDEX_FILE: index })('write-tree')).trim();
253
+ }
254
+ // A commit of the staged tree, with HEAD as its parent, that no ref holds.
255
+ // `git gc` prunes it.
256
+ async #stagedCommit() {
257
+ const parent = await this.#revision('HEAD');
258
+ const tree = await this.#stagedTree();
259
+ return (await gitIn(this.root, { ...this.#environment, ...stagedCommitter })('commit-tree', '--no-gpg-sign', '-m', 'the staged tree', ...(parent === undefined ? [] : ['-p', parent]), tree)).trim();
101
260
  }
261
+ // The object that `revision` names, or undefined before the first commit.
262
+ async #revision(revision) {
263
+ try {
264
+ return (await this.#git('rev-parse', '--verify', '--quiet', revision)).trim();
265
+ }
266
+ catch (error) {
267
+ // With --quiet, rev-parse exits with 1 and says nothing when HEAD names
268
+ // no commit yet.
269
+ if (Error.isError(error) && 'code' in error && error.code === 1)
270
+ return undefined;
271
+ throw error;
272
+ }
273
+ }
274
+ }
275
+ // The paths that `work` wrote in the clone, as diff entries against the
276
+ // commit that the clone checked out, that match one of the globs.
277
+ async function written(git, folder, globs) {
278
+ const status = await git('status', '--porcelain=v1', '-z', '--untracked-files=all', '--no-renames');
279
+ const entries = [];
280
+ for (const record of status.split('\0')) {
281
+ if (record === '')
282
+ continue;
283
+ const code = record.slice(0, 2);
284
+ const path = record.slice(3);
285
+ if (!matches(path, globs))
286
+ continue;
287
+ if (code.includes('D')) {
288
+ entries.push({ change: 'deleted', path });
289
+ continue;
290
+ }
291
+ const kind = (await lstat(join(folder, path))).isSymbolicLink()
292
+ ? 'link'
293
+ : 'file';
294
+ const change = code === '??'
295
+ ? 'added'
296
+ : code.includes('T')
297
+ ? 'type-changed'
298
+ : 'modified';
299
+ entries.push({ change, path, kind });
300
+ }
301
+ return entries;
302
+ }
303
+ // Writes the file or link at `source` to `destination`, with its executable
304
+ // bit.
305
+ async function place(source, destination) {
306
+ await mkdir(dirname(destination), { recursive: true });
307
+ await rm(destination, { force: true });
308
+ const stats = await lstat(source);
309
+ if (stats.isSymbolicLink())
310
+ return symlink(await readlink(source), destination);
311
+ await copyFile(source, destination);
312
+ await chmod(destination, stats.mode & 0o777);
313
+ }
314
+ // git runs the pre-commit hook of `git commit --only` or `git commit <paths>`
315
+ // with a temporary index, `next-index-<pid>.lock`, that it commits and then
316
+ // drops. A file staged there is in the commit, but the index of the repo does
317
+ // not get it, so the next commit would undo it. The name is the only sign of
318
+ // that mode: a predicting check, because no harmless trial tells the modes
319
+ // apart.
320
+ function isPartialCommit(environment) {
321
+ return basename(environment.GIT_INDEX_FILE ?? '').startsWith('next-index-');
322
+ }
323
+ async function gitWithInput(cwd, environment, input, args) {
324
+ const child = spawn('git', args, {
325
+ cwd,
326
+ env: environment,
327
+ stdio: ['pipe', 'pipe', 'pipe'],
328
+ });
329
+ child.stdin.end(input);
330
+ const [stdout, stderr, code] = await Promise.all([
331
+ text(child.stdout),
332
+ text(child.stderr),
333
+ new Promise((resolve, reject) => {
334
+ child.once('error', reject);
335
+ child.once('close', resolve);
336
+ }),
337
+ ]);
338
+ if (code !== 0)
339
+ throw new Error(`git ${args.join(' ')} exited with ${code}: ${stderr.trim()}`);
340
+ return stdout;
102
341
  }
103
342
  //# sourceMappingURL=commit.js.map