Skip to content

Commit e3c6268

Browse files
authored
Let package git depend on setting but not opposite (#15241)
* Let package git depend on setting but not opposite * private some package variables
1 parent e673e42 commit e3c6268

File tree

17 files changed

+113
-92
lines changed

17 files changed

+113
-92
lines changed

contrib/pr/checkout.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"time"
2727

2828
"code.gitea.io/gitea/models"
29+
gitea_git "code.gitea.io/gitea/modules/git"
2930
"code.gitea.io/gitea/modules/markup"
3031
"code.gitea.io/gitea/modules/markup/external"
3132
"code.gitea.io/gitea/modules/setting"
@@ -79,7 +80,7 @@ func runPR() {
7980
setting.RunUser = curUser.Username
8081

8182
log.Printf("[PR] Loading fixtures data ...\n")
82-
setting.CheckLFSVersion()
83+
gitea_git.CheckLFSVersion()
8384
//models.LoadConfigs()
8485
/*
8586
setting.Database.Type = "sqlite3"

integrations/git_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func standardCommitAndPushTest(t *testing.T, dstPath string) (little, big string
143143
func lfsCommitAndPushTest(t *testing.T, dstPath string) (littleLFS, bigLFS string) {
144144
t.Run("LFS", func(t *testing.T) {
145145
defer PrintCurrentTest(t)()
146-
setting.CheckLFSVersion()
146+
git.CheckLFSVersion()
147147
if !setting.LFS.StartServer {
148148
t.Skip()
149149
return
@@ -213,7 +213,7 @@ func rawTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS s
213213
resp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
214214
assert.Equal(t, littleSize, resp.Length)
215215

216-
setting.CheckLFSVersion()
216+
git.CheckLFSVersion()
217217
if setting.LFS.StartServer {
218218
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", littleLFS))
219219
resp := session.MakeRequest(t, req, http.StatusOK)
@@ -255,7 +255,7 @@ func mediaTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS
255255
resp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
256256
assert.Equal(t, littleSize, resp.Length)
257257

258-
setting.CheckLFSVersion()
258+
git.CheckLFSVersion()
259259
if setting.LFS.StartServer {
260260
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", littleLFS))
261261
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)

integrations/integration_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"code.gitea.io/gitea/models"
2828
"code.gitea.io/gitea/modules/base"
29+
"code.gitea.io/gitea/modules/git"
2930
"code.gitea.io/gitea/modules/graceful"
3031
"code.gitea.io/gitea/modules/log"
3132
"code.gitea.io/gitea/modules/queue"
@@ -162,7 +163,7 @@ func initIntegrationTest() {
162163
setting.SetCustomPathAndConf("", "", "")
163164
setting.NewContext()
164165
util.RemoveAll(models.LocalCopyPath())
165-
setting.CheckLFSVersion()
166+
git.CheckLFSVersion()
166167
setting.InitDBConfig()
167168
if err := storage.Init(); err != nil {
168169
fmt.Printf("Init storage failed: %v", err)

integrations/lfs_getobject_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"testing"
1414

1515
"code.gitea.io/gitea/models"
16+
"code.gitea.io/gitea/modules/git"
1617
"code.gitea.io/gitea/modules/lfs"
1718
"code.gitea.io/gitea/modules/setting"
1819
"code.gitea.io/gitea/routers/web"
@@ -81,7 +82,7 @@ func checkResponseTestContentEncoding(t *testing.T, content *[]byte, resp *httpt
8182

8283
func TestGetLFSSmall(t *testing.T) {
8384
defer prepareTestEnv(t)()
84-
setting.CheckLFSVersion()
85+
git.CheckLFSVersion()
8586
if !setting.LFS.StartServer {
8687
t.Skip()
8788
return
@@ -94,7 +95,7 @@ func TestGetLFSSmall(t *testing.T) {
9495

9596
func TestGetLFSLarge(t *testing.T) {
9697
defer prepareTestEnv(t)()
97-
setting.CheckLFSVersion()
98+
git.CheckLFSVersion()
9899
if !setting.LFS.StartServer {
99100
t.Skip()
100101
return
@@ -110,7 +111,7 @@ func TestGetLFSLarge(t *testing.T) {
110111

111112
func TestGetLFSGzip(t *testing.T) {
112113
defer prepareTestEnv(t)()
113-
setting.CheckLFSVersion()
114+
git.CheckLFSVersion()
114115
if !setting.LFS.StartServer {
115116
t.Skip()
116117
return
@@ -131,7 +132,7 @@ func TestGetLFSGzip(t *testing.T) {
131132

132133
func TestGetLFSZip(t *testing.T) {
133134
defer prepareTestEnv(t)()
134-
setting.CheckLFSVersion()
135+
git.CheckLFSVersion()
135136
if !setting.LFS.StartServer {
136137
t.Skip()
137138
return
@@ -154,7 +155,7 @@ func TestGetLFSZip(t *testing.T) {
154155

155156
func TestGetLFSRangeNo(t *testing.T) {
156157
defer prepareTestEnv(t)()
157-
setting.CheckLFSVersion()
158+
git.CheckLFSVersion()
158159
if !setting.LFS.StartServer {
159160
t.Skip()
160161
return
@@ -167,7 +168,7 @@ func TestGetLFSRangeNo(t *testing.T) {
167168

168169
func TestGetLFSRange(t *testing.T) {
169170
defer prepareTestEnv(t)()
170-
setting.CheckLFSVersion()
171+
git.CheckLFSVersion()
171172
if !setting.LFS.StartServer {
172173
t.Skip()
173174
return

integrations/migration-test/migration_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"code.gitea.io/gitea/models/migrations"
2424
"code.gitea.io/gitea/modules/base"
2525
"code.gitea.io/gitea/modules/charset"
26+
"code.gitea.io/gitea/modules/git"
2627
"code.gitea.io/gitea/modules/setting"
2728
"code.gitea.io/gitea/modules/util"
2829

@@ -61,7 +62,7 @@ func initMigrationTest(t *testing.T) func() {
6162
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
6263
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
6364

64-
setting.CheckLFSVersion()
65+
git.CheckLFSVersion()
6566
setting.InitDBConfig()
6667
setting.NewLogServices(true)
6768
return deferFn

models/migrations/migrations_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"code.gitea.io/gitea/models"
1818
"code.gitea.io/gitea/modules/base"
19+
"code.gitea.io/gitea/modules/git"
1920
"code.gitea.io/gitea/modules/setting"
2021
"code.gitea.io/gitea/modules/timeutil"
2122
"code.gitea.io/gitea/modules/util"
@@ -55,7 +56,7 @@ func TestMain(m *testing.M) {
5556

5657
setting.SetCustomPathAndConf("", "", "")
5758
setting.NewContext()
58-
setting.CheckLFSVersion()
59+
git.CheckLFSVersion()
5960
setting.InitDBConfig()
6061
setting.NewLogServices(true)
6162

modules/git/command.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ var (
2323
// GlobalCommandArgs global command args for external package setting
2424
GlobalCommandArgs []string
2525

26-
// DefaultCommandExecutionTimeout default command execution timeout duration
27-
DefaultCommandExecutionTimeout = 360 * time.Second
26+
// defaultCommandExecutionTimeout default command execution timeout duration
27+
defaultCommandExecutionTimeout = 360 * time.Second
2828
)
2929

3030
// DefaultLocale is the default LC_ALL to run git commands in.
@@ -111,7 +111,7 @@ func (c *Command) RunInDirTimeoutEnvFullPipeline(env []string, timeout time.Dura
111111
// it pipes stdout and stderr to given io.Writer and passes in an io.Reader as stdin. Between cmd.Start and cmd.Wait the passed in function is run.
112112
func (c *Command) RunInDirTimeoutEnvFullPipelineFunc(env []string, timeout time.Duration, dir string, stdout, stderr io.Writer, stdin io.Reader, fn func(context.Context, context.CancelFunc) error) error {
113113
if timeout == -1 {
114-
timeout = DefaultCommandExecutionTimeout
114+
timeout = defaultCommandExecutionTimeout
115115
}
116116

117117
if len(dir) == 0 {

modules/git/git.go

+33
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"code.gitea.io/gitea/modules/process"
17+
"code.gitea.io/gitea/modules/setting"
1718

1819
"github.com/hashicorp/go-version"
1920
)
@@ -106,10 +107,42 @@ func SetExecutablePath(path string) error {
106107
return nil
107108
}
108109

110+
// VersionInfo returns git version information
111+
func VersionInfo() string {
112+
var format = "Git Version: %s"
113+
var args = []interface{}{gitVersion.Original()}
114+
// Since git wire protocol has been released from git v2.18
115+
if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil {
116+
format += ", Wire Protocol %s Enabled"
117+
args = append(args, "Version 2") // for focus color
118+
}
119+
120+
return fmt.Sprintf(format, args...)
121+
}
122+
109123
// Init initializes git module
110124
func Init(ctx context.Context) error {
111125
DefaultContext = ctx
112126

127+
defaultCommandExecutionTimeout = time.Duration(setting.Git.Timeout.Default) * time.Second
128+
129+
if err := SetExecutablePath(setting.Git.Path); err != nil {
130+
return err
131+
}
132+
133+
// force cleanup args
134+
GlobalCommandArgs = []string{}
135+
136+
if CheckGitVersionAtLeast("2.9") == nil {
137+
// Explicitly disable credential helper, otherwise Git credentials might leak
138+
GlobalCommandArgs = append(GlobalCommandArgs, "-c", "credential.helper=")
139+
}
140+
141+
// Since git wire protocol has been released from git v2.18
142+
if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil {
143+
GlobalCommandArgs = append(GlobalCommandArgs, "-c", "protocol.version=2")
144+
}
145+
113146
// Save current git version on init to gitVersion otherwise it would require an RWMutex
114147
if err := LoadGitVersion(); err != nil {
115148
return err

modules/git/lfs.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2021 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+
"sync"
9+
10+
logger "code.gitea.io/gitea/modules/log"
11+
"code.gitea.io/gitea/modules/setting"
12+
)
13+
14+
var once sync.Once
15+
16+
// CheckLFSVersion will check lfs version, if not satisfied, then disable it.
17+
func CheckLFSVersion() {
18+
if setting.LFS.StartServer {
19+
//Disable LFS client hooks if installed for the current OS user
20+
//Needs at least git v2.1.2
21+
22+
err := LoadGitVersion()
23+
if err != nil {
24+
logger.Fatal("Error retrieving git version: %v", err)
25+
}
26+
27+
if CheckGitVersionAtLeast("2.1.2") != nil {
28+
setting.LFS.StartServer = false
29+
logger.Error("LFS server support needs at least Git v2.1.2")
30+
} else {
31+
once.Do(func() {
32+
GlobalCommandArgs = append(GlobalCommandArgs, "-c", "filter.lfs.required=",
33+
"-c", "filter.lfs.smudge=", "-c", "filter.lfs.clean=")
34+
})
35+
}
36+
}
37+
}

modules/git/repo_commit.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"io/ioutil"
1313
"strconv"
1414
"strings"
15+
16+
"code.gitea.io/gitea/modules/setting"
1517
)
1618

1719
// GetBranchCommitID returns last commit ID string of given branch.
@@ -85,12 +87,6 @@ func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) {
8587
return commits.Front().Value.(*Commit), nil
8688
}
8789

88-
// CommitsRangeSize the default commits range size
89-
var CommitsRangeSize = 50
90-
91-
// BranchesRangeSize the default branches range size
92-
var BranchesRangeSize = 20
93-
9490
func (repo *Repository) commitsByRange(id SHA1, page, pageSize int) (*list.List, error) {
9591
stdout, err := NewCommand("log", id.String(), "--skip="+strconv.Itoa((page-1)*pageSize),
9692
"--max-count="+strconv.Itoa(pageSize), prettyLogFormat).RunInDirBytes(repo.Path)
@@ -206,7 +202,7 @@ func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
206202

207203
// CommitsByFileAndRange return the commits according revison file and the page
208204
func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (*list.List, error) {
209-
skip := (page - 1) * CommitsRangeSize
205+
skip := (page - 1) * setting.Git.CommitsRangeSize
210206

211207
stdoutReader, stdoutWriter := io.Pipe()
212208
defer func() {
@@ -216,7 +212,7 @@ func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (
216212
go func() {
217213
stderr := strings.Builder{}
218214
err := NewCommand("log", revision, "--follow",
219-
"--max-count="+strconv.Itoa(CommitsRangeSize*page),
215+
"--max-count="+strconv.Itoa(setting.Git.CommitsRangeSize*page),
220216
prettyLogFormat, "--", file).
221217
RunInDirPipeline(repo.Path, stdoutWriter, &stderr)
222218
if err != nil {
@@ -247,7 +243,7 @@ func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (
247243
// CommitsByFileAndRangeNoFollow return the commits according revison file and the page
248244
func (repo *Repository) CommitsByFileAndRangeNoFollow(revision, file string, page int) (*list.List, error) {
249245
stdout, err := NewCommand("log", revision, "--skip="+strconv.Itoa((page-1)*50),
250-
"--max-count="+strconv.Itoa(CommitsRangeSize), prettyLogFormat, "--", file).RunInDirBytes(repo.Path)
246+
"--max-count="+strconv.Itoa(setting.Git.CommitsRangeSize), prettyLogFormat, "--", file).RunInDirBytes(repo.Path)
251247
if err != nil {
252248
return nil, err
253249
}

modules/setting/git.go

+3-35
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package setting
77
import (
88
"time"
99

10-
"code.gitea.io/gitea/modules/git"
1110
"code.gitea.io/gitea/modules/log"
1211
)
1312

@@ -19,8 +18,8 @@ var (
1918
MaxGitDiffLines int
2019
MaxGitDiffLineCharacters int
2120
MaxGitDiffFiles int
22-
CommitsRangeSize int
23-
BranchesRangeSize int
21+
CommitsRangeSize int // CommitsRangeSize the default commits range size
22+
BranchesRangeSize int // BranchesRangeSize the default branches range size
2423
VerbosePush bool
2524
VerbosePushDelay time.Duration
2625
GCArgs []string `ini:"GC_ARGS" delim:" "`
@@ -54,7 +53,7 @@ var (
5453
Pull int
5554
GC int `ini:"GC"`
5655
}{
57-
Default: int(git.DefaultCommandExecutionTimeout / time.Second),
56+
Default: 360,
5857
Migrate: 600,
5958
Mirror: 300,
6059
Clone: 300,
@@ -68,35 +67,4 @@ func newGit() {
6867
if err := Cfg.Section("git").MapTo(&Git); err != nil {
6968
log.Fatal("Failed to map Git settings: %v", err)
7069
}
71-
if err := git.SetExecutablePath(Git.Path); err != nil {
72-
log.Fatal("Failed to initialize Git settings: %v", err)
73-
}
74-
git.DefaultCommandExecutionTimeout = time.Duration(Git.Timeout.Default) * time.Second
75-
76-
version, err := git.LocalVersion()
77-
if err != nil {
78-
log.Fatal("Error retrieving git version: %v", err)
79-
}
80-
81-
// force cleanup args
82-
git.GlobalCommandArgs = []string{}
83-
84-
if git.CheckGitVersionAtLeast("2.9") == nil {
85-
// Explicitly disable credential helper, otherwise Git credentials might leak
86-
git.GlobalCommandArgs = append(git.GlobalCommandArgs, "-c", "credential.helper=")
87-
}
88-
89-
var format = "Git Version: %s"
90-
var args = []interface{}{version.Original()}
91-
// Since git wire protocol has been released from git v2.18
92-
if Git.EnableAutoGitWireProtocol && git.CheckGitVersionAtLeast("2.18") == nil {
93-
git.GlobalCommandArgs = append(git.GlobalCommandArgs, "-c", "protocol.version=2")
94-
format += ", Wire Protocol %s Enabled"
95-
args = append(args, "Version 2") // for focus color
96-
}
97-
98-
git.CommitsRangeSize = Git.CommitsRangeSize
99-
git.BranchesRangeSize = Git.BranchesRangeSize
100-
101-
log.Info(format, args...)
10270
}

0 commit comments

Comments
 (0)