@@ -6,6 +6,15 @@ package git
6
6
7
7
import "strings"
8
8
9
+ const (
10
+ // RemotePrefix is the base directory of the remotes information of git.
11
+ RemotePrefix = "refs/remotes/"
12
+ // PullPrefix is the base directory of the pull information of git.
13
+ PullPrefix = "refs/pull/"
14
+
15
+ pullLen = len (PullPrefix )
16
+ )
17
+
9
18
// Reference represents a Git ref.
10
19
type Reference struct {
11
20
Name string
@@ -24,17 +33,17 @@ func (ref *Reference) ShortName() string {
24
33
if ref == nil {
25
34
return ""
26
35
}
27
- if strings .HasPrefix (ref .Name , "refs/heads/" ) {
28
- return ref .Name [ 11 :]
36
+ if strings .HasPrefix (ref .Name , BranchPrefix ) {
37
+ return strings . TrimPrefix ( ref .Name , BranchPrefix )
29
38
}
30
- if strings .HasPrefix (ref .Name , "refs/tags/" ) {
31
- return ref .Name [ 10 :]
39
+ if strings .HasPrefix (ref .Name , TagPrefix ) {
40
+ return strings . TrimPrefix ( ref .Name , TagPrefix )
32
41
}
33
- if strings .HasPrefix (ref .Name , "refs/remotes/" ) {
34
- return ref .Name [ 13 :]
42
+ if strings .HasPrefix (ref .Name , RemotePrefix ) {
43
+ return strings . TrimPrefix ( ref .Name , RemotePrefix )
35
44
}
36
- if strings .HasPrefix (ref .Name , "refs/pull/" ) && strings .IndexByte (ref .Name [10 :], '/' ) > - 1 {
37
- return ref .Name [10 : strings .IndexByte (ref .Name [10 :], '/' )+ 10 ]
45
+ if strings .HasPrefix (ref .Name , PullPrefix ) && strings .IndexByte (ref .Name [pullLen :], '/' ) > - 1 {
46
+ return ref .Name [pullLen : strings .IndexByte (ref .Name [pullLen :], '/' )+ pullLen ]
38
47
}
39
48
40
49
return ref .Name
@@ -45,16 +54,16 @@ func (ref *Reference) RefGroup() string {
45
54
if ref == nil {
46
55
return ""
47
56
}
48
- if strings .HasPrefix (ref .Name , "refs/heads/" ) {
57
+ if strings .HasPrefix (ref .Name , BranchPrefix ) {
49
58
return "heads"
50
59
}
51
- if strings .HasPrefix (ref .Name , "refs/tags/" ) {
60
+ if strings .HasPrefix (ref .Name , TagPrefix ) {
52
61
return "tags"
53
62
}
54
- if strings .HasPrefix (ref .Name , "refs/remotes/" ) {
63
+ if strings .HasPrefix (ref .Name , RemotePrefix ) {
55
64
return "remotes"
56
65
}
57
- if strings .HasPrefix (ref .Name , "refs/pull/" ) && strings .IndexByte (ref .Name [10 :], '/' ) > - 1 {
66
+ if strings .HasPrefix (ref .Name , PullPrefix ) && strings .IndexByte (ref .Name [pullLen :], '/' ) > - 1 {
58
67
return "pull"
59
68
}
60
69
return ""
0 commit comments