Skip to content

Commit dac2594

Browse files
committed
Refactor git module, make Gitea use internal git config, add safe.directory config
1 parent 6d54799 commit dac2594

File tree

18 files changed

+171
-153
lines changed

18 files changed

+171
-153
lines changed

integrations/integration_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ func prepareTestEnv(t testing.TB, skip ...int) func() {
261261
deferFn := PrintCurrentTest(t, ourSkip)
262262
assert.NoError(t, unittest.LoadFixtures())
263263
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
264-
265264
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
265+
assert.NoError(t, git.Init(context.Background()))
266266
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
267267
if err != nil {
268268
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
@@ -563,6 +563,7 @@ func resetFixtures(t *testing.T) {
563563
assert.NoError(t, unittest.LoadFixtures())
564564
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
565565
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
566+
assert.NoError(t, git.Init(context.Background()))
566567
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
567568
if err != nil {
568569
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)

integrations/migration-test/migration_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func initMigrationTest(t *testing.T) func() {
6262
assert.True(t, len(setting.RepoRootPath) != 0)
6363
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
6464
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
65+
assert.NoError(t, git.Init(context.Background()))
6566
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
6667
if err != nil {
6768
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)

models/migrations/migrations_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,8 @@ func prepareTestEnv(t *testing.T, skip int, syncModels ...interface{}) (*xorm.En
202202
ourSkip += skip
203203
deferFn := PrintCurrentTest(t, ourSkip)
204204
assert.NoError(t, os.RemoveAll(setting.RepoRootPath))
205-
206-
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
207-
setting.RepoRootPath))
205+
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
206+
assert.NoError(t, git.Init(context.Background()))
208207
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
209208
if err != nil {
210209
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)

models/unittest/testdb.go

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"code.gitea.io/gitea/models/db"
1616
"code.gitea.io/gitea/modules/base"
17+
"code.gitea.io/gitea/modules/git"
1718
"code.gitea.io/gitea/modules/setting"
1819
"code.gitea.io/gitea/modules/storage"
1920
"code.gitea.io/gitea/modules/util"
@@ -106,6 +107,10 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
106107

107108
setting.Packages.Storage.Path = filepath.Join(setting.AppDataPath, "packages")
108109

110+
if err = git.Init(context.Background()); err != nil {
111+
fatalTestError("git.Init: %v\n", err)
112+
}
113+
109114
if err = storage.Init(); err != nil {
110115
fatalTestError("storage.Init: %v\n", err)
111116
}
@@ -198,6 +203,7 @@ func PrepareTestEnv(t testing.TB) {
198203
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
199204
metaPath := filepath.Join(giteaRoot, "integrations", "gitea-repositories-meta")
200205
assert.NoError(t, CopyDir(metaPath, setting.RepoRootPath))
206+
assert.NoError(t, git.Init(context.Background()))
201207

202208
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
203209
assert.NoError(t, err)

modules/git/command.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ package git
88
import (
99
"bytes"
1010
"context"
11+
"errors"
1112
"fmt"
1213
"io"
1314
"os"
1415
"os/exec"
16+
"path/filepath"
1517
"strings"
1618
"time"
1719
"unsafe"
@@ -148,13 +150,21 @@ func (c *Command) Run(opts *RunOpts) error {
148150
cmd.Env = opts.Env
149151
}
150152

153+
if GlobalConfigFile == "" {
154+
// TODO: now, some unit test code call the git module directly without initialization, which is incorrect.
155+
// at the moment, we just use a temp gitconfig to prevent from conflicting with user's gitconfig
156+
// in future, the git module should be initialized first before use.
157+
GlobalConfigFile = filepath.Join(os.TempDir(), "/gitea-temp-gitconfig")
158+
log.Warn("GlobalConfigFile is empty, the git module is not initialized correctly, using a temp gitconfig (%s) temporarily", GlobalConfigFile)
159+
}
160+
151161
cmd.Env = append(
152162
cmd.Env,
153163
fmt.Sprintf("LC_ALL=%s", DefaultLocale),
154-
// avoid prompting for credentials interactively, supported since git v2.3
155-
"GIT_TERMINAL_PROMPT=0",
156-
// ignore replace references (https://git-scm.com/docs/git-replace)
157-
"GIT_NO_REPLACE_OBJECTS=1",
164+
"GIT_TERMINAL_PROMPT=0", // avoid prompting for credentials interactively, supported since git v2.3
165+
"GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace)
166+
"GIT_CONFIG_NOSYSTEM=1", // https://git-scm.com/docs/git-config
167+
"GIT_CONFIG_GLOBAL="+GlobalConfigFile, // make Gitea use internal git config only, to prevent conflicts with user's git config
158168
)
159169

160170
cmd.Dir = opts.Dir
@@ -183,7 +193,9 @@ func (c *Command) Run(opts *RunOpts) error {
183193

184194
type RunStdError interface {
185195
error
196+
Unwrap() error
186197
Stderr() string
198+
IsExitCode(code int) bool
187199
}
188200

189201
type runStdError struct {
@@ -208,6 +220,14 @@ func (r *runStdError) Stderr() string {
208220
return r.stderr
209221
}
210222

223+
func (r *runStdError) IsExitCode(code int) bool {
224+
var exitError *exec.ExitError
225+
if errors.As(r.err, &exitError) {
226+
return exitError.ExitCode() == code
227+
}
228+
return false
229+
}
230+
211231
func bytesToString(b []byte) string {
212232
return *(*string)(unsafe.Pointer(&b)) // that's what Golang's strings.Builder.String() does (go/src/strings/builder.go)
213233
}

modules/git/commit.go

-5
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,6 @@ func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
398398

399399
// GetBranchName gets the closest branch name (as returned by 'git name-rev --name-only')
400400
func (c *Commit) GetBranchName() (string, error) {
401-
err := LoadGitVersion()
402-
if err != nil {
403-
return "", fmt.Errorf("Git version missing: %v", err)
404-
}
405-
406401
args := []string{
407402
"name-rev",
408403
}

0 commit comments

Comments
 (0)