Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down