Skip to content

Commit f01a3c4

Browse files
committed
fix: issue with tags in ParseGitRefLogLine
1 parent 23d821d commit f01a3c4

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

pkg/vcsrepository/git/client_git.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -124,23 +124,28 @@ func (c GitClient) VCSHead() (vcsHead vcsapi.VCSRef, err error) {
124124
// detached HEAD, check git reflog for the true reference
125125
gitRefLogFile := filepath.Join(c.dir, ".git", "logs", "HEAD")
126126
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
139128
}
140129

141130
return vcsapi.VCSRef{}, errors.New("can't determinate repo head")
142131
}
143132

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+
144149
func (c GitClient) GetTags() []vcsapi.VCSRef {
145150
if c.tags == nil {
146151
var tags []vcsapi.VCSRef

pkg/vcsrepository/git/client_git_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,10 @@ func TestFindLatestGitReleaseFromCommit(t *testing.T) {
135135
assert.Regexp(t, "v[0-9]+.[0-9]+.[0-9]+", release.Value)
136136
assert.Regexp(t, "[0-9]+.[0-9]+.[0-9]+", release.Version)
137137
}
138+
139+
func TestParseGitRefLogLine_Tag(t *testing.T) {
140+
vcsRef := ParseGitRefLogLine("0000000000000000000000000000000000000000 1cafbbdb80ce27304ac92a9e2fde6c3df8119a19 runner <runner@fv-az554-304.(none)> 1679700466 +0000\tcheckout: moving from master to refs/tags/v2.0.0-alpha.1", "1cafbbdb80ce27304ac92a9e2fde6c3df8119a19")
141+
assert.Equal(t, "1cafbbdb80ce27304ac92a9e2fde6c3df8119a19", vcsRef.Hash)
142+
assert.Equal(t, "tag", vcsRef.Type)
143+
assert.Equal(t, "v2.0.0-alpha.1", vcsRef.Value)
144+
}

0 commit comments

Comments
 (0)