diff --git a/pkg/git/init.go b/pkg/git/init.go index fe262780..5861e115 100644 --- a/pkg/git/init.go +++ b/pkg/git/init.go @@ -5,14 +5,17 @@ import ( "coopcloud.tech/abra/pkg/log" "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" ) // Init inits a new repo and commits all the stuff if you want func Init(repoPath string, commit bool, gitName, gitEmail string) error { - if _, err := git.PlainInit(repoPath, false); err != nil { + repo, err := git.PlainInit(repoPath, false) + if err != nil { return fmt.Errorf("git init: %s", err) } + MasterToMain(repo) log.Debugf("initialised new git repo in %s", repoPath) if commit { @@ -42,3 +45,21 @@ func Init(repoPath string, commit bool, gitName, gitEmail string) error { return nil } + +func MasterToMain(repo *git.Repository) { + // Create and set the 'main' branch as default + ref := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.ReferenceName("refs/heads/main")) + if err := repo.Storer.SetReference(ref); err != nil { + log.Fatalf("Failed to set 'main' as default branch: %v", err) + } + // Update the repository configuration + cfg, err := repo.Config() + if err != nil { + log.Fatalf("Failed to get repository config: %v", err) + } + cfg.Init.DefaultBranch = "main" + if err := repo.SetConfig(cfg); err != nil { + log.Fatalf("Failed to update repository config: %v", err) + } + fmt.Println("Set 'main' as the default branch.") +}