Skip to main content

git_workflow/state/
repo.rs

1//! Repository type detection (main repo vs worktree)
2
3use crate::error::Result;
4use crate::git;
5
6/// Type of git repository
7#[derive(Debug, Clone)]
8pub enum RepoType {
9    /// Main repository (home branch is the repo's default branch, e.g. main/master)
10    MainRepo { home_branch: String },
11    /// Worktree (home branch is directory name)
12    Worktree { home_branch: String },
13}
14
15impl RepoType {
16    /// Detect the current repository type
17    pub fn detect() -> Result<Self> {
18        if git::is_worktree()? {
19            let home_branch = git::current_dir_name()?;
20            Ok(RepoType::Worktree { home_branch })
21        } else {
22            // The main repo's home is the default branch — main OR master.
23            let home_branch = git::default_branch_name()?;
24            Ok(RepoType::MainRepo { home_branch })
25        }
26    }
27
28    /// Get the home branch for this repository type
29    pub fn home_branch(&self) -> &str {
30        match self {
31            RepoType::MainRepo { home_branch } => home_branch,
32            RepoType::Worktree { home_branch } => home_branch,
33        }
34    }
35
36    /// Check if a branch is protected (cannot be deleted)
37    pub fn is_protected(&self, branch: &str) -> bool {
38        // Always protect main and master
39        if branch == "main" || branch == "master" {
40            return true;
41        }
42        // Protect the home branch of this repo
43        branch == self.home_branch()
44    }
45}