aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--repository.go34
-rw-r--r--repository_test.go26
2 files changed, 45 insertions, 15 deletions
diff --git a/repository.go b/repository.go
index 9420af9..f548e9a 100644
--- a/repository.go
+++ b/repository.go
@@ -710,8 +710,9 @@ func (r *Repository) clone(ctx context.Context, o *CloneOptions) error {
}
c := &config.RemoteConfig{
- Name: o.RemoteName,
- URLs: []string{o.URL},
+ Name: o.RemoteName,
+ URLs: []string{o.URL},
+ Fetch: r.cloneRefSpec(o),
}
if _, err := r.CreateRemote(c); err != nil {
@@ -719,7 +720,7 @@ func (r *Repository) clone(ctx context.Context, o *CloneOptions) error {
}
ref, err := r.fetchAndUpdateReferences(ctx, &FetchOptions{
- RefSpecs: r.cloneRefSpec(o, c),
+ RefSpecs: c.Fetch,
Depth: o.Depth,
Auth: o.Auth,
Progress: o.Progress,
@@ -789,21 +790,26 @@ const (
refspecSingleBranchHEAD = "+HEAD:refs/remotes/%s/HEAD"
)
-func (r *Repository) cloneRefSpec(o *CloneOptions, c *config.RemoteConfig) []config.RefSpec {
- var rs string
-
+func (r *Repository) cloneRefSpec(o *CloneOptions) []config.RefSpec {
switch {
case o.ReferenceName.IsTag():
- rs = fmt.Sprintf(refspecTag, o.ReferenceName.Short())
+ return []config.RefSpec{
+ config.RefSpec(fmt.Sprintf(refspecTag, o.ReferenceName.Short())),
+ }
case o.SingleBranch && o.ReferenceName == plumbing.HEAD:
- rs = fmt.Sprintf(refspecSingleBranchHEAD, c.Name)
+ return []config.RefSpec{
+ config.RefSpec(fmt.Sprintf(refspecSingleBranchHEAD, o.RemoteName)),
+ config.RefSpec(fmt.Sprintf(refspecSingleBranch, plumbing.Master.Short(), o.RemoteName)),
+ }
case o.SingleBranch:
- rs = fmt.Sprintf(refspecSingleBranch, o.ReferenceName.Short(), c.Name)
+ return []config.RefSpec{
+ config.RefSpec(fmt.Sprintf(refspecSingleBranch, o.ReferenceName.Short(), o.RemoteName)),
+ }
default:
- return c.Fetch
+ return []config.RefSpec{
+ config.RefSpec(fmt.Sprintf(config.DefaultFetchRefSpec, o.RemoteName)),
+ }
}
-
- return []config.RefSpec{config.RefSpec(rs)}
}
func (r *Repository) setIsBare(isBare bool) error {
@@ -821,9 +827,7 @@ func (r *Repository) updateRemoteConfigIfNeeded(o *CloneOptions, c *config.Remot
return nil
}
- c.Fetch = []config.RefSpec{config.RefSpec(fmt.Sprintf(
- refspecSingleBranch, head.Name().Short(), c.Name,
- ))}
+ c.Fetch = r.cloneRefSpec(o)
cfg, err := r.Storer.Config()
if err != nil {
diff --git a/repository_test.go b/repository_test.go
index ba2cf1a..59ab89e 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -919,6 +919,32 @@ func (s *RepositorySuite) TestCloneSingleBranch(c *C) {
c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
}
+func (s *RepositorySuite) TestCloneSingleTag(c *C) {
+ r, _ := Init(memory.NewStorage(), nil)
+
+ url := s.GetLocalRepositoryURL(
+ fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
+ )
+
+ err := r.clone(context.Background(), &CloneOptions{
+ URL: url,
+ SingleBranch: true,
+ ReferenceName: plumbing.ReferenceName("refs/tags/commit-tag"),
+ })
+ c.Assert(err, IsNil)
+
+ branch, err := r.Reference("refs/tags/commit-tag", false)
+ c.Assert(err, IsNil)
+ c.Assert(branch, NotNil)
+
+ conf, err := r.Config()
+ c.Assert(err, IsNil)
+ originRemote := conf.Remotes["origin"]
+ c.Assert(originRemote, NotNil)
+ c.Assert(originRemote.Fetch, HasLen, 1)
+ c.Assert(originRemote.Fetch[0].String(), Equals, "+refs/tags/commit-tag:refs/tags/commit-tag")
+}
+
func (s *RepositorySuite) TestCloneDetachedHEAD(c *C) {
r, _ := Init(memory.NewStorage(), nil)
err := r.clone(context.Background(), &CloneOptions{