// Copyright (c) Kim Altintop // Copyright (c) The Radicle Team //! Git reference names. use super::*; /// Iterator over slash-separated refname components. pub type Iter<'a> = std::str::Split<'a, char>; /// Common refname components as strings. pub mod str { /// The `heads` component. pub const HEADS: &str = "heads"; /// The `main` component. pub const MAIN: &str = "main"; /// The `master` component. pub const MASTER: &str = "master"; /// The `namespaces` component. pub const NAMESPACES: &str = "namespaces"; /// The `notes` component. pub const NOTES: &str = "notes"; /// The `origin` component. pub const ORIGIN: &str = "origin"; /// The `refs` component. pub const REFS: &str = "refs"; /// The `remotes` component. pub const REMOTES: &str = "remotes"; /// The `tags` component. pub const TAGS: &str = "tags"; /// The `refs/heads/main` refname. pub const REFS_HEADS_MAIN: &str = "refs/heads/main"; /// The `refs/heads/master` refname. pub const REFS_HEADS_MASTER: &str = "refs/heads/master"; } /// Common refname components as bytes. pub mod bytes { use super::str; /// The `heads` component. pub const HEADS: &[u8] = str::HEADS.as_bytes(); /// The `main` component. pub const MAIN: &[u8] = str::MAIN.as_bytes(); /// The `master` component. pub const MASTER: &[u8] = str::MASTER.as_bytes(); /// The `namespaces` component. pub const NAMESPACES: &[u8] = str::NAMESPACES.as_bytes(); /// The `notes` component. pub const NOTES: &[u8] = str::NOTES.as_bytes(); /// The `origin` component. pub const ORIGIN: &[u8] = str::ORIGIN.as_bytes(); /// The `refs` component. pub const REFS: &[u8] = str::REFS.as_bytes(); /// The `remotes` component. pub const REMOTES: &[u8] = str::REMOTES.as_bytes(); /// The `tags` component. pub const TAGS: &[u8] = str::TAGS.as_bytes(); /// The `refs/heads/main` refname. pub const REFS_HEADS_MAIN: &[u8] = str::REFS_HEADS_MAIN.as_bytes(); /// The `refs/heads/master` refname. pub const REFS_HEADS_MASTER: &[u8] = str::REFS_HEADS_MASTER.as_bytes(); } /// The `heads` component. pub const HEADS: &RefStr = RefStr::from_str(str::HEADS); /// The `main` component. pub const MAIN: &RefStr = RefStr::from_str(str::MAIN); /// The `master` component. pub const MASTER: &RefStr = RefStr::from_str(str::MASTER); /// The `namespaces` component. pub const NAMESPACES: &RefStr = RefStr::from_str(str::NAMESPACES); /// The `notes` component. pub const NOTES: &RefStr = RefStr::from_str(str::NOTES); /// The `origin` component. pub const ORIGIN: &RefStr = RefStr::from_str(str::ORIGIN); /// The `refs` component. pub const REFS: &RefStr = RefStr::from_str(str::REFS); /// The `remotes` component. pub const REMOTES: &RefStr = RefStr::from_str(str::REMOTES); /// The `tags` component. pub const TAGS: &RefStr = RefStr::from_str(str::TAGS); /// The `refs/heads/main` refname. pub const REFS_HEADS_MAIN: &RefStr = RefStr::from_str(str::REFS_HEADS_MAIN); /// The `refs/heads/master` refname. pub const REFS_HEADS_MASTER: &RefStr = RefStr::from_str(str::REFS_HEADS_MASTER); /// Validation options for ordinary refnames. const CHECK_OPTS: check::Options = check::Options { allow_pattern: false, allow_onelevel: true, }; /// A borrowed valid Git refname. #[repr(transparent)] #[derive(Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] pub struct RefStr(str); impl RefStr { /// Validates `s` and returns it as a borrowed refname. pub fn try_from_str(s: &str) -> Result<&RefStr, check::Error> { TryFrom::try_from(s) } /// Returns the refname as a string slice. #[inline] pub fn as_str(&self) -> &str { self } /// Returns an owned copy of this refname. #[inline] pub fn to_ref_string(&self) -> RefString { self.to_owned() } /// Strips `base` followed by a slash from this refname. pub fn strip_prefix

(&self, base: P) -> Option<&RefStr> where P: AsRef, { self.0 .strip_prefix(base.as_ref().as_str()) .and_then(|s| s.strip_prefix('/')) .map(Self::from_str) } /// Joins another refname component or suffix to this refname. pub fn join(&self, other: R) -> RefString where R: AsRef, { let mut buf = self.to_ref_string(); buf.push(other); buf } /// Appends a refspec pattern suffix to this refname. pub fn to_pattern

(&self, pattern: P) -> refspec::PatternString where P: AsRef, { self.to_owned().with_pattern(pattern) } /// Returns this refname as a qualified refname, if it is qualified. #[inline] pub fn qualified(&self) -> Option> { Qualified::from_refstr(self) } /// Returns this refname as a namespaced refname, if it is namespaced. #[inline] pub fn to_namespaced(&self) -> Option> { self.into() } /// Returns slash-separated string components. #[inline] pub fn iter(&self) -> Iter<'_> { self.0.split('/') } /// Returns slash-separated refname components. #[inline] pub fn components(&self) -> Components<'_> { Components::from(self) } /// Returns the first component. pub fn head(&self) -> Component<'_> { match self.components().next() { Some(component) => component, None => Component(Cow::Borrowed(self)), } } /// Converts a known-valid string slice to a refname. pub(crate) const fn from_str(s: &str) -> &RefStr { // SAFETY: `RefStr` is a transparent wrapper around `str`. unsafe { &*(s as *const str as *const RefStr) } } } impl Deref for RefStr { type Target = str; #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl AsRef for RefStr { #[inline] fn as_ref(&self) -> &str { self } } impl AsRef for RefStr { #[inline] fn as_ref(&self) -> &RefStr { self } } impl<'a> TryFrom<&'a str> for &'a RefStr { type Error = check::Error; #[inline] fn try_from(s: &'a str) -> Result { check::ref_format(CHECK_OPTS, s).map(|()| RefStr::from_str(s)) } } impl<'a> From<&'a RefStr> for Cow<'a, RefStr> { #[inline] fn from(rs: &'a RefStr) -> Cow<'a, RefStr> { Cow::Borrowed(rs) } } impl Display for RefStr { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self) } } /// An owned valid Git refname. #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] pub struct RefString(String); impl RefString { /// Returns this owned refname as a borrowed refname. #[inline] pub fn as_refstr(&self) -> &RefStr { self } /// Joins another refname suffix to this refname. pub fn and(mut self, other: R) -> Self where R: AsRef, { self.push(other); self } /// Pushes another refname suffix onto this refname. pub fn push(&mut self, other: R) where R: AsRef, { self.0.push('/'); self.0.push_str(other.as_ref().as_str()); } /// Removes the final slash-separated component. pub fn pop(&mut self) -> bool { match self.0.rfind('/') { Some(index) => { self.0.truncate(index); true } None => false, } } /// Appends a refspec pattern suffix. pub fn with_pattern

(self, pattern: P) -> refspec::PatternString where P: AsRef, { let mut buf = self.0; buf.push('/'); buf.push_str(pattern.as_ref().as_str()); refspec::PatternString(buf) } /// Returns this refname as a qualified refname, if it is qualified. #[inline] pub fn into_qualified<'a>(self) -> Option> { Qualified::from_refstr(self) } /// Reserves capacity for at least `additional` bytes. #[inline] pub fn reserve(&mut self, additional: usize) { self.0.reserve(additional); } /// Shrinks the allocated capacity as much as possible. #[inline] pub fn shrink_to_fit(&mut self) { self.0.shrink_to_fit(); } } impl Deref for RefString { type Target = RefStr; #[inline] fn deref(&self) -> &Self::Target { self.borrow() } } impl AsRef for RefString { #[inline] fn as_ref(&self) -> &RefStr { self } } impl AsRef for RefString { #[inline] fn as_ref(&self) -> &str { self.0.as_str() } } impl Borrow for RefString { #[inline] fn borrow(&self) -> &RefStr { RefStr::from_str(self.0.as_str()) } } impl ToOwned for RefStr { type Owned = RefString; #[inline] fn to_owned(&self) -> Self::Owned { RefString(self.0.to_owned()) } } impl TryFrom<&str> for RefString { type Error = check::Error; #[inline] fn try_from(s: &str) -> Result { RefStr::try_from_str(s).map(ToOwned::to_owned) } } impl TryFrom for RefString { type Error = check::Error; #[inline] fn try_from(s: String) -> Result { check::ref_format(CHECK_OPTS, s.as_str()).map(|()| RefString(s)) } } impl<'a> From<&'a RefString> for Cow<'a, RefStr> { #[inline] fn from(rs: &'a RefString) -> Cow<'a, RefStr> { Cow::Borrowed(rs.as_refstr()) } } impl<'a> From for Cow<'a, RefStr> { #[inline] fn from(rs: RefString) -> Cow<'a, RefStr> { Cow::Owned(rs) } } impl From for String { #[inline] fn from(rs: RefString) -> Self { rs.0 } } impl Display for RefString { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.0) } } impl FromIterator for RefString where A: AsRef, { #[allow(clippy::panic)] fn from_iter(iter: T) -> Self where T: IntoIterator, { let mut iter = iter.into_iter(); let Some(first) = iter.next() else { panic!("RefString::from_iter: empty iterator"); }; let mut buf = first.as_ref().as_str().to_owned(); for component in iter { buf.push('/'); buf.push_str(component.as_ref().as_str()); } RefString(buf) } } impl Extend for RefString where A: AsRef, { fn extend(&mut self, iter: T) where T: IntoIterator, { for component in iter { self.push(component); } } } /// Iterator over borrowed refname components. #[must_use = "iterators are lazy and do nothing unless consumed"] #[derive(Clone)] pub struct Components<'a> { /// Inner string splitter. inner: Iter<'a>, } impl<'a> Iterator for Components<'a> { type Item = Component<'a>; #[inline] fn next(&mut self) -> Option { self.inner .next() .map(RefStr::from_str) .map(Cow::from) .map(Component) } } impl<'a> DoubleEndedIterator for Components<'a> { #[inline] fn next_back(&mut self) -> Option { self.inner .next_back() .map(RefStr::from_str) .map(Cow::from) .map(Component) } } impl<'a> From<&'a RefStr> for Components<'a> { #[inline] fn from(rs: &'a RefStr) -> Self { Self { inner: rs.as_str().split('/'), } } } /// A slash-free component of a Git refname. #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] pub struct Component<'a>(pub(crate) Cow<'a, RefStr>); impl<'a> Component<'a> { /// Returns `r` as a component if it contains no slash. #[inline] pub fn from_refstr(r: impl Into>) -> Option { let r = r.into(); if r.contains('/') { None } else { Some(Self(r)) } } /// Returns this component as a known literal, if it matches one. #[inline] pub fn as_lit(&self) -> Option { T::from_component(self) } /// Returns the wrapped borrowed or owned refname. #[inline] pub fn into_inner(self) -> Cow<'a, RefStr> { self.0 } } impl Deref for Component<'_> { type Target = RefStr; #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl AsRef for Component<'_> { #[inline] fn as_ref(&self) -> &RefStr { self } } impl<'a> From<&'a RefStr> for Option> { #[inline] fn from(r: &'a RefStr) -> Self { Component::from_refstr(r) } } impl<'a> From> for Cow<'a, RefStr> { #[inline] fn from(component: Component<'a>) -> Self { component.0 } } impl From for Component<'static> { #[inline] fn from(_: T) -> Self { Component(Cow::from(T::NAME)) } } impl<'a> From> for Component<'a> { #[inline] fn from(lit: lit::SomeLit<'a>) -> Self { match lit { lit::SomeLit::Known(known) => known.into(), lit::SomeLit::Any(component) => component, } } } impl Display for Component<'_> { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self.0.as_str()) } } /// Common refname components. pub mod component { use super::Component; use crate::git::refs::name; use std::borrow::Cow; /// The `heads` component. pub const HEADS: Component<'static> = Component(Cow::Borrowed(name::HEADS)); /// The `main` component. pub const MAIN: Component<'static> = Component(Cow::Borrowed(name::MAIN)); /// The `master` component. pub const MASTER: Component<'static> = Component(Cow::Borrowed(name::MASTER)); /// The `namespaces` component. pub const NAMESPACES: Component<'static> = Component(Cow::Borrowed(name::NAMESPACES)); /// The `notes` component. pub const NOTES: Component<'static> = Component(Cow::Borrowed(name::NOTES)); /// The `origin` component. pub const ORIGIN: Component<'static> = Component(Cow::Borrowed(name::ORIGIN)); /// The `refs` component. pub const REFS: Component<'static> = Component(Cow::Borrowed(name::REFS)); /// The `remotes` component. pub const REMOTES: Component<'static> = Component(Cow::Borrowed(name::REMOTES)); /// The `tags` component. pub const TAGS: Component<'static> = Component(Cow::Borrowed(name::TAGS)); }