vibe-workspace 0.0.12

Extremely lightweight CLI for managing multiple git repositories and workspace configurations
Documentation
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
//! Cleanup strategies and safety mechanisms for worktree management
//!
//! This module provides safe cleanup operations for merged worktrees with
//! multiple validation layers and recovery options to prevent data loss.

use anyhow::{bail, Result};
use colored::*;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::time::Duration;
use tokio::process::Command;
use tracing::{info, warn};

use crate::worktree::config::WorktreeConfig;
use crate::worktree::operations::{RemoveOptions, WorktreeOperations};
use crate::worktree::status::WorktreeInfo;

/// Different strategies for cleaning up worktrees
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CleanupStrategy {
    /// Simply remove the worktree (safest, requires manual verification)
    Discard,
    /// Merge worktree changes into the feature branch before removal
    MergeToFeature,
    /// Push worktree branch to origin before removal (backup)
    BackupToOrigin,
    /// Stash changes and remove worktree
    StashAndDiscard,
}

/// Options for cleanup operations
#[derive(Debug, Clone)]
pub struct CleanupOptions {
    /// Strategy to use for cleanup
    pub strategy: CleanupStrategy,

    /// Minimum age threshold for cleanup eligibility
    pub min_age_hours: Option<u64>,

    /// Force cleanup even with safety violations
    pub force: bool,

    /// Show what would be done without executing
    pub dry_run: bool,

    /// Skip interactive confirmations
    pub auto_confirm: bool,

    /// Only clean worktrees with specific branch prefix
    pub branch_prefix_filter: Option<String>,

    /// Only clean worktrees that are confirmed merged
    pub merged_only: bool,

    /// Minimum merge confidence required (0.0-1.0)
    pub min_merge_confidence: f32,
}

/// Result of cleanup operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CleanupReport {
    /// Total worktrees evaluated
    pub total_evaluated: usize,

    /// Number of worktrees successfully cleaned
    pub cleaned_count: usize,

    /// Number of worktrees skipped due to safety checks
    pub skipped_count: usize,

    /// Number of worktrees that failed during cleanup
    pub failed_count: usize,

    /// Detailed results for each worktree
    pub worktree_results: Vec<WorktreeCleanupResult>,

    /// Overall cleanup strategy used
    pub strategy_used: CleanupStrategy,

    /// Whether this was a dry run
    pub was_dry_run: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorktreeCleanupResult {
    /// Path to the worktree
    pub path: PathBuf,

    /// Branch name
    pub branch: String,

    /// Cleanup action taken
    pub action: CleanupAction,

    /// Reason for the action (or why it was skipped)
    pub reason: String,

    /// Any error that occurred
    pub error: Option<String>,

    /// Safety violations that were ignored (if force was used)
    pub safety_violations: Vec<SafetyViolation>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CleanupAction {
    Cleaned,
    Skipped,
    Failed,
    StashCreated,
    MergedToFeature,
    BackedUpToOrigin,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SafetyViolation {
    pub violation_type: SafetyViolationType,
    pub description: String,
    pub severity: ViolationSeverity,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SafetyViolationType {
    UncommittedChanges,
    UnpushedCommits,
    BranchTooNew,
    NoRemoteTracking,
    LowMergeConfidence,
    RemoteBranchMissing,
    WorktreeInUse,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ViolationSeverity {
    Warning,  // Can be overridden with --force
    Critical, // Should never be overridden
}

/// Main cleanup orchestrator
pub struct WorktreeCleanup {
    config: WorktreeConfig,
    operations: WorktreeOperations,
}

impl WorktreeCleanup {
    pub fn new(config: WorktreeConfig, operations: WorktreeOperations) -> Self {
        Self { config, operations }
    }

    /// Execute cleanup operation on all eligible worktrees
    pub async fn cleanup_worktrees(&self, options: CleanupOptions) -> Result<CleanupReport> {
        info!(
            "Starting worktree cleanup with strategy: {:?}",
            options.strategy
        );

        let all_worktrees = self.operations.list_worktrees().await?;
        let mut report = CleanupReport {
            total_evaluated: all_worktrees.len(),
            cleaned_count: 0,
            skipped_count: 0,
            failed_count: 0,
            worktree_results: Vec::new(),
            strategy_used: options.strategy.clone(),
            was_dry_run: options.dry_run,
        };

        for worktree in all_worktrees {
            let result = self
                .evaluate_and_cleanup_worktree(&worktree, &options)
                .await;

            match result {
                Ok(cleanup_result) => {
                    match cleanup_result.action {
                        CleanupAction::Cleaned
                        | CleanupAction::MergedToFeature
                        | CleanupAction::BackedUpToOrigin => {
                            report.cleaned_count += 1;
                        }
                        CleanupAction::Skipped => {
                            report.skipped_count += 1;
                        }
                        CleanupAction::Failed => {
                            report.failed_count += 1;
                        }
                        CleanupAction::StashCreated => {
                            // Count as cleaned if the worktree was also removed
                            report.cleaned_count += 1;
                        }
                    }
                    report.worktree_results.push(cleanup_result);
                }
                Err(e) => {
                    warn!(
                        "Failed to process worktree {}: {}",
                        worktree.path.display(),
                        e
                    );
                    report.failed_count += 1;
                    report.worktree_results.push(WorktreeCleanupResult {
                        path: worktree.path.clone(),
                        branch: worktree.branch.clone(),
                        action: CleanupAction::Failed,
                        reason: "Processing error".to_string(),
                        error: Some(e.to_string()),
                        safety_violations: Vec::new(),
                    });
                }
            }
        }

        info!(
            "Cleanup complete: {} cleaned, {} skipped, {} failed",
            report.cleaned_count, report.skipped_count, report.failed_count
        );

        Ok(report)
    }

    /// Evaluate and potentially clean up a single worktree
    async fn evaluate_and_cleanup_worktree(
        &self,
        worktree: &WorktreeInfo,
        options: &CleanupOptions,
    ) -> Result<WorktreeCleanupResult> {
        // Skip the main repository worktree
        if self.is_main_repository_worktree(worktree).await? {
            return Ok(WorktreeCleanupResult {
                path: worktree.path.clone(),
                branch: worktree.branch.clone(),
                action: CleanupAction::Skipped,
                reason: "Main repository worktree".to_string(),
                error: None,
                safety_violations: Vec::new(),
            });
        }

        // Apply filters
        if !self.matches_filters(worktree, options) {
            return Ok(WorktreeCleanupResult {
                path: worktree.path.clone(),
                branch: worktree.branch.clone(),
                action: CleanupAction::Skipped,
                reason: "Does not match cleanup filters".to_string(),
                error: None,
                safety_violations: Vec::new(),
            });
        }

        // Perform safety checks
        let safety_violations = self.check_safety_violations(worktree, options).await;

        // Determine if we should proceed
        let critical_violations: Vec<_> = safety_violations
            .iter()
            .filter(|v| v.severity == ViolationSeverity::Critical)
            .collect();

        if !critical_violations.is_empty() {
            return Ok(WorktreeCleanupResult {
                path: worktree.path.clone(),
                branch: worktree.branch.clone(),
                action: CleanupAction::Skipped,
                reason: format!(
                    "Critical safety violations: {}",
                    critical_violations
                        .iter()
                        .map(|v| v.description.as_str())
                        .collect::<Vec<_>>()
                        .join(", ")
                ),
                error: None,
                safety_violations,
            });
        }

        let warning_violations: Vec<_> = safety_violations
            .iter()
            .filter(|v| v.severity == ViolationSeverity::Warning)
            .collect();

        if !warning_violations.is_empty() && !options.force {
            return Ok(WorktreeCleanupResult {
                path: worktree.path.clone(),
                branch: worktree.branch.clone(),
                action: CleanupAction::Skipped,
                reason: format!(
                    "Safety violations (use --force to override): {}",
                    warning_violations
                        .iter()
                        .map(|v| v.description.as_str())
                        .collect::<Vec<_>>()
                        .join(", ")
                ),
                error: None,
                safety_violations,
            });
        }

        // Ask for confirmation if not auto-confirming
        if !options.auto_confirm && !options.dry_run {
            if !self
                .confirm_cleanup(worktree, options, &safety_violations)
                .await?
            {
                return Ok(WorktreeCleanupResult {
                    path: worktree.path.clone(),
                    branch: worktree.branch.clone(),
                    action: CleanupAction::Skipped,
                    reason: "User declined cleanup".to_string(),
                    error: None,
                    safety_violations,
                });
            }
        }

        // Perform the cleanup operation
        if options.dry_run {
            Ok(WorktreeCleanupResult {
                path: worktree.path.clone(),
                branch: worktree.branch.clone(),
                action: CleanupAction::Cleaned,
                reason: "Would be cleaned (dry run)".to_string(),
                error: None,
                safety_violations,
            })
        } else {
            self.execute_cleanup_strategy(worktree, options, safety_violations)
                .await
        }
    }

    /// Execute the specific cleanup strategy
    async fn execute_cleanup_strategy(
        &self,
        worktree: &WorktreeInfo,
        options: &CleanupOptions,
        safety_violations: Vec<SafetyViolation>,
    ) -> Result<WorktreeCleanupResult> {
        match options.strategy {
            CleanupStrategy::Discard => {
                self.execute_discard_strategy(worktree, options, safety_violations)
                    .await
            }
            CleanupStrategy::MergeToFeature => {
                self.execute_merge_to_feature_strategy(worktree, options, safety_violations)
                    .await
            }
            CleanupStrategy::BackupToOrigin => {
                self.execute_backup_to_origin_strategy(worktree, options, safety_violations)
                    .await
            }
            CleanupStrategy::StashAndDiscard => {
                self.execute_stash_and_discard_strategy(worktree, options, safety_violations)
                    .await
            }
        }
    }

    /// Execute discard strategy (simple removal)
    async fn execute_discard_strategy(
        &self,
        worktree: &WorktreeInfo,
        _options: &CleanupOptions,
        safety_violations: Vec<SafetyViolation>,
    ) -> Result<WorktreeCleanupResult> {
        let remove_options = RemoveOptions {
            target: worktree.branch.clone(),
            force: true, // We've already done safety checks
            delete_branch: self.config.cleanup.auto_delete_branch,
        };

        match self.operations.remove_worktree(remove_options).await {
            Ok(_) => Ok(WorktreeCleanupResult {
                path: worktree.path.clone(),
                branch: worktree.branch.clone(),
                action: CleanupAction::Cleaned,
                reason: "Worktree removed".to_string(),
                error: None,
                safety_violations,
            }),
            Err(e) => Ok(WorktreeCleanupResult {
                path: worktree.path.clone(),
                branch: worktree.branch.clone(),
                action: CleanupAction::Failed,
                reason: "Failed to remove worktree".to_string(),
                error: Some(e.to_string()),
                safety_violations,
            }),
        }
    }

    /// Execute merge to feature branch strategy
    async fn execute_merge_to_feature_strategy(
        &self,
        worktree: &WorktreeInfo,
        _options: &CleanupOptions,
        safety_violations: Vec<SafetyViolation>,
    ) -> Result<WorktreeCleanupResult> {
        // Determine the target feature branch (remove worktree prefix)
        let feature_branch = self.extract_feature_branch_name(&worktree.branch)?;

        // Ensure target branch exists
        if !self.branch_exists(&feature_branch).await? {
            return Ok(WorktreeCleanupResult {
                path: worktree.path.clone(),
                branch: worktree.branch.clone(),
                action: CleanupAction::Failed,
                reason: format!("Target feature branch '{}' does not exist", feature_branch),
                error: None,
                safety_violations,
            });
        }

        // Perform the merge
        match self
            .merge_worktree_to_branch(worktree, &feature_branch)
            .await
        {
            Ok(merge_result) => {
                if merge_result.has_conflicts {
                    // Don't remove worktree if there are conflicts
                    Ok(WorktreeCleanupResult {
                        path: worktree.path.clone(),
                        branch: worktree.branch.clone(),
                        action: CleanupAction::Failed,
                        reason: format!(
                            "Merge conflicts detected: {}",
                            merge_result.conflict_summary
                        ),
                        error: None,
                        safety_violations,
                    })
                } else {
                    // Merge successful, remove worktree
                    let remove_options = RemoveOptions {
                        target: worktree.branch.clone(),
                        force: true,
                        delete_branch: true, // Remove the worktree branch after successful merge
                    };

                    self.operations.remove_worktree(remove_options).await?;

                    Ok(WorktreeCleanupResult {
                        path: worktree.path.clone(),
                        branch: worktree.branch.clone(),
                        action: CleanupAction::MergedToFeature,
                        reason: format!("Merged to '{}' and cleaned", feature_branch),
                        error: None,
                        safety_violations,
                    })
                }
            }
            Err(e) => Ok(WorktreeCleanupResult {
                path: worktree.path.clone(),
                branch: worktree.branch.clone(),
                action: CleanupAction::Failed,
                reason: "Failed to merge to feature branch".to_string(),
                error: Some(e.to_string()),
                safety_violations,
            }),
        }
    }

    /// Execute backup to origin strategy
    async fn execute_backup_to_origin_strategy(
        &self,
        worktree: &WorktreeInfo,
        _options: &CleanupOptions,
        safety_violations: Vec<SafetyViolation>,
    ) -> Result<WorktreeCleanupResult> {
        // Push the branch to origin
        match self.push_branch_to_origin(worktree).await {
            Ok(_) => {
                // After successful backup, remove the worktree
                let remove_options = RemoveOptions {
                    target: worktree.branch.clone(),
                    force: true,
                    delete_branch: false, // Keep the branch since it's backed up
                };

                self.operations.remove_worktree(remove_options).await?;

                Ok(WorktreeCleanupResult {
                    path: worktree.path.clone(),
                    branch: worktree.branch.clone(),
                    action: CleanupAction::BackedUpToOrigin,
                    reason: "Backed up to origin and cleaned".to_string(),
                    error: None,
                    safety_violations,
                })
            }
            Err(e) => Ok(WorktreeCleanupResult {
                path: worktree.path.clone(),
                branch: worktree.branch.clone(),
                action: CleanupAction::Failed,
                reason: "Failed to backup to origin".to_string(),
                error: Some(e.to_string()),
                safety_violations,
            }),
        }
    }

    /// Execute stash and discard strategy
    async fn execute_stash_and_discard_strategy(
        &self,
        worktree: &WorktreeInfo,
        _options: &CleanupOptions,
        safety_violations: Vec<SafetyViolation>,
    ) -> Result<WorktreeCleanupResult> {
        // Create a stash with uncommitted changes
        let stash_name = format!(
            "vibe-cleanup-{}-{}",
            worktree.branch,
            chrono::Utc::now().format("%Y%m%d-%H%M%S")
        );

        let stash_result = self.create_stash(worktree, &stash_name).await;

        match stash_result {
            Ok(stash_created) => {
                // Remove the worktree
                let remove_options = RemoveOptions {
                    target: worktree.branch.clone(),
                    force: true,
                    delete_branch: self.config.cleanup.auto_delete_branch,
                };

                match self.operations.remove_worktree(remove_options).await {
                    Ok(_) => {
                        let reason = if stash_created {
                            format!("Stashed changes as '{}' and cleaned", stash_name)
                        } else {
                            "No changes to stash, worktree cleaned".to_string()
                        };

                        Ok(WorktreeCleanupResult {
                            path: worktree.path.clone(),
                            branch: worktree.branch.clone(),
                            action: CleanupAction::StashCreated,
                            reason,
                            error: None,
                            safety_violations,
                        })
                    }
                    Err(e) => Ok(WorktreeCleanupResult {
                        path: worktree.path.clone(),
                        branch: worktree.branch.clone(),
                        action: CleanupAction::Failed,
                        reason: "Stash created but failed to remove worktree".to_string(),
                        error: Some(e.to_string()),
                        safety_violations,
                    }),
                }
            }
            Err(e) => Ok(WorktreeCleanupResult {
                path: worktree.path.clone(),
                branch: worktree.branch.clone(),
                action: CleanupAction::Failed,
                reason: "Failed to create stash".to_string(),
                error: Some(e.to_string()),
                safety_violations,
            }),
        }
    }

    // Helper methods for safety checks and operations

    async fn check_safety_violations(
        &self,
        worktree: &WorktreeInfo,
        options: &CleanupOptions,
    ) -> Vec<SafetyViolation> {
        let mut violations = Vec::new();

        // Check age threshold
        if let Some(min_hours) = options
            .min_age_hours
            .or(Some(self.config.cleanup.age_threshold_hours))
        {
            let min_age = Duration::from_secs(min_hours * 3600);
            if worktree.age < min_age {
                violations.push(SafetyViolation {
                    violation_type: SafetyViolationType::BranchTooNew,
                    description: format!(
                        "Worktree is only {} old (minimum: {} hours)",
                        format_duration(worktree.age),
                        min_hours
                    ),
                    severity: ViolationSeverity::Warning,
                });
            }
        }

        // Check for uncommitted changes
        if !worktree.status.uncommitted_changes.is_empty()
            || !worktree.status.untracked_files.is_empty()
        {
            violations.push(SafetyViolation {
                violation_type: SafetyViolationType::UncommittedChanges,
                description: format!(
                    "{} uncommitted changes, {} untracked files",
                    worktree.status.uncommitted_changes.len(),
                    worktree.status.untracked_files.len()
                ),
                severity: ViolationSeverity::Warning,
            });
        }

        // Check for unpushed commits
        if !worktree.status.unpushed_commits.is_empty() {
            violations.push(SafetyViolation {
                violation_type: SafetyViolationType::UnpushedCommits,
                description: format!(
                    "{} unpushed commits",
                    worktree.status.unpushed_commits.len()
                ),
                severity: ViolationSeverity::Warning,
            });
        }

        // Check merge confidence if filtering by merged branches
        if options.merged_only {
            if let Some(merge_info) = &worktree.status.merge_info {
                if !merge_info.is_merged {
                    violations.push(SafetyViolation {
                        violation_type: SafetyViolationType::LowMergeConfidence,
                        description: "Branch does not appear to be merged".to_string(),
                        severity: ViolationSeverity::Critical,
                    });
                } else if merge_info.confidence < options.min_merge_confidence {
                    violations.push(SafetyViolation {
                        violation_type: SafetyViolationType::LowMergeConfidence,
                        description: format!(
                            "Merge confidence too low: {:.0}% (minimum: {:.0}%)",
                            merge_info.confidence * 100.0,
                            options.min_merge_confidence * 100.0
                        ),
                        severity: ViolationSeverity::Warning,
                    });
                }
            } else {
                violations.push(SafetyViolation {
                    violation_type: SafetyViolationType::LowMergeConfidence,
                    description: "No merge information available".to_string(),
                    severity: ViolationSeverity::Critical,
                });
            }
        }

        // Check if worktree is currently in use (current working directory)
        if let Ok(current_dir) = std::env::current_dir() {
            if current_dir.starts_with(&worktree.path) {
                violations.push(SafetyViolation {
                    violation_type: SafetyViolationType::WorktreeInUse,
                    description: "Worktree is currently in use (current directory)".to_string(),
                    severity: ViolationSeverity::Critical,
                });
            }
        }

        violations
    }

    async fn is_main_repository_worktree(&self, worktree: &WorktreeInfo) -> Result<bool> {
        // Check if this worktree is the main repository (contains .git directory)
        Ok(worktree.path.join(".git").is_dir())
    }

    fn matches_filters(&self, worktree: &WorktreeInfo, options: &CleanupOptions) -> bool {
        // Check branch prefix filter
        if let Some(ref prefix) = options.branch_prefix_filter {
            if !worktree.branch.starts_with(prefix) {
                return false;
            }
        }

        true
    }

    async fn confirm_cleanup(
        &self,
        worktree: &WorktreeInfo,
        options: &CleanupOptions,
        violations: &[SafetyViolation],
    ) -> Result<bool> {
        println!(
            "{} Cleanup worktree: {}",
            "?".yellow(),
            worktree.branch.cyan()
        );
        println!("  Path: {}", worktree.path.display().to_string().blue());
        println!("  Strategy: {:?}", options.strategy);

        if !violations.is_empty() {
            println!("  {} Safety concerns:", "⚠️".yellow());
            for violation in violations {
                let severity_icon = match violation.severity {
                    ViolationSeverity::Warning => "⚠️",
                    ViolationSeverity::Critical => "🚨",
                };
                println!("    {} {}", severity_icon, violation.description);
            }
        }

        use std::io::{self, Write};
        print!("  Proceed? (y/N): ");
        io::stdout().flush()?;

        let mut input = String::new();
        io::stdin().read_line(&mut input)?;

        Ok(input.trim().to_lowercase() == "y" || input.trim().to_lowercase() == "yes")
    }

    // Strategy implementation helpers

    fn extract_feature_branch_name(&self, worktree_branch: &str) -> Result<String> {
        if let Some(suffix) = worktree_branch.strip_prefix(&self.config.prefix) {
            Ok(suffix.to_string())
        } else {
            bail!(
                "Branch '{}' does not have expected prefix '{}'",
                worktree_branch,
                self.config.prefix
            );
        }
    }

    async fn branch_exists(&self, branch_name: &str) -> Result<bool> {
        let output = Command::new("git")
            .args(&[
                "show-ref",
                "--verify",
                "--quiet",
                &format!("refs/heads/{}", branch_name),
            ])
            .output()
            .await?;

        Ok(output.status.success())
    }

    async fn merge_worktree_to_branch(
        &self,
        worktree: &WorktreeInfo,
        target_branch: &str,
    ) -> Result<MergeResult> {
        // Switch to target branch
        let checkout_output = Command::new("git")
            .args(&["checkout", target_branch])
            .current_dir(&worktree.path.parent().unwrap_or(&worktree.path))
            .output()
            .await?;

        if !checkout_output.status.success() {
            bail!(
                "Failed to checkout target branch: {}",
                String::from_utf8_lossy(&checkout_output.stderr)
            );
        }

        // Attempt merge
        let merge_output = Command::new("git")
            .args(&["merge", &worktree.branch])
            .current_dir(&worktree.path.parent().unwrap_or(&worktree.path))
            .output()
            .await?;

        if merge_output.status.success() {
            Ok(MergeResult {
                success: true,
                has_conflicts: false,
                conflict_summary: String::new(),
            })
        } else {
            // Check if it's a merge conflict
            let stderr = String::from_utf8_lossy(&merge_output.stderr);
            if stderr.contains("conflict") || stderr.contains("CONFLICT") {
                let conflict_summary = self.get_merge_conflict_summary(&worktree.path).await?;
                Ok(MergeResult {
                    success: false,
                    has_conflicts: true,
                    conflict_summary,
                })
            } else {
                bail!("Merge failed: {}", stderr);
            }
        }
    }

    async fn get_merge_conflict_summary(&self, worktree_path: &Path) -> Result<String> {
        let output = Command::new("git")
            .args(&["diff", "--name-only", "--diff-filter=U"])
            .current_dir(worktree_path)
            .output()
            .await?;

        if output.status.success() {
            let conflicted_files = String::from_utf8_lossy(&output.stdout);
            let file_count = conflicted_files.lines().count();
            Ok(format!("{} conflicted files", file_count))
        } else {
            Ok("Unknown conflicts".to_string())
        }
    }

    async fn push_branch_to_origin(&self, worktree: &WorktreeInfo) -> Result<()> {
        let output = Command::new("git")
            .args(&["push", "origin", &worktree.branch])
            .current_dir(&worktree.path)
            .output()
            .await?;

        if output.status.success() {
            Ok(())
        } else {
            let stderr = String::from_utf8_lossy(&output.stderr);
            bail!("Failed to push to origin: {}", stderr);
        }
    }

    async fn create_stash(&self, worktree: &WorktreeInfo, stash_name: &str) -> Result<bool> {
        let output = Command::new("git")
            .args(&["stash", "push", "-m", stash_name])
            .current_dir(&worktree.path)
            .output()
            .await?;

        if output.status.success() {
            let stdout = String::from_utf8_lossy(&output.stdout);
            // Git returns different messages if there's nothing to stash
            Ok(!stdout.contains("No local changes to save"))
        } else {
            let stderr = String::from_utf8_lossy(&output.stderr);
            bail!("Failed to create stash: {}", stderr);
        }
    }
}

#[derive(Debug)]
struct MergeResult {
    #[allow(dead_code)]
    success: bool,
    has_conflicts: bool,
    conflict_summary: String,
}

impl Default for CleanupOptions {
    fn default() -> Self {
        Self {
            strategy: CleanupStrategy::Discard,
            min_age_hours: Some(24),
            force: false,
            dry_run: false,
            auto_confirm: false,
            branch_prefix_filter: None,
            merged_only: false,
            min_merge_confidence: 0.8,
        }
    }
}

/// Format a duration for human-readable display
fn format_duration(duration: Duration) -> String {
    let hours = duration.as_secs() / 3600;
    let days = hours / 24;

    if days > 0 {
        format!("{} days", days)
    } else if hours > 0 {
        format!("{} hours", hours)
    } else {
        format!("{} minutes", duration.as_secs() / 60)
    }
}

/// Convenience function to create cleanup options for merged worktrees only
pub fn merged_worktrees_cleanup_options() -> CleanupOptions {
    CleanupOptions {
        merged_only: true,
        min_merge_confidence: 0.7,
        ..Default::default()
    }
}

/// Convenience function to create cleanup options for old worktrees
pub fn old_worktrees_cleanup_options(min_age_days: u64) -> CleanupOptions {
    CleanupOptions {
        min_age_hours: Some(min_age_days * 24),
        ..Default::default()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cleanup_options_defaults() {
        let options = CleanupOptions::default();
        assert_eq!(options.strategy, CleanupStrategy::Discard);
        assert_eq!(options.min_age_hours, Some(24));
        assert!(!options.force);
        assert!(!options.dry_run);
    }

    #[test]
    fn test_format_duration() {
        let minutes = Duration::from_secs(30 * 60);
        let hours = Duration::from_secs(5 * 3600);
        let days = Duration::from_secs(3 * 24 * 3600);

        assert_eq!(format_duration(minutes), "30 minutes");
        assert_eq!(format_duration(hours), "5 hours");
        assert_eq!(format_duration(days), "3 days");
    }

    #[test]
    fn test_safety_violation_severity() {
        let warning = SafetyViolation {
            violation_type: SafetyViolationType::UncommittedChanges,
            description: "test".to_string(),
            severity: ViolationSeverity::Warning,
        };

        let critical = SafetyViolation {
            violation_type: SafetyViolationType::WorktreeInUse,
            description: "test".to_string(),
            severity: ViolationSeverity::Critical,
        };

        assert_eq!(warning.severity, ViolationSeverity::Warning);
        assert_eq!(critical.severity, ViolationSeverity::Critical);
    }
}