mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Refactor git module, make Gitea use internal git config (#19732)
* Refactor git module, make Gitea use internal git config, add safe.directory config * introduce git.InitSimple and git.InitWithConfigSync, make serv cmd use gitconfig * use HOME instead of GIT_CONFIG_GLOBAL, because git always needs a correct HOME * fix cmd env in cmd/serv.go * fine tune error message * Fix a incorrect test case * fix configAddNonExist * fix configAddNonExist logic, add `--fixed-value` flag, add tests * add configSetNonExist function in case it's needed. * use configSetNonExist for `user.name` and `user.email` * add some comments * Update cmd/serv.go Co-authored-by: zeripath <art27@cantab.net> * Update cmd/serv.go Co-authored-by: zeripath <art27@cantab.net> * Update modules/git/git.go Co-authored-by: zeripath <art27@cantab.net> * Update modules/setting/setting.go Co-authored-by: zeripath <art27@cantab.net> * Update modules/git/repo_attribute.go Co-authored-by: zeripath <art27@cantab.net> * fix spaces in messages * use `configSet("core.protectNTFS", ...)` instead of `globalCommandArgs` * remove GIT_CONFIG_NOSYSTEM, continue to use system's git config * Update cmd/serv.go Co-authored-by: zeripath <art27@cantab.net> * fix merge * remove code for safe.directory * separate git.CommonEnvs to CommonGitCmdEnvs and CommonCmdServEnvs * avoid Golang's data race error Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
		@@ -8,6 +8,7 @@ package git
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"context"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"os"
 | 
			
		||||
@@ -104,6 +105,25 @@ type RunOpts struct {
 | 
			
		||||
	PipelineFunc   func(context.Context, context.CancelFunc) error
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CommonGitCmdEnvs returns the common environment variables for a "git" command.
 | 
			
		||||
func CommonGitCmdEnvs() []string {
 | 
			
		||||
	// at the moment, do not set "GIT_CONFIG_NOSYSTEM", users may have put some configs like "receive.certNonceSeed" in it
 | 
			
		||||
	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)
 | 
			
		||||
		"HOME=" + HomeDir(),        // make Gitea use internal git config only, to prevent conflicts with user's git config
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CommonCmdServEnvs is like CommonGitCmdEnvs but it only returns minimal required environment variables for the "gitea serv" command
 | 
			
		||||
func CommonCmdServEnvs() []string {
 | 
			
		||||
	return []string{
 | 
			
		||||
		"GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace)
 | 
			
		||||
		"HOME=" + HomeDir(),        // make Gitea use internal git config only, to prevent conflicts with user's git config
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Run runs the command with the RunOpts
 | 
			
		||||
func (c *Command) Run(opts *RunOpts) error {
 | 
			
		||||
	if opts == nil {
 | 
			
		||||
@@ -148,16 +168,8 @@ func (c *Command) Run(opts *RunOpts) error {
 | 
			
		||||
		cmd.Env = opts.Env
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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",
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	process.SetSysProcAttribute(cmd)
 | 
			
		||||
	cmd.Env = append(cmd.Env, CommonGitCmdEnvs()...)
 | 
			
		||||
	cmd.Dir = opts.Dir
 | 
			
		||||
	cmd.Stdout = opts.Stdout
 | 
			
		||||
	cmd.Stderr = opts.Stderr
 | 
			
		||||
@@ -184,7 +196,9 @@ func (c *Command) Run(opts *RunOpts) error {
 | 
			
		||||
 | 
			
		||||
type RunStdError interface {
 | 
			
		||||
	error
 | 
			
		||||
	Unwrap() error
 | 
			
		||||
	Stderr() string
 | 
			
		||||
	IsExitCode(code int) bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type runStdError struct {
 | 
			
		||||
@@ -209,6 +223,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)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user