feat: set 'main' as default branch instead of 'master'

This commit is contained in:
Yar Kravtsov
2025-05-24 06:36:26 +03:00
parent d5812e1085
commit 11cca86f4d
3 changed files with 27 additions and 4 deletions

View File

@@ -22,12 +22,28 @@ func New(repoPath string) *Git {
// Init initializes a new Git repository
func (g *Git) Init() error {
cmd := exec.Command("git", "init")
// Try using git init -b main first (Git 2.28+)
cmd := exec.Command("git", "init", "-b", "main")
cmd.Dir = g.repoPath
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("git init failed: %w\nOutput: %s", err, string(output))
// Fallback to regular init + branch rename for older Git versions
cmd = exec.Command("git", "init")
cmd.Dir = g.repoPath
output, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("git init failed: %w\nOutput: %s", err, string(output))
}
// Set the default branch to main
cmd = exec.Command("git", "symbolic-ref", "HEAD", "refs/heads/main")
cmd.Dir = g.repoPath
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to set default branch to main: %w", err)
}
}
return nil