-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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 19 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,16 @@ type RunOpts struct { | |
PipelineFunc func(context.Context, context.CancelFunc) error | ||
} | ||
|
||
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, 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 | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
// Run runs the command with the RunOpts | ||
func (c *Command) Run(opts *RunOpts) error { | ||
if opts == nil { | ||
|
@@ -148,15 +160,15 @@ 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.
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.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not fix those tests? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR has changed a lot, I do not want to mix too many things together. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm... I have a feeling that not fixing those tests would come back to bite us later on. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am sure it's not that serious. It only affects the unit tests of To fix them, it requires extra work and will make this PR more complex. Without fixing them, everything is still fine. ps: even if some tests bite us later, there is a clear warning and the tests can be fixed easily. If these tests must be fixed, I still prefer to fix them in another PR, instead of mixing together. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created a separate issue to make sure the task won't be forgotten. |
||
} | ||
|
||
cmd.Env = append(cmd.Env, CommonEnvs()...) | ||
cmd.Dir = opts.Dir | ||
cmd.Stdout = opts.Stdout | ||
cmd.Stderr = opts.Stderr | ||
|
@@ -183,7 +195,9 @@ func (c *Command) Run(opts *RunOpts) error { | |
|
||
type RunStdError interface { | ||
error | ||
Unwrap() error | ||
Stderr() string | ||
IsExitCode(code int) bool | ||
} | ||
|
||
type runStdError struct { | ||
|
@@ -208,6 +222,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.