Skip to content

Commit ae3cfa8

Browse files
Only write to global gitconfig if necessary (#11876)
* Only write to global gitconfig if necessary Fix #11855 Signed-off-by: Andrew Thornton <[email protected]> * placate lint Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 48842ed commit ae3cfa8

File tree

2 files changed

+65
-25
lines changed

2 files changed

+65
-25
lines changed

modules/git/git.go

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -108,44 +108,58 @@ func SetExecutablePath(path string) error {
108108
// Init initializes git module
109109
func Init(ctx context.Context) error {
110110
DefaultContext = ctx
111-
// Git requires setting user.name and user.email in order to commit changes.
111+
// Git requires setting user.name and user.email in order to commit changes - if they're not set just add some defaults
112112
for configKey, defaultValue := range map[string]string{"user.name": "Gitea", "user.email": "[email protected]"} {
113-
if stdout, stderr, err := process.GetManager().Exec("git.Init(get setting)", GitExecutable, "config", "--get", configKey); err != nil || strings.TrimSpace(stdout) == "" {
114-
// ExitError indicates this config is not set
115-
if _, ok := err.(*exec.ExitError); ok || strings.TrimSpace(stdout) == "" {
116-
if _, stderr, gerr := process.GetManager().Exec("git.Init(set "+configKey+")", "git", "config", "--global", configKey, defaultValue); gerr != nil {
117-
return fmt.Errorf("Failed to set git %s(%s): %s", configKey, gerr, stderr)
118-
}
119-
} else {
120-
return fmt.Errorf("Failed to get git %s(%s): %s", configKey, err, stderr)
121-
}
113+
if err := checkAndSetConfig(configKey, defaultValue, false); err != nil {
114+
return err
122115
}
123116
}
124117

125-
// Set git some configurations.
126-
if _, stderr, err := process.GetManager().Exec("git.Init(git config --global core.quotepath false)",
127-
GitExecutable, "config", "--global", "core.quotepath", "false"); err != nil {
128-
return fmt.Errorf("Failed to execute 'git config --global core.quotepath false': %s", stderr)
118+
// Set git some configurations - these must be set to these values for gitea to work correctly
119+
if err := checkAndSetConfig("core.quotePath", "false", true); err != nil {
120+
return err
129121
}
130122

131123
if version.Compare(gitVersion, "2.18", ">=") {
132-
if _, stderr, err := process.GetManager().Exec("git.Init(git config --global core.commitGraph true)",
133-
GitExecutable, "config", "--global", "core.commitGraph", "true"); err != nil {
134-
return fmt.Errorf("Failed to execute 'git config --global core.commitGraph true': %s", stderr)
124+
if err := checkAndSetConfig("core.commitGraph", "true", true); err != nil {
125+
return err
135126
}
136-
137-
if _, stderr, err := process.GetManager().Exec("git.Init(git config --global gc.writeCommitGraph true)",
138-
GitExecutable, "config", "--global", "gc.writeCommitGraph", "true"); err != nil {
139-
return fmt.Errorf("Failed to execute 'git config --global gc.writeCommitGraph true': %s", stderr)
127+
if err := checkAndSetConfig("gc.writeCommitGraph", "true", true); err != nil {
128+
return err
140129
}
141130
}
142131

143132
if runtime.GOOS == "windows" {
144-
if _, stderr, err := process.GetManager().Exec("git.Init(git config --global core.longpaths true)",
145-
GitExecutable, "config", "--global", "core.longpaths", "true"); err != nil {
146-
return fmt.Errorf("Failed to execute 'git config --global core.longpaths true': %s", stderr)
133+
if err := checkAndSetConfig("core.longpaths", "true", true); err != nil {
134+
return err
135+
}
136+
}
137+
return nil
138+
}
139+
140+
func checkAndSetConfig(key, defaultValue string, forceToDefault bool) error {
141+
stdout, stderr, err := process.GetManager().Exec("git.Init(get setting)", GitExecutable, "config", "--get", key)
142+
if err != nil {
143+
perr, ok := err.(*process.Error)
144+
if !ok {
145+
return fmt.Errorf("Failed to get git %s(%v) errType %T: %s", key, err, err, stderr)
147146
}
147+
eerr, ok := perr.Err.(*exec.ExitError)
148+
if !ok || eerr.ExitCode() != 1 {
149+
return fmt.Errorf("Failed to get git %s(%v) errType %T: %s", key, err, err, stderr)
150+
}
151+
}
152+
153+
currValue := strings.TrimSpace(stdout)
154+
155+
if currValue == defaultValue || (!forceToDefault && len(currValue) > 0) {
156+
return nil
148157
}
158+
159+
if _, stderr, err = process.GetManager().Exec(fmt.Sprintf("git.Init(set %s)", key), "git", "config", "--global", key, defaultValue); err != nil {
160+
return fmt.Errorf("Failed to set git %s(%s): %s", key, err, stderr)
161+
}
162+
149163
return nil
150164
}
151165

modules/process/manager.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,14 @@ func (pm *Manager) ExecDirEnvStdIn(timeout time.Duration, dir, desc string, env
157157
pm.Remove(pid)
158158

159159
if err != nil {
160-
err = fmt.Errorf("exec(%d:%s) failed: %v(%v) stdout: %v stderr: %v", pid, desc, err, ctx.Err(), stdOut, stdErr)
160+
err = &Error{
161+
PID: pid,
162+
Description: desc,
163+
Err: err,
164+
CtxErr: ctx.Err(),
165+
Stdout: stdOut.String(),
166+
Stderr: stdErr.String(),
167+
}
161168
}
162169

163170
return stdOut.String(), stdErr.String(), err
@@ -176,3 +183,22 @@ func (l processList) Less(i, j int) bool {
176183
func (l processList) Swap(i, j int) {
177184
l[i], l[j] = l[j], l[i]
178185
}
186+
187+
// Error is a wrapped error describing the error results of Process Execution
188+
type Error struct {
189+
PID int64
190+
Description string
191+
Err error
192+
CtxErr error
193+
Stdout string
194+
Stderr string
195+
}
196+
197+
func (err *Error) Error() string {
198+
return fmt.Sprintf("exec(%d:%s) failed: %v(%v) stdout: %s stderr: %s", err.PID, err.Description, err.Err, err.CtxErr, err.Stdout, err.Stderr)
199+
}
200+
201+
// Unwrap implements the unwrappable implicit interface for go1.13 Unwrap()
202+
func (err *Error) Unwrap() error {
203+
return err.Err
204+
}

0 commit comments

Comments
 (0)