* 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>
Showing the changes to modules/git/git.go (1 of 2 files). Show all
Unified · Split
| @@ -108,44 +108,58 @@ func SetExecutablePath(path string) error { · 107 unmodified lines | ||
| 108 | 108 | // Init initializes git module |
| 109 | 109 | func Init(ctx context.Context) error { |
| 110 | 110 | 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 | |
| 112 | 112 | 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 | |
| 122 | 115 | } |
| 123 | 116 | } |
| 124 | 117 | |
| 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 | |
| 129 | 121 | } |
| 130 | 122 | |
| 131 | 123 | 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 | |
| 135 | 126 | } |
| 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 | |
| 140 | 129 | } |
| 141 | 130 | } |
| 142 | 131 | |
| 143 | 132 | 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) | |
| 147 | 146 | } |
| 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 | |
| 148 | 157 | } |
| 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 | + | |
| 149 | 163 | return nil |
| 150 | 164 | } |
| 151 | 165 | |