Skip to content

Commit

Permalink
Merge pull request #666 from fluxcd/optimize-refname
Browse files Browse the repository at this point in the history
git/gogit: use existing methods to clone branch/tag via refname
  • Loading branch information
aryan9600 authored Oct 17, 2023
2 parents 4292046 + d6f0e93 commit d2d97d5
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions git/gogit/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,28 +262,12 @@ func (g *Client) cloneCommit(ctx context.Context, url, commit string, opts repos
return nil, fmt.Errorf("unable to checkout commit '%s': %w", commit, err)
}

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 the commit being returned. Refnames 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)
}
}
}

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

func (g *Client) cloneSemVer(ctx context.Context, url, semverTag string, opts repository.CloneConfig) (*git.Commit, error) {
Expand Down Expand Up @@ -445,6 +429,24 @@ func (g *Client) cloneRefName(ctx context.Context, url string, refName string, c
}
}

ref := plumbing.ReferenceName(refName)
if ref.IsBranch() {
cloneOpts.LastObservedCommit = ""
return g.cloneBranch(ctx, url, ref.Short(), cloneOpts)
}
if ref.IsTag() {
cloneOpts.LastObservedCommit = ""
// Remove the tag dereference suffix before calling cloneTag() as it automatically
// handles annotated tags.
tagRef := plumbing.ReferenceName(strings.TrimSuffix(refName, tagDereferenceSuffix))
commit, err := g.cloneTag(ctx, url, tagRef.Short(), cloneOpts)
if err != nil {
return nil, err
}
commit.Reference = ref.String()
return commit, nil
}

return g.cloneCommit(ctx, url, hash.String(), cloneOpts)
}

Expand Down

0 comments on commit d2d97d5

Please sign in to comment.