File tree Expand file tree Collapse file tree 3 files changed +29
-8
lines changed Expand file tree Collapse file tree 3 files changed +29
-8
lines changed Original file line number Diff line number Diff line change @@ -117,6 +117,12 @@ impl FullName {
117
117
pub fn category ( & self ) -> Option < crate :: Category > {
118
118
self . to_ref ( ) . category ( )
119
119
}
120
+
121
+ /// Classify this name, or return `None` if it's unclassified. If `Some`,
122
+ /// the shortened name is returned as well.
123
+ pub fn category_and_short_name ( & self ) -> Option < ( crate :: Category , & BStr ) > {
124
+ self . to_ref ( ) . category_and_short_name ( )
125
+ }
120
126
}
121
127
122
128
impl < ' a > FullNameRef < ' a > {
Original file line number Diff line number Diff line change @@ -49,16 +49,27 @@ impl<'a> FullNameRef<'a> {
49
49
50
50
/// Classify this name, or return `None` if it's unclassified.
51
51
pub fn category ( & self ) -> Option < Category > {
52
- for kind in & [
53
- Category :: Tag ,
54
- Category :: LocalBranch ,
55
- Category :: RemoteBranch ,
56
- Category :: Note ,
57
- ] {
58
- if self . 0 . starts_with ( kind. prefix ( ) ) {
59
- return ( * kind) . into ( ) ;
52
+ self . category_and_short_name ( ) . map ( |( cat, _) | cat)
53
+ }
54
+
55
+ /// Classify this name, or return `None` if it's unclassified. If `Some`,
56
+ /// the shortened name is returned as well.
57
+ pub fn category_and_short_name ( & self ) -> Option < ( Category , & ' a BStr ) > {
58
+ for category in & [ Category :: Tag , Category :: LocalBranch , Category :: RemoteBranch ] {
59
+ if let Some ( shortened) = self . 0 . strip_prefix ( category. prefix ( ) . as_ref ( ) ) {
60
+ return Some ( ( * category, shortened. as_bstr ( ) ) ) ;
60
61
}
61
62
}
63
+
64
+ if self . 0 . starts_with ( Category :: Note . prefix ( ) ) {
65
+ return Some ( (
66
+ Category :: Note ,
67
+ self . 0
68
+ . strip_prefix ( b"refs/" )
69
+ . expect ( "we checked for refs/notes above" )
70
+ . as_bstr ( ) ,
71
+ ) ) ;
72
+ }
62
73
None
63
74
}
64
75
}
Original file line number Diff line number Diff line change @@ -19,6 +19,10 @@ fn shorten_and_category() {
19
19
assert_eq ! ( name. to_ref( ) . shorten( ) , expected) ;
20
20
assert_eq ! ( name. shorten( ) , expected) ;
21
21
assert_eq ! ( name. category( ) , category) ;
22
+ assert_eq ! (
23
+ name. category_and_short_name( ) ,
24
+ category. map( |cat| ( cat, expected. into( ) ) )
25
+ ) ;
22
26
assert_eq ! ( name. to_ref( ) . category( ) , category) ;
23
27
}
24
28
You can’t perform that action at this time.
0 commit comments