git_workflow/commands/
pause.rs1use crate::error::{GwError, Result};
4use crate::git;
5use crate::github;
6use crate::output;
7use crate::state::{RepoType, WorkingDirState};
8
9pub fn run(message: Option<String>, verbose: bool) -> Result<()> {
11 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 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 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 if let Some(msg) = &message {
48 if github::is_gh_available() {
49 if let Ok(Some(pr)) = github::get_pr_for_branch(¤t) {
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 output::info("Switching to home branch...");
64 git::fetch_prune(verbose)?;
65
66 if !git::branch_exists(home_branch) {
67 git::checkout_new_branch(home_branch, "origin/main", verbose)?;
68 } else {
69 git::checkout(home_branch, verbose)?;
70 }
71
72 git::pull("origin", "main", verbose)?;
74 output::success(&format!(
75 "Switched to {} and synced",
76 output::bold(home_branch)
77 ));
78
79 output::ready("Paused", home_branch);
80 output::hints(&[
81 &format!("git checkout {} # Return to paused branch", current),
82 "gw undo # Undo WIP commit after checkout",
83 "gw new <branch> # Start different work",
84 ]);
85
86 Ok(())
87}