History

Only write to global gitconfig if necessary (#11876)

* Only write to global gitconfig if necessary Fix #11855 Signed-off-by: Andrew Thornton <art27@cantab.net> * placate lint Signed-off-by: Andrew Thornton <art27@cantab.net>

zeripath committed 6 years ago

Commit ae3cfa81 parent 48842ed2 files · +65 −25Browse files

Showing the changes to modules/git/git.go (1 of 2 files). Show all

Unified · Split

modules/git/git.go+38−24
@@ -108,44 +108,58 @@ func SetExecutablePath(path string) error { · 107 unmodified lines
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": "gitea@fake.local"} {
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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t