From 8ecd388ae101a0dd88b78dc47ebfef9fe51699df Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Mon, 11 May 2020 00:17:29 +0200 Subject: Remote.Fetch: support exact SHA1 refspecs --- remote.go | 33 ++++++++++++++++++++++++++++++++- remote_test.go | 31 ++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/remote.go b/remote.go index 242df0d..98c4acf 100644 --- a/remote.go +++ b/remote.go @@ -29,6 +29,7 @@ var ( NoErrAlreadyUpToDate = errors.New("already up-to-date") 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") ) const ( @@ -303,6 +304,10 @@ func (r *Remote) fetch(ctx context.Context, o *FetchOptions) (sto storer.Referen return nil, err } + if err := r.isSupportedRefSpec(o.RefSpecs, ar); err != nil { + return nil, err + } + remoteRefs, err := ar.AllReferences() if err != nil { return nil, err @@ -546,6 +551,7 @@ func (r *Remote) addReferenceIfRefSpecMatches(rs config.RefSpec, func (r *Remote) references() ([]*plumbing.Reference, error) { var localRefs []*plumbing.Reference + iter, err := r.s.IterReferences() if err != nil { return nil, err @@ -701,6 +707,11 @@ func doCalculateRefs( return err } + if s.IsExactSHA1() { + ref := plumbing.NewHashReference(s.Dst(""), plumbing.NewHash(s.Src())) + return refs.SetReference(ref) + } + var matched bool err = iter.ForEach(func(ref *plumbing.Reference) error { if !s.Match(ref.Name()) { @@ -850,6 +861,26 @@ func (r *Remote) newUploadPackRequest(o *FetchOptions, return req, nil } +func (r *Remote) isSupportedRefSpec(refs []config.RefSpec, ar *packp.AdvRefs) error { + var containsIsExact bool + for _, ref := range refs { + if ref.IsExactSHA1() { + containsIsExact = true + } + } + + if !containsIsExact { + return nil + } + + if ar.Capabilities.Supports(capability.AllowReachableSHA1InWant) || + ar.Capabilities.Supports(capability.AllowTipSHA1InWant) { + return nil + } + + return ErrExactSHA1NotSupported +} + func buildSidebandIfSupported(l *capability.List, reader io.Reader, p sideband.Progress) io.Reader { var t sideband.Type @@ -883,7 +914,7 @@ func (r *Remote) updateLocalReferenceStorage( } for _, ref := range fetchedRefs { - if !spec.Match(ref.Name()) { + if !spec.Match(ref.Name()) && !spec.IsExactSHA1() { continue } diff --git a/remote_test.go b/remote_test.go index 6766514..0fc3449 100644 --- a/remote_test.go +++ b/remote_test.go @@ -20,8 +20,8 @@ import ( "github.com/go-git/go-git/v5/storage/memory" "github.com/go-git/go-billy/v5/osfs" - . "gopkg.in/check.v1" fixtures "github.com/go-git/go-git-fixtures/v4" + . "gopkg.in/check.v1" ) type RemoteSuite struct { @@ -71,6 +71,35 @@ func (s *RemoteSuite) TestFetchWildcard(c *C) { }) } +func (s *RemoteSuite) TestFetchExactSHA1(c *C) { + r := NewRemote(memory.NewStorage(), &config.RemoteConfig{ + URLs: []string{"https://github.com/git-fixtures/basic.git"}, + }) + + s.testFetch(c, r, &FetchOptions{ + RefSpecs: []config.RefSpec{ + config.RefSpec("35e85108805c84807bc66a02d91535e1e24b38b9:refs/heads/foo"), + }, + }, []*plumbing.Reference{ + plumbing.NewReferenceFromStrings("refs/heads/foo", "35e85108805c84807bc66a02d91535e1e24b38b9"), + }) +} + +func (s *RemoteSuite) TestFetchExactSHA1_NotSoported(c *C) { + r := NewRemote(memory.NewStorage(), &config.RemoteConfig{ + URLs: []string{s.GetBasicLocalRepositoryURL()}, + }) + + err := r.Fetch(&FetchOptions{ + RefSpecs: []config.RefSpec{ + config.RefSpec("35e85108805c84807bc66a02d91535e1e24b38b9:refs/heads/foo"), + }, + }) + + c.Assert(err, Equals, ErrExactSHA1NotSupported) + +} + func (s *RemoteSuite) TestFetchWildcardTags(c *C) { r := NewRemote(memory.NewStorage(), &config.RemoteConfig{ URLs: []string{s.GetLocalRepositoryURL(fixtures.ByTag("tags").One())}, -- cgit