Skip to content

Commit 8ec6597

Browse files
lunnylafriks
authored andcommitted
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
1 parent a71cabb commit 8ec6597

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

cmd/serv.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ import (
1717
"time"
1818

1919
"code.gitea.io/gitea/models"
20-
"code.gitea.io/gitea/modules/git"
2120
"code.gitea.io/gitea/modules/log"
2221
"code.gitea.io/gitea/modules/pprof"
2322
"code.gitea.io/gitea/modules/private"
2423
"code.gitea.io/gitea/modules/setting"
2524

2625
"github.com/Unknwon/com"
2726
"github.com/dgrijalva/jwt-go"
28-
version "github.com/mcuadros/go-version"
2927
"github.com/urfave/cli"
3028
)
3129

@@ -46,29 +44,9 @@ var CmdServ = cli.Command{
4644
},
4745
}
4846

49-
func checkLFSVersion() {
50-
if setting.LFS.StartServer {
51-
//Disable LFS client hooks if installed for the current OS user
52-
//Needs at least git v2.1.2
53-
binVersion, err := git.BinVersion()
54-
if err != nil {
55-
fail("LFS server error", "Error retrieving git version: %v", err)
56-
}
57-
58-
if !version.Compare(binVersion, "2.1.2", ">=") {
59-
setting.LFS.StartServer = false
60-
println("LFS server support needs at least Git v2.1.2, disabled")
61-
} else {
62-
git.GlobalCommandArgs = append(git.GlobalCommandArgs, "-c", "filter.lfs.required=",
63-
"-c", "filter.lfs.smudge=", "-c", "filter.lfs.clean=")
64-
}
65-
}
66-
}
67-
6847
func setup(logPath string) {
6948
_ = log.DelLogger("console")
7049
setting.NewContext()
71-
checkLFSVersion()
7250
}
7351

7452
func parseCmd(cmd string) (string, string) {

modules/git/git.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,30 @@ func init() {
9191
if version.Compare(gitVersion, GitVersionRequired, "<") {
9292
panic(fmt.Sprintf("Git version not supported. Requires version > %v", GitVersionRequired))
9393
}
94+
}
9495

96+
// Init initializes git module
97+
func Init() error {
9598
// Git requires setting user.name and user.email in order to commit changes.
9699
for configKey, defaultValue := range map[string]string{"user.name": "Gitea", "user.email": "[email protected]"} {
97100
if stdout, stderr, err := process.GetManager().Exec("git.Init(get setting)", GitExecutable, "config", "--get", configKey); err != nil || strings.TrimSpace(stdout) == "" {
98101
// ExitError indicates this config is not set
99102
if _, ok := err.(*exec.ExitError); ok || strings.TrimSpace(stdout) == "" {
100103
if _, stderr, gerr := process.GetManager().Exec("git.Init(set "+configKey+")", "git", "config", "--global", configKey, defaultValue); gerr != nil {
101-
panic(fmt.Sprintf("Failed to set git %s(%s): %s", configKey, gerr, stderr))
104+
return fmt.Errorf("Failed to set git %s(%s): %s", configKey, gerr, stderr)
102105
}
103106
} else {
104-
panic(fmt.Sprintf("Failed to get git %s(%s): %s", configKey, err, stderr))
107+
return fmt.Errorf("Failed to get git %s(%s): %s", configKey, err, stderr)
105108
}
106109
}
107110
}
108111

109112
// Set git some configurations.
110113
if _, stderr, err := process.GetManager().Exec("git.Init(git config --global core.quotepath false)",
111114
GitExecutable, "config", "--global", "core.quotepath", "false"); err != nil {
112-
panic(fmt.Sprintf("Failed to execute 'git config --global core.quotepath false': %s", stderr))
115+
return fmt.Errorf("Failed to execute 'git config --global core.quotepath false': %s", stderr)
113116
}
117+
return nil
114118
}
115119

116120
// Fsck verifies the connectivity and validity of the objects in the database

modules/git/git_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package git
6+
7+
import (
8+
"fmt"
9+
"os"
10+
"testing"
11+
)
12+
13+
func fatalTestError(fmtStr string, args ...interface{}) {
14+
fmt.Fprintf(os.Stderr, fmtStr, args...)
15+
os.Exit(1)
16+
}
17+
18+
func TestMain(m *testing.M) {
19+
if err := Init(); err != nil {
20+
fatalTestError("Init failed: %v", err)
21+
}
22+
23+
exitStatus := m.Run()
24+
os.Exit(exitStatus)
25+
}

routers/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ func initDBEngine() (err error) {
6565
// GlobalInit is for global configuration reload-able.
6666
func GlobalInit() {
6767
setting.NewContext()
68+
if err := git.Init(); err != nil {
69+
log.Fatal("Git module init failed: %v", err)
70+
}
6871
setting.CheckLFSVersion()
6972
log.Trace("AppPath: %s", setting.AppPath)
7073
log.Trace("AppWorkPath: %s", setting.AppWorkPath)

0 commit comments

Comments
 (0)