diff --git a/git-ref/src/fullname.rs b/git-ref/src/fullname.rs index b2d873ac08..98236ff730 100644 --- a/git-ref/src/fullname.rs +++ b/git-ref/src/fullname.rs @@ -106,4 +106,10 @@ impl<'a> FullNameRef<'a> { pub fn to_owned(&self) -> FullName { FullName(self.0.to_owned()) } + + /// Return the file name portion of a full name, for instance `main` if the + /// full name was `refs/heads/main`. + pub fn file_name(&self) -> &BStr { + self.0.rsplitn(2, |b| *b == b'/').next().expect("valid ref").as_bstr() + } } diff --git a/git-ref/src/name.rs b/git-ref/src/name.rs index a9835637a4..1b53875b03 100644 --- a/git-ref/src/name.rs +++ b/git-ref/src/name.rs @@ -13,6 +13,8 @@ pub type Error = git_validate::reference::name::Error; impl<'a> FullNameRef<'a> { /// Convert this name into the relative path identifying the reference location. + // TODO: use custom `Path` type instead, as this isn't really a path. See ref iteration with prefix for + // similar comment. pub fn to_path(self) -> Cow<'a, Path> { self.0.to_path().expect("UTF-8 conversion always succeeds").into() } diff --git a/git-ref/tests/fullname/mod.rs b/git-ref/tests/fullname/mod.rs index 97b0283aee..e2cc9c2356 100644 --- a/git-ref/tests/fullname/mod.rs +++ b/git-ref/tests/fullname/mod.rs @@ -1,5 +1,11 @@ use std::convert::TryInto; +#[test] +fn file_name() { + let name: git_ref::FullName = "refs/heads/main".try_into().unwrap(); + assert_eq!(name.to_ref().file_name(), "main"); +} + #[test] fn prefix_with_namespace_and_stripping() { let ns = git_ref::namespace::expand("foo").unwrap();