Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor git module, make Gitea use internal git config #19732

Merged
merged 32 commits into from Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
dac2594
Refactor git module, make Gitea use internal git config, add safe.dir…
wxiaoguang May 17, 2022
45a9375
introduce git.InitSimple and git.InitWithConfigSync, make serv cmd us…
wxiaoguang May 17, 2022
22d62bc
use HOME instead of GIT_CONFIG_GLOBAL, because git always needs a cor…
wxiaoguang May 17, 2022
b7f928c
fix cmd env in cmd/serv.go
wxiaoguang May 17, 2022
0841cc6
fine tune error message
wxiaoguang May 17, 2022
6274dc9
Fix a incorrect test case
wxiaoguang May 17, 2022
fae4484
Merge branch 'main' into refactor-git-fix-safedirectory
wxiaoguang May 17, 2022
b06a4ef
fix configAddNonExist
wxiaoguang May 17, 2022
fba95dc
fix configAddNonExist logic, add `--fixed-value` flag, add tests
wxiaoguang May 17, 2022
241104d
add configSetNonExist function in case it's needed.
wxiaoguang May 17, 2022
3b1e6d1
use configSetNonExist for `user.name` and `user.email`
wxiaoguang May 17, 2022
4e2679f
add some comments
wxiaoguang May 17, 2022
9866add
Update cmd/serv.go
wxiaoguang May 18, 2022
5d6cd68
Update cmd/serv.go
wxiaoguang May 18, 2022
7d96b39
Update modules/git/git.go
wxiaoguang May 18, 2022
c09f9c9
Update modules/setting/setting.go
wxiaoguang May 18, 2022
57d9b1b
Update modules/git/repo_attribute.go
wxiaoguang May 18, 2022
ae9d786
fix spaces in messages
wxiaoguang May 18, 2022
71eb7f4
use `configSet("core.protectNTFS", ...)` instead of `globalCommandArgs`
wxiaoguang May 18, 2022
1da696d
remove GIT_CONFIG_NOSYSTEM, continue to use system's git config
wxiaoguang May 19, 2022
58e909c
Merge branch 'main' into refactor-git-fix-safedirectory
wxiaoguang May 19, 2022
9eeabb1
Merge branch 'main' into refactor-git-fix-safedirectory
wxiaoguang May 21, 2022
733445c
Update cmd/serv.go
wxiaoguang Jun 2, 2022
1472714
Merge branch 'main' into refactor-git-fix-safedirectory
wxiaoguang Jun 4, 2022
30c49f3
fix merge
wxiaoguang Jun 4, 2022
c5f1e94
remove code for safe.directory
wxiaoguang Jun 4, 2022
b301197
separate git.CommonEnvs to CommonGitCmdEnvs and CommonCmdServEnvs
wxiaoguang Jun 5, 2022
985620e
Merge branch 'main' into refactor-git-fix-safedirectory
wxiaoguang Jun 8, 2022
25f1906
Merge branch 'main' into refactor-git-fix-safedirectory
lunny Jun 9, 2022
26fa087
avoid Golang's data race error
wxiaoguang Jun 9, 2022
7fb7c6e
Merge branch 'main' into refactor-git-fix-safedirectory
wxiaoguang Jun 9, 2022
dedca11
Merge branch 'main' into refactor-git-fix-safedirectory
lunny Jun 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion integrations/integration_test.go
Expand Up @@ -261,8 +261,8 @@ func prepareTestEnv(t testing.TB, skip ...int) func() {
deferFn := PrintCurrentTest(t, ourSkip)
assert.NoError(t, unittest.LoadFixtures())
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))

assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
assert.NoError(t, git.Init(context.Background()))
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
if err != nil {
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
Expand Down Expand Up @@ -563,6 +563,7 @@ func resetFixtures(t *testing.T) {
assert.NoError(t, unittest.LoadFixtures())
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
assert.NoError(t, git.Init(context.Background()))
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
if err != nil {
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
Expand Down
1 change: 1 addition & 0 deletions integrations/migration-test/migration_test.go
Expand Up @@ -62,6 +62,7 @@ func initMigrationTest(t *testing.T) func() {
assert.True(t, len(setting.RepoRootPath) != 0)
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
assert.NoError(t, git.Init(context.Background()))
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
if err != nil {
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
Expand Down
5 changes: 2 additions & 3 deletions models/migrations/migrations_test.go
Expand Up @@ -202,9 +202,8 @@ func prepareTestEnv(t *testing.T, skip int, syncModels ...interface{}) (*xorm.En
ourSkip += skip
deferFn := PrintCurrentTest(t, ourSkip)
assert.NoError(t, os.RemoveAll(setting.RepoRootPath))

assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
setting.RepoRootPath))
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
assert.NoError(t, git.Init(context.Background()))
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
if err != nil {
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
Expand Down
6 changes: 6 additions & 0 deletions models/unittest/testdb.go
Expand Up @@ -14,6 +14,7 @@ import (

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/util"
Expand Down Expand Up @@ -106,6 +107,10 @@ func MainTest(m *testing.M, testOpts *TestOptions) {

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

if err = git.Init(context.Background()); err != nil {
fatalTestError("git.Init: %v\n", err)
}

if err = storage.Init(); err != nil {
fatalTestError("storage.Init: %v\n", err)
}
Expand Down Expand Up @@ -198,6 +203,7 @@ func PrepareTestEnv(t testing.TB) {
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
metaPath := filepath.Join(giteaRoot, "integrations", "gitea-repositories-meta")
assert.NoError(t, CopyDir(metaPath, setting.RepoRootPath))
assert.NoError(t, git.Init(context.Background()))

ownerDirs, err := os.ReadDir(setting.RepoRootPath)
assert.NoError(t, err)
Expand Down
28 changes: 24 additions & 4 deletions modules/git/command.go
Expand Up @@ -8,10 +8,12 @@ package git
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"unsafe"
Expand Down Expand Up @@ -148,13 +150,21 @@ func (c *Command) Run(opts *RunOpts) error {
cmd.Env = opts.Env
}

if GlobalConfigFile == "" {
// TODO: now, some unit test code call the git module directly without initialization, which is incorrect.
// at the moment, we just use a temp gitconfig to prevent from conflicting with user's gitconfig
// in future, the git module should be initialized first before use.
GlobalConfigFile = filepath.Join(os.TempDir(), "/gitea-temp-gitconfig")
log.Warn("GlobalConfigFile is empty, the git module is not initialized correctly, using a temp gitconfig (%s) temporarily", GlobalConfigFile)
}

cmd.Env = append(
cmd.Env,
fmt.Sprintf("LC_ALL=%s", DefaultLocale),
// avoid prompting for credentials interactively, supported since git v2.3
"GIT_TERMINAL_PROMPT=0",
// ignore replace references (https://git-scm.com/docs/git-replace)
"GIT_NO_REPLACE_OBJECTS=1",
"GIT_TERMINAL_PROMPT=0", // avoid prompting for credentials interactively, supported since git v2.3
"GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace)
"GIT_CONFIG_NOSYSTEM=1", // https://git-scm.com/docs/git-config
"GIT_CONFIG_GLOBAL="+GlobalConfigFile, // make Gitea use internal git config only, to prevent conflicts with user's git config
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
)

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

type RunStdError interface {
error
Unwrap() error
Stderr() string
IsExitCode(code int) bool
}

type runStdError struct {
Expand All @@ -208,6 +220,14 @@ func (r *runStdError) Stderr() string {
return r.stderr
}

func (r *runStdError) IsExitCode(code int) bool {
var exitError *exec.ExitError
if errors.As(r.err, &exitError) {
return exitError.ExitCode() == code
}
return false
}

func bytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b)) // that's what Golang's strings.Builder.String() does (go/src/strings/builder.go)
}
Expand Down
5 changes: 0 additions & 5 deletions modules/git/commit.go
Expand Up @@ -398,11 +398,6 @@ func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {

// GetBranchName gets the closest branch name (as returned by 'git name-rev --name-only')
func (c *Commit) GetBranchName() (string, error) {
err := LoadGitVersion()
if err != nil {
return "", fmt.Errorf("Git version missing: %v", err)
}

args := []string{
"name-rev",
}
Expand Down