pub enum Category<'a> {
Tag,
LocalBranch,
RemoteBranch,
Note,
PseudoRef,
MainPseudoRef,
MainRef,
LinkedPseudoRef {
name: &'a BStr,
},
LinkedRef {
name: &'a BStr,
},
Bisect,
Rewritten,
WorktreePrivate,
}
Expand description
The various known categories of references.
This translates into a prefix containing all references of a given category.
Variants§
Tag
A tag in refs/tags
LocalBranch
A branch in refs/heads
RemoteBranch
A branch in refs/remotes
Note
A tag in refs/notes
PseudoRef
Something outside of ref/
in the current worktree, typically HEAD
.
MainPseudoRef
A PseudoRef
, but referenced so that it will always refer to the main worktree by
prefixing it with main-worktree/
.
MainRef
Any reference that is prefixed with main-worktree/refs/
LinkedPseudoRef
Fields
name: &'a BStr
The name of the worktree.
A PseudoRef
in another linked worktree, never in the main one, like worktrees/<id>/HEAD
.
LinkedRef
Fields
name: &'a BStr
The name of the worktree.
Any reference that is prefixed with worktrees/<id>/refs/
.
Bisect
A ref that is private to each worktree (linked or main), with refs/bisect/
prefix
Rewritten
A ref that is private to each worktree (linked or main), with refs/rewritten/
prefix
WorktreePrivate
A ref that is private to each worktree (linked or main), with refs/worktree/
prefix
Implementations§
source§impl<'a> Category<'a>
impl<'a> Category<'a>
sourcepub fn prefix(&self) -> &BStr
pub fn prefix(&self) -> &BStr
Return the prefix that would contain all references of our kind, or an empty string if the reference would
be directly inside of the git_dir()
.
Examples found in repository?
More examples
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
pub fn category_and_short_name(&self) -> Option<(Category<'_>, &BStr)> {
let name = self.0.as_bstr();
for category in &[Category::Tag, Category::LocalBranch, Category::RemoteBranch] {
if let Some(shortened) = name.strip_prefix(category.prefix().as_ref()) {
return Some((*category, shortened.as_bstr()));
}
}
for category in &[
Category::Note,
Category::Bisect,
Category::WorktreePrivate,
Category::Rewritten,
] {
if name.starts_with(category.prefix().as_ref()) {
return Some((
*category,
name.strip_prefix(b"refs/")
.expect("we checked for refs/* above")
.as_bstr(),
));
}
}
if is_pseudo_ref(name) {
Some((Category::PseudoRef, name))
} else if let Some(shortened) = name.strip_prefix(Category::MainPseudoRef.prefix().as_ref()) {
if shortened.starts_with_str("refs/") {
(Category::MainRef, shortened.as_bstr()).into()
} else {
is_pseudo_ref(shortened).then(|| (Category::MainPseudoRef, shortened.as_bstr()))
}
} else if let Some(shortened_with_worktree_name) =
name.strip_prefix(Category::LinkedPseudoRef { name: "".into() }.prefix().as_ref())
{
let (name, shortened) = shortened_with_worktree_name.find_byte(b'/').map(|pos| {
(
shortened_with_worktree_name[..pos].as_bstr(),
shortened_with_worktree_name[pos + 1..].as_bstr(),
)
})?;
if shortened.starts_with_str("refs/") {
(Category::LinkedRef { name }, shortened.as_bstr()).into()
} else {
is_pseudo_ref(shortened).then(|| (Category::LinkedPseudoRef { name }, shortened.as_bstr()))
}
} else {
None
}
}
sourcepub fn is_worktree_private(&self) -> bool
pub fn is_worktree_private(&self) -> bool
Returns true if the category is private to their worktrees, and never shared with other worktrees.
Examples found in repository?
More examples
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
fn possibly_adjust_name_for_prefixes(name: &FullNameRef) -> Option<FullName> {
match name.category_and_short_name() {
Some((c, sn)) => {
use crate::Category::*;
let sn = FullNameRef::new_unchecked(sn);
match c {
Bisect | Rewritten | WorktreePrivate | LinkedPseudoRef { .. } | PseudoRef | MainPseudoRef => None,
Tag | LocalBranch | RemoteBranch | Note => name.into(),
MainRef | LinkedRef { .. } => sn
.category()
.map_or(false, |cat| !cat.is_worktree_private())
.then(|| sn),
}
.map(|n| n.to_owned())
}
None => Some(name.to_owned()), // allow (uncategorized/very special) refs to be packed
}
}
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
pub(crate) fn to_base_dir_and_relative_name<'a>(
&self,
name: &'a FullNameRef,
is_reflog: bool,
) -> (Cow<'_, Path>, &'a FullNameRef) {
let commondir = self.common_dir_resolved();
let linked_git_dir =
|worktree_name: &BStr| commondir.join("worktrees").join(git_path::from_bstr(worktree_name));
name.category_and_short_name()
.and_then(|(c, sn)| {
use crate::Category::*;
let sn = FullNameRef::new_unchecked(sn);
Some(match c {
LinkedPseudoRef { name: worktree_name } => is_reflog
.then(|| (linked_git_dir(worktree_name).into(), sn))
.unwrap_or((commondir.into(), name)),
Tag | LocalBranch | RemoteBranch | Note => (commondir.into(), name),
MainRef | MainPseudoRef => (commondir.into(), sn),
LinkedRef { name: worktree_name } => sn
.category()
.map_or(false, |cat| cat.is_worktree_private())
.then(|| {
if is_reflog {
(linked_git_dir(worktree_name).into(), sn)
} else {
(commondir.into(), name)
}
})
.unwrap_or((commondir.into(), sn)),
PseudoRef | Bisect | Rewritten | WorktreePrivate => return None,
})
})
.unwrap_or((self.git_dir.as_path().into(), name))
}
Trait Implementations§
source§impl<'a> Ord for Category<'a>
impl<'a> Ord for Category<'a>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<'a> PartialEq<Category<'a>> for Category<'a>
impl<'a> PartialEq<Category<'a>> for Category<'a>
source§impl<'a> PartialOrd<Category<'a>> for Category<'a>
impl<'a> PartialOrd<Category<'a>> for Category<'a>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more