Skip to content

Commit

Permalink
git/gogit: add tag info to commit if refname points to an annotated tag
Browse files Browse the repository at this point in the history
Signed-off-by: Sanskar Jaiswal <[email protected]>
  • Loading branch information
aryan9600 committed Aug 10, 2023
1 parent 40d19d6 commit c35892e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
31 changes: 28 additions & 3 deletions git/gogit/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ func (g *Client) cloneCommit(ctx context.Context, url, commit string, opts repos
if err != nil {
return nil, fmt.Errorf("unable to construct auth method with options: %w", err)
}

// we only want to fetch tags if the refname provided is a tag
// and does not have the dereference suffix.
tagStrategy := extgogit.NoTags
if plumbing.ReferenceName(opts.RefName).IsTag() && !strings.HasSuffix(opts.RefName, tagDereferenceSuffix) {
tagStrategy = extgogit.TagFollowing
}
cloneOpts := &extgogit.CloneOptions{
URL: url,
Auth: authMethod,
Expand All @@ -218,7 +225,7 @@ func (g *Client) cloneCommit(ctx context.Context, url, commit string, opts repos
NoCheckout: true,
RecurseSubmodules: recurseSubmodules(opts.RecurseSubmodules),
Progress: nil,
Tags: extgogit.NoTags,
Tags: tagStrategy,
CABundle: caBundle(g.authOpts),
ProxyOptions: g.proxy,
}
Expand Down Expand Up @@ -254,11 +261,29 @@ func (g *Client) cloneCommit(ctx context.Context, url, commit string, opts repos
if err != nil {
return nil, fmt.Errorf("unable to checkout commit '%s': %w", commit, err)
}
g.repository = repo

var tagObj *object.Tag
if opts.RefName != "" {
cloneOpts.ReferenceName = plumbing.ReferenceName(opts.RefName)
// If the refname points to a tag then try to resolve the tag object and include
// in in the commit being returned. Refname that point to a tag but have the dereference
// suffix aren't considered, since the suffix indicates that the refname is pointing to
// the commit object and not the tag object.
if cloneOpts.ReferenceName.IsTag() && !strings.HasSuffix(opts.RefName, tagDereferenceSuffix) {
tagRef, err := repo.Tag(cloneOpts.ReferenceName.Short())
if err != nil {
return nil, fmt.Errorf("unable to find reference for tag ref '%s': %w", opts.RefName, err)
}

tagObj, err = repo.TagObject(tagRef.Hash())
if err != nil && err != plumbing.ErrObjectNotFound {
return nil, fmt.Errorf("unable to resolve tag object for tag ref '%s' with hash '%s': %w", opts.RefName, tagRef.Hash(), err)
}
}
}
return buildCommitWithRef(cc, nil, cloneOpts.ReferenceName)

g.repository = repo
return buildCommitWithRef(cc, tagObj, cloneOpts.ReferenceName)
}

func (g *Client) cloneSemVer(ctx context.Context, url, semverTag string, opts repository.CloneConfig) (*git.Commit, error) {
Expand Down
4 changes: 4 additions & 0 deletions git/gogit/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,10 @@ func TestClone_cloneRefName(t *testing.T) {
g.Expect(err).ToNot(HaveOccurred())
g.Expect(cc.AbsoluteReference()).To(Equal(tt.refName + "@" + git.HashTypeSHA1 + ":" + tt.expectedCommit))
g.Expect(git.IsConcreteCommit(*cc)).To(Equal(tt.expectedConcreteCommit))
if strings.Contains(tt.refName, "tags") && !strings.HasSuffix(tt.refName, tagDereferenceSuffix) {
g.Expect(cc.ReferencingTag).ToNot(BeNil())
g.Expect(cc.ReferencingTag.Message).To(ContainSubstring("Annotated tag for"))
}

for k, v := range tt.filesCreated {
g.Expect(filepath.Join(tmpDir, k)).To(BeARegularFile())
Expand Down

0 comments on commit c35892e

Please sign in to comment.