Skip to content

Commit 28e3251

Browse files
committed
feat: FullNameRef::file_name() (#298)
1 parent d726eb5 commit 28e3251

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

git-ref/src/fullname.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,10 @@ impl<'a> FullNameRef<'a> {
106106
pub fn to_owned(&self) -> FullName {
107107
FullName(self.0.to_owned())
108108
}
109+
110+
/// Return the file name portion of a full name, for instance `main` if the
111+
/// full name was `refs/heads/main`.
112+
pub fn file_name(&self) -> &BStr {
113+
self.0.rsplitn(2, |b| *b == b'/').next().expect("valid ref").as_bstr()
114+
}
109115
}

git-ref/src/name.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub type Error = git_validate::reference::name::Error;
1313

1414
impl<'a> FullNameRef<'a> {
1515
/// Convert this name into the relative path identifying the reference location.
16+
// TODO: use custom `Path` type instead, as this isn't really a path. See ref iteration with prefix for
17+
// similar comment.
1618
pub fn to_path(self) -> Cow<'a, Path> {
1719
self.0.to_path().expect("UTF-8 conversion always succeeds").into()
1820
}

git-ref/tests/fullname/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
use std::convert::TryInto;
22

3+
#[test]
4+
fn file_name() {
5+
let name: git_ref::FullName = "refs/heads/main".try_into().unwrap();
6+
assert_eq!(name.to_ref().file_name(), "main");
7+
}
8+
39
#[test]
410
fn prefix_with_namespace_and_stripping() {
511
let ns = git_ref::namespace::expand("foo").unwrap();

0 commit comments

Comments
 (0)