summaryrefslogtreecommitdiffstats
path: root/modules/git/command.go
diff options
context:
space:
mode:
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)
}