Skip to content

Commit 8ecd388

Browse files
committed
Remote.Fetch: support exact SHA1 refspecs
1 parent 21f106e commit 8ecd388

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

remote.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var (
2929
NoErrAlreadyUpToDate = errors.New("already up-to-date")
3030
ErrDeleteRefNotSupported = errors.New("server does not support delete-refs")
3131
ErrForceNeeded = errors.New("some refs were not updated")
32+
ErrExactSHA1NotSupported = errors.New("server does not support exact SHA1 refspec")
3233
)
3334

3435
const (
@@ -303,6 +304,10 @@ func (r *Remote) fetch(ctx context.Context, o *FetchOptions) (sto storer.Referen
303304
return nil, err
304305
}
305306

307+
if err := r.isSupportedRefSpec(o.RefSpecs, ar); err != nil {
308+
return nil, err
309+
}
310+
306311
remoteRefs, err := ar.AllReferences()
307312
if err != nil {
308313
return nil, err
@@ -546,6 +551,7 @@ func (r *Remote) addReferenceIfRefSpecMatches(rs config.RefSpec,
546551

547552
func (r *Remote) references() ([]*plumbing.Reference, error) {
548553
var localRefs []*plumbing.Reference
554+
549555
iter, err := r.s.IterReferences()
550556
if err != nil {
551557
return nil, err
@@ -701,6 +707,11 @@ func doCalculateRefs(
701707
return err
702708
}
703709

710+
if s.IsExactSHA1() {
711+
ref := plumbing.NewHashReference(s.Dst(""), plumbing.NewHash(s.Src()))
712+
return refs.SetReference(ref)
713+
}
714+
704715
var matched bool
705716
err = iter.ForEach(func(ref *plumbing.Reference) error {
706717
if !s.Match(ref.Name()) {
@@ -850,6 +861,26 @@ func (r *Remote) newUploadPackRequest(o *FetchOptions,
850861
return req, nil
851862
}
852863

864+
func (r *Remote) isSupportedRefSpec(refs []config.RefSpec, ar *packp.AdvRefs) error {
865+
var containsIsExact bool
866+
for _, ref := range refs {
867+
if ref.IsExactSHA1() {
868+
containsIsExact = true
869+
}
870+
}
871+
872+
if !containsIsExact {
873+
return nil
874+
}
875+
876+
if ar.Capabilities.Supports(capability.AllowReachableSHA1InWant) ||
877+
ar.Capabilities.Supports(capability.AllowTipSHA1InWant) {
878+
return nil
879+
}
880+
881+
return ErrExactSHA1NotSupported
882+
}
883+
853884
func buildSidebandIfSupported(l *capability.List, reader io.Reader, p sideband.Progress) io.Reader {
854885
var t sideband.Type
855886

@@ -883,7 +914,7 @@ func (r *Remote) updateLocalReferenceStorage(
883914
}
884915

885916
for _, ref := range fetchedRefs {
886-
if !spec.Match(ref.Name()) {
917+
if !spec.Match(ref.Name()) && !spec.IsExactSHA1() {
887918
continue
888919
}
889920

remote_test.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"github.com/go-git/go-git/v5/storage/memory"
2121

2222
"github.com/go-git/go-billy/v5/osfs"
23-
. "gopkg.in/check.v1"
2423
fixtures "github.com/go-git/go-git-fixtures/v4"
24+
. "gopkg.in/check.v1"
2525
)
2626

2727
type RemoteSuite struct {
@@ -71,6 +71,35 @@ func (s *RemoteSuite) TestFetchWildcard(c *C) {
7171
})
7272
}
7373

74+
func (s *RemoteSuite) TestFetchExactSHA1(c *C) {
75+
r := NewRemote(memory.NewStorage(), &config.RemoteConfig{
76+
URLs: []string{"https://github.com/git-fixtures/basic.git"},
77+
})
78+
79+
s.testFetch(c, r, &FetchOptions{
80+
RefSpecs: []config.RefSpec{
81+
config.RefSpec("35e85108805c84807bc66a02d91535e1e24b38b9:refs/heads/foo"),
82+
},
83+
}, []*plumbing.Reference{
84+
plumbing.NewReferenceFromStrings("refs/heads/foo", "35e85108805c84807bc66a02d91535e1e24b38b9"),
85+
})
86+
}
87+
88+
func (s *RemoteSuite) TestFetchExactSHA1_NotSoported(c *C) {
89+
r := NewRemote(memory.NewStorage(), &config.RemoteConfig{
90+
URLs: []string{s.GetBasicLocalRepositoryURL()},
91+
})
92+
93+
err := r.Fetch(&FetchOptions{
94+
RefSpecs: []config.RefSpec{
95+
config.RefSpec("35e85108805c84807bc66a02d91535e1e24b38b9:refs/heads/foo"),
96+
},
97+
})
98+
99+
c.Assert(err, Equals, ErrExactSHA1NotSupported)
100+
101+
}
102+
74103
func (s *RemoteSuite) TestFetchWildcardTags(c *C) {
75104
r := NewRemote(memory.NewStorage(), &config.RemoteConfig{
76105
URLs: []string{s.GetLocalRepositoryURL(fixtures.ByTag("tags").One())},

0 commit comments

Comments
 (0)