1use std::borrow::Cow;
4use std::path::{Path, PathBuf};
5
6use crate::refs::common_dir;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum RefWorktreeType {
11 Current,
13 Main,
15 Other,
17 Shared,
19}
20
21#[must_use]
23pub fn is_per_worktree_ref(refname: &str) -> bool {
24 refname.starts_with("refs/worktree/")
25 || refname.starts_with("refs/bisect/")
26 || refname.starts_with("refs/rewritten/")
27}
28
29fn is_root_ref_syntax(refname: &str) -> bool {
30 !refname.is_empty()
31 && refname
32 .chars()
33 .all(|c| c.is_ascii_uppercase() || c == '-' || c == '_')
34}
35
36fn is_current_worktree_ref(refname: &str) -> bool {
37 is_root_ref_syntax(refname) || is_per_worktree_ref(refname)
38}
39
40#[must_use]
44pub fn parse_worktree_ref(maybe: &str) -> (RefWorktreeType, Cow<'_, str>, Option<Cow<'_, str>>) {
45 if let Some(rest) = maybe.strip_prefix("worktrees/") {
46 if let Some((id, bare)) = rest.split_once('/') {
47 if is_current_worktree_ref(bare) {
48 return (
49 RefWorktreeType::Other,
50 Cow::Borrowed(bare),
51 Some(Cow::Borrowed(id)),
52 );
53 }
54 }
55 return (
56 RefWorktreeType::Other,
57 Cow::Borrowed(""),
58 Some(Cow::Borrowed(rest)),
59 );
60 }
61
62 if let Some(bare) = maybe.strip_prefix("main-worktree/") {
63 if is_current_worktree_ref(bare) {
64 return (RefWorktreeType::Main, Cow::Borrowed(bare), None);
65 }
66 }
67
68 if is_current_worktree_ref(maybe) {
69 return (RefWorktreeType::Current, Cow::Borrowed(maybe), None);
70 }
71
72 (RefWorktreeType::Shared, Cow::Borrowed(maybe), None)
73}
74
75#[must_use]
77pub fn resolve_ref_storage(git_dir: &Path, refname: &str) -> (PathBuf, String) {
78 let common = common_dir(git_dir).unwrap_or_else(|| git_dir.to_path_buf());
79 let (kind, bare, wt_id) = parse_worktree_ref(refname);
80 match kind {
81 RefWorktreeType::Main => (common, bare.into_owned()),
82 RefWorktreeType::Other => {
83 let id = wt_id.map(|c| c.into_owned()).unwrap_or_default();
84 (common.join("worktrees").join(id), bare.into_owned())
85 }
86 RefWorktreeType::Current => (git_dir.to_path_buf(), refname.to_owned()),
87 RefWorktreeType::Shared => (common, refname.to_owned()),
88 }
89}
90
91#[must_use]
93pub fn ref_visible_from_worktree(git_dir: &Path, refname: &str) -> bool {
94 if !is_per_worktree_ref(refname) {
95 return true;
96 }
97 let (store, stor_name) = resolve_ref_storage(git_dir, refname);
98 store.join(&stor_name).is_file()
99}
100
101#[must_use]
103pub fn is_linked_worktree_git_dir(git_dir: &Path) -> bool {
104 common_dir(git_dir).is_some()
105}
106
107const DWIM_RULES: &[&str] = &[
109 "{0}",
110 "refs/{0}",
111 "refs/tags/{0}",
112 "refs/heads/{0}",
113 "refs/remotes/{0}",
114 "refs/remotes/{0}/HEAD",
115];
116
117pub fn resolve_ref_dwim<F>(mut resolve: F, spec: &str) -> (usize, Option<crate::objects::ObjectId>)
121where
122 F: FnMut(&str) -> Option<crate::objects::ObjectId>,
123{
124 let mut count = 0usize;
125 let mut first = None;
126 for rule in DWIM_RULES {
127 let candidate = rule.replace("{0}", spec);
128 if let Some(oid) = resolve(&candidate) {
129 count += 1;
130 if first.is_none() {
131 first = Some(oid);
132 }
133 }
134 }
135 (count, first)
136}