From c51c371abeaadb53977d851cd6ffb424e3d1b201 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Fri, 28 Jul 2023 13:26:17 +0100 Subject: [PATCH] add option to initialize git repo with a particular branch Signed-off-by: Somtochi Onyekwere --- config.go | 19 ++++++++++--------- http.go | 9 +++++++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/config.go b/config.go index f7d51f9..484b7e1 100644 --- a/config.go +++ b/config.go @@ -7,15 +7,16 @@ import ( ) type Config struct { - KeyDir string // Directory for server ssh keys. Only used in SSH strategy. - Dir string // Directory that contains repositories - GitPath string // Path to git binary - GitUser string // User for ssh connections - AutoCreate bool // Automatically create repostories - AutoHooks bool // Automatically setup git hooks - Hooks *HookScripts // Scripts for hooks/* directory - Auth bool // Require authentication - ReadOnly bool // Simulates a user that has read-only access to the repository. + KeyDir string // Directory for server ssh keys. Only used in SSH strategy. + Dir string // Directory that contains repositories + GitPath string // Path to git binary + GitUser string // User for ssh connections + AutoCreate bool // Automatically create repostories + AutoHooks bool // Automatically setup git hooks + Hooks *HookScripts // Scripts for hooks/* directory + Auth bool // Require authentication + ReadOnly bool // Simulates a user that has read-only access to the repository. + DefaultBranch string // branch that will be used for initializing the git repository } // HookScripts represents all repository server-size git hooks diff --git a/http.go b/http.go index 6a72e31..8d87920 100644 --- a/http.go +++ b/http.go @@ -228,8 +228,13 @@ func (s *Server) Setup() error { func initRepo(name string, config *Config) error { fullPath := path.Join(config.Dir, name) - if err := exec.Command(config.GitPath, "init", "--bare", fullPath).Run(); err != nil { - return err + args := []string{"init", "--bare", fullPath} + if config.DefaultBranch != "" { + args = append(args, "--initial-branch", config.DefaultBranch) + } + cmd := exec.Command(config.GitPath, args...) + if out, err := cmd.CombinedOutput(); err != nil { + return fmt.Errorf("err running cmd: %w, output: '%s'", err, out) } if config.AutoHooks && config.Hooks != nil {