Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compute ref and ref_name prioritising HEAD pointer if tag is not set #2061

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 19 additions & 45 deletions pkg/common/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,78 +86,52 @@
// FindGitRef get the current git ref
func FindGitRef(ctx context.Context, file string) (string, error) {
logger := common.Logger(ctx)

logger.Debugf("Loading revision from git directory")
_, ref, err := FindGitRevision(ctx, file)
if err != nil {
return "", err
}

logger.Debugf("HEAD points to '%s'", ref)

// Prefer the git library to iterate over the references and find a matching tag or branch.
var refTag = ""
var refBranch = ""
refName := ""
repo, err := git.PlainOpenWithOptions(
file,
&git.PlainOpenOptions{
DetectDotGit: true,
EnableDotGitCommonDir: true,
},
)

if err != nil {
return "", err
}

iter, err := repo.References()
headRef, err := repo.Head()
if err != nil {
return "", err
}
logger.Debugf("HEAD points to revision '%s'", headRef.Hash().String())

// find the reference that matches the revision's has
err = iter.ForEach(func(r *plumbing.Reference) error {
/* tags and branches will have the same hash
* when a user checks out a tag, it is not mentioned explicitly
* in the go-git package, we must identify the revision
* then check if any tag matches that revision,
* if so then we checked out a tag
* else we look for branches and if matches,
* it means we checked out a branch
*
* If a branches matches first we must continue and check all tags (all references)
* in case we match with a tag later in the interation
*/
if r.Hash().String() == ref {
if r.Name().IsTag() {
refTag = r.Name().String()
}
if r.Name().IsBranch() {
refBranch = r.Name().String()
}
}
// The assumption is made that if the revision has a tag associated with it the ref and ref_name should reference it.
Copy link
Author

@GROwen GROwen Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not confident of what workflow triggers require the ref and ref_name to be set to the refs/tags prefix and therefore uncertain if this assumption is accurate or even desired.

I’m thinking this is relevant to working with the release workflow trigger?

Either way I’m not looking to change the current behaviour but just fix the value when multiple branches point to the same commit.

// This is relevant for the release workflow trigger.
// The Tags iterator is the most reliable method to lookup if a revision is tagged (the go-git repository.TagObject method only returns annotated tags).
tags, _ := repo.Tags()

// we found what we where looking for
if refTag != "" && refBranch != "" {
t := tags.ForEach(func(r *plumbing.Reference) error {
if r.Hash().String() == headRef.Hash().String() {
refName = r.Name().String()
return storer.ErrStop
}

return nil
return err
})

if err != nil {
return "", err
if t != nil {
logger.WithError(t).Error("Tag lookup failed.")

Check warning on line 123 in pkg/common/git/git.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/git/git.go#L123

Added line #L123 was not covered by tests
}

// order matters here see above comment.
if refTag != "" {
return refTag, nil
if refName == "" {
refName = headRef.Name().String()
}
if refBranch != "" {
return refBranch, nil

if refName != "" {
return refName, err
}

return "", fmt.Errorf("failed to identify reference (tag/branch) for the checked-out revision '%s'", ref)
return "", fmt.Errorf("Failed to identify preference (tag/branch) for the checked-out revision '%s'", headRef.Hash().String())

Check warning on line 134 in pkg/common/git/git.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/git/git.go#L134

Added line #L134 was not covered by tests
}

// FindGithubRepo get the repo
Expand Down
11 changes: 11 additions & 0 deletions pkg/common/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ func TestGitFindRef(t *testing.T) {
require.Equal(t, "refs/heads/mybranch", ref)
},
},
"current_head_is_master_and_pointer_exists_to_another_branch": {
Prepare: func(t *testing.T, dir string) {
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "msg"))
require.NoError(t, gitCmd("-C", dir, "checkout", "-b", "mybranch"))
require.NoError(t, gitCmd("-C", dir, "checkout", "master"))
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/heads/master", ref)
},
},
} {
tt := tt
name := name
Expand Down