Skip to content

Commit

Permalink
use HOME instead of GIT_CONFIG_GLOBAL, because git always needs a cor…
Browse files Browse the repository at this point in the history
…rect HOME
  • Loading branch information
wxiaoguang committed May 17, 2022
1 parent 45a9375 commit 22d62bc
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 17 deletions.
7 changes: 3 additions & 4 deletions models/unittest/testdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ func MainTest(m *testing.M, testOpts *TestOptions) {

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

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

if err = storage.Init(); err != nil {
fatalTestError("storage.Init: %v\n", err)
}
Expand All @@ -121,6 +117,9 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
if err = CopyDir(filepath.Join(testOpts.GiteaRootPath, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
fatalTestError("util.CopyDir: %v\n", err)
}
if err = git.InitWithConfigSync(context.Background()); err != nil {
fatalTestError("git.Init: %v\n", err)
}

ownerDirs, err := os.ReadDir(setting.RepoRootPath)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions modules/git/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ type RunOpts struct {
func CommonEnvs() []string {
return []string{
fmt.Sprintf("LC_ALL=%s", DefaultLocale),
"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
"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, and GIT_CONFIG_GLOBAL, they require git >= 2.32
"HOME=" + HomeDir, // make Gitea use internal git config only, to prevent conflicts with user's git config
}
}

Expand Down Expand Up @@ -160,12 +160,12 @@ func (c *Command) Run(opts *RunOpts) error {
cmd.Env = opts.Env
}

if GlobalConfigFile == "" {
if HomeDir == "" {
// 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
// at the moment, we just use a temp HomeDir to prevent from conflicting with user's git config
// 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)
HomeDir = filepath.Join(os.TempDir(), "/gitea-temp-home")
log.Warn("Git's HomeDir is empty, the git module is not initialized correctly, using a temp HomeDir (%s) temporarily", HomeDir)
}

cmd.Env = append(cmd.Env, CommonEnvs()...)
Expand Down
6 changes: 3 additions & 3 deletions modules/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ var (
// Could be updated to an absolute path while initialization
GitExecutable = "git"

// GlobalConfigFile is the global config file used by Gitea internally
GlobalConfigFile string
// HomeDir is the home dir for git to store the global config file used by Gitea internally
HomeDir string

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

GlobalConfigFile = setting.RepoRootPath + "/gitconfig"
HomeDir = setting.RepoRootPath

if err := SetExecutablePath(setting.Git.Path); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion modules/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestMain(m *testing.M) {

func TestGitConfig(t *testing.T) {
gitConfigContains := func(sub string) bool {
if b, err := os.ReadFile(GlobalConfigFile); err == nil {
if b, err := os.ReadFile(HomeDir + "/.gitconfig"); err == nil {
return strings.Contains(string(b), sub)
}
return false
Expand Down
1 change: 1 addition & 0 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ func loadFromConf(allowEmpty bool, extraConfig string) {

Cfg.NameMapper = ini.SnackCase

// TODO: when running gitea as a sub command inside git, the HOME directory is not the user's home directory
homeDir, err := util.HomeDir()
if err != nil {
log.Fatal("Failed to get home directory: %v", err)
Expand Down
1 change: 1 addition & 0 deletions modules/util/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func FileURLToPath(u *url.URL) (string, error) {
// it returns error when the variable does not exist.
func HomeDir() (home string, err error) {
// TODO: some users run Gitea with mismatched uid and "HOME=xxx" (they set HOME=xxx by environment manually)
// TODO: when running gitea as a sub command inside git, the HOME directory is not the user's home directory
// so at the moment we can not use `user.Current().HomeDir`
if isOSWindows() {
home = os.Getenv("USERPROFILE")
Expand Down
2 changes: 1 addition & 1 deletion routers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func GlobalInitInstalled(ctx context.Context) {
}

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

git.CheckLFSVersion()
log.Info("AppPath: %s", setting.AppPath)
Expand Down

0 comments on commit 22d62bc

Please sign in to comment.