Skip to main content

git_workflow/commands/
home.rs

1//! `gw home` command - Switch to home branch and sync with origin/main
2
3use super::helpers;
4use crate::error::{GwError, Result};
5use crate::git;
6use crate::output;
7use crate::state::RepoType;
8
9/// Execute the `home` command
10pub fn run(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    // Check for detached HEAD
17    if git::is_detached_head() {
18        return Err(GwError::Other(
19            "Cannot run from detached HEAD. Checkout a branch first.".to_string(),
20        ));
21    }
22
23    let repo_type = RepoType::detect()?;
24    let home_branch = repo_type.home_branch();
25    let current = git::current_branch()?;
26
27    println!();
28    output::info(&format!("Home branch: {}", output::bold(home_branch)));
29
30    // Fetch latest
31    output::info("Fetching from origin...");
32    git::fetch_prune(verbose)?;
33    output::success("Fetched (stale remote branches pruned)");
34
35    // Detect default remote branch (origin/main or origin/master)
36    let default_remote = git::get_default_remote_branch()?;
37    let default_branch = default_remote.strip_prefix("origin/").unwrap_or("main");
38
39    // Switch to home branch if needed
40    if current != home_branch {
41        if !git::branch_exists(home_branch) {
42            output::info(&format!("Creating home branch from {}...", default_remote));
43            git::checkout_new_branch(home_branch, &default_remote, verbose)?;
44            output::success(&format!(
45                "Created and switched to {}",
46                output::bold(home_branch)
47            ));
48        } else {
49            git::checkout(home_branch, verbose)?;
50            output::success(&format!("Switched to {}", output::bold(home_branch)));
51        }
52    } else {
53        output::success(&format!("Already on {}", output::bold(home_branch)));
54    }
55
56    // Sync with default remote branch
57    helpers::pull_with_output(&default_remote, default_branch, verbose)?;
58
59    output::ready("Ready", home_branch);
60    output::hints(&["mise run git:new feature/your-feature  # Create new branch"]);
61
62    Ok(())
63}