Skip to content

Commit

Permalink
Fixed cloning of a single tag
Browse files Browse the repository at this point in the history
Relates to src-d#870

Signed-off-by: Fedor Korotkov <fedor.korotkov@gmail.com>
  • Loading branch information
fkorotkov committed Aug 8, 2018
1 parent 43d17e1 commit ff13275
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
4 changes: 2 additions & 2 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,15 +619,15 @@ func getHaves(
return result, nil
}

const refspecTag = "+refs/tags/*:refs/tags/*"
const refspecAllTags = "+refs/tags/*:refs/tags/*"

func calculateRefs(
spec []config.RefSpec,
remoteRefs storer.ReferenceStorer,
tagMode TagMode,
) (memory.ReferenceStorage, error) {
if tagMode == AllTags {
spec = append(spec, refspecTag)
spec = append(spec, refspecAllTags)
}

refs := make(memory.ReferenceStorage)
Expand Down
6 changes: 3 additions & 3 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ func (r *Repository) clone(ctx context.Context, o *CloneOptions) error {
}

const (
refspecTagWithDepth = "+refs/tags/%s:refs/tags/%[1]s"
refspecTag = "+refs/tags/%s:refs/tags/%[1]s"
refspecSingleBranch = "+refs/heads/%s:refs/remotes/%s/%[1]s"
refspecSingleBranchHEAD = "+HEAD:refs/remotes/%s/HEAD"
)
Expand All @@ -592,8 +592,8 @@ func (r *Repository) cloneRefSpec(o *CloneOptions, c *config.RemoteConfig) []con
var rs string

switch {
case o.ReferenceName.IsTag() && o.Depth > 0:
rs = fmt.Sprintf(refspecTagWithDepth, o.ReferenceName.Short())
case o.ReferenceName.IsTag():
rs = fmt.Sprintf(refspecTag, o.ReferenceName.Short())
case o.SingleBranch && o.ReferenceName == plumbing.HEAD:
rs = fmt.Sprintf(refspecSingleBranchHEAD, c.Name)
case o.SingleBranch:
Expand Down
28 changes: 27 additions & 1 deletion repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,33 @@ func (s *RepositorySuite) TestCloneDetachedHEAD(c *C) {
objects, err := r.Objects()
c.Assert(err, IsNil)
objects.ForEach(func(object.Object) error { count++; return nil })
c.Assert(count, Equals, 31)
c.Assert(count, Equals, 28)
}

func (s *RepositorySuite) TestCloneDetachedHEADAndSingle(c *C) {
r, _ := Init(memory.NewStorage(), nil)
err := r.clone(context.Background(), &CloneOptions{
URL: s.GetBasicLocalRepositoryURL(),
ReferenceName: plumbing.ReferenceName("refs/tags/v1.0.0"),
SingleBranch: true,
})
c.Assert(err, IsNil)

cfg, err := r.Config()
c.Assert(err, IsNil)
c.Assert(cfg.Branches, HasLen, 0)

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, 28)
}

func (s *RepositorySuite) TestCloneDetachedHEADAndShallow(c *C) {
Expand Down

0 comments on commit ff13275

Please sign in to comment.