Skip to content

Commit 4220659

Browse files
committed
cmd/go: do not force use of git master branch (again)
This time with a test. Also adjust another test to skip when hg is not present, and delete no longer needed fixDetachedHead code. Fixes #9032 (again). Change-Id: I481717409e1d44b524f83c70a8dc377699d1a2a5 Reviewed-on: https://go-review.googlesource.com/18334 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 19e27c7 commit 4220659

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

src/cmd/go/go_test.go

+31-1
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,6 @@ func TestImportCommentConflict(t *testing.T) {
10631063
// cmd/go: custom import path checking should not apply to github.com/xxx/yyy.
10641064
func TestIssue10952(t *testing.T) {
10651065
testenv.MustHaveExternalNetwork(t)
1066-
10671066
if _, err := exec.LookPath("git"); err != nil {
10681067
t.Skip("skipping because git binary not found")
10691068
}
@@ -1081,6 +1080,34 @@ func TestIssue10952(t *testing.T) {
10811080
tg.run("get", "-d", "-u", importPath)
10821081
}
10831082

1083+
func TestGetGitDefaultBranch(t *testing.T) {
1084+
testenv.MustHaveExternalNetwork(t)
1085+
if _, err := exec.LookPath("git"); err != nil {
1086+
t.Skip("skipping because git binary not found")
1087+
}
1088+
1089+
tg := testgo(t)
1090+
defer tg.cleanup()
1091+
tg.parallel()
1092+
tg.tempDir("src")
1093+
tg.setenv("GOPATH", tg.path("."))
1094+
1095+
// This repo has two branches, master and another-branch.
1096+
// The another-branch is the default that you get from 'git clone'.
1097+
// The go get command variants should not override this.
1098+
const importPath = "github.com/rsc/go-get-default-branch"
1099+
1100+
tg.run("get", "-d", importPath)
1101+
repoDir := tg.path("src/" + importPath)
1102+
defer tg.resetReadOnlyFlagAll(repoDir)
1103+
tg.runGit(repoDir, "branch", "--contains", "HEAD")
1104+
tg.grepStdout(`\* another-branch`, "not on correct default branch")
1105+
1106+
tg.run("get", "-d", "-u", importPath)
1107+
tg.runGit(repoDir, "branch", "--contains", "HEAD")
1108+
tg.grepStdout(`\* another-branch`, "not on correct default branch")
1109+
}
1110+
10841111
func TestDisallowedCSourceFiles(t *testing.T) {
10851112
tg := testgo(t)
10861113
defer tg.cleanup()
@@ -2219,6 +2246,9 @@ func TestGoGetInsecureCustomDomain(t *testing.T) {
22192246

22202247
func TestIssue10193(t *testing.T) {
22212248
testenv.MustHaveExternalNetwork(t)
2249+
if _, err := exec.LookPath("hg"); err != nil {
2250+
t.Skip("skipping because hg binary not found")
2251+
}
22222252

22232253
tg := testgo(t)
22242254
defer tg.cleanup()

src/cmd/go/vcs.go

+2-28
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ var vcsGit = &vcsCmd{
137137
// both createCmd and downloadCmd update the working dir.
138138
// No need to do more here. We used to 'checkout master'
139139
// but that doesn't work if the default branch is not named master.
140+
// DO NOT add 'checkout master' here.
140141
// See golang.org/issue/9032.
141-
tagSyncDefault: []string{"checkout master", "submodule update --init --recursive"},
142+
tagSyncDefault: []string{"submodule update --init --recursive"},
142143

143144
scheme: []string{"git", "https", "http", "git+ssh", "ssh"},
144145
pingCmd: "ls-remote {scheme}://{repo}",
@@ -385,9 +386,6 @@ func (v *vcsCmd) create(dir, repo string) error {
385386

386387
// download downloads any new changes for the repo in dir.
387388
func (v *vcsCmd) download(dir string) error {
388-
if err := v.fixDetachedHead(dir); err != nil {
389-
return err
390-
}
391389
for _, cmd := range v.downloadCmd {
392390
if !go15VendorExperiment && strings.Contains(cmd, "submodule") {
393391
continue
@@ -399,30 +397,6 @@ func (v *vcsCmd) download(dir string) error {
399397
return nil
400398
}
401399

402-
// fixDetachedHead switches a Git repository in dir from a detached head to the master branch.
403-
// Go versions before 1.2 downloaded Git repositories in an unfortunate way
404-
// that resulted in the working tree state being on a detached head.
405-
// That meant the repository was not usable for normal Git operations.
406-
// Go 1.2 fixed that, but we can't pull into a detached head, so if this is
407-
// a Git repository we check for being on a detached head and switch to the
408-
// real branch, almost always called "master".
409-
// TODO(dsymonds): Consider removing this for Go 1.3.
410-
func (v *vcsCmd) fixDetachedHead(dir string) error {
411-
if v != vcsGit {
412-
return nil
413-
}
414-
415-
// "git symbolic-ref HEAD" succeeds iff we are not on a detached head.
416-
if err := v.runVerboseOnly(dir, "symbolic-ref HEAD"); err == nil {
417-
// not on a detached head
418-
return nil
419-
}
420-
if buildV {
421-
log.Printf("%s on detached head; repairing", dir)
422-
}
423-
return v.run(dir, "checkout master")
424-
}
425-
426400
// tags returns the list of available tags for the repo in dir.
427401
func (v *vcsCmd) tags(dir string) ([]string, error) {
428402
var tags []string

0 commit comments

Comments
 (0)