Skip to content

Commit cb8f972

Browse files
authored
Merge pull request #134 from troyready/fix_104
Remove hardcoded master branch reference
2 parents c712229 + 3db8f8b commit cb8f972

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

get_git.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package getter
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/base64"
67
"fmt"
@@ -9,6 +10,7 @@ import (
910
"os"
1011
"os/exec"
1112
"path/filepath"
13+
"regexp"
1214
"runtime"
1315
"strconv"
1416
"strings"
@@ -24,6 +26,8 @@ type GitGetter struct {
2426
getter
2527
}
2628

29+
var defaultBranchRegexp = regexp.MustCompile(`\s->\sorigin/(.*)`)
30+
2731
func (g *GitGetter) ClientMode(_ *url.URL) (ClientMode, error) {
2832
return ClientModeDir, nil
2933
}
@@ -182,10 +186,10 @@ func (g *GitGetter) update(ctx context.Context, dst, sshKeyFile, ref string, dep
182186
cmd.Dir = dst
183187

184188
if getRunCommand(cmd) != nil {
185-
// Not a branch, switch to master. This will also catch non-existent
186-
// branches, in which case we want to switch to master and then
187-
// checkout the proper branch later.
188-
ref = "master"
189+
// Not a branch, switch to default branch. This will also catch
190+
// non-existent branches, in which case we want to switch to default
191+
// and then checkout the proper branch later.
192+
ref = findDefaultBranch(dst)
189193
}
190194

191195
// We have to be on a branch to pull
@@ -216,6 +220,22 @@ func (g *GitGetter) fetchSubmodules(ctx context.Context, dst, sshKeyFile string,
216220
return getRunCommand(cmd)
217221
}
218222

223+
// findDefaultBranch checks the repo's origin remote for its default branch
224+
// (generally "master"). "master" is returned if an origin default branch
225+
// can't be determined.
226+
func findDefaultBranch(dst string) string {
227+
var stdoutbuf bytes.Buffer
228+
cmd := exec.Command("git", "branch", "-r", "--points-at", "refs/remotes/origin/HEAD")
229+
cmd.Dir = dst
230+
cmd.Stdout = &stdoutbuf
231+
err := cmd.Run()
232+
matches := defaultBranchRegexp.FindStringSubmatch(stdoutbuf.String())
233+
if err != nil || matches == nil {
234+
return "master"
235+
}
236+
return matches[len(matches)-1]
237+
}
238+
219239
// setupGitEnv sets up the environment for the given command. This is used to
220240
// pass configuration data to git and ssh and enables advanced cloning methods.
221241
func setupGitEnv(cmd *exec.Cmd, sshKeyFile string) {

get_git_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,44 @@ func TestGitGetter_branch(t *testing.T) {
8888
}
8989
}
9090

91+
func TestGitGetter_remoteWithoutMaster(t *testing.T) {
92+
if !testHasGit {
93+
t.Log("git not found, skipping")
94+
t.Skip()
95+
}
96+
97+
g := new(GitGetter)
98+
dst := tempDir(t)
99+
100+
repo := testGitRepo(t, "branch")
101+
repo.git("checkout", "-b", "test-branch")
102+
repo.commitFile("branch.txt", "branch")
103+
104+
q := repo.url.Query()
105+
repo.url.RawQuery = q.Encode()
106+
107+
if err := g.Get(dst, repo.url); err != nil {
108+
t.Fatalf("err: %s", err)
109+
}
110+
111+
// Verify the main file exists
112+
mainPath := filepath.Join(dst, "branch.txt")
113+
if _, err := os.Stat(mainPath); err != nil {
114+
t.Fatalf("err: %s", err)
115+
}
116+
117+
// Get again should work
118+
if err := g.Get(dst, repo.url); err != nil {
119+
t.Fatalf("err: %s", err)
120+
}
121+
122+
// Verify the main file exists
123+
mainPath = filepath.Join(dst, "branch.txt")
124+
if _, err := os.Stat(mainPath); err != nil {
125+
t.Fatalf("err: %s", err)
126+
}
127+
}
128+
91129
func TestGitGetter_shallowClone(t *testing.T) {
92130
if !testHasGit {
93131
t.Log("git not found, skipping")
@@ -380,6 +418,7 @@ func TestGitGetter_sshExplicitPort(t *testing.T) {
380418
}
381419
}
382420

421+
383422
func TestGitGetter_sshSCPStyleInvalidScheme(t *testing.T) {
384423
if !testHasGit {
385424
t.Skip("git not found, skipping")

0 commit comments

Comments
 (0)