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 "main")
10    MainRepo,
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            Ok(RepoType::MainRepo)
23        }
24    }
25
26    /// Get the home branch for this repository type
27    pub fn home_branch(&self) -> &str {
28        match self {
29            RepoType::MainRepo => "main",
30            RepoType::Worktree { home_branch } => home_branch,
31        }
32    }
33
34    /// Check if a branch is protected (cannot be deleted)
35    pub fn is_protected(&self, branch: &str) -> bool {
36        // Always protect main and master
37        if branch == "main" || branch == "master" {
38            return true;
39        }
40        // Protect the home branch of this repo
41        branch == self.home_branch()
42    }
43}