Skip to main content

git_workflow/commands/
home.rs

1//! `gw home` command - Switch to home branch and sync with origin/main
2
3use crate::error::{GwError, Result};
4use crate::git;
5use crate::output;
6use crate::state::RepoType;
7
8/// Execute the `home` command
9pub fn run(verbose: bool) -> Result<()> {
10    // Ensure we're in a git repo
11    if !git::is_git_repo() {
12        return Err(GwError::NotAGitRepository);
13    }
14
15    let repo_type = RepoType::detect()?;
16    let home_branch = repo_type.home_branch();
17    let current = git::current_branch()?;
18
19    println!();
20    output::info(&format!("Home branch: {}", output::bold(home_branch)));
21
22    // Fetch latest
23    output::info("Fetching from origin...");
24    git::fetch_prune(verbose)?;
25    output::success("Fetched (stale remote branches pruned)");
26
27    // Switch to home branch if needed
28    if current != home_branch {
29        if !git::branch_exists(home_branch) {
30            output::info("Creating home branch from origin/main...");
31            git::checkout_new_branch(home_branch, "origin/main", verbose)?;
32            output::success(&format!(
33                "Created and switched to {}",
34                output::bold(home_branch)
35            ));
36        } else {
37            git::checkout(home_branch, verbose)?;
38            output::success(&format!("Switched to {}", output::bold(home_branch)));
39        }
40    } else {
41        output::success(&format!("Already on {}", output::bold(home_branch)));
42    }
43
44    // Sync with origin/main
45    output::info("Syncing with origin/main...");
46    let before = git::head_commit()?;
47    git::pull("origin", "main", verbose)?;
48    let after = git::head_commit()?;
49
50    if before != after {
51        let count = git::commit_count(&before, &after)?;
52        output::success(&format!(
53            "Pulled {} commit(s) from origin/main",
54            output::bold(&count.to_string())
55        ));
56    } else {
57        output::success("Already up to date with origin/main");
58    }
59
60    output::ready("Ready", home_branch);
61    output::hints(&["mise run git:new feature/your-feature  # Create new branch"]);
62
63    Ok(())
64}