2024-11-21 10:23:57 +00:00
|
|
|
package git
|
|
|
|
|
2024-11-23 16:25:58 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type configEntry struct {
|
|
|
|
value string
|
|
|
|
fn func(string, string) error
|
|
|
|
}
|
2024-11-21 10:23:57 +00:00
|
|
|
|
|
|
|
func InitGitConfig() error {
|
2024-11-23 16:25:58 +00:00
|
|
|
configs := map[string]configEntry{
|
|
|
|
"receive.advertisePushOptions": {value: "true", fn: setGitConfig},
|
|
|
|
"safe.directory": {value: "*", fn: addGitConfig},
|
2024-11-21 10:23:57 +00:00
|
|
|
}
|
|
|
|
|
2024-11-23 16:25:58 +00:00
|
|
|
for key, entry := range configs {
|
|
|
|
if err := entry.fn(key, entry.value); err != nil {
|
2024-11-21 10:23:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setGitConfig(key, value string) error {
|
2024-11-23 16:25:58 +00:00
|
|
|
_, err := getGitConfig(key, value)
|
|
|
|
if err != nil && !checkErrorCode(err, 1) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-21 10:23:57 +00:00
|
|
|
cmd := exec.Command("git", "config", "--global", key, value)
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
2024-11-23 16:25:58 +00:00
|
|
|
|
|
|
|
func addGitConfig(key, value string) error {
|
|
|
|
_, err := getGitConfig(key, regexp.QuoteMeta(value))
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if checkErrorCode(err, 1) {
|
|
|
|
cmd := exec.Command("git", "config", "--global", "--add", key, value)
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGitConfig(key, value string) (string, error) {
|
|
|
|
cmd := exec.Command("git", "config", "--global", "--get", key, value)
|
|
|
|
out, err := cmd.Output()
|
|
|
|
return string(out), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkErrorCode(err error, code int) bool {
|
|
|
|
var exitError *exec.ExitError
|
|
|
|
if errors.As(err, &exitError) {
|
|
|
|
return exitError.ExitCode() == code
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|