cascade-cli 0.1.152

Stacked diffs CLI for Bitbucket Server
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
use crate::errors::{CascadeError, Result};
use crate::git::GitRepository;
use crate::stack::{Stack, StackManager};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use tracing::{debug, info, warn};

/// Information about a branch that can be cleaned up
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CleanupCandidate {
    /// Branch name
    pub branch_name: String,
    /// Stack entry ID if this branch is part of a stack
    pub entry_id: Option<uuid::Uuid>,
    /// Stack ID if this branch is part of a stack
    pub stack_id: Option<uuid::Uuid>,
    /// Whether the branch is merged to the base branch
    pub is_merged: bool,
    /// Whether the branch has a remote tracking branch
    pub has_remote: bool,
    /// Whether the branch is the current branch
    pub is_current: bool,
    /// Reason this branch is a cleanup candidate
    pub reason: CleanupReason,
    /// Additional safety information
    pub safety_info: String,
}

impl CleanupCandidate {
    /// Get a human-readable string for the cleanup reason
    pub fn reason_to_string(&self) -> &str {
        match self.reason {
            CleanupReason::FullyMerged => "fully merged",
            CleanupReason::StackEntryMerged => "PR merged",
            CleanupReason::Stale => "stale",
            CleanupReason::Orphaned => "orphaned",
        }
    }
}

/// Reason why a branch is a cleanup candidate
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum CleanupReason {
    /// Branch is fully merged to base branch
    FullyMerged,
    /// Stack entry was merged via PR
    StackEntryMerged,
    /// Branch is stale (old and no recent activity)
    Stale,
    /// Branch is a duplicate or orphaned branch
    Orphaned,
}

/// Options for cleanup operations
#[derive(Debug, Clone)]
pub struct CleanupOptions {
    /// Whether to run in dry-run mode (don't actually delete)
    pub dry_run: bool,
    /// Whether to skip confirmation prompts
    pub force: bool,
    /// Whether to include stale branches in cleanup
    pub include_stale: bool,
    /// Whether to cleanup remote tracking branches
    pub cleanup_remote: bool,
    /// Age threshold for stale branches (days)
    pub stale_threshold_days: u32,
    /// Whether to cleanup branches not in any stack
    pub cleanup_non_stack: bool,
}

/// Result of cleanup operation
#[derive(Debug, Clone)]
pub struct CleanupResult {
    /// Branches that were successfully cleaned up
    pub cleaned_branches: Vec<String>,
    /// Branches that failed to be cleaned up
    pub failed_branches: Vec<(String, String)>, // (branch_name, error)
    /// Branches that were skipped
    pub skipped_branches: Vec<(String, String)>, // (branch_name, reason)
    /// Total number of candidates found
    pub total_candidates: usize,
}

/// Manages cleanup operations for merged and stale branches
pub struct CleanupManager {
    stack_manager: StackManager,
    git_repo: GitRepository,
    options: CleanupOptions,
}

impl Default for CleanupOptions {
    fn default() -> Self {
        Self {
            dry_run: false,
            force: false,
            include_stale: false,
            cleanup_remote: false,
            stale_threshold_days: 30,
            cleanup_non_stack: false,
        }
    }
}

impl CleanupManager {
    /// Create a new cleanup manager
    pub fn new(
        stack_manager: StackManager,
        git_repo: GitRepository,
        options: CleanupOptions,
    ) -> Self {
        Self {
            stack_manager,
            git_repo,
            options,
        }
    }

    /// Find all branches that are candidates for cleanup
    pub fn find_cleanup_candidates(&self) -> Result<Vec<CleanupCandidate>> {
        debug!("Scanning for cleanup candidates...");

        let mut candidates = Vec::new();
        let all_branches = self.git_repo.list_branches()?;
        let current_branch = self.git_repo.get_current_branch().ok();

        // Get all stacks to identify stack branches
        let stacks = self.stack_manager.get_all_stacks_objects()?;
        let mut stack_branches = HashSet::new();
        let mut stack_branch_to_entry = std::collections::HashMap::new();

        for stack in &stacks {
            for entry in &stack.entries {
                stack_branches.insert(entry.branch.clone());
                stack_branch_to_entry.insert(entry.branch.clone(), (stack.id, entry.id));
            }
        }

        for branch_name in &all_branches {
            // Skip the current branch for safety
            if current_branch.as_ref() == Some(branch_name) {
                continue;
            }

            // Skip protected branches
            if self.is_protected_branch(branch_name) {
                continue;
            }

            let is_current = current_branch.as_ref() == Some(branch_name);
            let has_remote = self.git_repo.get_upstream_branch(branch_name)?.is_some();

            // Check if this branch is part of a stack
            let (stack_id, entry_id) =
                if let Some((stack_id, entry_id)) = stack_branch_to_entry.get(branch_name) {
                    (Some(*stack_id), Some(*entry_id))
                } else {
                    (None, None)
                };

            // Check different cleanup reasons
            if let Some(candidate) = self.evaluate_branch_for_cleanup(
                branch_name,
                stack_id,
                entry_id,
                is_current,
                has_remote,
                &stacks,
            )? {
                candidates.push(candidate);
            }
        }

        debug!("Found {} cleanup candidates", candidates.len());
        Ok(candidates)
    }

    /// Evaluate a single branch for cleanup
    fn evaluate_branch_for_cleanup(
        &self,
        branch_name: &str,
        stack_id: Option<uuid::Uuid>,
        entry_id: Option<uuid::Uuid>,
        is_current: bool,
        has_remote: bool,
        stacks: &[Stack],
    ) -> Result<Option<CleanupCandidate>> {
        // First check if branch is fully merged
        if let Ok(base_branch) = self.get_base_branch_for_branch(branch_name, stack_id, stacks) {
            if self.is_branch_merged_to_base(branch_name, &base_branch)? {
                return Ok(Some(CleanupCandidate {
                    branch_name: branch_name.to_string(),
                    entry_id,
                    stack_id,
                    is_merged: true,
                    has_remote,
                    is_current,
                    reason: CleanupReason::FullyMerged,
                    safety_info: format!("Branch fully merged to '{base_branch}'"),
                }));
            }
        }

        // Never clean up active stack branches - they're in use!
        if stack_id.is_some() {
            return Ok(None);
        }

        // Check for stale branches (if enabled) - only for NON-stack branches
        if self.options.include_stale {
            if let Some(candidate) =
                self.check_stale_branch(branch_name, stack_id, entry_id, has_remote, is_current)?
            {
                return Ok(Some(candidate));
            }
        }

        // Check for orphaned branches (if enabled)
        if self.options.cleanup_non_stack {
            if let Some(candidate) =
                self.check_orphaned_branch(branch_name, has_remote, is_current)?
            {
                return Ok(Some(candidate));
            }
        }

        Ok(None)
    }

    /// Check if a branch is stale based on last activity
    fn check_stale_branch(
        &self,
        branch_name: &str,
        stack_id: Option<uuid::Uuid>,
        entry_id: Option<uuid::Uuid>,
        _has_remote: bool,
        _is_current: bool,
    ) -> Result<Option<CleanupCandidate>> {
        let last_commit_age = self.get_branch_last_commit_age_days(branch_name)?;

        if last_commit_age > self.options.stale_threshold_days {
            return Ok(Some(CleanupCandidate {
                branch_name: branch_name.to_string(),
                entry_id,
                stack_id,
                is_merged: false,
                has_remote: _has_remote,
                is_current: _is_current,
                reason: CleanupReason::Stale,
                safety_info: format!("No activity for {last_commit_age} days"),
            }));
        }

        Ok(None)
    }

    /// Check if a branch is orphaned (not part of any stack)
    fn check_orphaned_branch(
        &self,
        _branch_name: &str,
        _has_remote: bool,
        _is_current: bool,
    ) -> Result<Option<CleanupCandidate>> {
        // For now, we'll be conservative and not automatically clean up non-stack branches
        // Users can explicitly enable this with --cleanup-non-stack
        Ok(None)
    }

    /// Perform cleanup based on the candidates found
    pub fn perform_cleanup(&mut self, candidates: &[CleanupCandidate]) -> Result<CleanupResult> {
        let mut result = CleanupResult {
            cleaned_branches: Vec::new(),
            failed_branches: Vec::new(),
            skipped_branches: Vec::new(),
            total_candidates: candidates.len(),
        };

        if candidates.is_empty() {
            info!("No cleanup candidates found");
            return Ok(result);
        }

        info!("Processing {} cleanup candidates", candidates.len());

        for candidate in candidates {
            match self.cleanup_single_branch(candidate) {
                Ok(true) => {
                    result.cleaned_branches.push(candidate.branch_name.clone());
                    info!("✅ Cleaned up branch: {}", candidate.branch_name);
                }
                Ok(false) => {
                    result.skipped_branches.push((
                        candidate.branch_name.clone(),
                        "Skipped by user or safety check".to_string(),
                    ));
                    debug!("⏭️  Skipped branch: {}", candidate.branch_name);
                }
                Err(e) => {
                    result
                        .failed_branches
                        .push((candidate.branch_name.clone(), e.to_string()));
                    warn!(
                        "❌ Failed to clean up branch {}: {}",
                        candidate.branch_name, e
                    );
                }
            }
        }

        Ok(result)
    }

    /// Clean up a single branch
    fn cleanup_single_branch(&mut self, candidate: &CleanupCandidate) -> Result<bool> {
        debug!(
            "Cleaning up branch: {} ({:?})",
            candidate.branch_name, candidate.reason
        );

        // Safety check: don't delete current branch
        if candidate.is_current {
            return Ok(false);
        }

        // In dry-run mode, just report what would be done
        if self.options.dry_run {
            info!("DRY RUN: Would delete branch '{}'", candidate.branch_name);
            return Ok(true);
        }

        // Delete the branch
        match candidate.reason {
            CleanupReason::FullyMerged | CleanupReason::StackEntryMerged => {
                // Safe to delete merged branches
                self.git_repo.delete_branch(&candidate.branch_name)?;
            }
            CleanupReason::Stale | CleanupReason::Orphaned => {
                // Run through standard safety checks even for stale/orphaned branches
                // This prevents accidental deletion when unpushed commits are present.
                self.git_repo.delete_branch(&candidate.branch_name)?;
            }
        }

        // Remove from stack metadata if it's a stack branch
        if let (Some(stack_id), Some(entry_id)) = (candidate.stack_id, candidate.entry_id) {
            self.remove_entry_from_stack(stack_id, entry_id)?;
        }

        Ok(true)
    }

    /// Remove an entry from stack metadata
    fn remove_entry_from_stack(
        &mut self,
        stack_id: uuid::Uuid,
        entry_id: uuid::Uuid,
    ) -> Result<()> {
        debug!("Removing entry {} from stack {}", entry_id, stack_id);

        match self.stack_manager.remove_stack_entry(&stack_id, &entry_id) {
            Ok(Some(_entry)) => {
                if let Some(stack) = self.stack_manager.get_stack(&stack_id) {
                    if stack.entries.is_empty() {
                        info!("Stack '{}' is now empty after cleanup", stack.name);
                    }
                }
                Ok(())
            }
            Ok(None) => {
                warn!(
                    "Skip removing entry {} from stack {} (entry not found or still has dependents)",
                    entry_id, stack_id
                );
                Ok(())
            }
            Err(e) => Err(e),
        }
    }

    /// Check if a branch is merged to its base branch
    fn is_branch_merged_to_base(&self, branch_name: &str, base_branch: &str) -> Result<bool> {
        // Get the commits between base and the branch
        match self.git_repo.get_commits_between(base_branch, branch_name) {
            Ok(commits) => Ok(commits.is_empty()),
            Err(_) => {
                // If we can't determine, assume not merged for safety
                Ok(false)
            }
        }
    }

    /// Get the base branch for a given branch
    fn get_base_branch_for_branch(
        &self,
        _branch_name: &str,
        stack_id: Option<uuid::Uuid>,
        stacks: &[Stack],
    ) -> Result<String> {
        if let Some(stack_id) = stack_id {
            if let Some(stack) = stacks.iter().find(|s| s.id == stack_id) {
                return Ok(stack.base_branch.clone());
            }
        }

        // Default to main/master if not in a stack
        let main_branches = ["main", "master", "develop"];
        for branch in &main_branches {
            if self.git_repo.branch_exists(branch) {
                return Ok(branch.to_string());
            }
        }

        Err(CascadeError::config(
            "Could not determine base branch".to_string(),
        ))
    }

    /// Check if a branch is protected (shouldn't be deleted)
    fn is_protected_branch(&self, branch_name: &str) -> bool {
        let protected_branches = [
            "main",
            "master",
            "develop",
            "development",
            "staging",
            "production",
            "release",
        ];

        protected_branches.contains(&branch_name)
    }

    /// Get the age of the last commit on a branch in days
    fn get_branch_last_commit_age_days(&self, branch_name: &str) -> Result<u32> {
        let commit_hash = self.git_repo.get_branch_commit_hash(branch_name)?;
        let commit = self.git_repo.get_commit(&commit_hash)?;

        let now = std::time::SystemTime::now();
        let commit_time =
            std::time::UNIX_EPOCH + std::time::Duration::from_secs(commit.time().seconds() as u64);

        let age = now
            .duration_since(commit_time)
            .unwrap_or_else(|_| std::time::Duration::from_secs(0));

        Ok((age.as_secs() / 86400) as u32) // Convert to days
    }

    /// Get cleanup statistics
    pub fn get_cleanup_stats(&self) -> Result<CleanupStats> {
        let candidates = self.find_cleanup_candidates()?;

        let mut stats = CleanupStats {
            total_branches: self.git_repo.list_branches()?.len(),
            fully_merged: 0,
            stack_entry_merged: 0,
            stale: 0,
            orphaned: 0,
            protected: 0,
        };

        for candidate in &candidates {
            match candidate.reason {
                CleanupReason::FullyMerged => stats.fully_merged += 1,
                CleanupReason::StackEntryMerged => stats.stack_entry_merged += 1,
                CleanupReason::Stale => stats.stale += 1,
                CleanupReason::Orphaned => stats.orphaned += 1,
            }
        }

        // Count protected branches
        let all_branches = self.git_repo.list_branches()?;
        stats.protected = all_branches
            .iter()
            .filter(|branch| self.is_protected_branch(branch))
            .count();

        Ok(stats)
    }
}

/// Statistics about cleanup candidates
#[derive(Debug, Clone)]
pub struct CleanupStats {
    pub total_branches: usize,
    pub fully_merged: usize,
    pub stack_entry_merged: usize,
    pub stale: usize,
    pub orphaned: usize,
    pub protected: usize,
}

impl CleanupStats {
    pub fn cleanup_candidates(&self) -> usize {
        self.fully_merged + self.stack_entry_merged + self.stale + self.orphaned
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::process::Command;
    use tempfile::TempDir;

    fn create_test_repo() -> (TempDir, std::path::PathBuf) {
        let temp_dir = TempDir::new().unwrap();
        let repo_path = temp_dir.path().to_path_buf();

        // Initialize git repository
        Command::new("git")
            .args(["init"])
            .current_dir(&repo_path)
            .output()
            .unwrap();

        Command::new("git")
            .args(["config", "user.name", "Test"])
            .current_dir(&repo_path)
            .output()
            .unwrap();

        Command::new("git")
            .args(["config", "user.email", "test@example.com"])
            .current_dir(&repo_path)
            .output()
            .unwrap();

        (temp_dir, repo_path)
    }

    #[test]
    fn test_cleanup_reason_serialization() {
        let reason = CleanupReason::FullyMerged;
        let serialized = serde_json::to_string(&reason).unwrap();
        let deserialized: CleanupReason = serde_json::from_str(&serialized).unwrap();
        assert_eq!(reason, deserialized);
    }

    #[test]
    fn test_cleanup_options_default() {
        let options = CleanupOptions::default();
        assert!(!options.dry_run);
        assert!(!options.force);
        assert!(!options.include_stale);
        assert_eq!(options.stale_threshold_days, 30);
    }

    #[test]
    fn test_protected_branches() {
        let (_temp_dir, repo_path) = create_test_repo();
        let git_repo = crate::git::GitRepository::open(&repo_path).unwrap();
        let stack_manager = crate::stack::StackManager::new(&repo_path).unwrap();
        let options = CleanupOptions::default();

        let cleanup_manager = CleanupManager::new(stack_manager, git_repo, options);

        assert!(cleanup_manager.is_protected_branch("main"));
        assert!(cleanup_manager.is_protected_branch("master"));
        assert!(cleanup_manager.is_protected_branch("develop"));
        assert!(!cleanup_manager.is_protected_branch("feature-branch"));
    }

    #[test]
    fn test_cleanup_stats() {
        let stats = CleanupStats {
            total_branches: 10,
            fully_merged: 3,
            stack_entry_merged: 2,
            stale: 1,
            orphaned: 0,
            protected: 4,
        };

        assert_eq!(stats.cleanup_candidates(), 6);
    }
}