@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,218 @@
1
+ import { GitCommand } from "../git-command.js";
2
+
3
+ /**
4
+ * Result of a clean operation
5
+ */
6
+ export interface CleanResult {
7
+ /** Files that were (or would be) removed */
8
+ cleaned: Set<string>;
9
+ /** Whether this was a dry run (no files actually removed) */
10
+ dryRun: boolean;
11
+ }
12
+
13
+ /**
14
+ * Remove untracked files from working tree.
15
+ *
16
+ * Equivalent to `git clean`.
17
+ *
18
+ * Based on JGit's CleanCommand.
19
+ *
20
+ * This command DELETES files from the working tree when dry-run is disabled.
21
+ * Dry-run is the default for exactly that reason: a caller must opt in to
22
+ * destruction with `setDryRun(false)`. The returned `dryRun` flag always
23
+ * reports what the caller asked for, so a `false` there means files were
24
+ * really removed.
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * // Preview what would be cleaned (dry run - the default)
29
+ * const result = await git.clean()
30
+ * .setDryRun(true)
31
+ * .call();
32
+ * for (const file of result.cleaned) {
33
+ * console.log(`Would remove: ${file}`);
34
+ * }
35
+ *
36
+ * // Actually delete the untracked files
37
+ * const result = await git.clean()
38
+ * .setDryRun(false)
39
+ * .call();
40
+ *
41
+ * // Include directories
42
+ * const result = await git.clean()
43
+ * .setCleanDirectories(true)
44
+ * .call();
45
+ * ```
46
+ */
47
+ export class CleanCommand extends GitCommand<CleanResult> {
48
+ private paths = new Set<string>();
49
+ private dryRun = true; // Default to dry-run for safety
50
+ private directories = false;
51
+ private ignore = true; // Respect .gitignore
52
+
53
+ /**
54
+ * Limit clean to specific paths.
55
+ *
56
+ * @param paths Set of paths to clean
57
+ */
58
+ setPaths(paths: Set<string>): this {
59
+ this.checkCallable();
60
+ this.paths = paths;
61
+ return this;
62
+ }
63
+
64
+ /**
65
+ * Set dry run mode.
66
+ *
67
+ * When true (default), no files are actually deleted.
68
+ * The result will still contain the list of files that would be cleaned.
69
+ *
70
+ * @param dryRun Whether to run in dry-run mode
71
+ */
72
+ setDryRun(dryRun: boolean): this {
73
+ this.checkCallable();
74
+ this.dryRun = dryRun;
75
+ return this;
76
+ }
77
+
78
+ /**
79
+ * Set whether to also clean directories.
80
+ *
81
+ * @param directories Whether to clean directories
82
+ */
83
+ setCleanDirectories(directories: boolean): this {
84
+ this.checkCallable();
85
+ this.directories = directories;
86
+ return this;
87
+ }
88
+
89
+ /**
90
+ * Set whether to respect .gitignore patterns.
91
+ *
92
+ * When true (default), ignored files are not cleaned.
93
+ * When false, ignored files are also cleaned.
94
+ *
95
+ * @param ignore Whether to respect .gitignore
96
+ */
97
+ setIgnore(ignore: boolean): this {
98
+ this.checkCallable();
99
+ this.ignore = ignore;
100
+ return this;
101
+ }
102
+
103
+ /**
104
+ * Execute the clean command.
105
+ *
106
+ * @returns CleanResult with set of cleaned files
107
+ */
108
+ async call(): Promise<CleanResult> {
109
+ this.checkCallable();
110
+ this.setCallable(false);
111
+
112
+ const cleaned = new Set<string>();
113
+ const toRemove: Array<{ path: string; isDirectory: boolean }> = [];
114
+
115
+ // Check if worktree is available
116
+ const worktree = this.worktreeAccess;
117
+ if (!worktree) {
118
+ throw new Error("CleanCommand requires a WorkingCopy with worktree");
119
+ }
120
+
121
+ // Get entries from staging to compare
122
+ const stagedPaths = new Set<string>();
123
+ for await (const entry of this.staging.entries()) {
124
+ stagedPaths.add(entry.path);
125
+ }
126
+
127
+ // Get entries from HEAD commit (if any) to compare
128
+ const headPaths = new Set<string>();
129
+ try {
130
+ const head = await this.refsStore.resolve("HEAD");
131
+ if (head?.objectId) {
132
+ const commit = await this.commits.load(head.objectId);
133
+ if (commit) {
134
+ await this.collectTreePaths(commit.tree, "", headPaths);
135
+ }
136
+ }
137
+ } catch {
138
+ // No HEAD yet, all files are "new"
139
+ }
140
+
141
+ // Walk worktree and find untracked files
142
+ const options = {
143
+ includeIgnored: !this.ignore,
144
+ includeDirectories: this.directories,
145
+ };
146
+
147
+ for await (const entry of worktree.walk(options)) {
148
+ const entryTyped = entry as { path: string; isDirectory: boolean; isIgnored?: boolean };
149
+
150
+ // Skip if in specific paths and not matching
151
+ if (this.paths.size > 0) {
152
+ let matches = false;
153
+ for (const p of this.paths) {
154
+ if (entryTyped.path === p || entryTyped.path.startsWith(`${p}/`)) {
155
+ matches = true;
156
+ break;
157
+ }
158
+ }
159
+ if (!matches) continue;
160
+ }
161
+
162
+ // Skip if ignored and respecting .gitignore
163
+ if (this.ignore && entryTyped.isIgnored) {
164
+ continue;
165
+ }
166
+
167
+ // Skip if tracked (in staging or HEAD)
168
+ if (stagedPaths.has(entryTyped.path) || headPaths.has(entryTyped.path)) {
169
+ continue;
170
+ }
171
+
172
+ // Skip directories unless specified
173
+ if (entryTyped.isDirectory && !this.directories) {
174
+ continue;
175
+ }
176
+
177
+ // This file is untracked and should be cleaned
178
+ const displayPath = entryTyped.isDirectory ? `${entryTyped.path}/` : entryTyped.path;
179
+ cleaned.add(displayPath);
180
+
181
+ // Collect rather than delete inline: removing entries while the walk is
182
+ // still iterating the same directories is not safe for all Worktree
183
+ // implementations.
184
+ if (!this.dryRun) {
185
+ toRemove.push({ path: entryTyped.path, isDirectory: entryTyped.isDirectory });
186
+ }
187
+ }
188
+
189
+ for (const { path, isDirectory } of toRemove) {
190
+ await worktree.remove(path, { recursive: isDirectory });
191
+ }
192
+
193
+ return {
194
+ cleaned,
195
+ dryRun: this.dryRun,
196
+ };
197
+ }
198
+
199
+ /**
200
+ * Collect all paths from a tree recursively.
201
+ */
202
+ private async collectTreePaths(
203
+ treeId: string,
204
+ prefix: string,
205
+ paths: Set<string>,
206
+ ): Promise<void> {
207
+ for await (const entry of this.trees.loadTree(treeId)) {
208
+ const fullPath = prefix ? `${prefix}/${entry.name}` : entry.name;
209
+
210
+ // Check if directory mode (0o040000)
211
+ if ((entry.mode & 0o170000) === 0o040000) {
212
+ await this.collectTreePaths(entry.id, fullPath, paths);
213
+ } else {
214
+ paths.add(fullPath);
215
+ }
216
+ }
217
+ }
218
+ }