From fce3b888f21c2a3ee9fd8f3ab9f2d24c3c730816 Mon Sep 17 00:00:00 2001 From: Brandon Mitchell Date: Sat, 22 Jul 2023 21:38:37 -0400 Subject: [PATCH] Prefer peeled hashes for tags Signed-off-by: Brandon Mitchell --- internal/source/git.go | 11 ++++++++++- internal/source/source_test.go | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/source/git.go b/internal/source/git.go index 95d1949..22c9582 100644 --- a/internal/source/git.go +++ b/internal/source/git.go @@ -40,7 +40,9 @@ func (g gitSource) getRefs(confExp config.Source) ([]*plumbing.Reference, error) Name: "origin", URLs: []string{confExp.Args["url"]}, }) - return rem.List(&git.ListOptions{}) + return rem.List(&git.ListOptions{ + PeelingOption: git.AppendPeeled, + }) } func (g gitSource) getCommit(confExp config.Source) (string, error) { @@ -54,6 +56,13 @@ func (g gitSource) getCommit(confExp config.Source) (string, error) { for _, ref := range refs { verData.VerMap[ref.Name().Short()] = ref.Hash().String() } + // loop over the map entries to prefer the peeled hash (underlying commit vs signed/annotated tag hash) + for k := range verData.VerMap { + if _, ok := verData.VerMap[k+"^{}"]; ok { + verData.VerMap[k] = verData.VerMap[k+"^{}"] + delete(verData.VerMap, k+"^{}") + } + } if len(verData.VerMap) == 0 { return "", fmt.Errorf("ref %s not found on %s", confExp.Args["ref"], confExp.Args["url"]) } diff --git a/internal/source/source_test.go b/internal/source/source_test.go index b8476ca..7d86f4f 100644 --- a/internal/source/source_test.go +++ b/internal/source/source_test.go @@ -117,7 +117,7 @@ func TestSource(t *testing.T) { ScanMatch: map[string]string{}, SourceArgs: map[string]string{}, }, - expectGet: "6f5dc406130fdf939cc0f49fb0a5904b35a3c46f", + expectGet: "b0ac3e9413b1079c8b14df5c201a2a2129d9d9e1", expectKey: "git ref", }, {