From 9510c04aeb49261f729b0cc2d4dac8e29e184e87 Mon Sep 17 00:00:00 2001 From: Ammar Hussein Date: Thu, 12 Dec 2024 19:08:18 -0800 Subject: [PATCH 1/4] 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.") +} -- 2.49.0 From 8d076a308aca62ea6f186bc073ed775bf21fd427 Mon Sep 17 00:00:00 2001 From: Ammar Hussein Date: Sat, 14 Dec 2024 18:26:22 -0800 Subject: [PATCH 2/4] bubble up errors on branch switch --- pkg/git/init.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/git/init.go b/pkg/git/init.go index 5861e115..89454692 100644 --- a/pkg/git/init.go +++ b/pkg/git/init.go @@ -15,7 +15,9 @@ func Init(repoPath string, commit bool, gitName, gitEmail string) error { if err != nil { return fmt.Errorf("git init: %s", err) } - MasterToMain(repo) + if err = SwitchToMain(repo); err != nil { + return fmt.Errorf("git branch rename: %s", err) + } log.Debugf("initialised new git repo in %s", repoPath) if commit { @@ -46,20 +48,21 @@ func Init(repoPath string, commit bool, gitName, gitEmail string) error { return nil } -func MasterToMain(repo *git.Repository) { +func SwitchToMain(repo *git.Repository) error { // 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) + return fmt.Errorf("set reference: %s", err) } // Update the repository configuration cfg, err := repo.Config() if err != nil { - log.Fatalf("Failed to get repository config: %v", err) + return fmt.Errorf("repo config: %s", err) } cfg.Init.DefaultBranch = "main" if err := repo.SetConfig(cfg); err != nil { - log.Fatalf("Failed to update repository config: %v", err) + return fmt.Errorf("repo set config: %s", err) } - fmt.Println("Set 'main' as the default branch.") + log.Debug("set 'main' as the default branch.") + return nil } -- 2.49.0 From 730fef09a304c758c95553c709454307ab1f1309 Mon Sep 17 00:00:00 2001 From: Ammar Hussein Date: Sat, 14 Dec 2024 18:41:34 -0800 Subject: [PATCH 3/4] add test for SwitchToMain --- pkg/git/init_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 pkg/git/init_test.go diff --git a/pkg/git/init_test.go b/pkg/git/init_test.go new file mode 100644 index 00000000..ff0338a8 --- /dev/null +++ b/pkg/git/init_test.go @@ -0,0 +1,42 @@ +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" +) + +func TestSwitchToMain(t *testing.T) { + // Create a new in-memory repository + storage := memory.NewStorage() + repo, err := git.Init(storage, nil) + if err != nil { + t.Fatalf("failed to create in-memory repository: %v", err) + } + + // Call the function under test + err = SwitchToMain(repo) + if err != nil { + t.Fatalf("SwitchToMain failed: %v", err) + } + + // Verify that HEAD points to the 'main' branch + 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()) + } + + // Verify that the default branch is set to 'main' in the config + 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) + } +} -- 2.49.0 From 28c7676413e084b4cb58a90a2790e240bb35afe7 Mon Sep 17 00:00:00 2001 From: Ammar Hussein Date: Sun, 15 Dec 2024 09:53:28 -0800 Subject: [PATCH 4/4] replace code-descriptive comments with method level comments --- pkg/git/init.go | 3 +-- pkg/git/init_test.go | 8 +------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/pkg/git/init.go b/pkg/git/init.go index 89454692..30ffa1ae 100644 --- a/pkg/git/init.go +++ b/pkg/git/init.go @@ -48,13 +48,12 @@ func Init(repoPath string, commit bool, gitName, gitEmail string) error { return nil } +// Set the default branch for the passed repo as "main" func SwitchToMain(repo *git.Repository) error { - // 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 { return fmt.Errorf("set reference: %s", err) } - // Update the repository configuration cfg, err := repo.Config() if err != nil { return fmt.Errorf("repo config: %s", err) diff --git a/pkg/git/init_test.go b/pkg/git/init_test.go index ff0338a8..29fcb057 100644 --- a/pkg/git/init_test.go +++ b/pkg/git/init_test.go @@ -8,21 +8,17 @@ import ( "github.com/go-git/go-git/v5/storage/memory" ) +// Verify that SwitchToMain has the desired effect on the repo func TestSwitchToMain(t *testing.T) { - // Create a new in-memory repository storage := memory.NewStorage() repo, err := git.Init(storage, nil) if err != nil { t.Fatalf("failed to create in-memory repository: %v", err) } - - // Call the function under test err = SwitchToMain(repo) if err != nil { t.Fatalf("SwitchToMain failed: %v", err) } - - // Verify that HEAD points to the 'main' branch ref, err := repo.Reference(plumbing.HEAD, false) if err != nil { t.Fatalf("failed to get HEAD reference: %v", err) @@ -30,8 +26,6 @@ func TestSwitchToMain(t *testing.T) { if ref.Target().String() != "refs/heads/main" { t.Errorf("expected HEAD to point to 'refs/heads/main', got %s", ref.Target().String()) } - - // Verify that the default branch is set to 'main' in the config cfg, err := repo.Config() if err != nil { t.Fatalf("failed to get repository config: %v", err) -- 2.49.0