Skip to content

Commit 6a83ccd

Browse files
wxiaoguangzeripathlunny
authored and
Sysoev, Vladimir
committed
Refactor legacy git init (go-gitea#20376)
* merge `CheckLFSVersion` into `InitFull` (renamed from `InitWithSyncOnce`) * remove the `Once` during git init, no data-race now * for doctor sub-commands, `InitFull` should only be called in initialization stage Co-authored-by: zeripath <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent f9c11be commit 6a83ccd

File tree

14 files changed

+58
-144
lines changed

14 files changed

+58
-144
lines changed

cmd/doctor.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/models/db"
1515
"code.gitea.io/gitea/models/migrations"
1616
"code.gitea.io/gitea/modules/doctor"
17+
"code.gitea.io/gitea/modules/git"
1718
"code.gitea.io/gitea/modules/log"
1819
"code.gitea.io/gitea/modules/setting"
1920

@@ -124,13 +125,18 @@ func runRecreateTable(ctx *cli.Context) error {
124125
}
125126

126127
func runDoctor(ctx *cli.Context) error {
128+
stdCtx, cancel := installSignals()
129+
defer cancel()
130+
131+
// some doctor sub-commands need to use git command
132+
if err := git.InitFull(stdCtx); err != nil {
133+
return err
134+
}
135+
127136
// Silence the default loggers
128137
log.DelNamedLogger("console")
129138
log.DelNamedLogger(log.DEFAULT)
130139

131-
stdCtx, cancel := installSignals()
132-
defer cancel()
133-
134140
// Now setup our own
135141
logFile := ctx.String("log-file")
136142
if !ctx.IsSet("log-file") {

contrib/pr/checkout.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func runPR() {
8080
setting.RunUser = curUser.Username
8181

8282
log.Printf("[PR] Loading fixtures data ...\n")
83-
gitea_git.CheckLFSVersion()
8483
//models.LoadConfigs()
8584
/*
8685
setting.Database.Type = "sqlite3"

integrations/git_test.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,6 @@ func standardCommitAndPushTest(t *testing.T, dstPath string) (little, big string
156156
func lfsCommitAndPushTest(t *testing.T, dstPath string) (littleLFS, bigLFS string) {
157157
t.Run("LFS", func(t *testing.T) {
158158
defer PrintCurrentTest(t)()
159-
git.CheckLFSVersion()
160-
if !setting.LFS.StartServer {
161-
t.Skip()
162-
return
163-
}
164159
prefix := "lfs-data-file-"
165160
err := git.NewCommand(git.DefaultContext, "lfs").AddArguments("install").Run(&git.RunOpts{Dir: dstPath})
166161
assert.NoError(t, err)
@@ -226,7 +221,6 @@ func rawTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS s
226221
resp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
227222
assert.Equal(t, littleSize, resp.Length)
228223

229-
git.CheckLFSVersion()
230224
if setting.LFS.StartServer {
231225
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", littleLFS))
232226
resp := session.MakeRequest(t, req, http.StatusOK)
@@ -268,12 +262,9 @@ func mediaTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS
268262
resp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
269263
assert.Equal(t, littleSize, resp.Length)
270264

271-
git.CheckLFSVersion()
272-
if setting.LFS.StartServer {
273-
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", littleLFS))
274-
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
275-
assert.Equal(t, littleSize, resp.Length)
276-
}
265+
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", littleLFS))
266+
resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
267+
assert.Equal(t, littleSize, resp.Length)
277268

278269
if !testing.Short() {
279270
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/media/branch/master/", big))

integrations/integration_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,9 @@ func initIntegrationTest() {
175175
setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master"
176176
_ = util.RemoveAll(repo_module.LocalCopyPath())
177177

178-
if err := git.InitOnceWithSync(context.Background()); err != nil {
178+
if err := git.InitFull(context.Background()); err != nil {
179179
log.Fatal("git.InitOnceWithSync: %v", err)
180180
}
181-
git.CheckLFSVersion()
182181

183182
setting.InitDBConfig()
184183
if err := storage.Init(); err != nil {
@@ -285,7 +284,6 @@ func prepareTestEnv(t testing.TB, skip ...int) func() {
285284
assert.NoError(t, unittest.LoadFixtures())
286285
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
287286
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
288-
assert.NoError(t, git.InitOnceWithSync(context.Background())) // the gitconfig has been removed above, so sync the gitconfig again
289287
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
290288
if err != nil {
291289
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
@@ -586,7 +584,6 @@ func resetFixtures(t *testing.T) {
586584
assert.NoError(t, unittest.LoadFixtures())
587585
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
588586
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
589-
assert.NoError(t, git.InitOnceWithSync(context.Background())) // the gitconfig has been removed above, so sync the gitconfig again
590587
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
591588
if err != nil {
592589
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)

integrations/lfs_getobject_test.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
git_model "code.gitea.io/gitea/models/git"
1616
repo_model "code.gitea.io/gitea/models/repo"
17-
"code.gitea.io/gitea/modules/git"
1817
"code.gitea.io/gitea/modules/json"
1918
"code.gitea.io/gitea/modules/lfs"
2019
"code.gitea.io/gitea/modules/setting"
@@ -83,11 +82,6 @@ func checkResponseTestContentEncoding(t *testing.T, content *[]byte, resp *httpt
8382

8483
func TestGetLFSSmall(t *testing.T) {
8584
defer prepareTestEnv(t)()
86-
git.CheckLFSVersion()
87-
if !setting.LFS.StartServer {
88-
t.Skip()
89-
return
90-
}
9185
content := []byte("A very small file\n")
9286

9387
resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
@@ -96,11 +90,6 @@ func TestGetLFSSmall(t *testing.T) {
9690

9791
func TestGetLFSLarge(t *testing.T) {
9892
defer prepareTestEnv(t)()
99-
git.CheckLFSVersion()
100-
if !setting.LFS.StartServer {
101-
t.Skip()
102-
return
103-
}
10493
content := make([]byte, web.GzipMinSize*10)
10594
for i := range content {
10695
content[i] = byte(i % 256)
@@ -112,11 +101,6 @@ func TestGetLFSLarge(t *testing.T) {
112101

113102
func TestGetLFSGzip(t *testing.T) {
114103
defer prepareTestEnv(t)()
115-
git.CheckLFSVersion()
116-
if !setting.LFS.StartServer {
117-
t.Skip()
118-
return
119-
}
120104
b := make([]byte, web.GzipMinSize*10)
121105
for i := range b {
122106
b[i] = byte(i % 256)
@@ -133,11 +117,6 @@ func TestGetLFSGzip(t *testing.T) {
133117

134118
func TestGetLFSZip(t *testing.T) {
135119
defer prepareTestEnv(t)()
136-
git.CheckLFSVersion()
137-
if !setting.LFS.StartServer {
138-
t.Skip()
139-
return
140-
}
141120
b := make([]byte, web.GzipMinSize*10)
142121
for i := range b {
143122
b[i] = byte(i % 256)
@@ -156,11 +135,6 @@ func TestGetLFSZip(t *testing.T) {
156135

157136
func TestGetLFSRangeNo(t *testing.T) {
158137
defer prepareTestEnv(t)()
159-
git.CheckLFSVersion()
160-
if !setting.LFS.StartServer {
161-
t.Skip()
162-
return
163-
}
164138
content := []byte("123456789\n")
165139

166140
resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
@@ -169,11 +143,6 @@ func TestGetLFSRangeNo(t *testing.T) {
169143

170144
func TestGetLFSRange(t *testing.T) {
171145
defer prepareTestEnv(t)()
172-
git.CheckLFSVersion()
173-
if !setting.LFS.StartServer {
174-
t.Skip()
175-
return
176-
}
177146
content := []byte("123456789\n")
178147

179148
tests := []struct {

integrations/migration-test/migration_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ func initMigrationTest(t *testing.T) func() {
8282
}
8383
}
8484

85-
assert.NoError(t, git.InitOnceWithSync(context.Background()))
86-
git.CheckLFSVersion()
85+
assert.NoError(t, git.InitFull(context.Background()))
8786
setting.InitDBConfig()
8887
setting.NewLogServices(true)
8988
return deferFn

models/migrations/migrations_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,10 @@ func TestMain(m *testing.M) {
6666

6767
setting.SetCustomPathAndConf("", "", "")
6868
setting.LoadForTest()
69-
if err = git.InitOnceWithSync(context.Background()); err != nil {
70-
fmt.Printf("Unable to InitOnceWithSync: %v\n", err)
69+
if err = git.InitFull(context.Background()); err != nil {
70+
fmt.Printf("Unable to InitFull: %v\n", err)
7171
os.Exit(1)
7272
}
73-
git.CheckLFSVersion()
7473
setting.InitDBConfig()
7574
setting.NewLogServices(true)
7675

@@ -207,7 +206,6 @@ func prepareTestEnv(t *testing.T, skip int, syncModels ...interface{}) (*xorm.En
207206
deferFn := PrintCurrentTest(t, ourSkip)
208207
assert.NoError(t, os.RemoveAll(setting.RepoRootPath))
209208
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
210-
assert.NoError(t, git.InitOnceWithSync(context.Background())) // the gitconfig has been removed above, so sync the gitconfig again
211209
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
212210
if err != nil {
213211
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)

models/unittest/testdb.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,9 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
120120
fatalTestError("util.CopyDir: %v\n", err)
121121
}
122122

123-
if err = git.InitOnceWithSync(context.Background()); err != nil {
123+
if err = git.InitFull(context.Background()); err != nil {
124124
fatalTestError("git.Init: %v\n", err)
125125
}
126-
git.CheckLFSVersion()
127-
128126
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
129127
if err != nil {
130128
fatalTestError("unable to read the new repo root: %v\n", err)
@@ -206,8 +204,6 @@ func PrepareTestEnv(t testing.TB) {
206204
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
207205
metaPath := filepath.Join(giteaRoot, "integrations", "gitea-repositories-meta")
208206
assert.NoError(t, CopyDir(metaPath, setting.RepoRootPath))
209-
assert.NoError(t, git.InitOnceWithSync(context.Background())) // the gitconfig has been removed above, so sync the gitconfig again
210-
211207
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
212208
assert.NoError(t, err)
213209
for _, ownerDir := range ownerDirs {

modules/doctor/mergebase.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ func iteratePRs(ctx context.Context, repo *repo_model.Repository, each func(*rep
3030
}
3131

3232
func checkPRMergeBase(ctx context.Context, logger log.Logger, autofix bool) error {
33-
if err := git.InitOnceWithSync(ctx); err != nil {
34-
return err
35-
}
3633
numRepos := 0
3734
numPRs := 0
3835
numPRsUpdated := 0

modules/doctor/misc.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,6 @@ func checkDaemonExport(ctx context.Context, logger log.Logger, autofix bool) err
190190
}
191191

192192
func checkCommitGraph(ctx context.Context, logger log.Logger, autofix bool) error {
193-
if err := git.InitOnceWithSync(ctx); err != nil {
194-
return err
195-
}
196-
197193
numRepos := 0
198194
numNeedUpdate := 0
199195
numWritten := 0

0 commit comments

Comments
 (0)