Skip to content

Commit

Permalink
fix: issue with tags in ParseGitRefLogLine
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippHeuer committed Mar 24, 2023
1 parent 23d821d commit f01a3c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
29 changes: 17 additions & 12 deletions pkg/vcsrepository/git/client_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,28 @@ func (c GitClient) VCSHead() (vcsHead vcsapi.VCSRef, err error) {
// detached HEAD, check git reflog for the true reference
gitRefLogFile := filepath.Join(c.dir, ".git", "logs", "HEAD")
lastLine := readLastLine(gitRefLogFile)

pattern := regexp.MustCompile(`.*checkout: moving from (?P<FROM>.*) to (?P<TO>.*)$`)
match := pattern.FindStringSubmatch(lastLine)

if strings.HasPrefix(match[2], "refs/remotes/pull") {
// handle github merge request as virtual branch
return vcsapi.VCSRef{Type: "branch", Value: match[2][13:], Hash: ref.Hash().String()}, nil
} else if len(match[2]) == 40 {
return vcsapi.VCSRef{Type: "branch", Value: match[1], Hash: ref.Hash().String()}, nil
} else {
return vcsapi.VCSRef{Type: "tag", Value: match[2], Hash: ref.Hash().String()}, nil
}
return ParseGitRefLogLine(lastLine, ref.Hash().String()), nil
}

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

func ParseGitRefLogLine(line string, hash string) vcsapi.VCSRef {
pattern := regexp.MustCompile(`.*checkout: moving from (?P<FROM>.*) to (?P<TO>.*)$`)
match := pattern.FindStringSubmatch(line)

if strings.HasPrefix(match[2], "refs/remotes/pull") {
// handle github merge request as virtual branch
return vcsapi.VCSRef{Type: "branch", Value: match[2][13:], Hash: hash}
} else if len(match[2]) == 40 {
return vcsapi.VCSRef{Type: "branch", Value: match[1], Hash: hash}
} else if strings.HasPrefix(match[2], "refs/tags/") {
return vcsapi.VCSRef{Type: "tag", Value: match[2][10:], Hash: hash}
} else {
return vcsapi.VCSRef{Type: "tag", Value: match[2], Hash: hash}
}
}

func (c GitClient) GetTags() []vcsapi.VCSRef {
if c.tags == nil {
var tags []vcsapi.VCSRef
Expand Down
7 changes: 7 additions & 0 deletions pkg/vcsrepository/git/client_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,10 @@ func TestFindLatestGitReleaseFromCommit(t *testing.T) {
assert.Regexp(t, "v[0-9]+.[0-9]+.[0-9]+", release.Value)
assert.Regexp(t, "[0-9]+.[0-9]+.[0-9]+", release.Version)
}

func TestParseGitRefLogLine_Tag(t *testing.T) {
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")
assert.Equal(t, "1cafbbdb80ce27304ac92a9e2fde6c3df8119a19", vcsRef.Hash)
assert.Equal(t, "tag", vcsRef.Type)
assert.Equal(t, "v2.0.0-alpha.1", vcsRef.Value)
}

0 comments on commit f01a3c4

Please sign in to comment.