@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
@@ -1,4 +1,7 @@
1
+ import { FileMode, isValidWorktreePath } from "@statewalker/vcs-core";
2
+ import { directoriesOf, type Worktree } from "@statewalker/vcs-working-tree";
1
3
  import { GitCommand } from "../git-command.js";
4
+ import { removeDirectoryIfEmpty } from "./worktree-overwrite-checks.js";
2
5
 
3
6
  /**
4
7
  * Result of a clean operation
@@ -6,6 +9,11 @@ import { GitCommand } from "../git-command.js";
6
9
  export interface CleanResult {
7
10
  /** Files that were (or would be) removed */
8
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>;
9
17
  /** Whether this was a dry run (no files actually removed) */
10
18
  dryRun: boolean;
11
19
  }
@@ -49,6 +57,7 @@ export class CleanCommand extends GitCommand<CleanResult> {
49
57
  private dryRun = true; // Default to dry-run for safety
50
58
  private directories = false;
51
59
  private ignore = true; // Respect .gitignore
60
+ private forceNested = false;
52
61
 
53
62
  /**
54
63
  * Limit clean to specific paths.
@@ -76,7 +85,8 @@ export class CleanCommand extends GitCommand<CleanResult> {
76
85
  }
77
86
 
78
87
  /**
79
- * 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.
80
90
  *
81
91
  * @param directories Whether to clean directories
82
92
  */
@@ -101,7 +111,30 @@ export class CleanCommand extends GitCommand<CleanResult> {
101
111
  }
102
112
 
103
113
  /**
104
- * 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").
105
138
  *
106
139
  * @returns CleanResult with set of cleaned files
107
140
  */
@@ -109,110 +142,183 @@ export class CleanCommand extends GitCommand<CleanResult> {
109
142
  this.checkCallable();
110
143
  this.setCallable(false);
111
144
 
112
- const cleaned = new Set<string>();
113
- const toRemove: Array<{ path: string; isDirectory: boolean }> = [];
114
-
115
- // Check if worktree is available
116
145
  const worktree = this.worktreeAccess;
117
146
  if (!worktree) {
118
147
  throw new Error("CleanCommand requires a WorkingCopy with worktree");
119
148
  }
149
+ return worktree.batch ? worktree.batch(() => this.clean(worktree)) : this.clean(worktree);
150
+ }
120
151
 
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);
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;
125
169
  }
170
+ return false;
171
+ }
126
172
 
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"
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;
139
178
  }
179
+ return false;
180
+ }
140
181
 
141
- // Walk worktree and find untracked files
142
- const options = {
143
- includeIgnored: !this.ignore,
144
- 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;
145
196
  };
146
197
 
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;
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;
160
203
  }
204
+ return false;
205
+ };
161
206
 
162
- // Skip if ignored and respecting .gitignore
163
- if (this.ignore && entryTyped.isIgnored) {
164
- continue;
165
- }
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
+ };
166
218
 
167
- // Skip if tracked (in staging or HEAD)
168
- if (stagedPaths.has(entryTyped.path) || headPaths.has(entryTyped.path)) {
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);
169
256
  continue;
170
257
  }
171
-
172
- // Skip directories unless specified
173
- if (entryTyped.isDirectory && !this.directories) {
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);
174
266
  continue;
175
267
  }
268
+ removeFiles.push(path);
269
+ }
176
270
 
177
- // This file is untracked and should be cleaned
178
- const displayPath = entryTyped.isDirectory ? `${entryTyped.path}/` : entryTyped.path;
179
- 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, "/"));
180
283
 
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 });
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
+ }
186
296
  }
187
297
  }
188
298
 
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
- };
299
+ return { cleaned, skipped, dryRun: this.dryRun };
197
300
  }
301
+ }
198
302
 
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);
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;
215
320
  }
216
- }
321
+ } else out += c.replace(/[.+^${}()|\\/]/g, "\\$&");
217
322
  }
323
+ return new RegExp(`^${out}$`);
218
324
  }