From 11cca86f4d26c6532588b587f72def3ff4ffff44 Mon Sep 17 00:00:00 2001 From: Yar Kravtsov Date: Sat, 24 May 2025 06:36:26 +0300 Subject: [PATCH] feat: set 'main' as default branch instead of 'master' --- README.md | 4 ++-- internal/git/git.go | 20 ++++++++++++++++++-- test/integration_test.go | 7 +++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 500e0f6..20ddbd6 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ sudo mv lnk /usr/local/bin/ lnk init ``` -This creates `$XDG_CONFIG_HOME/lnk` (or `~/.config/lnk`) and initializes a Git repository. +This creates `$XDG_CONFIG_HOME/lnk` (or `~/.config/lnk`) and initializes a Git repository with `main` as the default branch. ### Initialize with remote @@ -52,7 +52,7 @@ lnk init --remote https://github.com/user/dotfiles.git lnk init -r git@github.com:user/dotfiles.git ``` -This initializes the repository and adds the specified URL as the `origin` remote, allowing you to sync your dotfiles with a Git hosting service. +This initializes the repository with `main` as the default branch and adds the specified URL as the `origin` remote, allowing you to sync your dotfiles with a Git hosting service. ### Add a file diff --git a/internal/git/git.go b/internal/git/git.go index fb21d57..b87d3dc 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -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 diff --git a/test/integration_test.go b/test/integration_test.go index 3148531..9e02bea 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -65,6 +65,13 @@ func (suite *LnkIntegrationTestSuite) TestInit() { // Verify it's a non-bare repo configPath := filepath.Join(gitDir, "config") suite.FileExists(configPath) + + // Verify the default branch is set to 'main' + cmd := exec.Command("git", "symbolic-ref", "HEAD") + cmd.Dir = lnkDir + output, err := cmd.Output() + suite.Require().NoError(err) + suite.Equal("refs/heads/main", strings.TrimSpace(string(output))) } func (suite *LnkIntegrationTestSuite) TestAddFile() {