use crate::error::{GwError, Result};
use crate::git;
use crate::github;
use crate::output;
use crate::state::{RepoType, WorkingDirState};
pub fn run(message: Option<String>, verbose: bool) -> Result<()> {
if !git::is_git_repo() {
return Err(GwError::NotAGitRepository);
}
let repo_type = RepoType::detect()?;
let home_branch = repo_type.home_branch();
let current = git::current_branch()?;
let working_dir = WorkingDirState::detect();
println!();
if current == home_branch {
output::info(&format!("Already on home branch '{}'", home_branch));
if !working_dir.is_clean() {
output::warn(
"You have uncommitted changes. Consider committing or using 'gw new <branch>'.",
);
}
return Ok(());
}
if !working_dir.is_clean() {
let commit_message = match &message {
Some(msg) => format!("WIP: {}", msg),
None => "WIP: paused".to_string(),
};
output::info("Creating WIP commit...");
git::add_all(verbose)?;
git::commit(&commit_message, verbose)?;
output::success(&format!("Created: {}", commit_message));
if let Some(msg) = &message {
if github::is_gh_available() {
if let Ok(Some(pr)) = github::get_pr_for_branch(¤t) {
let comment = format!("⏸️ Paused work on this PR: {}", msg);
match github::add_pr_comment(pr.number, &comment) {
Ok(()) => output::success(&format!("Added comment to PR #{}", pr.number)),
Err(e) => output::warn(&format!("Could not add PR comment: {}", e)),
}
}
}
}
} else {
output::info("Working directory is clean. No WIP commit needed.");
}
output::info("Switching to home branch...");
git::fetch_prune(verbose)?;
let default_remote = git::get_default_remote_branch()?;
let default_branch = default_remote.strip_prefix("origin/").unwrap_or("main");
if !git::branch_exists(home_branch) {
git::checkout_new_branch(home_branch, &default_remote, verbose)?;
} else {
git::checkout(home_branch, verbose)?;
}
git::pull("origin", default_branch, verbose)?;
output::success(&format!(
"Switched to {} and synced",
output::bold(home_branch)
));
output::ready("Paused", home_branch);
output::hints(&[
&format!("git checkout {} # Return to paused branch", current),
"gw undo # Undo WIP commit after checkout",
"gw new <branch> # Start different work",
]);
Ok(())
}