Skip to content

Commit 568154c

Browse files
authored
Merge pull request go-git#58 from go-git/exact-sha1
Remote.Fetch: support exact SHA1 refspecs
2 parents e4166c5 + 8ecd388 commit 568154c

File tree

6 files changed

+100
-5
lines changed

6 files changed

+100
-5
lines changed

config/refspec.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var (
2525
// reference even if it isn’t a fast-forward.
2626
// eg.: "+refs/heads/*:refs/remotes/origin/*"
2727
//
28-
// https://git-scm.com/book/es/v2/Git-Internals-The-Refspec
28+
// https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
2929
type RefSpec string
3030

3131
// Validate validates the RefSpec
@@ -59,6 +59,11 @@ func (s RefSpec) IsDelete() bool {
5959
return s[0] == refSpecSeparator[0]
6060
}
6161

62+
// IsExactSHA1 returns true if the source is a SHA1 hash.
63+
func (s RefSpec) IsExactSHA1() bool {
64+
return plumbing.IsHash(s.Src())
65+
}
66+
6267
// Src return the src side.
6368
func (s RefSpec) Src() string {
6469
spec := string(s)
@@ -69,8 +74,8 @@ func (s RefSpec) Src() string {
6974
} else {
7075
start = 0
7176
}
72-
end := strings.Index(spec, refSpecSeparator)
7377

78+
end := strings.Index(spec, refSpecSeparator)
7479
return spec[start:end]
7580
}
7681

config/refspec_test.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package config
33
import (
44
"testing"
55

6-
. "gopkg.in/check.v1"
76
"github.com/go-git/go-git/v5/plumbing"
7+
. "gopkg.in/check.v1"
88
)
99

1010
type RefSpecSuite struct{}
@@ -37,6 +37,12 @@ func (s *RefSpecSuite) TestRefSpecIsValid(c *C) {
3737

3838
spec = RefSpec("refs/heads:")
3939
c.Assert(spec.Validate(), Equals, ErrRefSpecMalformedSeparator)
40+
41+
spec = RefSpec("12039e008f9a4e3394f3f94f8ea897785cb09448:refs/heads/foo")
42+
c.Assert(spec.Validate(), Equals, nil)
43+
44+
spec = RefSpec("12039e008f9a4e3394f3f94f8ea897785cb09448:refs/heads/*")
45+
c.Assert(spec.Validate(), Equals, ErrRefSpecMalformedWildcard)
4046
}
4147

4248
func (s *RefSpecSuite) TestRefSpecIsForceUpdate(c *C) {
@@ -58,6 +64,14 @@ func (s *RefSpecSuite) TestRefSpecIsDelete(c *C) {
5864
c.Assert(spec.IsDelete(), Equals, false)
5965
}
6066

67+
func (s *RefSpecSuite) TestRefSpecIsExactSHA1(c *C) {
68+
spec := RefSpec("foo:refs/heads/master")
69+
c.Assert(spec.IsExactSHA1(), Equals, false)
70+
71+
spec = RefSpec("12039e008f9a4e3394f3f94f8ea897785cb09448:refs/heads/foo")
72+
c.Assert(spec.IsExactSHA1(), Equals, true)
73+
}
74+
6175
func (s *RefSpecSuite) TestRefSpecSrc(c *C) {
6276
spec := RefSpec("refs/heads/*:refs/remotes/origin/*")
6377
c.Assert(spec.Src(), Equals, "refs/heads/*")

plumbing/hash.go

+10
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,13 @@ type HashSlice []Hash
7171
func (p HashSlice) Len() int { return len(p) }
7272
func (p HashSlice) Less(i, j int) bool { return bytes.Compare(p[i][:], p[j][:]) < 0 }
7373
func (p HashSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
74+
75+
// IsHash returns true if the given string is a valid hash.
76+
func IsHash(s string) bool {
77+
if len(s) != 40 {
78+
return false
79+
}
80+
81+
_, err := hex.DecodeString(s)
82+
return err == nil
83+
}

plumbing/hash_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,9 @@ func (s *HashSuite) TestHashesSort(c *C) {
5252
c.Assert(i[0], Equals, NewHash("1111111111111111111111111111111111111111"))
5353
c.Assert(i[1], Equals, NewHash("2222222222222222222222222222222222222222"))
5454
}
55+
56+
func (s *HashSuite) TestIsHash(c *C) {
57+
c.Assert(IsHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"), Equals, true)
58+
c.Assert(IsHash("foo"), Equals, false)
59+
c.Assert(IsHash("zab686eafeb1f44702738c8b0f24f2567c36da6d"), Equals, false)
60+
}

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)