From 9510c04aeb49261f729b0cc2d4dac8e29e184e87 Mon Sep 17 00:00:00 2001 From: Ammar Hussein Date: Thu, 12 Dec 2024 19:08:18 -0800 Subject: [PATCH] new recipe default branch main instead of master --- pkg/git/init.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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.") +}