@@ -124,23 +124,28 @@ func (c GitClient) VCSHead() (vcsHead vcsapi.VCSRef, err error) {
124
124
// detached HEAD, check git reflog for the true reference
125
125
gitRefLogFile := filepath .Join (c .dir , ".git" , "logs" , "HEAD" )
126
126
lastLine := readLastLine (gitRefLogFile )
127
-
128
- pattern := regexp .MustCompile (`.*checkout: moving from (?P<FROM>.*) to (?P<TO>.*)$` )
129
- match := pattern .FindStringSubmatch (lastLine )
130
-
131
- if strings .HasPrefix (match [2 ], "refs/remotes/pull" ) {
132
- // handle github merge request as virtual branch
133
- return vcsapi.VCSRef {Type : "branch" , Value : match [2 ][13 :], Hash : ref .Hash ().String ()}, nil
134
- } else if len (match [2 ]) == 40 {
135
- return vcsapi.VCSRef {Type : "branch" , Value : match [1 ], Hash : ref .Hash ().String ()}, nil
136
- } else {
137
- return vcsapi.VCSRef {Type : "tag" , Value : match [2 ], Hash : ref .Hash ().String ()}, nil
138
- }
127
+ return ParseGitRefLogLine (lastLine , ref .Hash ().String ()), nil
139
128
}
140
129
141
130
return vcsapi.VCSRef {}, errors .New ("can't determinate repo head" )
142
131
}
143
132
133
+ func ParseGitRefLogLine (line string , hash string ) vcsapi.VCSRef {
134
+ pattern := regexp .MustCompile (`.*checkout: moving from (?P<FROM>.*) to (?P<TO>.*)$` )
135
+ match := pattern .FindStringSubmatch (line )
136
+
137
+ if strings .HasPrefix (match [2 ], "refs/remotes/pull" ) {
138
+ // handle github merge request as virtual branch
139
+ return vcsapi.VCSRef {Type : "branch" , Value : match [2 ][13 :], Hash : hash }
140
+ } else if len (match [2 ]) == 40 {
141
+ return vcsapi.VCSRef {Type : "branch" , Value : match [1 ], Hash : hash }
142
+ } else if strings .HasPrefix (match [2 ], "refs/tags/" ) {
143
+ return vcsapi.VCSRef {Type : "tag" , Value : match [2 ][10 :], Hash : hash }
144
+ } else {
145
+ return vcsapi.VCSRef {Type : "tag" , Value : match [2 ], Hash : hash }
146
+ }
147
+ }
148
+
144
149
func (c GitClient ) GetTags () []vcsapi.VCSRef {
145
150
if c .tags == nil {
146
151
var tags []vcsapi.VCSRef
0 commit comments