Better default branch selection

4524076222a2e8f9b28835d13f694551292b7a66
Alexis Sellier committed ago 1 parent f58727be
git_cli.go +9 -12
70 70
	}
71 71
	return "", "", "", fmt.Errorf("cannot resolve ref from path segments: %s", strings.Join(segments, "/"))
72 72
}
73 73
74 74
func (g *gitCLIBackend) getDefaultBranch() string {
75 -
	out, err := g.cmd("symbolic-ref", "--short", "HEAD")
76 -
	if err != nil {
77 -
		return "main"
78 -
	}
79 -
	branch := strings.TrimSpace(out)
80 -
81 -
	// Verify the branch actually exists; HEAD may point to a non-existent branch
82 -
	// (e.g. HEAD -> master but only a "main" branch exists).
83 -
	if _, err := g.resolveRef(branch); err == nil {
84 -
		return branch
75 +
	// Use init.defaultBranch from git config.
76 +
	out, err := g.cmd("config", "--get", "init.defaultBranch")
77 +
	if err == nil {
78 +
		branch := strings.TrimSpace(out)
79 +
		if _, err := g.resolveRef(branch); err == nil {
80 +
			return branch
81 +
		}
85 82
	}
86 83
87 -
	// Fall back to the first available branch.
84 +
	// Fall back to the most recently committed branch.
88 85
	branches, err := g.getBranches()
89 86
	if err == nil && len(branches) > 0 {
90 87
		return branches[0].Name
91 88
	}
92 -
	return branch
89 +
	return "master"
93 90
}
94 91
95 92
func (g *gitCLIBackend) getLog(hash string, page, perPage int) ([]CommitInfo, bool, error) {
96 93
	skip := page * perPage
97 94
	out, err := g.cmd("log", "--format=%H%n%h%n%an%n%ae%n%aI%n%s%n%b%x00",
+0 -0