-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Refactor git module, make Gitea use internal git config #19732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 28 commits
dac2594
45a9375
22d62bc
b7f928c
0841cc6
6274dc9
fae4484
b06a4ef
fba95dc
241104d
3b1e6d1
4e2679f
9866add
5d6cd68
7d96b39
c09f9c9
57d9b1b
ae9d786
71eb7f4
1da696d
58e909c
9eeabb1
733445c
1472714
30c49f3
c5f1e94
b301197
985620e
25f1906
26fa087
7fb7c6e
dedca11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,10 +8,12 @@ package git | |
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
| "unsafe" | ||
|
|
@@ -104,6 +106,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 +169,16 @@ 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", | ||
| ) | ||
| 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 HomeDir to prevent from conflicting with user's git config | ||
| // in future, the git module should be initialized first before use. | ||
| HomeDir = filepath.Join(os.TempDir(), "/gitea-temp-home") | ||
lunny marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| log.Warn("Git's HomeDir is empty, the git module is not initialized correctly, using a temp HomeDir (%s) temporarily", HomeDir) | ||
wxiaoguang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| process.SetSysProcAttribute(cmd) | ||
| cmd.Env = append(cmd.Env, CommonGitCmdEnvs()...) | ||
| cmd.Dir = opts.Dir | ||
| cmd.Stdout = opts.Stdout | ||
| cmd.Stderr = opts.Stderr | ||
|
|
@@ -184,7 +205,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 +232,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) | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.