aboutsummaryrefslogtreecommitdiffstats
path: root/modules/git/command.go
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2022-06-10 09:57:49 +0800
committerGitHub <noreply@github.com>2022-06-10 09:57:49 +0800
commita0051634b982608d94399033a8b76e7e3b1537ac (patch)
tree174bf860eb23c80bff16dc0e94f8d7d1a3437f0a /modules/git/command.go
parente26f84a9b75f250f951e0efa5c5c62860b3c8370 (diff)
downloadgitea-a0051634b982608d94399033a8b76e7e3b1537ac.tar.gz
gitea-a0051634b982608d94399033a8b76e7e3b1537ac.zip
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>
Diffstat (limited to 'modules/git/command.go')
-rw-r--r--modules/git/command.go40
1 files changed, 31 insertions, 9 deletions
diff --git a/modules/git/command.go b/modules/git/command.go
index f6344dbfd1..d71497f1d7 100644
--- a/modules/git/command.go
+++ b/modules/git/command.go
@@ -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)
}