@@ -15,15 +15,16 @@ const (
15
15
symrefPrefix = "ref: "
16
16
)
17
17
18
- var (
19
- refPrefixes = []string {
20
- refHeadPrefix ,
21
- refTagPrefix ,
22
- refRemotePrefix ,
23
- refNotePrefix ,
24
- refPrefix ,
25
- }
26
- )
18
+ // refRevParseRules are a set of rules to parse references into short names.
19
+ // These are the same rules as used by git in shorten_unambiguous_ref.
20
+ // See: https://github.com/git/git/blob/e0aaa1b6532cfce93d87af9bc813fb2e7a7ce9d7/refs.c#L417
21
+ var refRevParseRules = []string {
22
+ "refs/%s" ,
23
+ "refs/tags/%s" ,
24
+ "refs/heads/%s" ,
25
+ "refs/remotes/%s" ,
26
+ "refs/remotes/%s/HEAD" ,
27
+ }
27
28
28
29
var (
29
30
ErrReferenceNotFound = errors .New ("reference not found" )
@@ -60,17 +61,16 @@ func (r ReferenceName) String() string {
60
61
61
62
// Short returns the short name of a ReferenceName
62
63
func (r ReferenceName ) Short () string {
63
- return r .removeRefPrefix ()
64
- }
65
-
66
- // Instead of hardcoding a number of components, we should remove the prefixes
67
- // refHeadPrefix, refTagPrefix, refRemotePrefix, refNotePrefix and refPrefix
68
- func (r ReferenceName ) removeRefPrefix () string {
69
64
s := string (r )
70
- for _ , prefix := range refPrefixes {
71
- s = strings .TrimPrefix (s , prefix )
65
+ res := s
66
+ for _ , format := range refRevParseRules {
67
+ _ , err := fmt .Sscanf (s , format , & res )
68
+ if err == nil {
69
+ continue
70
+ }
72
71
}
73
- return s
72
+
73
+ return res
74
74
}
75
75
76
76
const (
0 commit comments