use super::helpers;
use crate::error::{GwError, Result};
use crate::git;
use crate::output;
use crate::state::RepoType;
pub fn run(verbose: bool) -> Result<()> {
if !git::is_git_repo() {
return Err(GwError::NotAGitRepository);
}
if git::is_detached_head() {
return Err(GwError::Other(
"Cannot run from detached HEAD. Checkout a branch first.".to_string(),
));
}
let repo_type = RepoType::detect()?;
let home_branch = repo_type.home_branch();
let current = git::current_branch()?;
println!();
output::info(&format!("Home branch: {}", output::bold(home_branch)));
output::info("Fetching from origin...");
git::fetch_prune(verbose)?;
output::success("Fetched (stale remote branches pruned)");
let default_remote = git::get_default_remote_branch()?;
let default_branch = default_remote.strip_prefix("origin/").unwrap_or("main");
if current != home_branch {
if !git::branch_exists(home_branch) {
output::info(&format!("Creating home branch from {}...", default_remote));
git::checkout_new_branch(home_branch, &default_remote, verbose)?;
output::success(&format!(
"Created and switched to {}",
output::bold(home_branch)
));
} else {
git::checkout(home_branch, verbose)?;
output::success(&format!("Switched to {}", output::bold(home_branch)));
}
} else {
output::success(&format!("Already on {}", output::bold(home_branch)));
}
helpers::pull_with_output(&default_remote, default_branch, verbose)?;
output::ready("Ready", home_branch);
output::hints(&["mise run git:new feature/your-feature # Create new branch"]);
Ok(())
}