diff options
author | Paulo Gomes <pjbgf@linux.com> | 2023-05-03 08:39:07 +0100 |
---|---|---|
committer | Paulo Gomes <pjbgf@linux.com> | 2023-05-03 08:39:07 +0100 |
commit | 19b39e150071832541f2cdcb669446a8609f57e7 (patch) | |
tree | 86f0f043a8176a790c132d2862f8f3f4565c398e /remote.go | |
parent | 0542a302c2be7ed7de276411f7e20b87309734b9 (diff) | |
download | go-git-19b39e150071832541f2cdcb669446a8609f57e7.tar.gz |
git: Add support to ls-remote with peeled references. Fixes #749
A new PeelingOption field was introduced into ListOptions. The new options
include the default (and backwards compatible) IgnorePeeled. Plus another
two variations which either only returns peeled references (OnlyPeeled), or
append peeled references to the list (AppendPeeled).
The ls-remote example was updated to align with upstream, in which peeled
references are appended to the results by default.
A new ErrEmptyUrls error is now returned when List or ListContext do not
receive a URL to work with, to improve overall execution flow.
Signed-off-by: Paulo Gomes <pjbgf@linux.com>
Diffstat (limited to 'remote.go')
-rw-r--r-- | remote.go | 29 |
1 files changed, 23 insertions, 6 deletions
@@ -33,6 +33,7 @@ var ( ErrDeleteRefNotSupported = errors.New("server does not support delete-refs") ErrForceNeeded = errors.New("some refs were not updated") ErrExactSHA1NotSupported = errors.New("server does not support exact SHA1 refspec") + ErrEmptyUrls = errors.New("URLs cannot be empty") ) type NoMatchingRefSpecError struct { @@ -54,6 +55,9 @@ const ( // repo containing this remote, when not using the multi-ack // protocol. Setting this to 0 means there is no limit. maxHavesToVisitPerRef = 100 + + // peeledSuffix is the suffix used to build peeled reference names. + peeledSuffix = "^{}" ) // Remote represents a connection to a remote repository. @@ -1259,6 +1263,10 @@ func (r *Remote) List(o *ListOptions) (rfs []*plumbing.Reference, err error) { } func (r *Remote) list(ctx context.Context, o *ListOptions) (rfs []*plumbing.Reference, err error) { + if r.c == nil || len(r.c.URLs) == 0 { + return nil, ErrEmptyUrls + } + s, err := newUploadPackSession(r.c.URLs[0], o.Auth, o.InsecureSkipTLS, o.CABundle) if err != nil { return nil, err @@ -1282,13 +1290,22 @@ func (r *Remote) list(ctx context.Context, o *ListOptions) (rfs []*plumbing.Refe } var resultRefs []*plumbing.Reference - err = refs.ForEach(func(ref *plumbing.Reference) error { - resultRefs = append(resultRefs, ref) - return nil - }) - if err != nil { - return nil, err + if o.PeelingOption == AppendPeeled || o.PeelingOption == IgnorePeeled { + err = refs.ForEach(func(ref *plumbing.Reference) error { + resultRefs = append(resultRefs, ref) + return nil + }) + if err != nil { + return nil, err + } } + + if o.PeelingOption == AppendPeeled || o.PeelingOption == OnlyPeeled { + for k, v := range ar.Peeled { + resultRefs = append(resultRefs, plumbing.NewReferenceFromStrings(k+"^{}", v.String())) + } + } + return resultRefs, nil } |