Skip to content

Commit 22d62bc

Browse files
committed
use HOME instead of GIT_CONFIG_GLOBAL, because git always needs a correct HOME
1 parent 45a9375 commit 22d62bc

File tree

7 files changed

+18
-17
lines changed

7 files changed

+18
-17
lines changed

models/unittest/testdb.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
107107

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

110-
if err = git.InitWithConfigSync(context.Background()); err != nil {
111-
fatalTestError("git.Init: %v\n", err)
112-
}
113-
114110
if err = storage.Init(); err != nil {
115111
fatalTestError("storage.Init: %v\n", err)
116112
}
@@ -121,6 +117,9 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
121117
if err = CopyDir(filepath.Join(testOpts.GiteaRootPath, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
122118
fatalTestError("util.CopyDir: %v\n", err)
123119
}
120+
if err = git.InitWithConfigSync(context.Background()); err != nil {
121+
fatalTestError("git.Init: %v\n", err)
122+
}
124123

125124
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
126125
if err != nil {

modules/git/command.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ type RunOpts struct {
109109
func CommonEnvs() []string {
110110
return []string{
111111
fmt.Sprintf("LC_ALL=%s", DefaultLocale),
112-
"GIT_TERMINAL_PROMPT=0", // avoid prompting for credentials interactively, supported since git v2.3
113-
"GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace)
114-
"GIT_CONFIG_NOSYSTEM=1", // https://git-scm.com/docs/git-config
115-
"GIT_CONFIG_GLOBAL=" + GlobalConfigFile, // make Gitea use internal git config only, to prevent conflicts with user's git config
112+
"GIT_TERMINAL_PROMPT=0", // avoid prompting for credentials interactively, supported since git v2.3
113+
"GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace)
114+
"GIT_CONFIG_NOSYSTEM=1", // https://git-scm.com/docs/git-config, and GIT_CONFIG_GLOBAL, they require git >= 2.32
115+
"HOME=" + HomeDir, // make Gitea use internal git config only, to prevent conflicts with user's git config
116116
}
117117
}
118118

@@ -160,12 +160,12 @@ func (c *Command) Run(opts *RunOpts) error {
160160
cmd.Env = opts.Env
161161
}
162162

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

171171
cmd.Env = append(cmd.Env, CommonEnvs()...)

modules/git/git.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ var (
3131
// Could be updated to an absolute path while initialization
3232
GitExecutable = "git"
3333

34-
// GlobalConfigFile is the global config file used by Gitea internally
35-
GlobalConfigFile string
34+
// HomeDir is the home dir for git to store the global config file used by Gitea internally
35+
HomeDir string
3636

3737
// DefaultContext is the default context to run git commands in
3838
// will be overwritten by InitWithConfigSync with HammerContext
@@ -142,7 +142,7 @@ func InitSimple(ctx context.Context) error {
142142
return errors.New("RepoRootPath is empty, git module needs that setting before initialization")
143143
}
144144

145-
GlobalConfigFile = setting.RepoRootPath + "/gitconfig"
145+
HomeDir = setting.RepoRootPath
146146

147147
if err := SetExecutablePath(setting.Git.Path); err != nil {
148148
return err

modules/git/git_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestMain(m *testing.M) {
4848

4949
func TestGitConfig(t *testing.T) {
5050
gitConfigContains := func(sub string) bool {
51-
if b, err := os.ReadFile(GlobalConfigFile); err == nil {
51+
if b, err := os.ReadFile(HomeDir + "/.gitconfig"); err == nil {
5252
return strings.Contains(string(b), sub)
5353
}
5454
return false

modules/setting/setting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
612612

613613
Cfg.NameMapper = ini.SnackCase
614614

615+
// TODO: when running gitea as a sub command inside git, the HOME directory is not the user's home directory
615616
homeDir, err := util.HomeDir()
616617
if err != nil {
617618
log.Fatal("Failed to get home directory: %v", err)

modules/util/path.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ func FileURLToPath(u *url.URL) (string, error) {
182182
// it returns error when the variable does not exist.
183183
func HomeDir() (home string, err error) {
184184
// TODO: some users run Gitea with mismatched uid and "HOME=xxx" (they set HOME=xxx by environment manually)
185+
// TODO: when running gitea as a sub command inside git, the HOME directory is not the user's home directory
185186
// so at the moment we can not use `user.Current().HomeDir`
186187
if isOSWindows() {
187188
home = os.Getenv("USERPROFILE")

routers/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func GlobalInitInstalled(ctx context.Context) {
103103
}
104104

105105
mustInitCtx(ctx, git.InitWithConfigSync)
106-
log.Info("Git Version: %s (config: %s)", git.VersionInfo(), git.GlobalConfigFile)
106+
log.Info("Git Version: %s (home: %s)", git.VersionInfo(), git.HomeDir)
107107

108108
git.CheckLFSVersion()
109109
log.Info("AppPath: %s", setting.AppPath)

0 commit comments

Comments
 (0)