From 8ec659722df32048561c713f2d533a20a7f0d5ef Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 20 Jun 2019 00:53:37 +0800 Subject: [PATCH] Only check and config git on web subcommand but not others (#7236) * only check and config git on web subcommand but not others * add Init in git tests --- cmd/serv.go | 22 ---------------------- modules/git/git.go | 10 +++++++--- modules/git/git_test.go | 25 +++++++++++++++++++++++++ routers/init.go | 3 +++ 4 files changed, 35 insertions(+), 25 deletions(-) create mode 100644 modules/git/git_test.go diff --git a/cmd/serv.go b/cmd/serv.go index c1c8fd3a9..2ea89757d 100644 --- a/cmd/serv.go +++ b/cmd/serv.go @@ -17,7 +17,6 @@ import ( "time" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/pprof" "code.gitea.io/gitea/modules/private" @@ -25,7 +24,6 @@ import ( "github.com/Unknwon/com" "github.com/dgrijalva/jwt-go" - version "github.com/mcuadros/go-version" "github.com/urfave/cli" ) @@ -46,29 +44,9 @@ var CmdServ = cli.Command{ }, } -func checkLFSVersion() { - if setting.LFS.StartServer { - //Disable LFS client hooks if installed for the current OS user - //Needs at least git v2.1.2 - binVersion, err := git.BinVersion() - if err != nil { - fail("LFS server error", "Error retrieving git version: %v", err) - } - - if !version.Compare(binVersion, "2.1.2", ">=") { - setting.LFS.StartServer = false - println("LFS server support needs at least Git v2.1.2, disabled") - } else { - git.GlobalCommandArgs = append(git.GlobalCommandArgs, "-c", "filter.lfs.required=", - "-c", "filter.lfs.smudge=", "-c", "filter.lfs.clean=") - } - } -} - func setup(logPath string) { _ = log.DelLogger("console") setting.NewContext() - checkLFSVersion() } func parseCmd(cmd string) (string, string) { diff --git a/modules/git/git.go b/modules/git/git.go index 632af539c..bda39da91 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -91,17 +91,20 @@ func init() { if version.Compare(gitVersion, GitVersionRequired, "<") { panic(fmt.Sprintf("Git version not supported. Requires version > %v", GitVersionRequired)) } +} +// Init initializes git module +func Init() error { // Git requires setting user.name and user.email in order to commit changes. for configKey, defaultValue := range map[string]string{"user.name": "Gitea", "user.email": "gitea@fake.local"} { if stdout, stderr, err := process.GetManager().Exec("git.Init(get setting)", GitExecutable, "config", "--get", configKey); err != nil || strings.TrimSpace(stdout) == "" { // ExitError indicates this config is not set if _, ok := err.(*exec.ExitError); ok || strings.TrimSpace(stdout) == "" { if _, stderr, gerr := process.GetManager().Exec("git.Init(set "+configKey+")", "git", "config", "--global", configKey, defaultValue); gerr != nil { - panic(fmt.Sprintf("Failed to set git %s(%s): %s", configKey, gerr, stderr)) + return fmt.Errorf("Failed to set git %s(%s): %s", configKey, gerr, stderr) } } else { - panic(fmt.Sprintf("Failed to get git %s(%s): %s", configKey, err, stderr)) + return fmt.Errorf("Failed to get git %s(%s): %s", configKey, err, stderr) } } } @@ -109,8 +112,9 @@ func init() { // Set git some configurations. if _, stderr, err := process.GetManager().Exec("git.Init(git config --global core.quotepath false)", GitExecutable, "config", "--global", "core.quotepath", "false"); err != nil { - panic(fmt.Sprintf("Failed to execute 'git config --global core.quotepath false': %s", stderr)) + return fmt.Errorf("Failed to execute 'git config --global core.quotepath false': %s", stderr) } + return nil } // Fsck verifies the connectivity and validity of the objects in the database diff --git a/modules/git/git_test.go b/modules/git/git_test.go new file mode 100644 index 000000000..0c6259a9c --- /dev/null +++ b/modules/git/git_test.go @@ -0,0 +1,25 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package git + +import ( + "fmt" + "os" + "testing" +) + +func fatalTestError(fmtStr string, args ...interface{}) { + fmt.Fprintf(os.Stderr, fmtStr, args...) + os.Exit(1) +} + +func TestMain(m *testing.M) { + if err := Init(); err != nil { + fatalTestError("Init failed: %v", err) + } + + exitStatus := m.Run() + os.Exit(exitStatus) +} diff --git a/routers/init.go b/routers/init.go index e6b23cf8b..e43126068 100644 --- a/routers/init.go +++ b/routers/init.go @@ -65,6 +65,9 @@ func initDBEngine() (err error) { // GlobalInit is for global configuration reload-able. func GlobalInit() { setting.NewContext() + if err := git.Init(); err != nil { + log.Fatal("Git module init failed: %v", err) + } setting.CheckLFSVersion() log.Trace("AppPath: %s", setting.AppPath) log.Trace("AppWorkPath: %s", setting.AppWorkPath)