Skip to content

Commit 3db8f8b

Browse files
committed
fallback to master branch if origin default branch can't be found
Signed-off-by: troyready <[email protected]>
1 parent 196a58c commit 3db8f8b

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

get_git.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,18 @@ func (g *GitGetter) fetchSubmodules(ctx context.Context, dst, sshKeyFile string,
221221
}
222222

223223
// findDefaultBranch checks the repo's origin remote for its default branch
224-
// (generally "master")
224+
// (generally "master"). "master" is returned if an origin default branch
225+
// can't be determined.
225226
func findDefaultBranch(dst string) string {
226227
var stdoutbuf bytes.Buffer
227228
cmd := exec.Command("git", "branch", "-r", "--points-at", "refs/remotes/origin/HEAD")
228229
cmd.Dir = dst
229230
cmd.Stdout = &stdoutbuf
230-
cmd.Run()
231+
err := cmd.Run()
231232
matches := defaultBranchRegexp.FindStringSubmatch(stdoutbuf.String())
233+
if err != nil || matches == nil {
234+
return "master"
235+
}
232236
return matches[len(matches)-1]
233237
}
234238

get_git_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ func TestGitGetter_sshExplicitPort(t *testing.T) {
418418
}
419419
}
420420

421+
421422
func TestGitGetter_sshSCPStyleInvalidScheme(t *testing.T) {
422423
if !testHasGit {
423424
t.Skip("git not found, skipping")

0 commit comments

Comments
 (0)