Skip to content

Commit 87d35b7

Browse files
authored
Merge pull request #758 from AriehSchneier/head-not-master
git: Clone HEAD should not force master. Fixes #363
2 parents d37c8b9 + 5889b75 commit 87d35b7

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

repository.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,6 @@ func (r *Repository) cloneRefSpec(o *CloneOptions) []config.RefSpec {
967967
case o.SingleBranch && o.ReferenceName == plumbing.HEAD:
968968
return []config.RefSpec{
969969
config.RefSpec(fmt.Sprintf(refspecSingleBranchHEAD, o.RemoteName)),
970-
config.RefSpec(fmt.Sprintf(refspecSingleBranch, plumbing.Master.Short(), o.RemoteName)),
971970
}
972971
case o.SingleBranch:
973972
return []config.RefSpec{

repository_test.go

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,49 @@ func (s *RepositorySuite) testCloneSingleBranchAndNonHEADReference(c *C, ref str
11191119
c.Assert(branch.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881")
11201120
}
11211121

1122+
func (s *RepositorySuite) TestCloneSingleBranchHEADMain(c *C) {
1123+
r, _ := Init(memory.NewStorage(), nil)
1124+
1125+
head, err := r.Head()
1126+
c.Assert(err, Equals, plumbing.ErrReferenceNotFound)
1127+
c.Assert(head, IsNil)
1128+
1129+
err = r.clone(context.Background(), &CloneOptions{
1130+
URL: s.GetLocalRepositoryURL(fixtures.ByTag("no-master-head").One()),
1131+
SingleBranch: true,
1132+
})
1133+
1134+
c.Assert(err, IsNil)
1135+
1136+
remotes, err := r.Remotes()
1137+
c.Assert(err, IsNil)
1138+
c.Assert(remotes, HasLen, 1)
1139+
1140+
cfg, err := r.Config()
1141+
c.Assert(err, IsNil)
1142+
c.Assert(cfg.Branches, HasLen, 1)
1143+
c.Assert(cfg.Branches["main"].Name, Equals, "main")
1144+
c.Assert(cfg.Branches["main"].Remote, Equals, "origin")
1145+
c.Assert(cfg.Branches["main"].Merge, Equals, plumbing.ReferenceName("refs/heads/main"))
1146+
1147+
head, err = r.Reference(plumbing.HEAD, false)
1148+
c.Assert(err, IsNil)
1149+
c.Assert(head, NotNil)
1150+
c.Assert(head.Type(), Equals, plumbing.SymbolicReference)
1151+
c.Assert(head.Target().String(), Equals, "refs/heads/main")
1152+
1153+
branch, err := r.Reference(head.Target(), false)
1154+
c.Assert(err, IsNil)
1155+
c.Assert(branch, NotNil)
1156+
c.Assert(branch.Hash().String(), Equals, "786dafbd351e587da1ae97e5fb9fbdf868b4a28f")
1157+
1158+
branch, err = r.Reference("refs/remotes/origin/HEAD", false)
1159+
c.Assert(err, IsNil)
1160+
c.Assert(branch, NotNil)
1161+
c.Assert(branch.Type(), Equals, plumbing.HashReference)
1162+
c.Assert(branch.Hash().String(), Equals, "786dafbd351e587da1ae97e5fb9fbdf868b4a28f")
1163+
}
1164+
11221165
func (s *RepositorySuite) TestCloneSingleBranch(c *C) {
11231166
r, _ := Init(memory.NewStorage(), nil)
11241167

@@ -1154,12 +1197,6 @@ func (s *RepositorySuite) TestCloneSingleBranch(c *C) {
11541197
c.Assert(err, IsNil)
11551198
c.Assert(branch, NotNil)
11561199
c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
1157-
1158-
branch, err = r.Reference("refs/remotes/origin/master", false)
1159-
c.Assert(err, IsNil)
1160-
c.Assert(branch, NotNil)
1161-
c.Assert(branch.Type(), Equals, plumbing.HashReference)
1162-
c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
11631200
}
11641201

11651202
func (s *RepositorySuite) TestCloneSingleTag(c *C) {

0 commit comments

Comments
 (0)