@@ -6,6 +6,15 @@ package git
66
77import "strings"
88
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+
918// Reference represents a Git ref.
1019type Reference struct {
1120 Name string
@@ -24,17 +33,17 @@ func (ref *Reference) ShortName() string {
2433 if ref == nil {
2534 return ""
2635 }
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 )
2938 }
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 )
3241 }
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 )
3544 }
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 ]
3847 }
3948
4049 return ref .Name
@@ -45,16 +54,16 @@ func (ref *Reference) RefGroup() string {
4554 if ref == nil {
4655 return ""
4756 }
48- if strings .HasPrefix (ref .Name , "refs/heads/" ) {
57+ if strings .HasPrefix (ref .Name , BranchPrefix ) {
4958 return "heads"
5059 }
51- if strings .HasPrefix (ref .Name , "refs/tags/" ) {
60+ if strings .HasPrefix (ref .Name , TagPrefix ) {
5261 return "tags"
5362 }
54- if strings .HasPrefix (ref .Name , "refs/remotes/" ) {
63+ if strings .HasPrefix (ref .Name , RemotePrefix ) {
5564 return "remotes"
5665 }
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 {
5867 return "pull"
5968 }
6069 return ""
0 commit comments