@statewalker/vcs-commands 0.3.2 → 0.5.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 (133) hide show
  1. package/README.md +19 -0
  2. package/dist/commands/add-command.d.ts +0 -6
  3. package/dist/commands/add-command.d.ts.map +1 -1
  4. package/dist/commands/blame-command.d.ts +14 -0
  5. package/dist/commands/blame-command.d.ts.map +1 -1
  6. package/dist/commands/branch-command.d.ts.map +1 -1
  7. package/dist/commands/checkout-command.d.ts +7 -24
  8. package/dist/commands/checkout-command.d.ts.map +1 -1
  9. package/dist/commands/clean-command.d.ts +34 -4
  10. package/dist/commands/clean-command.d.ts.map +1 -1
  11. package/dist/commands/clone-command.d.ts +61 -21
  12. package/dist/commands/clone-command.d.ts.map +1 -1
  13. package/dist/commands/core-config.d.ts +16 -0
  14. package/dist/commands/core-config.d.ts.map +1 -0
  15. package/dist/commands/fetch-command.d.ts +104 -37
  16. package/dist/commands/fetch-command.d.ts.map +1 -1
  17. package/dist/commands/index.d.ts +1 -0
  18. package/dist/commands/index.d.ts.map +1 -1
  19. package/dist/commands/index.js +2 -2
  20. package/dist/commands/init-command.d.ts.map +1 -1
  21. package/dist/commands/merge-command.d.ts +15 -2
  22. package/dist/commands/merge-command.d.ts.map +1 -1
  23. package/dist/commands/push-command.d.ts +58 -25
  24. package/dist/commands/push-command.d.ts.map +1 -1
  25. package/dist/commands/remote-command.d.ts +115 -6
  26. package/dist/commands/remote-command.d.ts.map +1 -1
  27. package/dist/commands/reset-command.d.ts.map +1 -1
  28. package/dist/commands/stash-apply-command.d.ts +35 -13
  29. package/dist/commands/stash-apply-command.d.ts.map +1 -1
  30. package/dist/commands/stash-create-command.d.ts +59 -25
  31. package/dist/commands/stash-create-command.d.ts.map +1 -1
  32. package/dist/commands/stash-drop-command.d.ts +8 -2
  33. package/dist/commands/stash-drop-command.d.ts.map +1 -1
  34. package/dist/commands/stash-list-command.d.ts +2 -4
  35. package/dist/commands/stash-list-command.d.ts.map +1 -1
  36. package/dist/commands/stash-working-tree.d.ts +72 -0
  37. package/dist/commands/stash-working-tree.d.ts.map +1 -0
  38. package/dist/commands/status-command.d.ts +29 -22
  39. package/dist/commands/status-command.d.ts.map +1 -1
  40. package/dist/commands/tag-command.d.ts.map +1 -1
  41. package/dist/commands/tree-restore.d.ts +52 -5
  42. package/dist/commands/tree-restore.d.ts.map +1 -1
  43. package/dist/commands/worktree-overwrite-checks.d.ts +64 -0
  44. package/dist/commands/worktree-overwrite-checks.d.ts.map +1 -0
  45. package/dist/{commands-COO4tJhv.js → commands-CsLNyvZ1.js} +3370 -1057
  46. package/dist/core-commands/add.command.impl.d.ts +0 -4
  47. package/dist/core-commands/add.command.impl.d.ts.map +1 -1
  48. package/dist/core-commands/index.d.ts +1 -3
  49. package/dist/core-commands/index.d.ts.map +1 -1
  50. package/dist/core-commands/store-worktree-file.d.ts +6 -0
  51. package/dist/core-commands/store-worktree-file.d.ts.map +1 -0
  52. package/dist/errors/checkout-errors.d.ts +13 -0
  53. package/dist/errors/checkout-errors.d.ts.map +1 -1
  54. package/dist/errors/index.js +18 -1
  55. package/dist/git-command.d.ts +10 -1
  56. package/dist/git-command.d.ts.map +1 -1
  57. package/dist/git.d.ts +21 -3
  58. package/dist/git.d.ts.map +1 -1
  59. package/dist/index.js +4 -4
  60. package/dist/remote-config/index.d.ts +1 -0
  61. package/dist/remote-config/index.d.ts.map +1 -1
  62. package/dist/remote-config/remote-config-store.d.ts +22 -0
  63. package/dist/remote-config/remote-config-store.d.ts.map +1 -1
  64. package/dist/remote-config/remote-refs.d.ts +37 -0
  65. package/dist/remote-config/remote-refs.d.ts.map +1 -0
  66. package/dist/results/clone-result.d.ts +6 -0
  67. package/dist/results/clone-result.d.ts.map +1 -1
  68. package/dist/results/diff-formatter.d.ts +14 -7
  69. package/dist/results/diff-formatter.d.ts.map +1 -1
  70. package/dist/results/fetch-result.d.ts +16 -1
  71. package/dist/results/fetch-result.d.ts.map +1 -1
  72. package/dist/results/index.js +28 -23
  73. package/dist/results/push-result.d.ts +9 -2
  74. package/dist/results/push-result.d.ts.map +1 -1
  75. package/dist/results/status-result.d.ts +91 -20
  76. package/dist/results/status-result.d.ts.map +1 -1
  77. package/dist/{status-result-BwAyxq6R.js → status-result-BpyUK1Sg.js} +115 -5
  78. package/dist/transport-command.d.ts +12 -0
  79. package/dist/transport-command.d.ts.map +1 -1
  80. package/package.json +14 -12
  81. package/src/commands/add-command.ts +23 -30
  82. package/src/commands/blame-command.ts +41 -0
  83. package/src/commands/branch-command.ts +2 -53
  84. package/src/commands/checkout-command.ts +205 -127
  85. package/src/commands/clean-command.ts +187 -81
  86. package/src/commands/clone-command.ts +334 -137
  87. package/src/commands/core-config.ts +41 -0
  88. package/src/commands/fetch-command.ts +536 -211
  89. package/src/commands/gc-command.ts +1 -1
  90. package/src/commands/index.ts +1 -0
  91. package/src/commands/init-command.ts +108 -11
  92. package/src/commands/merge-command.ts +60 -27
  93. package/src/commands/pack-refs-command.ts +2 -2
  94. package/src/commands/push-command.ts +242 -308
  95. package/src/commands/reflog-command.ts +2 -2
  96. package/src/commands/remote-command.ts +429 -19
  97. package/src/commands/reset-command.ts +18 -5
  98. package/src/commands/stash-apply-command.ts +632 -97
  99. package/src/commands/stash-create-command.ts +142 -62
  100. package/src/commands/stash-drop-command.ts +53 -5
  101. package/src/commands/stash-list-command.ts +10 -4
  102. package/src/commands/stash-working-tree.ts +243 -0
  103. package/src/commands/status-command.ts +197 -91
  104. package/src/commands/tag-command.ts +2 -53
  105. package/src/commands/tree-restore.ts +143 -18
  106. package/src/commands/worktree-overwrite-checks.ts +215 -0
  107. package/src/core-commands/add.command.impl.ts +15 -20
  108. package/src/core-commands/index.ts +1 -8
  109. package/src/core-commands/store-worktree-file.ts +5 -0
  110. package/src/errors/checkout-errors.ts +20 -0
  111. package/src/git-command.ts +42 -1
  112. package/src/git.ts +30 -2
  113. package/src/remote-config/index.ts +1 -0
  114. package/src/remote-config/remote-config-store.ts +95 -8
  115. package/src/remote-config/remote-refs.ts +103 -0
  116. package/src/results/clone-result.ts +6 -0
  117. package/src/results/diff-formatter.ts +31 -38
  118. package/src/results/fetch-result.ts +16 -1
  119. package/src/results/push-result.ts +9 -2
  120. package/src/results/status-result.ts +212 -29
  121. package/src/transport-command.ts +18 -0
  122. package/dist/core-commands/checkout.command.d.ts +0 -103
  123. package/dist/core-commands/checkout.command.d.ts.map +0 -1
  124. package/dist/core-commands/checkout.command.impl.d.ts +0 -114
  125. package/dist/core-commands/checkout.command.impl.d.ts.map +0 -1
  126. package/dist/pack-import/index.d.ts +0 -8
  127. package/dist/pack-import/index.d.ts.map +0 -1
  128. package/dist/pack-import/pack-import.d.ts +0 -11
  129. package/dist/pack-import/pack-import.d.ts.map +0 -1
  130. package/src/core-commands/checkout.command.impl.ts +0 -586
  131. package/src/core-commands/checkout.command.ts +0 -125
  132. package/src/pack-import/index.ts +0 -8
  133. package/src/pack-import/pack-import.ts +0 -24
@@ -123,7 +123,7 @@ export class GarbageCollectCommand extends GitCommand<GCCommandResult> {
123
123
 
124
124
  // Run garbage collection if GCController is available
125
125
  // Note: This requires access to the Repository's gc property
126
- // For now, we'll check if the RefStore supports the gc operations
126
+ // For now, we'll check if the ref store supports the gc operations
127
127
 
128
128
  // Check if refs support packRefs
129
129
  if (this._packRefs && this.refsStore.packRefs) {
@@ -27,5 +27,6 @@ export * from "./stash-apply-command.js";
27
27
  export * from "./stash-create-command.js";
28
28
  export * from "./stash-drop-command.js";
29
29
  export * from "./stash-list-command.js";
30
+ export * from "./stash-working-tree.js";
30
31
  export * from "./status-command.js";
31
32
  export * from "./tag-command.js";
@@ -1,5 +1,17 @@
1
- import { createInMemoryFilesApi, createMemoryHistory, type FilesApi } from "@statewalker/vcs-core";
2
- import { createFileWorktree } from "@statewalker/vcs-store-files";
1
+ import {
2
+ createInMemoryFilesApi,
3
+ createMemoryHistory,
4
+ type FilesApi,
5
+ type History,
6
+ type PosixFilesApi,
7
+ } from "@statewalker/vcs-core";
8
+ import {
9
+ createFileWorktree,
10
+ createGitFilesHistoryFromFiles,
11
+ FileStagingStore,
12
+ probeCaseFolding,
13
+ resolveLinkedGitDir,
14
+ } from "@statewalker/vcs-store-files";
3
15
  import { MemoryStagingStore } from "@statewalker/vcs-store-mem";
4
16
  import {
5
17
  createMemoryCheckout,
@@ -219,15 +231,68 @@ export class InitCommand {
219
231
  // Determine git directory
220
232
  const gitDir = this.resolveGitDir();
221
233
 
222
- // Create the history (repository)
223
- const history = createMemoryHistory();
224
- await history.initialize();
225
-
226
- // Set initial branch by creating symbolic ref for HEAD
227
- await history.refs.setSymbolic("HEAD", `refs/heads/${this.initialBranch}`);
228
-
229
- // Create staging store (use provided or default to MemoryStagingStore)
230
- const staging = this.stagingStore ?? new MemoryStagingStore();
234
+ // Create the history (repository). With a FilesApi it lives in `gitDir` on those files,
235
+ // in git's layout, so it survives a reload and native git can read it; like `git init`
236
+ // on an existing repository, a second init keeps its HEAD, config and objects. Without
237
+ // one it stays in memory.
238
+ let history: History;
239
+ let staging: Staging;
240
+ if (this.files) {
241
+ const dir = gitDir.replace(/\/+$/, "") || ".";
242
+ // a `.git` the user linked elsewhere is followed (resolved through the FilesApi)
243
+ const repoFiles = await resolveLinkedGitDir(files, dir);
244
+ const isFile = async (path: string) => (await repoFiles.stats(path))?.kind === "file";
245
+ const hasHead = await isFile(`${dir}/HEAD`);
246
+ // A repository with a config but no HEAD (damaged or half-made) is opened, not
247
+ // re-created: creating rewrites the config, and its remotes and settings with it.
248
+ const fresh = !hasHead && !(await isFile(`${dir}/config`));
249
+ const open = (create: boolean) =>
250
+ createGitFilesHistoryFromFiles({
251
+ files: repoFiles,
252
+ gitDir,
253
+ create,
254
+ defaultBranch: this.initialBranch,
255
+ });
256
+ if (fresh && this.bare && dir !== ".") {
257
+ // The directory layout writes `core.bare = true` only for a gitDir of "."; set it
258
+ // before opening, so what reads the config (reflog defaults) sees a bare repository.
259
+ await open(true);
260
+ await markBare(repoFiles, `${dir}/config`);
261
+ history = await open(false);
262
+ } else {
263
+ history = await open(fresh);
264
+ }
265
+ await history.initialize();
266
+ if (fresh) {
267
+ // git init's probe: a filesystem that folds case or Unicode composition
268
+ const folding = await probeCaseFolding(repoFiles as PosixFilesApi, dir);
269
+ if (folding.ignoreCase)
270
+ await setCoreValue(repoFiles, `${dir}/config`, "ignorecase", "true");
271
+ if (folding.precomposeUnicode) {
272
+ await setCoreValue(repoFiles, `${dir}/config`, "precomposeunicode", "true");
273
+ }
274
+ }
275
+ if (!hasHead && !fresh) {
276
+ await history.refs.setSymbolic("HEAD", `refs/heads/${this.initialBranch}`);
277
+ }
278
+ // The index lives in the repository too: a reopened repository keeps what was staged
279
+ // (its last commit's files), so the next commit does not drop them.
280
+ if (this.stagingStore) {
281
+ staging = this.stagingStore;
282
+ } else if (this.bare) {
283
+ staging = new MemoryStagingStore();
284
+ } else {
285
+ const fileStaging = new FileStagingStore(repoFiles, `${dir}/index`);
286
+ await fileStaging.read();
287
+ staging = fileStaging;
288
+ }
289
+ } else {
290
+ history = createMemoryHistory();
291
+ await history.initialize();
292
+ // Set initial branch by creating symbolic ref for HEAD
293
+ await history.refs.setSymbolic("HEAD", `refs/heads/${this.initialBranch}`);
294
+ staging = this.stagingStore ?? new MemoryStagingStore();
295
+ }
231
296
 
232
297
  // Create worktree if enabled and not bare
233
298
  let worktree: Worktree | undefined;
@@ -363,3 +428,35 @@ export class InitCommand {
363
428
  this.callable = value;
364
429
  }
365
430
  }
431
+
432
+ /** Sets `core.bare = true` in a config file written by the git directory layout. */
433
+ async function markBare(files: FilesApi, configPath: string): Promise<void> {
434
+ await setCoreValue(files, configPath, "bare", "true");
435
+ }
436
+
437
+ /** Sets `core.<key>` in the fresh config `init` wrote (one `[core]` section). */
438
+ async function setCoreValue(
439
+ files: FilesApi,
440
+ configPath: string,
441
+ key: string,
442
+ value: string,
443
+ ): Promise<void> {
444
+ const chunks: Uint8Array[] = [];
445
+ for await (const chunk of files.read(configPath)) chunks.push(chunk);
446
+ const text = new TextDecoder().decode(concatChunks(chunks));
447
+ const existing = new RegExp(`^(\\s*${key}\\s*=\\s*)\\S+`, "mi");
448
+ const updated = existing.test(text)
449
+ ? text.replace(existing, `$1${value}`)
450
+ : text.replace(/^\[core\]\s*$/m, `[core]\n\t${key} = ${value}`);
451
+ await files.write(configPath, [new TextEncoder().encode(updated)]);
452
+ }
453
+
454
+ function concatChunks(chunks: Uint8Array[]): Uint8Array {
455
+ const out = new Uint8Array(chunks.reduce((n, c) => n + c.length, 0));
456
+ let offset = 0;
457
+ for (const c of chunks) {
458
+ out.set(c, offset);
459
+ offset += c.length;
460
+ }
461
+ return out;
462
+ }
@@ -1,5 +1,10 @@
1
1
  import type { Commit, ObjectId, TreeEntry } from "@statewalker/vcs-core";
2
- import { FileMode, isSymbolicRef } from "@statewalker/vcs-core";
2
+ import {
3
+ DEFAULT_BIG_FILE_THRESHOLD,
4
+ FileMode,
5
+ isSymbolicRef,
6
+ probeBlob,
7
+ } from "@statewalker/vcs-core";
3
8
  import {
4
9
  merge3Way,
5
10
  MergeContentStrategy as UtilsMergeContentStrategy,
@@ -68,6 +73,7 @@ export class MergeCommand extends GitCommand<MergeResult> {
68
73
  private squash = false;
69
74
  private commit = true;
70
75
  private message?: string;
76
+ private bigFileThreshold = DEFAULT_BIG_FILE_THRESHOLD;
71
77
 
72
78
  /**
73
79
  * Add a commit/branch to merge.
@@ -129,6 +135,22 @@ export class MergeCommand extends GitCommand<MergeResult> {
129
135
  return this;
130
136
  }
131
137
 
138
+ /**
139
+ * Set the size above which files are binary to the content merge (git's
140
+ * `core.bigFileThreshold`, default 50 MiB).
141
+ *
142
+ * A binary file (over the threshold, or a NUL byte in its first 8000 bytes) is
143
+ * never content-merged and never loaded: as git does, OURS and THEIRS take that
144
+ * side's file whole, and UNION reports a conflict.
145
+ *
146
+ * @param threshold Size in bytes
147
+ */
148
+ setBigFileThreshold(threshold: number): this {
149
+ this.checkCallable();
150
+ this.bigFileThreshold = threshold;
151
+ return this;
152
+ }
153
+
132
154
  /**
133
155
  * Set squash mode.
134
156
  *
@@ -518,10 +540,22 @@ export class MergeCommand extends GitCommand<MergeResult> {
518
540
  }
519
541
 
520
542
  try {
521
- // Load content from all three versions
522
- const baseContent = base ? await this.collectBlobContent(base.id) : new Uint8Array(0);
523
- const oursContent = await this.collectBlobContent(ours.id);
524
- const theirsContent = await this.collectBlobContent(theirs.id);
543
+ // Binary or big files are never content-merged (git's ll_binary_merge)
544
+ const contents = await this.loadTexts([base?.id, ours.id, theirs.id]);
545
+ if (!contents) {
546
+ switch (this.contentStrategy) {
547
+ case ContentMergeStrategy.OURS:
548
+ return ours;
549
+ case ContentMergeStrategy.THEIRS:
550
+ return { ...theirs, name: ours.name };
551
+ default:
552
+ return undefined;
553
+ }
554
+ }
555
+
556
+ // Load content from all three versions (text under the threshold)
557
+ // (read once, by the probe)
558
+ const [baseContent, oursContent, theirsContent] = contents;
525
559
 
526
560
  // Convert ContentMergeStrategy to utils MergeContentStrategy
527
561
  const strategy = this.convertContentStrategy(this.contentStrategy);
@@ -531,7 +565,10 @@ export class MergeCommand extends GitCommand<MergeResult> {
531
565
 
532
566
  // If using OURS/THEIRS/UNION, conflicts are auto-resolved
533
567
  // Store the merged content
534
- const mergedBlobId = await this.blobs.store([mergeResult.content]);
568
+ const mergedBlobId = await this.blobs.store(
569
+ [mergeResult.content],
570
+ mergeResult.content.length,
571
+ );
535
572
 
536
573
  return {
537
574
  name: ours.name,
@@ -545,30 +582,26 @@ export class MergeCommand extends GitCommand<MergeResult> {
545
582
  }
546
583
 
547
584
  /**
548
- * Collect all bytes from a blob into a single Uint8Array.
585
+ * The blobs' contents, each read once; nothing when one is binary or over the
586
+ * threshold (then the rest are not read). A missing id is empty content.
549
587
  */
550
- private async collectBlobContent(blobId: ObjectId): Promise<Uint8Array> {
551
- const chunks: Uint8Array[] = [];
552
- const blobContent = await this.blobs.load(blobId);
553
- if (blobContent) {
554
- for await (const chunk of blobContent) {
555
- chunks.push(chunk);
588
+ private async loadTexts(
589
+ ids: [ObjectId | undefined, ObjectId, ObjectId],
590
+ ): Promise<[Uint8Array, Uint8Array, Uint8Array] | undefined> {
591
+ const out: Uint8Array[] = [];
592
+ for (const id of ids) {
593
+ if (!id) {
594
+ out.push(new Uint8Array(0));
595
+ continue;
556
596
  }
597
+ const probe = await probeBlob(this.blobs, id, {
598
+ bigFileThreshold: this.bigFileThreshold,
599
+ loadText: true,
600
+ });
601
+ if (probe.binary || !probe.content) return undefined;
602
+ out.push(probe.content);
557
603
  }
558
- if (chunks.length === 0) {
559
- return new Uint8Array(0);
560
- }
561
- if (chunks.length === 1) {
562
- return chunks[0];
563
- }
564
- const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
565
- const result = new Uint8Array(totalLength);
566
- let offset = 0;
567
- for (const chunk of chunks) {
568
- result.set(chunk, offset);
569
- offset += chunk.length;
570
- }
571
- return result;
604
+ return out as [Uint8Array, Uint8Array, Uint8Array];
572
605
  }
573
606
 
574
607
  /**
@@ -69,9 +69,9 @@ export class PackRefsCommand extends GitCommand<PackRefsResult> {
69
69
  this.checkCallable();
70
70
  this.setCallable(false);
71
71
 
72
- // Check if RefStore supports packRefs
72
+ // Check if the ref store supports packRefs
73
73
  if (!this.refsStore.packRefs) {
74
- throw new Error("RefStore does not support packRefs");
74
+ throw new Error("This ref store does not support packRefs");
75
75
  }
76
76
 
77
77
  let refsPacked = 0;