feat: set 'main' as default branch instead of 'master'

This commit is contained in:
Yar Kravtsov
2025-05-24 06:36:26 +03:00
parent d5812e1085
commit 11cca86f4d
3 changed files with 27 additions and 4 deletions

View File

@@ -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

View File

@@ -22,14 +22,30 @@ 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 {
// 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
}

View File

@@ -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() {