@solaqua/gji 0.7.2 → 0.8.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.
package/dist/clean.js CHANGED
@@ -3,7 +3,7 @@ import { loadEffectiveConfig } from "./config.js";
3
3
  import { isBranchMergedInto, readWorktreeHealth, resolveRemoteDefaultBranch, runGit, } from "./git.js";
4
4
  import { isHeadless } from "./headless.js";
5
5
  import { formatLastCommit, formatUpstreamState, readWorktreeInfos, serializeWorktreeInfo, } from "./worktree-info.js";
6
- import { deleteBranch, forceDeleteBranch, forceRemoveWorktree, isBranchUnmergedError, isWorktreeDirtyError, loadLinkedWorktrees, removeWorktree, } from "./worktree-management.js";
6
+ import { deleteBranch, forceDeleteBranch, forceRemoveWorktree, isBranchUnmergedError, isWorktreeDeletionError, isWorktreeForceRemovalError, loadLinkedWorktrees, removeWorktree, } from "./worktree-management.js";
7
7
  import { buildWorktreePromptEntries, promptForMultipleWorktrees, } from "./worktree-picker.js";
8
8
  import { defaultConfirmForceDeleteBranch, defaultConfirmForceRemoveWorktree, } from "./worktree-prompts.js";
9
9
  export function createCleanCommand(dependencies = {}) {
@@ -77,8 +77,8 @@ export function createCleanCommand(dependencies = {}) {
77
77
  }
78
78
  return 0;
79
79
  }
80
- const removedPaths = [];
81
80
  const removedWorktrees = [];
81
+ const failures = [];
82
82
  for (const worktree of selectedWorktrees) {
83
83
  if (options.stale &&
84
84
  !(await isStaleCleanupCandidate(repository.repoRoot, worktree, staleBaseRef))) {
@@ -89,31 +89,39 @@ export function createCleanCommand(dependencies = {}) {
89
89
  await removeWorktree(repository.repoRoot, worktree.path);
90
90
  }
91
91
  catch (error) {
92
- if (!isWorktreeDirtyError(error)) {
93
- throw error;
92
+ if (!isWorktreeForceRemovalError(error)) {
93
+ failures.push({
94
+ branch: worktree.branch,
95
+ message: toMessage(error),
96
+ path: worktree.path,
97
+ });
98
+ continue;
94
99
  }
95
- if (options.stale) {
100
+ if (options.stale && !isWorktreeDeletionError(error)) {
96
101
  options.stderr(`Skipped ${worktree.path}: no longer a safe stale cleanup candidate\n`);
97
102
  continue;
98
103
  }
99
104
  if (!options.force &&
100
105
  !(await confirmForceRemoveWorktree(worktree.path))) {
101
- reportRemovedPaths(removedPaths, options.stderr);
102
- options.stderr("Aborted\n");
103
- return 1;
106
+ failures.push({
107
+ branch: worktree.branch,
108
+ message: "force removal declined",
109
+ path: worktree.path,
110
+ });
111
+ continue;
104
112
  }
105
113
  try {
106
114
  await forceRemoveWorktree(repository.repoRoot, worktree.path);
107
115
  }
108
116
  catch (forceError) {
109
- if (!options.json) {
110
- reportRemovedPaths(removedPaths, options.stderr);
111
- }
112
- emitError(options, `Failed to remove worktree at ${worktree.path}: ${toMessage(forceError)}`);
113
- return 1;
117
+ failures.push({
118
+ branch: worktree.branch,
119
+ message: toMessage(forceError),
120
+ path: worktree.path,
121
+ });
122
+ continue;
114
123
  }
115
124
  }
116
- removedPaths.push(worktree.path);
117
125
  removedWorktrees.push(worktree);
118
126
  if (worktree.branch) {
119
127
  try {
@@ -145,12 +153,16 @@ export function createCleanCommand(dependencies = {}) {
145
153
  ? { branch: worktree.branch, path: worktree.path }
146
154
  : serializeWorktreeInfo(info);
147
155
  });
148
- options.stdout(`${JSON.stringify({ removed }, null, 2)}\n`);
156
+ const payload = failures.length === 0 ? { removed } : { removed, failed: failures };
157
+ options.stdout(`${JSON.stringify(payload, null, 2)}\n`);
158
+ }
159
+ else if (failures.length > 0) {
160
+ reportCleanFailures(failures, options.stderr);
149
161
  }
150
162
  else {
151
163
  options.stdout(`${repository.repoRoot}\n`);
152
164
  }
153
- return 0;
165
+ return failures.length === 0 ? 0 : 1;
154
166
  };
155
167
  }
156
168
  export const runCleanCommand = createCleanCommand();
@@ -216,9 +228,12 @@ function resolveSelectedWorktrees(worktrees, selections) {
216
228
  }
217
229
  return selectedWorktrees;
218
230
  }
219
- function reportRemovedPaths(paths, stderr) {
220
- if (paths.length > 0) {
221
- stderr(`Already removed: ${paths.join(", ")}\n`);
231
+ function reportCleanFailures(failures, stderr) {
232
+ const noun = failures.length === 1 ? "worktree" : "worktrees";
233
+ stderr(`Failed to clean ${failures.length} ${noun}:\n`);
234
+ for (const failure of failures) {
235
+ const branch = failure.branch === null ? "detached" : failure.branch;
236
+ stderr(`- ${failure.path} (${branch}): ${failure.message}\n`);
222
237
  }
223
238
  }
224
239
  function formatCleanInfo(info) {