new recipe default branch main instead of master #451

Merged
decentral1se merged 4 commits from defaultBranch into main 2024-12-21 18:11:13 +00:00
2 changed files with 60 additions and 1 deletions

View File

@ -5,14 +5,19 @@ 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)
}
if err = SwitchToMain(repo); err != nil {
ammaratef45 marked this conversation as resolved Outdated

Since Init returns an error, MasterToMain should also do the same to bubble up the errors.

Since `Init` returns an `error`, `MasterToMain` should also do the same to bubble up the errors.

Also, since the whole idea is to remove the word master, we could do SwitchToMain as the function name?

Also, since the whole idea is to remove the word `master`, we could do `SwitchToMain` as the function name?

ah good idea!

ah good idea!
return fmt.Errorf("git branch rename: %s", err)
}
log.Debugf("initialised new git repo in %s", repoPath)
if commit {
@ -42,3 +47,21 @@ func Init(repoPath string, commit bool, gitName, gitEmail string) error {
return nil
}
ammaratef45 marked this conversation as resolved Outdated

I'd really appreciate a unit / integration test for this one! To ensure it works, to avoid regressions etc.

I'd really appreciate a unit / integration test for this one! To ensure it works, to avoid regressions etc.

added, I ran out of spoons during the first draft but should've made it a WIP PR and got back to it rather than submitting it without the test ig

added, I ran out of spoons during the first draft but should've made it a WIP PR and got back to it rather than submitting it without the test ig
// Set the default branch for the passed repo as "main"
func SwitchToMain(repo *git.Repository) error {
ref := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.ReferenceName("refs/heads/main"))
if err := repo.Storer.SetReference(ref); err != nil {
return fmt.Errorf("set reference: %s", err)
}
cfg, err := repo.Config()
if err != nil {
return fmt.Errorf("repo config: %s", err)
}
cfg.Init.DefaultBranch = "main"
if err := repo.SetConfig(cfg); err != nil {
return fmt.Errorf("repo set config: %s", err)
}
ammaratef45 marked this conversation as resolved Outdated

We normally use slog to do logging? Do you want to do INFO or DEBUG here? You'll see it used elsewhere instead of fmt.Println.

Also, all logging uses lowercase AFAIR, so it's good to follow this convention.

We normally use `slog` to do logging? Do you want to do `INFO` or `DEBUG` here? You'll see it used elsewhere instead of `fmt.Println`. Also, all logging uses lowercase AFAIR, so it's good to follow this convention.

rip, copy-pasting code then forgetting to clean up is embarrassing lol

rip, copy-pasting code then forgetting to clean up is embarrassing lol
log.Debug("set 'main' as the default branch.")
return nil
}

36
pkg/git/init_test.go Normal file
View File

@ -0,0 +1,36 @@
package git
import (
"testing"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
)
// Verify that SwitchToMain has the desired effect on the repo
func TestSwitchToMain(t *testing.T) {
storage := memory.NewStorage()
repo, err := git.Init(storage, nil)
if err != nil {
t.Fatalf("failed to create in-memory repository: %v", err)
}
err = SwitchToMain(repo)
if err != nil {
t.Fatalf("SwitchToMain failed: %v", err)
}
ref, err := repo.Reference(plumbing.HEAD, false)
if err != nil {
t.Fatalf("failed to get HEAD reference: %v", err)
}
if ref.Target().String() != "refs/heads/main" {
t.Errorf("expected HEAD to point to 'refs/heads/main', got %s", ref.Target().String())
}
cfg, err := repo.Config()
if err != nil {
t.Fatalf("failed to get repository config: %v", err)
}
if cfg.Init.DefaultBranch != "main" {
t.Errorf("expected default branch to be 'main', got %s", cfg.Init.DefaultBranch)
}
}