1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261import { spawnSync } from 'child_process';
import { existsSync } from 'fs';
import { WorktreeInfo, OperationResult, RemovalStrategy } from '../types/index.js';
/**
* Worktree Remover
*
* Safely removes Git worktrees with proper error handling:
* - Executes git worktree remove
* - Handles locked worktrees
* - Handles missing directories
* - Handles permission errors
* - Supports force removal
*/
export class WorktreeRemover {
/**
* Remove a worktree
*
* @param worktree - Worktree information
* @param force - Force removal even if worktree has changes
* @returns Result object with success status and message
*/
async remove(
worktree: WorktreeInfo,
force: boolean = false
): Promise<OperationResult> {
// Check if worktree is locked
if (worktree.isLocked && !force) {
return {
success: false,
message: 'Worktree is locked. Use --force to remove anyway.'
};
}
// Check if worktree is prunable (directory missing)
if (worktree.isPrunable) {
// Use git worktree prune instead
return this.prune(worktree);
}
// Check if directory exists
const exists = existsSync(worktree.path);
if (!exists && !force) {
return {
success: false,
message:
'Worktree directory does not exist. Use git worktree prune to clean up, or use --force.'
};
}
// Build git worktree remove command
const args = ['worktree', 'remove'];
if (force) {
args.push('--force');
}
args.push(worktree.path);
// Execute removal
const result = spawnSync('git', args, {
encoding: 'utf8',
stdio: 'pipe'
});
if (result.status === 0) {
return {
success: true,
message: `Worktree removed successfully: ${worktree.path}`
};
}
// Handle errors
const errorMessage = result.stderr || result.stdout || 'Unknown error';
// Check for common error patterns
if (errorMessage.includes('locked')) {
return {
success: false,
message: 'Worktree is locked. Use --force to remove anyway.'
};
}
if (errorMessage.includes('uncommitted changes') || errorMessage.includes('modified files')) {
return {
success: false,
message: 'Worktree has uncommitted changes. Commit, stash, or use --force.'
};
}
if (errorMessage.includes('Permission denied')) {
return {
success: false,
message: 'Permission denied. Check file permissions.'
};
}
return {
success: false,
message: `Failed to remove worktree: ${errorMessage.trim()}`
};
}
/**
* Prune worktree (when directory is missing)
*
* @param worktree - Worktree information
* @returns Result object with success status and message
*/
private async prune(worktree: WorktreeInfo): Promise<OperationResult> {
const result = spawnSync('git', ['worktree', 'prune'], {
encoding: 'utf8',
stdio: 'pipe'
});
if (result.status === 0) {
return {
success: true,
message: `Worktree pruned successfully: ${worktree.path}`
};
}
return {
success: false,
message: `Failed to prune worktree: ${result.stderr || result.stdout || 'Unknown error'}`
};
}
/**
* Check if worktree can be removed without force
*
* @param worktree - Worktree information
* @returns True if removal is safe without --force
*/
canRemoveSafely(worktree: WorktreeInfo): boolean {
// Cannot remove locked worktrees without force
if (worktree.isLocked) {
return false;
}
// Can prune if directory is missing
if (worktree.isPrunable) {
return true;
}
// Otherwise, check if directory exists
return existsSync(worktree.path);
}
/**
* Get removal strategy for a worktree
*
* @param worktree - Worktree information
* @returns Recommended removal strategy
*/
getRemovalStrategy(worktree: WorktreeInfo): RemovalStrategy {
if (worktree.isPrunable) {
return {
strategy: 'prune',
reason: 'Worktree directory is missing, use git worktree prune'
};
}
if (worktree.isLocked) {
return {
strategy: 'force-remove',
reason: 'Worktree is locked, requires --force'
};
}
if (!existsSync(worktree.path)) {
return {
strategy: 'prune',
reason: 'Worktree directory does not exist'
};
}
return {
strategy: 'remove',
reason: 'Normal removal'
};
}
/**
* Unlock a worktree
*
* @param worktree - Worktree information
* @returns Result object with success status and message
*/
async unlock(worktree: WorktreeInfo): Promise<OperationResult> {
if (!worktree.isLocked) {
return {
success: true,
message: 'Worktree is not locked'
};
}
const result = spawnSync('git', ['worktree', 'unlock', worktree.path], {
encoding: 'utf8',
stdio: 'pipe'
});
if (result.status === 0) {
return {
success: true,
message: 'Worktree unlocked successfully'
};
}
return {
success: false,
message: `Failed to unlock worktree: ${result.stderr || result.stdout || 'Unknown error'}`
};
}
/**
* Lock a worktree
*
* @param worktree - Worktree information
* @param reason - Optional reason for locking
* @returns Result object with success status and message
*/
async lock(
worktree: WorktreeInfo,
reason?: string
): Promise<OperationResult> {
if (worktree.isLocked) {
return {
success: true,
message: 'Worktree is already locked'
};
}
const args = ['worktree', 'lock'];
if (reason) {
args.push('--reason', reason);
}
args.push(worktree.path);
const result = spawnSync('git', args, {
encoding: 'utf8',
stdio: 'pipe'
});
if (result.status === 0) {
return {
success: true,
message: 'Worktree locked successfully'
};
}
return {
success: false,
message: `Failed to lock worktree: ${result.stderr || result.stdout || 'Unknown error'}`
};
}
}