Skip to main content

git_workflow/commands/
pause.rs

1//! `gw pause` command - Pause current work by creating a WIP commit and returning to home
2
3use crate::error::{GwError, Result};
4use crate::git;
5use crate::github;
6use crate::output;
7use crate::state::{RepoType, WorkingDirState};
8
9/// Execute the `pause` command
10pub fn run(message: Option<String>, verbose: bool) -> Result<()> {
11    // Ensure we're in a git repo
12    if !git::is_git_repo() {
13        return Err(GwError::NotAGitRepository);
14    }
15
16    let repo_type = RepoType::detect()?;
17    let home_branch = repo_type.home_branch();
18    let current = git::current_branch()?;
19    let working_dir = WorkingDirState::detect();
20
21    println!();
22
23    // If on home branch, just inform
24    if current == home_branch {
25        output::info(&format!("Already on home branch '{}'", home_branch));
26        if !working_dir.is_clean() {
27            output::warn(
28                "You have uncommitted changes. Consider committing or using 'gw new <branch>'.",
29            );
30        }
31        return Ok(());
32    }
33
34    // Create WIP commit if there are changes
35    if !working_dir.is_clean() {
36        let commit_message = match &message {
37            Some(msg) => format!("WIP: {}", msg),
38            None => "WIP: paused".to_string(),
39        };
40
41        output::info("Creating WIP commit...");
42        git::add_all(verbose)?;
43        git::commit(&commit_message, verbose)?;
44        output::success(&format!("Created: {}", commit_message));
45
46        // Try to add PR comment if message provided
47        if let Some(msg) = &message {
48            if github::is_gh_available() {
49                if let Ok(Some(pr)) = github::get_pr_for_branch(&current) {
50                    let comment = format!("⏸️ Paused work on this PR: {}", msg);
51                    match github::add_pr_comment(pr.number, &comment) {
52                        Ok(()) => output::success(&format!("Added comment to PR #{}", pr.number)),
53                        Err(e) => output::warn(&format!("Could not add PR comment: {}", e)),
54                    }
55                }
56            }
57        }
58    } else {
59        output::info("Working directory is clean. No WIP commit needed.");
60    }
61
62    // Switch to home branch
63    output::info("Switching to home branch...");
64    git::fetch_prune(verbose)?;
65
66    // Detect default remote branch
67    let default_remote = git::get_default_remote_branch()?;
68    let default_branch = default_remote.strip_prefix("origin/").unwrap_or("main");
69
70    if !git::branch_exists(home_branch) {
71        git::checkout_new_branch(home_branch, &default_remote, verbose)?;
72    } else {
73        git::checkout(home_branch, verbose)?;
74    }
75
76    // Sync with default remote
77    git::pull("origin", default_branch, verbose)?;
78    output::success(&format!(
79        "Switched to {} and synced",
80        output::bold(home_branch)
81    ));
82
83    output::ready("Paused", home_branch);
84    output::hints(&[
85        &format!("git checkout {}  # Return to paused branch", current),
86        "gw undo              # Undo WIP commit after checkout",
87        "gw new <branch>      # Start different work",
88    ]);
89
90    Ok(())
91}