Skip to content

Commit

Permalink
fix: initial default branch name (#340)
Browse files Browse the repository at this point in the history
Use git plumbing to set the initial default branch name.

Fixes: #147
Fixes: #260
  • Loading branch information
aymanbagabas authored Jul 18, 2023
1 parent e0882cc commit 050a0d1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
11 changes: 7 additions & 4 deletions git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,13 @@ func (r *Repository) SetConfig(key, value string, opts ...ConfigOptions) error {

// SymbolicRef returns or updates the symbolic reference for the given name.
// Both name and ref can be empty.
func (r *Repository) SymbolicRef(name string, ref string) (string, error) {
opt := git.SymbolicRefOptions{
Name: name,
Ref: ref,
func (r *Repository) SymbolicRef(name string, ref string, opts ...git.SymbolicRefOptions) (string, error) {
var opt git.SymbolicRefOptions
if len(opts) > 0 {
opt = opts[0]
}

opt.Name = name
opt.Ref = ref
return r.Repository.SymbolicRef(opt)
}
10 changes: 5 additions & 5 deletions server/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/charmbracelet/log"
"github.com/charmbracelet/soft-serve/git"
"github.com/go-git/go-git/v5/plumbing/format/pktline"
gitm "github.com/gogs/git-module"
)

var (
Expand Down Expand Up @@ -102,11 +103,10 @@ func EnsureDefaultBranch(ctx context.Context, scmd ServiceCommand) error {
}
}

cmd := git.NewCommand("branch", "-M", branch).WithContext(ctx)
if err := cmd.RunInDirWithOptions(scmd.Dir, git.RunInDirOptions{
Stdin: scmd.Stdin,
Stdout: scmd.Stdout,
Stderr: scmd.Stderr,
if _, err := r.SymbolicRef(git.HEAD, git.RefsHeads+branch, gitm.SymbolicRefOptions{
CommandOptions: gitm.CommandOptions{
Context: ctx,
},
}); err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion server/ssh/cmd/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ func branchDefaultCommand() *cobra.Command {
return git.ErrReferenceNotExist
}

if _, err := r.SymbolicRef("HEAD", gitm.RefsHeads+branch); err != nil {
if _, err := r.SymbolicRef(git.HEAD, gitm.RefsHeads+branch, gitm.SymbolicRefOptions{
CommandOptions: gitm.CommandOptions{
Context: ctx,
},
}); err != nil {
return err
}
}
Expand Down

0 comments on commit 050a0d1

Please sign in to comment.