Skip to content

Commit

Permalink
feat: FullNameRef::file_name() (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jan 20, 2022
1 parent d726eb5 commit 28e3251
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions git-ref/src/fullname.rs
Expand Up @@ -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()
}
}
2 changes: 2 additions & 0 deletions git-ref/src/name.rs
Expand Up @@ -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()
}
Expand Down
6 changes: 6 additions & 0 deletions 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();
Expand Down

0 comments on commit 28e3251

Please sign in to comment.