File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -105,6 +105,13 @@ impl FullName {
105
105
}
106
106
self
107
107
}
108
+
109
+ /// Strip well-known prefixes from the name and return it.
110
+ ///
111
+ /// If there is no such prefix, the original name is returned.
112
+ pub fn strip_prefix ( & self ) -> & BStr {
113
+ self . to_ref ( ) . strip_prefix ( )
114
+ }
108
115
}
109
116
110
117
impl < ' a > FullNameRef < ' a > {
Original file line number Diff line number Diff line change @@ -21,6 +21,19 @@ impl<'a> FullNameRef<'a> {
21
21
pub fn as_bstr ( & self ) -> & ' a BStr {
22
22
self . 0
23
23
}
24
+
25
+ /// Strip well-known prefixes from the name and return it.
26
+ ///
27
+ /// If there is no such prefix, the original name is returned.
28
+ pub fn strip_prefix ( & self ) -> & ' a BStr {
29
+ let n = self . as_bstr ( ) ;
30
+ n. strip_prefix ( b"refs/tags/" )
31
+ . or_else ( || n. strip_prefix ( b"refs/heads/" ) )
32
+ . or_else ( || n. strip_prefix ( b"refs/remotes/" ) )
33
+ . or_else ( || n. strip_prefix ( b"refs/" ) )
34
+ . map ( |n| n. as_bstr ( ) )
35
+ . unwrap_or ( n)
36
+ }
24
37
}
25
38
26
39
impl < ' a > PartialNameRef < ' a > {
Original file line number Diff line number Diff line change @@ -5,6 +5,28 @@ fn file_name() {
5
5
let name: git_ref:: FullName = "refs/heads/main" . try_into ( ) . unwrap ( ) ;
6
6
assert_eq ! ( name. to_ref( ) . file_name( ) , "main" ) ;
7
7
}
8
+ #[ test]
9
+ fn strip_prefix ( ) {
10
+ for ( input, expected) in vec ! [
11
+ ( "refs/tags/tag-name" , "tag-name" ) ,
12
+ ( "refs/heads/main" , "main" ) ,
13
+ ( "refs/remotes/origin/main" , "origin/main" ) ,
14
+ ( "refs/notes/note-name" , "notes/note-name" ) ,
15
+ ] {
16
+ let name: git_ref:: FullName = input. try_into ( ) . unwrap ( ) ;
17
+ assert_eq ! ( name. to_ref( ) . strip_prefix( ) , expected) ;
18
+ assert_eq ! ( name. strip_prefix( ) , expected) ;
19
+ }
20
+
21
+ let special = "HEAD" ;
22
+ let name: git_ref:: FullName = special. try_into ( ) . unwrap ( ) ;
23
+ assert_eq ! (
24
+ name. strip_prefix( ) ,
25
+ special,
26
+ "the whole name is returned if there is no prefix"
27
+ ) ;
28
+ assert_eq ! ( name. strip_prefix( ) , name. to_ref( ) . strip_prefix( ) ) ;
29
+ }
8
30
9
31
#[ test]
10
32
fn prefix_with_namespace_and_stripping ( ) {
You can’t perform that action at this time.
0 commit comments