From 4f440f1239a44769deb74b153922260c3de752e0 Mon Sep 17 00:00:00 2001 From: Guy Owen Date: Mon, 23 Oct 2023 17:12:58 +1100 Subject: [PATCH] [#2056] Updates ghc ref and ref_name computation. --- pkg/common/git/git.go | 69 +++++++++++--------------------------- pkg/common/git/git_test.go | 10 ++++++ 2 files changed, 30 insertions(+), 49 deletions(-) diff --git a/pkg/common/git/git.go b/pkg/common/git/git.go index bf7715582b4..3c83c0e25e5 100644 --- a/pkg/common/git/git.go +++ b/pkg/common/git/git.go @@ -86,18 +86,9 @@ func FindGitRevision(ctx context.Context, file string) (shortSha string, sha str // FindGitRef get the current git ref func FindGitRef(ctx context.Context, file string) (string, error) { logger := common.Logger(ctx) - + refName := "" 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 = "" repo, err := git.PlainOpenWithOptions( file, &git.PlainOpenOptions{ @@ -105,59 +96,39 @@ func FindGitRef(ctx context.Context, file string) (string, error) { EnableDotGitCommonDir: true, }, ) - if err != nil { return "", err } - iter, err := repo.References() - if err != nil { - return "", err - } + headRef, _ := repo.Head() + 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. + // 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.") } - // order matters here see above comment. - if refTag != "" { - return refTag, nil - } - if refBranch != "" { - return refBranch, nil + if refName == "" { + refName = headRef.Name().String() } - return "", fmt.Errorf("failed to identify reference (tag/branch) for the checked-out revision '%s'", ref) + if refName != "" { + return refName, err + } else { + return "", fmt.Errorf("Failed to identify preference (tag/branch) for the checked-out revision '%s'", headRef.Hash().String()) + } } // FindGithubRepo get the repo diff --git a/pkg/common/git/git_test.go b/pkg/common/git/git_test.go index 6ad66b67aab..f340e9f687a 100644 --- a/pkg/common/git/git_test.go +++ b/pkg/common/git/git_test.go @@ -162,6 +162,16 @@ 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, "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