aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-08-02 14:26:58 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2017-08-02 14:26:58 +0200
commit595de2b38d0cee2e0bc92e1a0559f16ccca851dc (patch)
tree46d8f90bf72935e5226fe5ea57063928588a34b3
parent902347aef9a7cd618c48e3baa1121b72e2beab68 (diff)
downloadgo-git-595de2b38d0cee2e0bc92e1a0559f16ccca851dc.tar.gz
Remote.Clone fix clone of tags in shallow mode
-rw-r--r--repository.go29
-rw-r--r--repository_test.go30
2 files changed, 44 insertions, 15 deletions
diff --git a/repository.go b/repository.go
index e2a18c4..72beefd 100644
--- a/repository.go
+++ b/repository.go
@@ -448,20 +448,24 @@ func (r *Repository) clone(ctx context.Context, o *CloneOptions) error {
return r.updateRemoteConfigIfNeeded(o, c, head)
}
-func (r *Repository) cloneRefSpec(o *CloneOptions,
- c *config.RemoteConfig) []config.RefSpec {
-
- if !o.SingleBranch {
- return c.Fetch
- }
+const (
+ refspecTagWithDepth = "+refs/tags/%s:refs/tags/%[1]s"
+ refspecSingleBranch = "+refs/heads/%s:refs/remotes/%s/%[1]s"
+ refspecSingleBranchHEAD = "+HEAD:refs/remotes/%s/HEAD"
+)
+func (r *Repository) cloneRefSpec(o *CloneOptions, c *config.RemoteConfig) []config.RefSpec {
var rs string
- if o.ReferenceName == plumbing.HEAD {
+ switch {
+ case o.ReferenceName.IsTag() && o.Depth > 0:
+ rs = fmt.Sprintf(refspecTagWithDepth, o.ReferenceName.Short())
+ case o.SingleBranch && o.ReferenceName == plumbing.HEAD:
rs = fmt.Sprintf(refspecSingleBranchHEAD, c.Name)
- } else {
- rs = fmt.Sprintf(refspecSingleBranch,
- o.ReferenceName.Short(), c.Name)
+ case o.SingleBranch:
+ rs = fmt.Sprintf(refspecSingleBranch, o.ReferenceName.Short(), c.Name)
+ default:
+ return c.Fetch
}
return []config.RefSpec{config.RefSpec(rs)}
@@ -477,11 +481,6 @@ func (r *Repository) setIsBare(isBare bool) error {
return r.Storer.SetConfig(cfg)
}
-const (
- refspecSingleBranch = "+refs/heads/%s:refs/remotes/%s/%[1]s"
- refspecSingleBranchHEAD = "+HEAD:refs/remotes/%s/HEAD"
-)
-
func (r *Repository) updateRemoteConfigIfNeeded(o *CloneOptions, c *config.RemoteConfig, head *plumbing.Reference) error {
if !o.SingleBranch {
return nil
diff --git a/repository_test.go b/repository_test.go
index 3077807..2fecc98 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -613,12 +613,42 @@ func (s *RepositorySuite) TestCloneDetachedHEAD(c *C) {
URL: s.GetBasicLocalRepositoryURL(),
ReferenceName: plumbing.ReferenceName("refs/tags/v1.0.0"),
})
+ c.Assert(err, IsNil)
+
+ head, err := r.Reference(plumbing.HEAD, false)
+ c.Assert(err, IsNil)
+ c.Assert(head, NotNil)
+ c.Assert(head.Type(), Equals, plumbing.HashReference)
+ c.Assert(head.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
+
+ count := 0
+ objects, err := r.Objects()
+ c.Assert(err, IsNil)
+ objects.ForEach(func(object.Object) error { count++; return nil })
+ c.Assert(count, Equals, 31)
+}
+
+func (s *RepositorySuite) TestCloneDetachedHEADAndShallow(c *C) {
+ r, _ := Init(memory.NewStorage(), memfs.New())
+ err := r.clone(context.Background(), &CloneOptions{
+ URL: s.GetBasicLocalRepositoryURL(),
+ ReferenceName: plumbing.ReferenceName("refs/tags/v1.0.0"),
+ Depth: 1,
+ })
+
+ c.Assert(err, IsNil)
head, err := r.Reference(plumbing.HEAD, false)
c.Assert(err, IsNil)
c.Assert(head, NotNil)
c.Assert(head.Type(), Equals, plumbing.HashReference)
c.Assert(head.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
+
+ count := 0
+ objects, err := r.Objects()
+ c.Assert(err, IsNil)
+ objects.ForEach(func(object.Object) error { count++; return nil })
+ c.Assert(count, Equals, 15)
}
func (s *RepositorySuite) TestPush(c *C) {