Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 770800d

Browse files
authored
Merge pull request #576 from mcuadros/clone-tags
Repository.Clone added Tags option, and set by default AllTags
2 parents c20028f + 7cdc443 commit 770800d

File tree

5 files changed

+57
-13
lines changed

5 files changed

+57
-13
lines changed

options.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ type CloneOptions struct {
5050
// stored, if nil nothing is stored and the capability (if supported)
5151
// no-progress, is sent to the server to avoid send this information.
5252
Progress sideband.Progress
53+
// Tags describe how the tags will be fetched from the remote repository,
54+
// by default is AllTags.
55+
Tags TagMode
5356
}
5457

5558
// Validate validates the fields and sets the default values.
@@ -66,6 +69,10 @@ func (o *CloneOptions) Validate() error {
6669
o.ReferenceName = plumbing.HEAD
6770
}
6871

72+
if o.Tags == InvalidTagMode {
73+
o.Tags = AllTags
74+
}
75+
6976
return nil
7077
}
7178

@@ -103,18 +110,19 @@ func (o *PullOptions) Validate() error {
103110
return nil
104111
}
105112

106-
type TagFetchMode int
113+
type TagMode int
107114

108-
var (
115+
const (
116+
InvalidTagMode TagMode = iota
109117
// TagFollowing any tag that points into the histories being fetched is also
110118
// fetched. TagFollowing requires a server with `include-tag` capability
111119
// in order to fetch the annotated tags objects.
112-
TagFollowing TagFetchMode = 0
120+
TagFollowing
113121
// AllTags fetch all tags from the remote (i.e., fetch remote tags
114122
// refs/tags/* into local tags with the same name)
115-
AllTags TagFetchMode = 1
123+
AllTags
116124
//NoTags fetch no tags from the remote at all
117-
NoTags TagFetchMode = 2
125+
NoTags
118126
)
119127

120128
// FetchOptions describes how a fetch should be performed
@@ -133,7 +141,7 @@ type FetchOptions struct {
133141
Progress sideband.Progress
134142
// Tags describe how the tags will be fetched from the remote repository,
135143
// by default is TagFollowing.
136-
Tags TagFetchMode
144+
Tags TagMode
137145
}
138146

139147
// Validate validates the fields and sets the default values.
@@ -142,6 +150,10 @@ func (o *FetchOptions) Validate() error {
142150
o.RemoteName = DefaultRemoteName
143151
}
144152

153+
if o.Tags == InvalidTagMode {
154+
o.Tags = TagFollowing
155+
}
156+
145157
for _, r := range o.RefSpecs {
146158
if err := r.Validate(); err != nil {
147159
return err

remote.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func (r *Remote) fetch(ctx context.Context, o *FetchOptions) (storer.ReferenceSt
279279
}
280280
}
281281

282-
updated, err := r.updateLocalReferenceStorage(o.RefSpecs, refs, remoteRefs)
282+
updated, err := r.updateLocalReferenceStorage(o.RefSpecs, refs, remoteRefs, o.Tags)
283283
if err != nil {
284284
return nil, err
285285
}
@@ -481,10 +481,17 @@ func getHaves(localRefs storer.ReferenceStorer) ([]plumbing.Hash, error) {
481481
return result, nil
482482
}
483483

484-
func calculateRefs(spec []config.RefSpec,
484+
const refspecTag = "+refs/tags/*:refs/tags/*"
485+
486+
func calculateRefs(
487+
spec []config.RefSpec,
485488
remoteRefs storer.ReferenceStorer,
486-
tags TagFetchMode,
489+
tagMode TagMode,
487490
) (memory.ReferenceStorage, error) {
491+
if tagMode == AllTags {
492+
spec = append(spec, refspecTag)
493+
}
494+
488495
iter, err := remoteRefs.IterReferences()
489496
if err != nil {
490497
return nil, err
@@ -493,9 +500,7 @@ func calculateRefs(spec []config.RefSpec,
493500
refs := make(memory.ReferenceStorage, 0)
494501
return refs, iter.ForEach(func(ref *plumbing.Reference) error {
495502
if !config.MatchAny(spec, ref.Name()) {
496-
if !ref.Name().IsTag() || tags != AllTags {
497-
return nil
498-
}
503+
return nil
499504
}
500505

501506
if ref.Type() == plumbing.SymbolicReference {
@@ -645,6 +650,7 @@ func buildSidebandIfSupported(l *capability.List, reader io.Reader, p sideband.P
645650
func (r *Remote) updateLocalReferenceStorage(
646651
specs []config.RefSpec,
647652
fetchedRefs, remoteRefs memory.ReferenceStorage,
653+
tagMode TagMode,
648654
) (updated bool, err error) {
649655
isWildcard := true
650656
for _, spec := range specs {
@@ -674,6 +680,10 @@ func (r *Remote) updateLocalReferenceStorage(
674680
}
675681
}
676682

683+
if tagMode == NoTags {
684+
return updated, nil
685+
}
686+
677687
tags := fetchedRefs
678688
if isWildcard {
679689
tags = remoteRefs

remote_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (s *RemoteSuite) TestFetchWithNoTags(c *C) {
143143
s.testFetch(c, r, &FetchOptions{
144144
Tags: NoTags,
145145
RefSpecs: []config.RefSpec{
146-
config.RefSpec("+refs/heads/master:refs/remotes/origin/master"),
146+
config.RefSpec("+refs/heads/*:refs/remotes/origin/*"),
147147
},
148148
}, []*plumbing.Reference{
149149
plumbing.NewReferenceFromStrings("refs/remotes/origin/master", "f7b877701fbf855b44c0a9e86f3fdce2c298b07f"),

repository.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ func (r *Repository) clone(ctx context.Context, o *CloneOptions) error {
440440
Depth: o.Depth,
441441
Auth: o.Auth,
442442
Progress: o.Progress,
443+
Tags: o.Tags,
443444
}, o.ReferenceName)
444445
if err != nil {
445446
return err

repository_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,27 @@ func (s *RepositorySuite) TestCloneContext(c *C) {
177177
c.Assert(err, NotNil)
178178
}
179179

180+
func (s *RepositorySuite) TestCloneWithTags(c *C) {
181+
url := s.GetLocalRepositoryURL(
182+
fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
183+
)
184+
185+
r, err := Clone(memory.NewStorage(), nil, &CloneOptions{URL: url, Tags: NoTags})
186+
c.Assert(err, IsNil)
187+
188+
remotes, err := r.Remotes()
189+
c.Assert(err, IsNil)
190+
c.Assert(remotes, HasLen, 1)
191+
192+
i, err := r.References()
193+
c.Assert(err, IsNil)
194+
195+
var count int
196+
i.ForEach(func(r *plumbing.Reference) error { count++; return nil })
197+
198+
c.Assert(count, Equals, 3)
199+
}
200+
180201
func (s *RepositorySuite) TestCreateRemoteAndRemote(c *C) {
181202
r, _ := Init(memory.NewStorage(), nil)
182203
remote, err := r.CreateRemote(&config.RemoteConfig{

0 commit comments

Comments
 (0)