Merge pull request #441 from tstorch/git-setup

Standard git user.name and user.email if not set
This commit is contained in:
Lunny Xiao 2014-09-04 22:55:05 +08:00
commit 8db8771be7
1 changed files with 16 additions and 10 deletions

View File

@ -99,20 +99,26 @@ func NewRepoContext() {
log.Fatal(4, "Gogs requires Git version greater or equal to 1.8.0") log.Fatal(4, "Gogs requires Git version greater or equal to 1.8.0")
} }
// Check if server has basic git setting. // Check if server has basic git setting and set if not.
stdout, stderr, err := process.Exec("NewRepoContext(get setting)", "git", "config", "--get", "user.name") if stdout, stderr, err := process.Exec("NewRepoContext(get setting)", "git", "config", "--get", "user.name"); err != nil || strings.TrimSpace(stdout) == "" {
if err != nil { // ExitError indicates user.name is not set
log.Fatal(4, "Fail to get git user.name: %s", stderr) if _, ok := err.(*exec.ExitError); ok || strings.TrimSpace(stdout) == "" {
} else if err != nil || len(strings.TrimSpace(stdout)) == 0 { stndrdUserName := "Gogs"
if _, stderr, err = process.Exec("NewRepoContext(set email)", "git", "config", "--global", "user.email", "gogitservice@gmail.com"); err != nil { stndrdUserEmail := "gogitservice@gmail.com"
log.Fatal(4, "Fail to set git user.email: %s", stderr) if _, stderr, gerr := process.Exec("NewRepoContext(set name)", "git", "config", "--global", "user.name", stndrdUserName); gerr != nil {
} else if _, stderr, err = process.Exec("NewRepoContext(set name)", "git", "config", "--global", "user.name", "Gogs"); err != nil { log.Fatal(4, "Fail to set git user.name(%s): %s", gerr, stderr)
log.Fatal(4, "Fail to set git user.name: %s", stderr) }
if _, stderr, gerr := process.Exec("NewRepoContext(set email)", "git", "config", "--global", "user.email", stndrdUserEmail); gerr != nil {
log.Fatal(4, "Fail to set git user.email(%s): %s", gerr, stderr)
}
log.Info("Git user.name and user.email set to %s <%s>", stndrdUserName, stndrdUserEmail)
} else {
log.Fatal(4, "Fail to get git user.name(%s): %s", err, stderr)
} }
} }
// Set git some configurations. // Set git some configurations.
if _, stderr, err = process.Exec("NewRepoContext(git config --global core.quotepath false)", if _, stderr, err := process.Exec("NewRepoContext(git config --global core.quotepath false)",
"git", "config", "--global", "core.quotepath", "false"); err != nil { "git", "config", "--global", "core.quotepath", "false"); err != nil {
log.Fatal(4, "Fail to execute 'git config --global core.quotepath false': %s", stderr) log.Fatal(4, "Fail to execute 'git config --global core.quotepath false': %s", stderr)
} }