Fix some bugs around the default branch

2757cc279058366d1672ddf2f1b91d8373a9f069
Alexis Sellier committed ago 1 parent 924b4f0d
git_cli.go +11 -2
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 +
	// Use HEAD's symbolic ref (the branch HEAD points to).
76 +
	out, err := g.cmd("symbolic-ref", "--short", "HEAD")
77 +
	if err == nil {
78 +
		branch := strings.TrimSpace(out)
79 +
		if _, err := g.resolveRef(branch); err == nil {
80 +
			return branch
81 +
		}
82 +
	}
83 +
75 84
	// Use init.defaultBranch from git config.
76 -
	out, err := g.cmd("config", "--get", "init.defaultBranch")
85 +
	out, err = g.cmd("config", "--get", "init.defaultBranch")
77 86
	if err == nil {
78 87
		branch := strings.TrimSpace(out)
79 88
		if _, err := g.resolveRef(branch); err == nil {
80 89
			return branch
81 90
		}
82 91
	}
83 92
84 -
	// Fall back to the most recently committed branch.
93 +
	// Fall back to the first available branch.
85 94
	branches, err := g.getBranches()
86 95
	if err == nil && len(branches) > 0 {
87 96
		return branches[0].Name
88 97
	}
89 98
	return "master"
+0 -0