Skip to content

Commit 710245e

Browse files
authored
Refactor models.NewRepoContext to extract git related codes to modules/git (#6941)
* refactor models.NewRepoContext to extract git related codes to modules/git * fix imports * refactor
1 parent 95d3d42 commit 710245e

File tree

6 files changed

+34
-49
lines changed

6 files changed

+34
-49
lines changed

models/repo.go

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"io/ioutil"
1414
"net/url"
1515
"os"
16-
"os/exec"
1716
"path"
1817
"path/filepath"
1918
"regexp"
@@ -32,11 +31,9 @@ import (
3231
"code.gitea.io/gitea/modules/sync"
3332
"code.gitea.io/gitea/modules/util"
3433

35-
"github.com/Unknwon/cae/zip"
3634
"github.com/Unknwon/com"
3735
"github.com/go-xorm/builder"
3836
"github.com/go-xorm/xorm"
39-
version "github.com/mcuadros/go-version"
4037
ini "gopkg.in/ini.v1"
4138
)
4239

@@ -67,8 +64,8 @@ var (
6764
ItemsPerPage = 40
6865
)
6966

70-
// LoadRepoConfig loads the repository config
71-
func LoadRepoConfig() {
67+
// loadRepoConfig loads the repository config
68+
func loadRepoConfig() {
7269
// Load .gitignore and license files and readme templates.
7370
types := []string{"gitignore", "license", "readme", "label"}
7471
typeFiles := make([][]string, 4)
@@ -119,45 +116,7 @@ func LoadRepoConfig() {
119116

120117
// NewRepoContext creates a new repository context
121118
func NewRepoContext() {
122-
zip.Verbose = false
123-
124-
// Check Git installation.
125-
if _, err := exec.LookPath("git"); err != nil {
126-
log.Fatal("Failed to test 'git' command: %v (forgotten install?)", err)
127-
}
128-
129-
// Check Git version.
130-
var err error
131-
setting.Git.Version, err = git.BinVersion()
132-
if err != nil {
133-
log.Fatal("Failed to get Git version: %v", err)
134-
}
135-
136-
log.Info("Git Version: %s", setting.Git.Version)
137-
if version.Compare("1.7.1", setting.Git.Version, ">") {
138-
log.Fatal("Gitea requires Git version greater or equal to 1.7.1")
139-
}
140-
141-
// Git requires setting user.name and user.email in order to commit changes.
142-
for configKey, defaultValue := range map[string]string{"user.name": "Gitea", "user.email": "[email protected]"} {
143-
if stdout, stderr, err := process.GetManager().Exec("NewRepoContext(get setting)", "git", "config", "--get", configKey); err != nil || strings.TrimSpace(stdout) == "" {
144-
// ExitError indicates this config is not set
145-
if _, ok := err.(*exec.ExitError); ok || strings.TrimSpace(stdout) == "" {
146-
if _, stderr, gerr := process.GetManager().Exec("NewRepoContext(set "+configKey+")", "git", "config", "--global", configKey, defaultValue); gerr != nil {
147-
log.Fatal("Failed to set git %s(%s): %s", configKey, gerr, stderr)
148-
}
149-
log.Info("Git config %s set to %s", configKey, defaultValue)
150-
} else {
151-
log.Fatal("Failed to get git %s(%s): %s", configKey, err, stderr)
152-
}
153-
}
154-
}
155-
156-
// Set git some configurations.
157-
if _, stderr, err := process.GetManager().Exec("NewRepoContext(git config --global core.quotepath false)",
158-
"git", "config", "--global", "core.quotepath", "false"); err != nil {
159-
log.Fatal("Failed to execute 'git config --global core.quotepath false': %s", stderr)
160-
}
119+
loadRepoConfig()
161120

162121
RemoveAllWithNotice("Clean up repository temporary data", filepath.Join(setting.AppDataPath, "tmp"))
163122
}

modules/git/git.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strings"
1212
"time"
1313

14+
"code.gitea.io/gitea/modules/process"
15+
1416
"github.com/mcuadros/go-version"
1517
)
1618

@@ -31,6 +33,8 @@ var (
3133
// GitExecutable is the command name of git
3234
// Could be updated to an absolute path while initialization
3335
GitExecutable = "git"
36+
37+
gitVersion string
3438
)
3539

3640
func log(format string, args ...interface{}) {
@@ -46,8 +50,6 @@ func log(format string, args ...interface{}) {
4650
}
4751
}
4852

49-
var gitVersion string
50-
5153
// BinVersion returns current Git version from shell.
5254
func BinVersion() (string, error) {
5355
if len(gitVersion) > 0 {
@@ -89,6 +91,26 @@ func init() {
8991
if version.Compare(gitVersion, GitVersionRequired, "<") {
9092
panic(fmt.Sprintf("Git version not supported. Requires version > %v", GitVersionRequired))
9193
}
94+
95+
// Git requires setting user.name and user.email in order to commit changes.
96+
for configKey, defaultValue := range map[string]string{"user.name": "Gitea", "user.email": "[email protected]"} {
97+
if stdout, stderr, err := process.GetManager().Exec("git.Init(get setting)", GitExecutable, "config", "--get", configKey); err != nil || strings.TrimSpace(stdout) == "" {
98+
// ExitError indicates this config is not set
99+
if _, ok := err.(*exec.ExitError); ok || strings.TrimSpace(stdout) == "" {
100+
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))
102+
}
103+
} else {
104+
panic(fmt.Sprintf("Failed to get git %s(%s): %s", configKey, err, stderr))
105+
}
106+
}
107+
}
108+
109+
// Set git some configurations.
110+
if _, stderr, err := process.GetManager().Exec("git.Init(git config --global core.quotepath false)",
111+
GitExecutable, "config", "--global", "core.quotepath", "false"); err != nil {
112+
panic(fmt.Sprintf("Failed to execute 'git config --global core.quotepath false': %s", stderr))
113+
}
92114
}
93115

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

modules/setting/git.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
var (
1717
// Git settings
1818
Git = struct {
19-
Version string `ini:"-"`
2019
DisableDiffHighlight bool
2120
MaxGitDiffLines int
2221
MaxGitDiffLineCharacters int
@@ -65,6 +64,8 @@ func newGit() {
6564
log.Fatal("Error retrieving git version: %v", err)
6665
}
6766

67+
log.Info("Git Version: %s", binVersion)
68+
6869
if version.Compare(binVersion, "2.9", ">=") {
6970
// Explicitly disable credential helper, otherwise Git credentials might leak
7071
git.GlobalCommandArgs = append(git.GlobalCommandArgs, "-c", "credential.helper=")

modules/setting/setting.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
_ "code.gitea.io/gitea/modules/minwinsvc" // import minwinsvc for windows services
2828
"code.gitea.io/gitea/modules/user"
2929

30+
"github.com/Unknwon/cae/zip"
3031
"github.com/Unknwon/com"
3132
_ "github.com/go-macaron/cache/memcache" // memcache plugin for cache
3233
_ "github.com/go-macaron/cache/redis"
@@ -931,6 +932,8 @@ func NewContext() {
931932
sec = Cfg.Section("U2F")
932933
U2F.TrustedFacets, _ = shellquote.Split(sec.Key("TRUSTED_FACETS").MustString(strings.TrimRight(AppURL, "/")))
933934
U2F.AppID = sec.Key("APP_ID").MustString(strings.TrimRight(AppURL, "/"))
935+
936+
zip.Verbose = false
934937
}
935938

936939
func loadInternalToken(sec *ini.Section) string {

routers/admin/admin.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/base"
1919
"code.gitea.io/gitea/modules/context"
2020
"code.gitea.io/gitea/modules/cron"
21+
"code.gitea.io/gitea/modules/git"
2122
"code.gitea.io/gitea/modules/process"
2223
"code.gitea.io/gitea/modules/setting"
2324
)
@@ -210,7 +211,7 @@ func Config(ctx *context.Context) {
210211
ctx.Data["DisableRouterLog"] = setting.DisableRouterLog
211212
ctx.Data["RunUser"] = setting.RunUser
212213
ctx.Data["RunMode"] = strings.Title(macaron.Env)
213-
ctx.Data["GitVersion"] = setting.Git.Version
214+
ctx.Data["GitVersion"], _ = git.BinVersion()
214215
ctx.Data["RepoRootPath"] = setting.RepoRootPath
215216
ctx.Data["CustomRootPath"] = setting.CustomPath
216217
ctx.Data["StaticRootPath"] = setting.StaticRootPath

routers/init.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ func GlobalInit() {
8686
log.Fatal("Failed to initialize OAuth2 support: %v", err)
8787
}
8888

89-
models.LoadRepoConfig()
9089
models.NewRepoContext()
9190

9291
// Booting long running goroutines.

0 commit comments

Comments
 (0)