Skip to content

Commit d2cd2ef

Browse files
committed
feat: Category::to_full_name() can now be passed a full name without prefix duplication.
This is for convenience, as it can then be passed a valid partial name.
1 parent f7700e4 commit d2cd2ef

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

gix-ref/src/fullname.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
use crate::{bstr::ByteVec, name::is_pseudo_ref, Category, FullName, FullNameRef, Namespace, PartialNameRef};
12
use gix_object::bstr::{BStr, BString, ByteSlice};
23
use std::{borrow::Borrow, path::Path};
34

4-
use crate::{bstr::ByteVec, name::is_pseudo_ref, Category, FullName, FullNameRef, Namespace, PartialNameRef};
5-
65
impl TryFrom<&str> for FullName {
76
type Error = gix_validate::reference::name::Error;
87

@@ -165,6 +164,8 @@ impl FullNameRef {
165164
impl Category<'_> {
166165
/// As the inverse of [`FullNameRef::category_and_short_name()`], use the prefix of this category alongside
167166
/// the `short_name` to create a valid fully qualified [reference name](FullName).
167+
///
168+
/// If `short_name` already contains the prefix that it would receive (and is thus a full name), no duplication will occur.
168169
pub fn to_full_name<'a>(&self, short_name: impl Into<&'a BStr>) -> Result<FullName, crate::name::Error> {
169170
let mut out: BString = self.prefix().into();
170171
let short_name = short_name.into();
@@ -185,8 +186,12 @@ impl Category<'_> {
185186
| Category::PseudoRef
186187
| Category::MainPseudoRef => short_name,
187188
};
188-
out.extend_from_slice(partial_name);
189-
FullName::try_from(out)
189+
if out.is_empty() || !partial_name.starts_with(&out) {
190+
out.extend_from_slice(partial_name);
191+
FullName::try_from(out)
192+
} else {
193+
FullName::try_from(partial_name.as_bstr())
194+
}
190195
}
191196
}
192197

gix-ref/tests/refs/fullname.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ fn shorten_and_category() {
117117
);
118118
}
119119

120+
#[test]
121+
fn to_full_name() -> gix_testtools::Result {
122+
assert_eq!(
123+
Category::LocalBranch.to_full_name("refs/heads/full")?.as_bstr(),
124+
"refs/heads/full",
125+
"prefixes aren't duplicated"
126+
);
127+
128+
assert_eq!(
129+
Category::LocalBranch
130+
.to_full_name("refs/remotes/origin/other")?
131+
.as_bstr(),
132+
"refs/heads/refs/remotes/origin/other",
133+
"full names with a different category will be prefixed, to support 'main-worktree' special cases"
134+
);
135+
136+
Ok(())
137+
}
138+
120139
#[test]
121140
fn prefix_with_namespace_and_stripping() {
122141
let ns = gix_ref::namespace::expand("foo").unwrap();

0 commit comments

Comments
 (0)