Skip to main content

grit_lib/
worktree_ref.rs

1//! Per-worktree ref name parsing and storage location (Git `parse_worktree_ref` / `files_ref_path`).
2
3use std::borrow::Cow;
4use std::path::{Path, PathBuf};
5
6use crate::refs::common_dir;
7
8/// How a ref name maps to on-disk storage across worktrees.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum RefWorktreeType {
11    /// `HEAD`, `refs/worktree/*`, `refs/bisect/*`, `refs/rewritten/*` in this checkout's git dir.
12    Current,
13    /// `main-worktree/HEAD` → common dir + bare name.
14    Main,
15    /// `worktrees/<id>/HEAD` → `common/worktrees/<id>/` + bare name.
16    Other,
17    /// Shared refs (`refs/heads/*`, …) in the common git directory.
18    Shared,
19}
20
21/// `refs/worktree/*`, `refs/bisect/*`, and `refs/rewritten/*` are per-worktree.
22#[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/// Parse `maybe_worktree_ref` like Git `parse_worktree_ref`.
41///
42/// Returns worktree kind, bare ref name for storage, and worktree id for `worktrees/<id>/…`.
43#[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/// Git directory and on-disk ref path for `refname` from the current process's linked checkout.
76#[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/// Whether `refname` should appear in `for-each-ref` from a linked worktree.
92#[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/// True when `git_dir` is a linked worktree administrative directory.
102#[must_use]
103pub fn is_linked_worktree_git_dir(git_dir: &Path) -> bool {
104    common_dir(git_dir).is_some()
105}
106
107/// DWIM rules matching Git `ref_rev_parse_rules` (`refs.c`).
108const 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
117/// Resolve `spec` using Git `ref_rev_parse_rules` / `expand_ref`.
118///
119/// Returns the number of matching rules and the OID from the first match.
120pub 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}