@statewalker/vcs-commands 0.1.1 → 0.3.1

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 (85) hide show
  1. package/dist/commands/commit-command.d.ts.map +1 -1
  2. package/dist/commands/init-command.d.ts.map +1 -1
  3. package/dist/commands/push-command.d.ts.map +1 -1
  4. package/dist/core-commands/checkout.command.impl.d.ts +1 -1
  5. package/dist/core-commands/checkout.command.impl.d.ts.map +1 -1
  6. package/dist/git-command.d.ts.map +1 -1
  7. package/package.json +19 -14
  8. package/src/commands/add-command.ts +494 -0
  9. package/src/commands/blame-command.ts +1241 -0
  10. package/src/commands/branch-command.ts +487 -0
  11. package/src/commands/checkout-command.ts +681 -0
  12. package/src/commands/cherry-pick-command.ts +544 -0
  13. package/src/commands/clean-command.ts +218 -0
  14. package/src/commands/clone-command.ts +532 -0
  15. package/src/commands/commit-command.ts +529 -0
  16. package/src/commands/describe-command.ts +454 -0
  17. package/src/commands/diff-command.ts +257 -0
  18. package/src/commands/fetch-command.ts +615 -0
  19. package/src/commands/gc-command.ts +155 -0
  20. package/src/commands/index.ts +31 -0
  21. package/src/commands/init-command.ts +365 -0
  22. package/src/commands/log-command.ts +549 -0
  23. package/src/commands/ls-remote-command.ts +237 -0
  24. package/src/commands/merge-command.ts +750 -0
  25. package/src/commands/pack-refs-command.ts +101 -0
  26. package/src/commands/pull-command.ts +258 -0
  27. package/src/commands/push-command.ts +682 -0
  28. package/src/commands/rebase-command.ts +665 -0
  29. package/src/commands/reflog-command.ts +68 -0
  30. package/src/commands/remote-command.ts +385 -0
  31. package/src/commands/reset-command.ts +292 -0
  32. package/src/commands/revert-command.ts +552 -0
  33. package/src/commands/rm-command.ts +178 -0
  34. package/src/commands/stash-apply-command.ts +402 -0
  35. package/src/commands/stash-create-command.ts +339 -0
  36. package/src/commands/stash-drop-command.ts +115 -0
  37. package/src/commands/stash-list-command.ts +116 -0
  38. package/src/commands/status-command.ts +173 -0
  39. package/src/commands/tag-command.ts +378 -0
  40. package/src/commands/tree-restore.ts +126 -0
  41. package/src/core-commands/add.command.impl.ts +274 -0
  42. package/src/core-commands/add.command.ts +81 -0
  43. package/src/core-commands/checkout.command.impl.ts +586 -0
  44. package/src/core-commands/checkout.command.ts +125 -0
  45. package/src/core-commands/index.ts +13 -0
  46. package/src/errors/add-errors.ts +20 -0
  47. package/src/errors/checkout-errors.ts +42 -0
  48. package/src/errors/command-errors.ts +68 -0
  49. package/src/errors/commit-errors.ts +40 -0
  50. package/src/errors/git-api-error.ts +11 -0
  51. package/src/errors/index.ts +12 -0
  52. package/src/errors/merge-errors.ts +94 -0
  53. package/src/errors/rebase-errors.ts +34 -0
  54. package/src/errors/ref-errors.ts +100 -0
  55. package/src/errors/remote-errors.ts +27 -0
  56. package/src/errors/stash-errors.ts +42 -0
  57. package/src/errors/tag-errors.ts +16 -0
  58. package/src/errors/transport-errors.ts +68 -0
  59. package/src/git-command.ts +421 -0
  60. package/src/git.ts +910 -0
  61. package/src/index.ts +18 -0
  62. package/src/pack-import/index.ts +8 -0
  63. package/src/pack-import/pack-import.ts +24 -0
  64. package/src/remote-config/index.ts +12 -0
  65. package/src/remote-config/remote-config-store.ts +285 -0
  66. package/src/rename/index.ts +5 -0
  67. package/src/rename/similarity-index.ts +185 -0
  68. package/src/results/cherry-pick-result.ts +35 -0
  69. package/src/results/clone-result.ts +25 -0
  70. package/src/results/diff-entry.ts +162 -0
  71. package/src/results/diff-formatter.ts +416 -0
  72. package/src/results/fetch-result.ts +91 -0
  73. package/src/results/index.ts +13 -0
  74. package/src/results/init-result.ts +29 -0
  75. package/src/results/merge-result.ts +135 -0
  76. package/src/results/pull-result.ts +27 -0
  77. package/src/results/push-result.ts +86 -0
  78. package/src/results/rebase-result.ts +140 -0
  79. package/src/results/revert-result.ts +35 -0
  80. package/src/results/stash-result.ts +60 -0
  81. package/src/results/status-result.ts +65 -0
  82. package/src/transport-command.ts +125 -0
  83. package/src/tree-merge/index.ts +8 -0
  84. package/src/tree-merge/tree-merge.ts +249 -0
  85. package/src/types.ts +70 -0
@@ -0,0 +1,249 @@
1
+ import type { ObjectId } from "@statewalker/vcs-core";
2
+
3
+ import type { CommandTrees } from "../git-command.js";
4
+
5
+ /**
6
+ * Directory mode constant.
7
+ */
8
+ const TREE_MODE = 0o040000;
9
+
10
+ /**
11
+ * The object and file mode a tree records for one path.
12
+ */
13
+ interface PathEntry {
14
+ id: ObjectId;
15
+ mode: number;
16
+ }
17
+
18
+ /**
19
+ * The outcome of a three-way tree merge.
20
+ *
21
+ * `conflicts` is absent on a clean merge. When it is present `tree` is the
22
+ * *theirs* tree, not a merged one: a conflicting merge never builds a tree, so
23
+ * callers must halt rather than record `tree` as a result.
24
+ */
25
+ export interface TreeMergeResult {
26
+ tree: ObjectId;
27
+ conflicts?: string[];
28
+ }
29
+
30
+ /**
31
+ * Recursively walk a tree, yielding all entries with full paths.
32
+ */
33
+ async function* walkTreeRecursive(
34
+ trees: CommandTrees,
35
+ treeId: ObjectId,
36
+ prefix = "",
37
+ ): AsyncGenerator<{ path: string; id: ObjectId; mode: number }> {
38
+ for await (const entry of trees.loadTree(treeId)) {
39
+ const fullPath = prefix ? `${prefix}/${entry.name}` : entry.name;
40
+
41
+ if ((entry.mode & TREE_MODE) === TREE_MODE) {
42
+ // It's a directory - recurse into it
43
+ yield* walkTreeRecursive(trees, entry.id, fullPath);
44
+ } else {
45
+ // It's a file
46
+ yield { path: fullPath, id: entry.id, mode: entry.mode };
47
+ }
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Build a tree from flat path entries (handles nested directories).
53
+ */
54
+ async function buildTreeFromPaths(
55
+ trees: CommandTrees,
56
+ entries: Map<string, PathEntry>,
57
+ ): Promise<ObjectId> {
58
+ // Group entries by top-level directory
59
+ const rootEntries: Map<string, PathEntry> = new Map();
60
+ const subDirs: Map<string, Map<string, PathEntry>> = new Map();
61
+
62
+ for (const [path, entry] of entries) {
63
+ const slashIndex = path.indexOf("/");
64
+ if (slashIndex === -1) {
65
+ // Top-level file
66
+ rootEntries.set(path, entry);
67
+ } else {
68
+ // Nested path - group by first component
69
+ const dirName = path.substring(0, slashIndex);
70
+ const restPath = path.substring(slashIndex + 1);
71
+
72
+ if (!subDirs.has(dirName)) {
73
+ subDirs.set(dirName, new Map());
74
+ }
75
+ subDirs.get(dirName)?.set(restPath, entry);
76
+ }
77
+ }
78
+
79
+ // Recursively create subtrees
80
+ for (const [dirName, subEntries] of subDirs) {
81
+ const subTreeId = await buildTreeFromPaths(trees, subEntries);
82
+ rootEntries.set(dirName, { id: subTreeId, mode: TREE_MODE });
83
+ }
84
+
85
+ // Create the tree
86
+ const treeEntries = Array.from(rootEntries.entries()).map(([name, { id, mode }]) => ({
87
+ name,
88
+ id,
89
+ mode,
90
+ }));
91
+
92
+ return trees.storeTree(treeEntries);
93
+ }
94
+
95
+ /**
96
+ * Whether two optional tree entries denote the same state for a path.
97
+ *
98
+ * Both absent means the path is absent on both sides. When both are present,
99
+ * content *and* mode must match: comparing object ids alone treats a
100
+ * mode-only change (setting the executable bit on unchanged content, say) as
101
+ * no change at all, which silently discards it.
102
+ */
103
+ function entriesEqual(a: PathEntry | undefined, b: PathEntry | undefined): boolean {
104
+ if (!a || !b) {
105
+ return !a && !b;
106
+ }
107
+ return a.id === b.id && a.mode === b.mode;
108
+ }
109
+
110
+ /**
111
+ * The paths in a merged entry set that a directory would overwrite.
112
+ *
113
+ * Every entry is a file - the recursive walk never yields a tree - so a path
114
+ * is claimed as a *directory* only implicitly, by another entry lying below
115
+ * it. `a` and `a/b` cannot both exist in a tree: building one from that set
116
+ * writes `a` as a file and then overwrites it with the `a/` subtree, losing
117
+ * the file. The separator is what makes a prefix a directory boundary - `ab`
118
+ * is not inside `a/` and does not collide with it.
119
+ *
120
+ * @param paths The paths of the merged entries
121
+ * @returns The colliding file paths, sorted
122
+ */
123
+ function findDirectoryCollisions(paths: Iterable<string>): string[] {
124
+ const allPaths = [...paths];
125
+ const directories = new Set<string>();
126
+ for (const path of allPaths) {
127
+ for (let at = path.indexOf("/"); at !== -1; at = path.indexOf("/", at + 1)) {
128
+ directories.add(path.substring(0, at));
129
+ }
130
+ }
131
+ return allPaths.filter((path) => directories.has(path)).sort();
132
+ }
133
+
134
+ /**
135
+ * Path-level three-way tree merge with recursive support.
136
+ *
137
+ * Merges per path against the `base` tree: a path only one side changed takes
138
+ * that side, a path both sides changed identically merges cleanly, and a path
139
+ * the two sides changed differently is reported as a conflict. So is a path
140
+ * one side made a file and the other made a directory, which no tree can
141
+ * represent.
142
+ *
143
+ * The merge is path-level only - it never merges the *contents* of a file.
144
+ * Two sides editing different regions of the same file conflict here where
145
+ * git would resolve them.
146
+ *
147
+ * Shared by `rebase` (base = the replayed commit's parent, ours = that commit,
148
+ * theirs = the commit being replayed onto) and `stash apply` (base = the
149
+ * commit the stash was taken from, ours = the stash, theirs = HEAD). It is
150
+ * *not* what `merge` uses: that command keeps its own entry-level merge
151
+ * because it also resolves conflicts at the content level and writes conflict
152
+ * stages into the index.
153
+ *
154
+ * @param trees The tree store to read from and write the merged tree to
155
+ * @param base The common ancestor tree
156
+ * @param ours The tree whose changes are being applied
157
+ * @param theirs The tree being applied onto
158
+ * @returns The merged tree, or `theirs` plus the conflicting paths
159
+ */
160
+ export async function mergeTreesThreeWay(
161
+ trees: CommandTrees,
162
+ base: ObjectId,
163
+ ours: ObjectId,
164
+ theirs: ObjectId,
165
+ ): Promise<TreeMergeResult> {
166
+ // Simplified merge: if trees are identical, no conflict
167
+ if (ours === theirs) {
168
+ return { tree: theirs };
169
+ }
170
+ if (base === ours) {
171
+ return { tree: theirs };
172
+ }
173
+ if (base === theirs) {
174
+ return { tree: ours };
175
+ }
176
+
177
+ // Collect all entries from all trees recursively
178
+ const conflicts: string[] = [];
179
+ const mergedEntries: Map<string, PathEntry> = new Map();
180
+
181
+ const baseEntries = new Map<string, PathEntry>();
182
+ const oursEntries = new Map<string, PathEntry>();
183
+ const theirsEntries = new Map<string, PathEntry>();
184
+
185
+ // Recursively walk all trees
186
+ for await (const entry of walkTreeRecursive(trees, base)) {
187
+ baseEntries.set(entry.path, { id: entry.id, mode: entry.mode });
188
+ }
189
+ for await (const entry of walkTreeRecursive(trees, ours)) {
190
+ oursEntries.set(entry.path, { id: entry.id, mode: entry.mode });
191
+ }
192
+ for await (const entry of walkTreeRecursive(trees, theirs)) {
193
+ theirsEntries.set(entry.path, { id: entry.id, mode: entry.mode });
194
+ }
195
+
196
+ // Collect all paths
197
+ const allPaths = new Set([...baseEntries.keys(), ...oursEntries.keys(), ...theirsEntries.keys()]);
198
+
199
+ for (const path of allPaths) {
200
+ const baseEntry = baseEntries.get(path);
201
+ const oursEntry = oursEntries.get(path);
202
+ const theirsEntry = theirsEntries.get(path);
203
+
204
+ // Only one side changed the path relative to base - take that side. If
205
+ // that side deleted the path, it is simply not carried into the merge.
206
+ if (entriesEqual(oursEntry, baseEntry)) {
207
+ if (theirsEntry) {
208
+ mergedEntries.set(path, theirsEntry);
209
+ }
210
+ continue;
211
+ }
212
+ if (entriesEqual(theirsEntry, baseEntry)) {
213
+ if (oursEntry) {
214
+ mergedEntries.set(path, oursEntry);
215
+ }
216
+ continue;
217
+ }
218
+
219
+ // Both sides changed the path. Identical changes merge cleanly - and
220
+ // that includes both sides deleting it, which leaves it deleted.
221
+ if (entriesEqual(oursEntry, theirsEntry)) {
222
+ if (oursEntry) {
223
+ mergedEntries.set(path, oursEntry);
224
+ }
225
+ continue;
226
+ }
227
+
228
+ // Conflict: both sides changed the path differently. Conflicts are only
229
+ // collected here - a conflicting merge never builds a tree from
230
+ // `mergedEntries` (see the early return below) and the caller halts, so
231
+ // there is no merged content to record for this path.
232
+ conflicts.push(path);
233
+ }
234
+
235
+ // One side made a path a file while the other made it a directory. Both are
236
+ // one-sided carries above - a path both sides changed cannot collide, since
237
+ // carrying it needs the two sides to agree and no single tree holds `a` as a
238
+ // file and `a/b` at once - so this is only detectable once the whole merged
239
+ // set is known, not inside the loop.
240
+ conflicts.push(...findDirectoryCollisions(mergedEntries.keys()));
241
+
242
+ if (conflicts.length > 0) {
243
+ return { tree: theirs, conflicts };
244
+ }
245
+
246
+ // Build tree from merged entries (handles nested paths)
247
+ const newTree = await buildTreeFromPaths(trees, mergedEntries);
248
+ return { tree: newTree };
249
+ }
package/src/types.ts ADDED
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Reset mode for ResetCommand.
3
+ *
4
+ * Based on JGit's ResetCommand.ResetType enum.
5
+ */
6
+ export enum ResetMode {
7
+ /** Move HEAD only */
8
+ SOFT = "soft",
9
+ /** Move HEAD and reset staging (default) */
10
+ MIXED = "mixed",
11
+ /** Move HEAD, reset staging, and reset working tree */
12
+ HARD = "hard",
13
+ /** Keep local changes if they're not in the way */
14
+ KEEP = "keep",
15
+ /** Like --hard but preserves untracked files */
16
+ MERGE = "merge",
17
+ }
18
+
19
+ /**
20
+ * Branch listing mode for ListBranchCommand.
21
+ *
22
+ * Based on JGit's ListBranchCommand.ListMode enum.
23
+ */
24
+ export enum ListBranchMode {
25
+ /** List local branches only (refs/heads/) */
26
+ LOCAL = "local",
27
+ /** List remote tracking branches only (refs/remotes/) */
28
+ REMOTE = "remote",
29
+ /** List both local and remote branches */
30
+ ALL = "all",
31
+ }
32
+
33
+ // ============ Core Type Re-exports ============
34
+
35
+ /**
36
+ * Re-export WorkingCopy and related types from core for convenience.
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * import { WorkingCopy, History, Checkout, Worktree, Blobs, Trees, Commits } from "@statewalker/vcs-commands";
41
+ *
42
+ * // Or import directly from core:
43
+ * import { WorkingCopy } from "@statewalker/vcs-working-tree";
44
+ * ```
45
+ */
46
+ export type {
47
+ /** Blobs interface - file content storage */
48
+ Blobs,
49
+ /** Commits interface - commit object storage */
50
+ Commits,
51
+ /** History interface - immutable repository objects */
52
+ History,
53
+ /** Refs interface - branch and tag references */
54
+ Refs,
55
+ /** Tags interface - annotated tag storage */
56
+ Tags,
57
+ /** Trees interface - directory structure storage */
58
+ Trees,
59
+ } from "@statewalker/vcs-core";
60
+ // Working-tree symbols were relocated out of vcs-core into vcs-working-tree.
61
+ export type {
62
+ /** Checkout interface - mutable local state */
63
+ Checkout,
64
+ /** Staging interface - index/staging area */
65
+ Staging,
66
+ /** WorkingCopy interface - unified repository access */
67
+ WorkingCopy,
68
+ /** Worktree interface - filesystem access */
69
+ Worktree,
70
+ } from "@statewalker/vcs-working-tree";