@statewalker/vcs-commands 0.10.0 → 0.11.0

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 (72) hide show
  1. package/dist/commands/am/am-command.d.ts.map +1 -1
  2. package/dist/commands/cat-file/cat-file-command.d.ts +9 -0
  3. package/dist/commands/cat-file/cat-file-command.d.ts.map +1 -1
  4. package/dist/commands/checkout-command.d.ts.map +1 -1
  5. package/dist/commands/clone-command.d.ts +9 -0
  6. package/dist/commands/clone-command.d.ts.map +1 -1
  7. package/dist/commands/default-branch.d.ts +12 -0
  8. package/dist/commands/default-branch.d.ts.map +1 -1
  9. package/dist/commands/diff-command.d.ts.map +1 -1
  10. package/dist/commands/format-rev-command.d.ts +6 -4
  11. package/dist/commands/format-rev-command.d.ts.map +1 -1
  12. package/dist/commands/fsmonitor-ipc.d.ts +30 -0
  13. package/dist/commands/fsmonitor-ipc.d.ts.map +1 -0
  14. package/dist/commands/fsmonitor-refresh.d.ts.map +1 -1
  15. package/dist/commands/index.d.ts +1 -0
  16. package/dist/commands/index.d.ts.map +1 -1
  17. package/dist/commands/index.js +6 -6
  18. package/dist/commands/log-output.d.ts +6 -0
  19. package/dist/commands/log-output.d.ts.map +1 -1
  20. package/dist/commands/ls-tree-command.d.ts.map +1 -1
  21. package/dist/commands/repo-command.d.ts +28 -6
  22. package/dist/commands/repo-command.d.ts.map +1 -1
  23. package/dist/commands/reset-command.d.ts +19 -2
  24. package/dist/commands/reset-command.d.ts.map +1 -1
  25. package/dist/commands/rev-parse-command.d.ts +3 -7
  26. package/dist/commands/rev-parse-command.d.ts.map +1 -1
  27. package/dist/commands/revision-resolver.d.ts +15 -1
  28. package/dist/commands/revision-resolver.d.ts.map +1 -1
  29. package/dist/commands/signing.d.ts +8 -1
  30. package/dist/commands/signing.d.ts.map +1 -1
  31. package/dist/commands/status-command.d.ts +14 -1
  32. package/dist/commands/status-command.d.ts.map +1 -1
  33. package/dist/commands/submodule-add-command.d.ts +12 -0
  34. package/dist/commands/submodule-add-command.d.ts.map +1 -1
  35. package/dist/commands/submodule-update-command.d.ts +14 -0
  36. package/dist/commands/submodule-update-command.d.ts.map +1 -1
  37. package/dist/commands/tag-command.d.ts.map +1 -1
  38. package/dist/commands/update-index-command.d.ts +21 -0
  39. package/dist/commands/update-index-command.d.ts.map +1 -1
  40. package/dist/{commands-Mlo1dxQN.js → commands-bQQ9OmCh.js} +354 -77
  41. package/dist/{fsck-CrhVMNIH.js → fsck-BHT9bUS-.js} +1 -1
  42. package/dist/git-command.d.ts +8 -2
  43. package/dist/git-command.d.ts.map +1 -1
  44. package/dist/index.js +6 -6
  45. package/dist/{maintenance-run-command-Bw21HEkH.js → maintenance-run-command-Cx2PlGV9.js} +9 -4
  46. package/dist/{remote-transport-Bu4PGSMD.js → remote-transport-BrQZfyHu.js} +3 -3
  47. package/dist/results/index.js +1 -1
  48. package/dist/{results-ERo9vELd.js → results-CNHQnU5Y.js} +327 -54
  49. package/package.json +12 -12
  50. package/src/commands/am/am-command.ts +30 -14
  51. package/src/commands/cat-file/cat-file-command.ts +46 -8
  52. package/src/commands/checkout-command.ts +22 -1
  53. package/src/commands/clone-command.ts +41 -2
  54. package/src/commands/default-branch.ts +19 -0
  55. package/src/commands/diff-command.ts +19 -1
  56. package/src/commands/format-rev-command.ts +28 -12
  57. package/src/commands/fsmonitor-ipc.ts +121 -0
  58. package/src/commands/fsmonitor-refresh.ts +7 -2
  59. package/src/commands/index.ts +1 -0
  60. package/src/commands/log-output.ts +11 -0
  61. package/src/commands/ls-tree-command.ts +25 -1
  62. package/src/commands/repo-command.ts +124 -25
  63. package/src/commands/reset-command.ts +75 -3
  64. package/src/commands/rev-parse-command.ts +14 -28
  65. package/src/commands/revision-resolver.ts +53 -11
  66. package/src/commands/signing.ts +54 -7
  67. package/src/commands/status-command.ts +31 -2
  68. package/src/commands/submodule-add-command.ts +18 -1
  69. package/src/commands/submodule-update-command.ts +18 -1
  70. package/src/commands/tag-command.ts +33 -11
  71. package/src/commands/update-index-command.ts +137 -5
  72. package/src/git-command.ts +48 -4
@@ -12,6 +12,10 @@
12
12
  *
13
13
  * // git update-index --refresh
14
14
  * const { output, exitCode } = await git.updateIndex().refresh().call();
15
+ *
16
+ * // git update-index --fsmonitor, then --fsmonitor-valid a
17
+ * await git.updateIndex().setFsmonitor(true).call();
18
+ * await git.updateIndex().setFsmonitorValid(true).addPath("a").call();
15
19
  * ```
16
20
  */
17
21
 
@@ -26,6 +30,7 @@ import {
26
30
  basicStatMatches,
27
31
  compareWithWorktree,
28
32
  entryStatOf,
33
+ FsmonitorState,
29
34
  invalidateCacheTreePath,
30
35
  modeFromWorktree,
31
36
  type ResolveUndoRecord,
@@ -38,7 +43,11 @@ import {
38
43
  } from "@statewalker/vcs-working-tree";
39
44
  import { UpdateIndexFailedError } from "../errors/index.js";
40
45
  import { GitCommand } from "../git-command.js";
46
+ import { RemoteConfigStore } from "../remote-config/index.js";
41
47
  import { worktreeModeTrust } from "./core-config.js";
48
+ import { fsmonitorModeOf } from "./fsmonitor-refresh.js";
49
+ import { hookHostOf } from "./git-hooks.js";
50
+ import { hostEnvironmentOf } from "./git-ident.js";
42
51
  import { configBool } from "./merge-config.js";
43
52
  import { inputLines, inputPaths, unquoteLine } from "./plumbing-input.js";
44
53
  import { worktreePathRules } from "./worktree-path-rules.js";
@@ -74,6 +83,8 @@ interface State {
74
83
  markValid?: boolean;
75
84
  /** `--[no-]skip-worktree` */
76
85
  markSkipWorktree?: boolean;
86
+ /** `--[no-]fsmonitor-valid` */
87
+ markFsmonitor?: boolean;
77
88
  ignoreSkipWorktreeEntries: boolean;
78
89
  chmod?: "+x" | "-x";
79
90
  quiet: boolean;
@@ -133,6 +144,7 @@ export class UpdateIndexCommand extends GitCommand<UpdateIndexResult> {
133
144
  private indexVersion?: number;
134
145
  private showIndexVersion = false;
135
146
  private forceWrite = false;
147
+ private fsmonitor?: boolean;
136
148
 
137
149
  private set(apply: (state: State) => void): this {
138
150
  this.checkCallable();
@@ -203,6 +215,28 @@ export class UpdateIndexCommand extends GitCommand<UpdateIndexResult> {
203
215
  });
204
216
  }
205
217
 
218
+ /**
219
+ * `--fsmonitor-valid` (true) / `--no-fsmonitor-valid` (false) for the paths after it: the
220
+ * entry's fsmonitor valid bit (`CE_FSMONITOR_VALID`) set or cleared.
221
+ */
222
+ setFsmonitorValid(mark: boolean): this {
223
+ return this.set((s) => {
224
+ s.markFsmonitor = mark;
225
+ });
226
+ }
227
+
228
+ /**
229
+ * `--fsmonitor` (true) / `--no-fsmonitor` (false), applied after the paths, as git does:
230
+ * the index's `FSMN` extension added (`add_fsmonitor`; a warning when `core.fsmonitor` is
231
+ * unset; git's death in a bare repository with `core.fsmonitor` set) or removed (a warning
232
+ * when `core.fsmonitor` is set).
233
+ */
234
+ setFsmonitor(enable: boolean): this {
235
+ this.checkCallable();
236
+ this.fsmonitor = enable;
237
+ return this;
238
+ }
239
+
206
240
  /** `--ignore-skip-worktree-entries`: `--remove` leaves skip-worktree entries alone. */
207
241
  setIgnoreSkipWorktreeEntries(ignore = true): this {
208
242
  return this.set((s) => {
@@ -315,8 +349,24 @@ export class UpdateIndexCommand extends GitCommand<UpdateIndexResult> {
315
349
  async call(): Promise<UpdateIndexResult> {
316
350
  this.checkCallable();
317
351
  this.setCallable(false);
318
- await this.readCurrentIndex();
319
- const run = new UpdateIndexRun(this.context());
352
+ // fsm_settings__get_reason: a bare repository is incompatible with a configured monitor
353
+ // (git dies after reading the index, which a bare working copy here does not give)
354
+ if (this.fsmonitor && !this.hasWorktree()) {
355
+ const mode = fsmonitorModeOf(
356
+ RemoteConfigStore.from(this.workingCopy).value("core.fsmonitor"),
357
+ );
358
+ if (mode.kind !== "disabled") {
359
+ throw new UpdateIndexFailedError(
360
+ `fatal: bare repository '${await this.currentDirectory()}' is incompatible with fsmonitor`,
361
+ );
362
+ }
363
+ }
364
+ await this.readCurrentIndex(true, false);
365
+ // tweak_fsmonitor on the index read: the monitor asked, the extension added or removed
366
+ const hadFsmonitor = !!this.staging.getFsmonitor?.();
367
+ const fsm = await this.fsmonitorRefresh(this.staging);
368
+ const run = new UpdateIndexRun(this.context(fsm?.state));
369
+ for (const warning of fsm?.warnings ?? []) run.stderr.push(`warning: ${warning}`);
320
370
  try {
321
371
  await this.inOneOperation(async () => {
322
372
  for (const step of this.steps) await run.step(step);
@@ -330,6 +380,7 @@ export class UpdateIndexCommand extends GitCommand<UpdateIndexResult> {
330
380
  await run.updateOne(path);
331
381
  }
332
382
  }
383
+ await this.applyFsmonitor(run);
333
384
  });
334
385
  } catch (error) {
335
386
  if (error instanceof Death) {
@@ -340,7 +391,10 @@ export class UpdateIndexCommand extends GitCommand<UpdateIndexResult> {
340
391
  }
341
392
  throw error;
342
393
  }
343
- if (run.changed || this.forceWrite) {
394
+ // FSMONITOR_CHANGED: the extension added or removed, bits set
395
+ const fsmonitorNow = this.staging.getFsmonitor?.();
396
+ const fsmonitorChanged = !!fsmonitorNow?.changed || (hadFsmonitor && !fsmonitorNow);
397
+ if (run.changed || this.forceWrite || fsmonitorChanged) {
344
398
  try {
345
399
  await run.write();
346
400
  } catch (error) {
@@ -360,6 +414,50 @@ export class UpdateIndexCommand extends GitCommand<UpdateIndexResult> {
360
414
  };
361
415
  }
362
416
 
417
+ /** `--[no-]fsmonitor`: `add_fsmonitor` / `remove_fsmonitor` and their warnings. */
418
+ private async applyFsmonitor(run: UpdateIndexRun): Promise<void> {
419
+ if (this.fsmonitor === undefined) return;
420
+ const mode = fsmonitorModeOf(RemoteConfigStore.from(this.workingCopy).value("core.fsmonitor"));
421
+ const staging = this.staging;
422
+ if (this.fsmonitor) {
423
+ if (mode.kind === "disabled") {
424
+ run.stderr.push(
425
+ "warning: core.fsmonitor is unset; set it if you really want to enable fsmonitor",
426
+ );
427
+ }
428
+ if (!staging.getFsmonitor?.() && staging.setFsmonitor) {
429
+ // add_fsmonitor: a nanosecond token, every entry dirty; written although the
430
+ // monitor is off (the next read with it off removes it)
431
+ const state = new FsmonitorState(String(BigInt(Date.now()) * 1_000_000n));
432
+ state.changed = true;
433
+ state.writtenWhileDisabled = mode.kind === "disabled";
434
+ staging.setFsmonitor(state);
435
+ }
436
+ run.report("fsmonitor enabled");
437
+ } else {
438
+ if (mode.kind !== "disabled" && this.hasWorktree()) {
439
+ run.stderr.push(
440
+ "warning: core.fsmonitor is set; remove it if you really want to disable fsmonitor",
441
+ );
442
+ }
443
+ staging.setFsmonitor?.(undefined);
444
+ run.report("fsmonitor disabled");
445
+ }
446
+ }
447
+
448
+ /** git's `xgetcwd()` for a bare repository: the git directory's host path. */
449
+ private async currentDirectory(): Promise<string> {
450
+ const layout = await hookHostOf(this.workingCopy).layout();
451
+ if (!layout) return ".";
452
+ const root = (layout.files as { rootDirectory?: string }).rootDirectory;
453
+ if (!root) return layout.gitDir;
454
+ const base = root.replace(/\/+$/, "");
455
+ const rest = layout.gitDir.replace(/^\.?\/*|\/+$/g, "");
456
+ const path = rest ? `${base}/${rest}` : base || "/";
457
+ const environment = hostEnvironmentOf(this.workingCopy);
458
+ return (await environment?.realPath?.(path).catch(() => undefined)) ?? path;
459
+ }
460
+
363
461
  private applyIndexVersion(run: UpdateIndexRun): void {
364
462
  const staging = this.staging as unknown as {
365
463
  _getVersion?(): number;
@@ -376,9 +474,10 @@ export class UpdateIndexCommand extends GitCommand<UpdateIndexResult> {
376
474
  if (staging._setVersion) run.setVersion = (v) => staging._setVersion?.(v);
377
475
  }
378
476
 
379
- private context(): RunContext {
477
+ private context(fsmonitor: FsmonitorState | undefined): RunContext {
380
478
  const worktree = this.worktreeAccess;
381
479
  return {
480
+ fsmonitor,
382
481
  staging: this.staging,
383
482
  worktree,
384
483
  blobs: this.blobs,
@@ -394,6 +493,8 @@ export class UpdateIndexCommand extends GitCommand<UpdateIndexResult> {
394
493
  }
395
494
 
396
495
  interface RunContext {
496
+ /** The index's fsmonitor state, the monitor asked (undefined while it is off) */
497
+ fsmonitor: FsmonitorState | undefined;
397
498
  staging: UpdateIndexCommand["staging"];
398
499
  worktree: Worktree | undefined;
399
500
  blobs: UpdateIndexCommand["blobs"];
@@ -607,7 +708,10 @@ class UpdateIndexRun {
607
708
  async updateOne(path: string): Promise<void> {
608
709
  await this.load();
609
710
  const s = this.state;
610
- const markOnly = s.markValid !== undefined || s.markSkipWorktree !== undefined;
711
+ const markOnly =
712
+ s.markValid !== undefined ||
713
+ s.markSkipWorktree !== undefined ||
714
+ s.markFsmonitor !== undefined;
611
715
  let current: WorktreeEntry | undefined;
612
716
  let missing = false;
613
717
  let mode = 0;
@@ -632,6 +736,12 @@ class UpdateIndexRun {
632
736
  }
633
737
  return this.chmodAfter(path);
634
738
  }
739
+ if (s.markFsmonitor !== undefined) {
740
+ if (!this.markFsmonitor(path, s.markFsmonitor)) {
741
+ throw new Death(`Unable to mark file ${path}`);
742
+ }
743
+ return this.chmodAfter(path);
744
+ }
635
745
  if (s.forceRemove) {
636
746
  this.removeFile(path);
637
747
  this.report(`remove '${path}'`);
@@ -672,6 +782,21 @@ class UpdateIndexRun {
672
782
  return true;
673
783
  }
674
784
 
785
+ /** `mark_ce_flags` with `CE_FSMONITOR_VALID`: false when the path has no stage-0 entry. */
786
+ private markFsmonitor(path: string, mark: boolean): boolean {
787
+ const pos = this.namePos(path);
788
+ if (pos < 0) return false;
789
+ const ce = this.entries[pos] as Entry;
790
+ const state = this.ctx.fsmonitor;
791
+ if (state) {
792
+ state.markInvalid(ce);
793
+ if (mark) state.markValid(ce, { gitlinks: true });
794
+ }
795
+ this.invalidate(path);
796
+ this.changed = true;
797
+ return true;
798
+ }
799
+
675
800
  /** `remove_one_path`. */
676
801
  private removeOnePath(path: string): boolean {
677
802
  if (!this.state.remove) {
@@ -902,6 +1027,7 @@ class UpdateIndexRun {
902
1027
  const worktree = this.ctx.worktree;
903
1028
  if (!worktree) return;
904
1029
  const trust = this.ctx.trust;
1030
+ const fsmonitor = this.ctx.fsmonitor;
905
1031
  for (let i = 0; i < this.entries.length; i++) {
906
1032
  const ce = this.entries[i] as Entry;
907
1033
  if (s.ignoreSubmodules && ce.mode === FileMode.GITLINK) continue;
@@ -916,6 +1042,8 @@ class UpdateIndexRun {
916
1042
  // refresh_cache_ent: what the user promised not to have changed
917
1043
  if (ce.skipWorktree) continue;
918
1044
  if (!really && ce.assumeValid) continue;
1045
+ // CE_FSMONITOR_VALID: the monitor reported nothing for it, taken as up to date unseen
1046
+ if (fsmonitor?.isValid(ce)) continue;
919
1047
  const current = await worktree.getEntry(ce.path).catch(() => undefined);
920
1048
  if (!current && s.ignoreMissing) continue;
921
1049
  let changed: boolean;
@@ -948,8 +1076,12 @@ class UpdateIndexRun {
948
1076
  continue;
949
1077
  }
950
1078
  if (fresh && !sameStat(fresh, ce)) {
1079
+ // replaced (its stat stored): no longer fsmonitor-valid (replace_index_entry)
951
1080
  this.entries[i] = fresh;
952
1081
  this.changed = true;
1082
+ } else {
1083
+ // up to date as it is: mark_fsmonitor_valid (gitlinks never)
1084
+ fsmonitor?.markValid(ce);
953
1085
  }
954
1086
  }
955
1087
  // has_racy_timestamp: written anyway, so the racy entries stop being racy
@@ -46,6 +46,7 @@ import {
46
46
  clearSkipWorktreeFromPresentFiles,
47
47
  compareWithWorktree,
48
48
  IndexChangedError,
49
+ isIndexUnborn,
49
50
  type OperationOptions,
50
51
  type SparseCheckoutPatterns,
51
52
  type Staging,
@@ -62,6 +63,7 @@ import {
62
63
  worktreeModeTrust,
63
64
  } from "./commands/core-config.js";
64
65
  import type { ForkPointSource } from "./commands/fork-point.js";
66
+ import { FSMONITOR_DAEMON_IPC_NAME, queryFsmonitorDaemon } from "./commands/fsmonitor-ipc.js";
65
67
  import { type FsmonitorRun, refreshFsmonitor } from "./commands/fsmonitor-refresh.js";
66
68
  import { type HookHost, HookSession, refTransactionHookOf } from "./commands/git-hooks.js";
67
69
  import {
@@ -456,11 +458,26 @@ export abstract class GitCommand<T> {
456
458
  return refreshFsmonitor({
457
459
  staging,
458
460
  owner: this,
459
- config: (key) => config.value(key),
461
+ // fsm_settings: a bare repository is incompatible, the monitor off (tweak_fsmonitor
462
+ // then removes the extension)
463
+ config: (key) =>
464
+ key === "core.fsmonitor" && !this.hasWorktree() ? undefined : config.value(key),
460
465
  ignoreCase: parseGitBool(config.value("core.ignorecase")) === true,
461
466
  query: async (query) => {
462
467
  if (environment?.fsmonitor) return environment.fsmonitor.query(query);
463
- if (query.mode !== "hook" || query.command === undefined) return undefined;
468
+ if (query.mode === "ipc") {
469
+ // the builtin daemon at <git dir>/fsmonitor--daemon.ipc, through the host's port
470
+ if (!environment?.openIpcPort) return undefined;
471
+ const gitDir = await this.hooks.hostGitDir({ real: false });
472
+ if (!gitDir) return undefined;
473
+ const port = await environment.openIpcPort({
474
+ service: "fsmonitor--daemon",
475
+ path: `${gitDir}/${FSMONITOR_DAEMON_IPC_NAME}`,
476
+ });
477
+ const stdout = await queryFsmonitorDaemon(port, query.token);
478
+ return stdout ? { exitCode: 0, stdout } : undefined;
479
+ }
480
+ if (query.command === undefined) return undefined;
464
481
  return this.hooks.runShellCommandBytes("fsmonitor", query.command, [
465
482
  String(query.version),
466
483
  query.token,
@@ -469,6 +486,11 @@ export abstract class GitCommand<T> {
469
486
  });
470
487
  }
471
488
 
489
+ /** Whether the working copy has a worktree (a bare repository's refuses to give one). */
490
+ protected hasWorktree(): boolean {
491
+ return "worktree" in this._workingCopy && !!this._workingCopy.worktree;
492
+ }
493
+
472
494
  /**
473
495
  * The hooks this command runs (git's `run_hooks_opt`), through the host environment's
474
496
  * `hookRunner`; with none, no hook runs.
@@ -1285,8 +1307,14 @@ export abstract class GitCommand<T> {
1285
1307
  *
1286
1308
  * @param refuseUnwritten false for a command that writes no index (`clean`): unwritten
1287
1309
  * edits are then kept and used instead of refusing
1310
+ *
1311
+ * The file system monitor is then asked (git's `tweak_fsmonitor`, see `fsmonitorRefresh`).
1288
1312
  */
1289
- protected async readCurrentIndex(refuseUnwritten = true): Promise<void> {
1313
+ protected async readCurrentIndex(
1314
+ refuseUnwritten = true,
1315
+ /** false: the command asks the monitor itself (it needs what the read found first) */
1316
+ fsmonitor = true,
1317
+ ): Promise<void> {
1290
1318
  const staging = this.staging;
1291
1319
  if (await staging.isOutdated()) {
1292
1320
  if (staging.hasUnwrittenChanges?.() !== false) {
@@ -1296,6 +1324,10 @@ export abstract class GitCommand<T> {
1296
1324
  await staging.read();
1297
1325
  }
1298
1326
  await this.clearPresentSkipWorktree();
1327
+ // do_read_index's tweak_fsmonitor: the monitor asked (an extension added when missing,
1328
+ // the valid bits dropped on a failed answer) for every index read, not only by the
1329
+ // commands that use the bits; a missing index file is read without it
1330
+ if (fsmonitor && !(await isIndexUnborn(staging))) await this.fsmonitorRefresh(staging);
1299
1331
  }
1300
1332
 
1301
1333
  /**
@@ -1349,7 +1381,7 @@ export abstract class GitCommand<T> {
1349
1381
  });
1350
1382
  }
1351
1383
 
1352
- private resolveStaging(): Staging {
1384
+ protected resolveStaging(): Staging {
1353
1385
  // Try new architecture first (checkout.staging)
1354
1386
  if (this._workingCopy.checkout?.staging) {
1355
1387
  return this._workingCopy.checkout.staging as unknown as Staging;
@@ -1694,6 +1726,18 @@ export abstract class GitCommand<T> {
1694
1726
  * from its stored bytes without the name check. Undefined when the tree cannot be read.
1695
1727
  */
1696
1728
  protected async listedTreeEntries(id: ObjectId): Promise<readonly TreeEntry[] | undefined> {
1729
+ const compat = this.compatObjects;
1730
+ if (compat && id.length !== this.objectFormat.hexLength) {
1731
+ // a tree named by its compatibility id (GET_OID_HASH_ANY): read converted, its entries
1732
+ // the compatibility format's ids, as git's parse_tree of such an id lists them
1733
+ const tree = await compat.readAsCompat(id).catch(() => undefined);
1734
+ if (tree?.type !== "tree") return undefined;
1735
+ return decodeTreeBytes(tree.content, {
1736
+ canonicalModes: true,
1737
+ idLength: compat.compat.rawLength,
1738
+ checkNames: false,
1739
+ });
1740
+ }
1697
1741
  try {
1698
1742
  const loaded = await this.trees.load(id);
1699
1743
  if (!loaded) return undefined;