@statewalker/vcs-commands 0.4.0 → 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 (99) hide show
  1. package/dist/commands/add-command.d.ts.map +1 -1
  2. package/dist/commands/checkout-command.d.ts +7 -24
  3. package/dist/commands/checkout-command.d.ts.map +1 -1
  4. package/dist/commands/clean-command.d.ts +34 -4
  5. package/dist/commands/clean-command.d.ts.map +1 -1
  6. package/dist/commands/clone-command.d.ts +62 -7
  7. package/dist/commands/clone-command.d.ts.map +1 -1
  8. package/dist/commands/core-config.d.ts +16 -0
  9. package/dist/commands/core-config.d.ts.map +1 -0
  10. package/dist/commands/fetch-command.d.ts +96 -14
  11. package/dist/commands/fetch-command.d.ts.map +1 -1
  12. package/dist/commands/index.d.ts +1 -0
  13. package/dist/commands/index.d.ts.map +1 -1
  14. package/dist/commands/index.js +2 -2
  15. package/dist/commands/init-command.d.ts.map +1 -1
  16. package/dist/commands/remote-command.d.ts +115 -6
  17. package/dist/commands/remote-command.d.ts.map +1 -1
  18. package/dist/commands/reset-command.d.ts.map +1 -1
  19. package/dist/commands/stash-apply-command.d.ts +35 -13
  20. package/dist/commands/stash-apply-command.d.ts.map +1 -1
  21. package/dist/commands/stash-create-command.d.ts +59 -25
  22. package/dist/commands/stash-create-command.d.ts.map +1 -1
  23. package/dist/commands/stash-drop-command.d.ts +8 -2
  24. package/dist/commands/stash-drop-command.d.ts.map +1 -1
  25. package/dist/commands/stash-list-command.d.ts +2 -4
  26. package/dist/commands/stash-list-command.d.ts.map +1 -1
  27. package/dist/commands/stash-working-tree.d.ts +72 -0
  28. package/dist/commands/stash-working-tree.d.ts.map +1 -0
  29. package/dist/commands/status-command.d.ts +29 -22
  30. package/dist/commands/status-command.d.ts.map +1 -1
  31. package/dist/commands/tree-restore.d.ts +52 -5
  32. package/dist/commands/tree-restore.d.ts.map +1 -1
  33. package/dist/commands/worktree-overwrite-checks.d.ts +64 -0
  34. package/dist/commands/worktree-overwrite-checks.d.ts.map +1 -0
  35. package/dist/{commands-BpeXZTC3.js → commands-CsLNyvZ1.js} +2940 -631
  36. package/dist/core-commands/add.command.impl.d.ts.map +1 -1
  37. package/dist/core-commands/index.d.ts +0 -3
  38. package/dist/core-commands/index.d.ts.map +1 -1
  39. package/dist/errors/checkout-errors.d.ts +13 -0
  40. package/dist/errors/checkout-errors.d.ts.map +1 -1
  41. package/dist/errors/index.js +18 -1
  42. package/dist/git-command.d.ts +10 -1
  43. package/dist/git-command.d.ts.map +1 -1
  44. package/dist/git.d.ts +21 -3
  45. package/dist/git.d.ts.map +1 -1
  46. package/dist/index.js +4 -4
  47. package/dist/remote-config/index.d.ts +1 -0
  48. package/dist/remote-config/index.d.ts.map +1 -1
  49. package/dist/remote-config/remote-config-store.d.ts +18 -0
  50. package/dist/remote-config/remote-config-store.d.ts.map +1 -1
  51. package/dist/remote-config/remote-refs.d.ts +37 -0
  52. package/dist/remote-config/remote-refs.d.ts.map +1 -0
  53. package/dist/results/clone-result.d.ts +6 -0
  54. package/dist/results/clone-result.d.ts.map +1 -1
  55. package/dist/results/fetch-result.d.ts +9 -1
  56. package/dist/results/fetch-result.d.ts.map +1 -1
  57. package/dist/results/index.js +2 -2
  58. package/dist/results/status-result.d.ts +91 -20
  59. package/dist/results/status-result.d.ts.map +1 -1
  60. package/dist/{status-result-BapZoQaR.js → status-result-BpyUK1Sg.js} +113 -5
  61. package/dist/transport-command.d.ts +12 -0
  62. package/dist/transport-command.d.ts.map +1 -1
  63. package/package.json +11 -10
  64. package/src/commands/add-command.ts +9 -7
  65. package/src/commands/checkout-command.ts +200 -118
  66. package/src/commands/clean-command.ts +187 -88
  67. package/src/commands/clone-command.ts +244 -40
  68. package/src/commands/core-config.ts +41 -0
  69. package/src/commands/fetch-command.ts +476 -57
  70. package/src/commands/index.ts +1 -0
  71. package/src/commands/init-command.ts +32 -7
  72. package/src/commands/remote-command.ts +429 -19
  73. package/src/commands/reset-command.ts +18 -5
  74. package/src/commands/stash-apply-command.ts +632 -97
  75. package/src/commands/stash-create-command.ts +142 -62
  76. package/src/commands/stash-drop-command.ts +53 -5
  77. package/src/commands/stash-list-command.ts +10 -4
  78. package/src/commands/stash-working-tree.ts +243 -0
  79. package/src/commands/status-command.ts +197 -91
  80. package/src/commands/tree-restore.ts +142 -13
  81. package/src/commands/worktree-overwrite-checks.ts +215 -0
  82. package/src/core-commands/add.command.impl.ts +2 -4
  83. package/src/core-commands/index.ts +0 -8
  84. package/src/errors/checkout-errors.ts +20 -0
  85. package/src/git-command.ts +42 -1
  86. package/src/git.ts +30 -2
  87. package/src/remote-config/index.ts +1 -0
  88. package/src/remote-config/remote-config-store.ts +84 -8
  89. package/src/remote-config/remote-refs.ts +103 -0
  90. package/src/results/clone-result.ts +6 -0
  91. package/src/results/fetch-result.ts +9 -1
  92. package/src/results/status-result.ts +212 -29
  93. package/src/transport-command.ts +18 -0
  94. package/dist/core-commands/checkout.command.d.ts +0 -103
  95. package/dist/core-commands/checkout.command.d.ts.map +0 -1
  96. package/dist/core-commands/checkout.command.impl.d.ts +0 -110
  97. package/dist/core-commands/checkout.command.impl.d.ts.map +0 -1
  98. package/src/core-commands/checkout.command.impl.ts +0 -564
  99. package/src/core-commands/checkout.command.ts +0 -125
@@ -1,5 +1,7 @@
1
- import { isValidWorktreePath } from "@statewalker/vcs-core";
1
+ import { FileMode, isValidWorktreePath } from "@statewalker/vcs-core";
2
+ import { directoriesOf, type Worktree } from "@statewalker/vcs-working-tree";
2
3
  import { GitCommand } from "../git-command.js";
4
+ import { removeDirectoryIfEmpty } from "./worktree-overwrite-checks.js";
3
5
 
4
6
  /**
5
7
  * Result of a clean operation
@@ -7,6 +9,11 @@ import { GitCommand } from "../git-command.js";
7
9
  export interface CleanResult {
8
10
  /** Files that were (or would be) removed */
9
11
  cleaned: Set<string>;
12
+ /**
13
+ * Nested repositories left in untracked directories (`git clean -n`: "Would skip
14
+ * repository"); one directly under a tracked directory is passed over silently, as git
15
+ */
16
+ skipped: Set<string>;
10
17
  /** Whether this was a dry run (no files actually removed) */
11
18
  dryRun: boolean;
12
19
  }
@@ -50,6 +57,7 @@ export class CleanCommand extends GitCommand<CleanResult> {
50
57
  private dryRun = true; // Default to dry-run for safety
51
58
  private directories = false;
52
59
  private ignore = true; // Respect .gitignore
60
+ private forceNested = false;
53
61
 
54
62
  /**
55
63
  * Limit clean to specific paths.
@@ -77,7 +85,8 @@ export class CleanCommand extends GitCommand<CleanResult> {
77
85
  }
78
86
 
79
87
  /**
80
- * Set whether to also clean directories.
88
+ * Set whether to also clean untracked directories (`git clean -d`): their untracked
89
+ * files are removed and each directory once it is empty.
81
90
  *
82
91
  * @param directories Whether to clean directories
83
92
  */
@@ -102,7 +111,30 @@ export class CleanCommand extends GitCommand<CleanResult> {
102
111
  }
103
112
 
104
113
  /**
105
- * Execute the clean command.
114
+ * Set whether nested repositories (untracked directories with their own `.git`) are
115
+ * removed too, as `git clean -ff`. Off by default: they are skipped, as `git clean -f`
116
+ * skips them, and listed in `skipped`.
117
+ *
118
+ * @param force Whether to remove nested repositories
119
+ */
120
+ setForceNestedRepositories(force: boolean): this {
121
+ this.checkCallable();
122
+ this.forceNested = force;
123
+ return this;
124
+ }
125
+
126
+ /**
127
+ * Execute the clean command, as `git clean -f` (`-d` with `setCleanDirectories(true)`,
128
+ * `-x` with `setIgnore(false)`, `-ff` with `setForceNestedRepositories(true)`, `-n` with
129
+ * dry run):
130
+ * - only files the index does not track are removed, one at a time;
131
+ * - an untracked directory (one holding no tracked or staged path) is entered only with
132
+ * `-d` or a path filter; with `-d` it is removed once empty, deepest first and never
133
+ * recursively, so whatever stays in it (an ignored file without `-x`, a nested
134
+ * repository without `-ff`) keeps it;
135
+ * - `cleaned` lists what `git clean -n` prints after "Would remove": a wholly removed
136
+ * untracked directory as `dir/`, else the files; `skipped` the nested repositories
137
+ * ("Would skip repository").
106
138
  *
107
139
  * @returns CleanResult with set of cleaned files
108
140
  */
@@ -110,116 +142,183 @@ export class CleanCommand extends GitCommand<CleanResult> {
110
142
  this.checkCallable();
111
143
  this.setCallable(false);
112
144
 
113
- const cleaned = new Set<string>();
114
- const toRemove: Array<{ path: string; isDirectory: boolean }> = [];
115
-
116
- // Check if worktree is available
117
145
  const worktree = this.worktreeAccess;
118
146
  if (!worktree) {
119
147
  throw new Error("CleanCommand requires a WorkingCopy with worktree");
120
148
  }
149
+ return worktree.batch ? worktree.batch(() => this.clean(worktree)) : this.clean(worktree);
150
+ }
121
151
 
122
- // Get entries from staging to compare
123
- const stagedPaths = new Set<string>();
124
- for await (const entry of this.staging.entries()) {
125
- stagedPaths.add(entry.path);
152
+ /**
153
+ * Whether `path` is or lies in one of the requested paths, as git's pathspec matches it: a
154
+ * plain path matches itself and what is below it; one with `*`, `?` or `[...]` is a glob
155
+ * over the whole path (`*` matching `/` too).
156
+ */
157
+ private matchesPaths(path: string, isDirectory = false): boolean {
158
+ if (this.paths.size === 0) return true;
159
+ for (const p of this.paths) {
160
+ const spec = p.replace(/\/+$/, "");
161
+ if (spec === "" || spec === ".") return true;
162
+ if (/[*?[]/.test(spec)) {
163
+ // a directory is matched with its trailing slash too (`u/*` names `u/`, as git)
164
+ const glob = globToRegExp(spec);
165
+ if (glob.test(path) || (isDirectory && glob.test(`${path}/`))) return true;
166
+ continue;
167
+ }
168
+ if (path === spec || path.startsWith(`${spec}/`)) return true;
126
169
  }
170
+ return false;
171
+ }
127
172
 
128
- // Get entries from HEAD commit (if any) to compare
129
- const headPaths = new Set<string>();
130
- try {
131
- const head = await this.refsStore.resolve("HEAD");
132
- if (head?.objectId) {
133
- const commit = await this.commits.load(head.objectId);
134
- if (commit) {
135
- await this.collectTreePaths(commit.tree, "", headPaths);
136
- }
137
- }
138
- } catch {
139
- // No HEAD yet, all files are "new"
173
+ /** Whether a requested path names `path` itself. */
174
+ private namesPath(path: string): boolean {
175
+ for (const p of this.paths) {
176
+ const spec = p.replace(/\/+$/, "");
177
+ if (spec === path || (/[*?[]/.test(spec) && globToRegExp(spec).test(path))) return true;
140
178
  }
179
+ return false;
180
+ }
141
181
 
142
- // Walk worktree and find untracked files
143
- const options = {
144
- includeIgnored: !this.ignore,
145
- includeDirectories: this.directories,
182
+ private async clean(worktree: Worktree): Promise<CleanResult> {
183
+ // what the index tracks (any stage): git clean looks at the index, not at HEAD
184
+ const tracked = new Set<string>();
185
+ for await (const entry of this.staging.entries()) tracked.add(entry.path);
186
+ const trackedDirs = directoriesOf(tracked);
187
+ const removeIgnored = !this.ignore;
188
+ // without -d (and without a path filter) git does not enter untracked directories; a
189
+ // path filter removes the directories it names as -d does
190
+ const enterUntracked = this.directories || this.paths.size > 0;
191
+ const ancestors = (path: string) => {
192
+ const parts = path.split("/");
193
+ const out: string[] = [];
194
+ for (let k = 1; k < parts.length; k++) out.push(parts.slice(0, k).join("/"));
195
+ return out;
146
196
  };
147
197
 
148
- for await (const entry of worktree.walk(options)) {
149
- const entryTyped = entry as { path: string; isDirectory: boolean; isIgnored?: boolean };
150
-
151
- // Skip if in specific paths and not matching
152
- if (this.paths.size > 0) {
153
- let matches = false;
154
- for (const p of this.paths) {
155
- if (entryTyped.path === p || entryTyped.path.startsWith(`${p}/`)) {
156
- matches = true;
157
- break;
158
- }
159
- }
160
- if (!matches) continue;
161
- }
162
-
163
- // Skip if ignored and respecting .gitignore
164
- if (this.ignore && entryTyped.isIgnored) {
165
- continue;
198
+ // whether something under `dir` stays (an ignored file without -x)
199
+ const holdsKept = async (dir: string) => {
200
+ if (removeIgnored) return false;
201
+ for await (const inside of worktree.walk({ pathPrefix: dir, includeIgnored: true })) {
202
+ if (inside.isIgnored) return true;
166
203
  }
204
+ return false;
205
+ };
167
206
 
168
- // Skip if tracked (in staging or HEAD)
169
- if (stagedPaths.has(entryTyped.path) || headPaths.has(entryTyped.path)) {
170
- continue;
171
- }
207
+ const removeFiles: string[] = [];
208
+ const removeRepos: string[] = [];
209
+ const untrackedDirs: string[] = [];
210
+ const notEntered = new Set<string>();
211
+ const skipped = new Set<string>();
212
+ // the untracked directories something stays in: they stay too
213
+ const kept = new Set<string>();
214
+ const keep = (path: string, isDirectory = false) => {
215
+ for (const dir of ancestors(path)) kept.add(dir);
216
+ if (isDirectory) kept.add(path);
217
+ };
172
218
 
173
- // Skip directories unless specified
174
- if (entryTyped.isDirectory && !this.directories) {
219
+ for await (const entry of worktree.walk({ includeIgnored: true, includeDirectories: true })) {
220
+ const path = entry.path;
221
+ if (ancestors(path).some((dir) => notEntered.has(dir))) continue;
222
+ const keptAsIgnored = entry.isIgnored && !removeIgnored;
223
+ if (entry.isDirectory) {
224
+ if (trackedDirs.has(path)) continue;
225
+ // a directory where the index has a file, link or submodule: git never enters it
226
+ if (tracked.has(path) || !enterUntracked) {
227
+ notEntered.add(path);
228
+ continue;
229
+ }
230
+ if (entry.mode === FileMode.GITLINK) {
231
+ notEntered.add(path);
232
+ // a `.git` that is no repository (an empty directory): an ordinary directory
233
+ // to git clean, removed with what is in it
234
+ if (worktree.isRepository && !(await worktree.isRepository(path))) {
235
+ if (keptAsIgnored || !this.matchesPaths(path) || (await holdsKept(path))) {
236
+ keep(path, true);
237
+ } else removeRepos.push(path);
238
+ continue;
239
+ }
240
+ // a nested repository: only `git clean -ff` removes it (whole)
241
+ if (keptAsIgnored || !this.matchesPaths(path)) keep(path, true);
242
+ else if (!this.forceNested) {
243
+ // git reports the ones it meets while removing an untracked directory
244
+ // ("Would skip repository u/sub"); one directly under a tracked directory, or
245
+ // named by a requested path, is passed over silently
246
+ if (ancestors(path).some((dir) => !trackedDirs.has(dir)) && !this.namesPath(path)) {
247
+ skipped.add(path);
248
+ }
249
+ keep(path, true);
250
+ } else removeRepos.push(path);
251
+ continue;
252
+ }
253
+ untrackedDirs.push(path);
254
+ // an ignored directory stays whole without -x
255
+ if (keptAsIgnored) keep(path, true);
175
256
  continue;
176
257
  }
177
-
178
- // A name that spells `.git` (`.GIT`, `git~1`, …) is refused by the worktree (git's
179
- // verify_path): leave it in place rather than abort the whole clean on it.
180
- if (!isValidWorktreePath(entryTyped.path)) {
258
+ if (tracked.has(path)) continue;
259
+ if (
260
+ keptAsIgnored ||
261
+ !this.matchesPaths(path) ||
262
+ // a name that spells `.git` is refused by the worktree (git's verify_path): left
263
+ !isValidWorktreePath(path)
264
+ ) {
265
+ keep(path);
181
266
  continue;
182
267
  }
268
+ removeFiles.push(path);
269
+ }
183
270
 
184
- // This file is untracked and should be cleaned
185
- const displayPath = entryTyped.isDirectory ? `${entryTyped.path}/` : entryTyped.path;
186
- cleaned.add(displayPath);
271
+ // what git clean -n prints: the topmost wholly removed untracked directory, else files
272
+ const dirMode = this.directories || this.paths.size > 0;
273
+ const removedDir = (dir: string) =>
274
+ dirMode && !trackedDirs.has(dir) && !kept.has(dir) && this.matchesPaths(dir, true);
275
+ const reportFor = (path: string, suffix = "") => {
276
+ for (const dir of ancestors(path)) if (removedDir(dir)) return `${dir}/`;
277
+ return `${path}${suffix}`;
278
+ };
279
+ const cleaned = new Set<string>();
280
+ for (const path of removeFiles) cleaned.add(reportFor(path));
281
+ for (const path of removeRepos) cleaned.add(reportFor(path, "/"));
282
+ for (const dir of untrackedDirs) if (removedDir(dir)) cleaned.add(reportFor(dir, "/"));
187
283
 
188
- // Collect rather than delete inline: removing entries while the walk is
189
- // still iterating the same directories is not safe for all Worktree
190
- // implementations.
191
- if (!this.dryRun) {
192
- toRemove.push({ path: entryTyped.path, isDirectory: entryTyped.isDirectory });
284
+ if (!this.dryRun) {
285
+ for (const path of removeFiles) await worktree.remove(path);
286
+ // `git clean -ff` (or a `.git` that is no repository): the directory goes, whole
287
+ for (const path of removeRepos) await worktree.remove(path, { recursive: true });
288
+ if (dirMode) {
289
+ const deepestFirst = [...untrackedDirs].sort(
290
+ (a, b) => b.split("/").length - a.split("/").length || (a < b ? 1 : -1),
291
+ );
292
+ // only the directories the path filter names (a glob emptying one leaves it)
293
+ for (const dir of deepestFirst) {
294
+ if (this.matchesPaths(dir, true)) await removeDirectoryIfEmpty(worktree, dir);
295
+ }
193
296
  }
194
297
  }
195
298
 
196
- for (const { path, isDirectory } of toRemove) {
197
- await worktree.remove(path, { recursive: isDirectory });
198
- }
199
-
200
- return {
201
- cleaned,
202
- dryRun: this.dryRun,
203
- };
299
+ return { cleaned, skipped, dryRun: this.dryRun };
204
300
  }
301
+ }
205
302
 
206
- /**
207
- * Collect all paths from a tree recursively.
208
- */
209
- private async collectTreePaths(
210
- treeId: string,
211
- prefix: string,
212
- paths: Set<string>,
213
- ): Promise<void> {
214
- for await (const entry of this.trees.loadTree(treeId)) {
215
- const fullPath = prefix ? `${prefix}/${entry.name}` : entry.name;
216
-
217
- // Check if directory mode (0o040000)
218
- if ((entry.mode & 0o170000) === 0o040000) {
219
- await this.collectTreePaths(entry.id, fullPath, paths);
220
- } else {
221
- paths.add(fullPath);
303
+ /** A pathspec glob as a regular expression over the whole path (`*` matches `/` too). */
304
+ function globToRegExp(glob: string): RegExp {
305
+ let out = "";
306
+ for (let i = 0; i < glob.length; i++) {
307
+ const c = glob[i];
308
+ if (c === "*") out += ".*";
309
+ else if (c === "?") out += "[^/]";
310
+ else if (c === "[") {
311
+ const end = glob.indexOf("]", i + 1);
312
+ if (end < 0) out += "\\[";
313
+ else {
314
+ const body = glob
315
+ .slice(i + 1, end)
316
+ .replace(/^!/, "^")
317
+ .replace(/\\/g, "\\\\");
318
+ out += `[${body}]`;
319
+ i = end;
222
320
  }
223
- }
321
+ } else out += c.replace(/[.+^${}()|\\/]/g, "\\$&");
224
322
  }
323
+ return new RegExp(`^${out}$`);
225
324
  }