forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup-worktree.ts
More file actions
428 lines (355 loc) · 10.8 KB
/
Copy pathcleanup-worktree.ts
File metadata and controls
428 lines (355 loc) · 10.8 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env bun
import { spawn } from 'child_process'
import { existsSync, readdirSync, readFileSync, rmSync } from 'fs'
import { join, resolve } from 'path'
import { createInterface } from 'readline'
import { z } from 'zod/v4'
// Validation schemas
const WorktreeArgsSchema = z.object({
name: z
.string()
.min(1, 'Worktree name cannot be empty')
.max(50, 'Worktree name must be 50 characters or less')
.regex(
/^[a-zA-Z0-9_/-]+$/,
'Worktree name must contain only letters, numbers, hyphens, underscores, and forward slashes',
),
})
type WorktreeArgs = z.infer<typeof WorktreeArgsSchema>
interface WorktreePorts {
webPort?: number
backendPort?: number
}
class WorktreeError extends Error {
constructor(
message: string,
public code: string = 'WORKTREE_ERROR',
) {
super(message)
this.name = 'WorktreeError'
}
}
const WORKTREES_DIR = '../codebuff-worktrees'
// Utility functions
function parseArgs(): WorktreeArgs | null {
const args = process.argv.slice(2)
if (args.length === 0) {
return null // Will clean up all worktrees
}
const [name] = args
return { name }
}
function validateArgs(args: WorktreeArgs): string[] {
const result = WorktreeArgsSchema.safeParse(args)
if (!result.success) {
return result.error.issues.map((err) => err.message)
}
return []
}
async function runCommand(
command: string,
args: string[],
cwd?: string,
): Promise<{ stdout: string; stderr: string; exitCode: number }> {
return new Promise((resolve, reject) => {
const proc = spawn(command, args, {
cwd,
stdio: 'pipe',
shell: false,
})
let stdout = ''
let stderr = ''
proc.stdout?.on('data', (data) => {
stdout += data.toString()
})
proc.stderr?.on('data', (data) => {
stderr += data.toString()
})
proc.on('close', (code) => {
resolve({ stdout, stderr, exitCode: code || 0 })
})
proc.on('error', (error) => {
reject(
new WorktreeError(
`Failed to run ${command}: ${error.message}`,
'COMMAND_ERROR',
),
)
})
})
}
async function promptUser(question: string): Promise<boolean> {
const rl = createInterface({
input: process.stdin,
output: process.stdout,
})
return new Promise((resolve) => {
rl.question(question, (answer) => {
rl.close()
resolve(/^[Yy]$/.test(answer.trim()))
})
})
}
function getWorktreePorts(worktreePath: string): WorktreePorts {
const ports: WorktreePorts = {}
const envFiles = [
'.env.development.local',
'.env.development',
'.env.worktree',
]
for (const envFile of envFiles) {
const envPath = join(worktreePath, envFile)
if (existsSync(envPath)) {
try {
const content = readFileSync(envPath, 'utf-8')
// Parse PORT or NEXT_PUBLIC_WEB_PORT for web port
const webPortMatch = content.match(
/^(?:PORT|NEXT_PUBLIC_WEB_PORT)=(\d+)/m,
)
if (webPortMatch) {
ports.webPort = parseInt(webPortMatch[1], 10)
}
// Parse backend port from NEXT_PUBLIC_CODEBUFF_BACKEND_URL
const backendUrlMatch = content.match(
/^NEXT_PUBLIC_CODEBUFF_BACKEND_URL=.*:(\d+)/m,
)
if (backendUrlMatch) {
ports.backendPort = parseInt(backendUrlMatch[1], 10)
}
if (ports.webPort || ports.backendPort) {
break // Found ports, no need to check other files
}
} catch {
// Continue to next file
}
}
}
return ports
}
async function killProcessOnPort(port: number): Promise<boolean> {
try {
// Find processes using the port
const result = await runCommand('lsof', ['-ti', `:${port}`])
if (result.exitCode !== 0 || !result.stdout.trim()) {
return false // No process found on port
}
const pids = result.stdout.trim().split('\n').filter(Boolean)
for (const pid of pids) {
try {
await runCommand('kill', ['-9', pid])
console.log(` Killed process ${pid} on port ${port}`)
} catch {
// Process may have already exited
}
}
return true
} catch {
return false
}
}
async function killWorktreePorts(worktreePath: string): Promise<void> {
const ports = getWorktreePorts(worktreePath)
if (!ports.webPort && !ports.backendPort) {
console.log(' No port configuration found, skipping port cleanup')
return
}
console.log(' Killing processes on worktree ports...')
if (ports.webPort) {
const killed = await killProcessOnPort(ports.webPort)
if (killed) {
console.log(` ✓ Cleaned up web port ${ports.webPort}`)
} else {
console.log(` ○ No process found on web port ${ports.webPort}`)
}
}
if (ports.backendPort) {
const killed = await killProcessOnPort(ports.backendPort)
if (killed) {
console.log(` ✓ Cleaned up backend port ${ports.backendPort}`)
} else {
console.log(` ○ No process found on backend port ${ports.backendPort}`)
}
}
}
async function isWorktreeInGitList(worktreePath: string): Promise<boolean> {
try {
const result = await runCommand('git', ['worktree', 'list', '--porcelain'])
return result.stdout.includes(worktreePath)
} catch {
return false
}
}
async function removeGitWorktree(
worktreePath: string,
force: boolean = false,
): Promise<boolean> {
const args = ['worktree', 'remove', worktreePath]
if (force) {
args.push('--force')
}
const result = await runCommand('git', args)
return result.exitCode === 0
}
async function cleanupWorktree(worktreeName: string): Promise<boolean> {
const worktreePath = resolve(WORKTREES_DIR, worktreeName)
console.log(`\nCleaning up worktree: ${worktreeName}`)
console.log(` Path: ${worktreePath}`)
// Step 1: Kill processes on worktree ports
if (existsSync(worktreePath)) {
await killWorktreePorts(worktreePath)
}
// Step 2: Remove git worktree
const isInGitList = await isWorktreeInGitList(worktreePath)
if (isInGitList) {
console.log(' Removing git worktree...')
// Try regular remove first
let removed = await removeGitWorktree(worktreePath, false)
if (!removed) {
console.log(
' ⚠️ Worktree has uncommitted changes or is locked, forcing removal...',
)
removed = await removeGitWorktree(worktreePath, true)
}
if (removed) {
console.log(' ✓ Git worktree removed')
} else {
console.log(' ⚠️ Failed to remove git worktree')
}
} else {
console.log(' ○ Worktree not in git worktree list (already removed)')
}
// Step 3: Clean up remaining directory
if (existsSync(worktreePath)) {
console.log(' Removing remaining files...')
try {
rmSync(worktreePath, { recursive: true, force: true })
console.log(' ✓ Directory removed')
} catch (error) {
console.log(` ⚠️ Failed to remove directory: ${error}`)
}
}
// Step 4: Verification checks
console.log(' Running verification checks...')
let allClean = true
// Verify directory is gone
if (existsSync(worktreePath)) {
console.log(' ❌ VERIFICATION FAILED: Directory still exists')
allClean = false
} else {
console.log(' ✓ Verified: Directory removed')
}
// Verify git worktree is removed
const stillInGitList = await isWorktreeInGitList(worktreePath)
if (stillInGitList) {
console.log(' ❌ VERIFICATION FAILED: Still in git worktree list')
allClean = false
} else {
console.log(' ✓ Verified: Removed from git worktree list')
}
// Prune any stale worktree references
await runCommand('git', ['worktree', 'prune'])
if (allClean) {
console.log(` ✅ Worktree '${worktreeName}' fully cleaned up`)
} else {
console.log(` ⚠️ Worktree '${worktreeName}' cleanup incomplete`)
}
return allClean
}
function getExistingWorktrees(): string[] {
const worktreesPath = resolve(WORKTREES_DIR)
if (!existsSync(worktreesPath)) {
return []
}
try {
return readdirSync(worktreesPath, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
} catch {
return []
}
}
async function main(): Promise<void> {
try {
console.log('Codebuff Worktree Cleanup')
console.log('=========================')
const args = parseArgs()
if (args) {
// Single worktree cleanup
const validationErrors = validateArgs(args)
if (validationErrors.length > 0) {
console.error('Validation errors:')
validationErrors.forEach((error) => {
console.error(` - ${error}`)
})
process.exit(1)
}
const worktreePath = resolve(WORKTREES_DIR, args.name)
// Check if worktree exists
const isInGitList = await isWorktreeInGitList(worktreePath)
if (!existsSync(worktreePath) && !isInGitList) {
console.log(`\nWorktree '${args.name}' does not exist.`)
process.exit(0)
}
const success = await cleanupWorktree(args.name)
process.exit(success ? 0 : 1)
} else {
// All worktrees cleanup
const worktrees = getExistingWorktrees()
if (worktrees.length === 0) {
console.log('\nNo worktrees found to clean up.')
process.exit(0)
}
console.log(`\nFound ${worktrees.length} worktree(s):`)
worktrees.forEach((name) => console.log(` - ${name}`))
const shouldContinue = await promptUser(
'\nClean up ALL worktrees? This will kill associated processes. (y/N) ',
)
if (!shouldContinue) {
console.log('Aborted.')
process.exit(0)
}
let allSuccess = true
for (const worktreeName of worktrees) {
const success = await cleanupWorktree(worktreeName)
if (!success) {
allSuccess = false
}
}
// Try to remove the worktrees directory if empty
const remainingWorktrees = getExistingWorktrees()
if (remainingWorktrees.length === 0) {
const worktreesPath = resolve(WORKTREES_DIR)
if (existsSync(worktreesPath)) {
try {
rmSync(worktreesPath, { recursive: true, force: true })
console.log('\n✓ Removed worktrees directory')
} catch {
console.log('\n⚠️ Could not remove worktrees directory')
}
}
}
console.log('\n=========================')
if (allSuccess) {
console.log('✅ All worktree cleanup operations complete!')
} else {
console.log('⚠️ Some worktree cleanup operations had issues')
process.exit(1)
}
}
} catch (error) {
if (error instanceof WorktreeError) {
console.error(`Error: ${error.message}`)
process.exit(1)
} else {
console.error('Unexpected error:', error)
process.exit(1)
}
}
}
// Run the script
if (require.main === module) {
main().catch((error) => {
console.error('Fatal error:', error)
process.exit(1)
})
}