summaryrefslogtreecommitdiffstats
path: root/modules/git/git.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-05-15 03:57:00 +0200
committerGitHub <noreply@github.com>2019-05-15 03:57:00 +0200
commit710245e81e0d65c72231dbb3b5c9f860cdc71899 (patch)
tree6987b804cb38ab175e6749e7c4b9e77b0362e499 /modules/git/git.go
parentFix code overflow (#6914) (diff)
downloadforgejo-710245e81e0d65c72231dbb3b5c9f860cdc71899.tar.xz
forgejo-710245e81e0d65c72231dbb3b5c9f860cdc71899.zip
Refactor models.NewRepoContext to extract git related codes to modules/git (#6941)
* refactor models.NewRepoContext to extract git related codes to modules/git * fix imports * refactor
Diffstat (limited to 'modules/git/git.go')
-rw-r--r--modules/git/git.go26
1 files changed, 24 insertions, 2 deletions
diff --git a/modules/git/git.go b/modules/git/git.go
index abae0423c2..632af539cc 100644
--- a/modules/git/git.go
+++ b/modules/git/git.go
@@ -11,6 +11,8 @@ import (
"strings"
"time"
+ "code.gitea.io/gitea/modules/process"
+
"github.com/mcuadros/go-version"
)
@@ -31,6 +33,8 @@ var (
// GitExecutable is the command name of git
// Could be updated to an absolute path while initialization
GitExecutable = "git"
+
+ gitVersion string
)
func log(format string, args ...interface{}) {
@@ -46,8 +50,6 @@ func log(format string, args ...interface{}) {
}
}
-var gitVersion string
-
// BinVersion returns current Git version from shell.
func BinVersion() (string, error) {
if len(gitVersion) > 0 {
@@ -89,6 +91,26 @@ func init() {
if version.Compare(gitVersion, GitVersionRequired, "<") {
panic(fmt.Sprintf("Git version not supported. Requires version > %v", GitVersionRequired))
}
+
+ // Git requires setting user.name and user.email in order to commit changes.
+ for configKey, defaultValue := range map[string]string{"user.name": "Gitea", "user.email": "gitea@fake.local"} {
+ if stdout, stderr, err := process.GetManager().Exec("git.Init(get setting)", GitExecutable, "config", "--get", configKey); err != nil || strings.TrimSpace(stdout) == "" {
+ // ExitError indicates this config is not set
+ if _, ok := err.(*exec.ExitError); ok || strings.TrimSpace(stdout) == "" {
+ if _, stderr, gerr := process.GetManager().Exec("git.Init(set "+configKey+")", "git", "config", "--global", configKey, defaultValue); gerr != nil {
+ panic(fmt.Sprintf("Failed to set git %s(%s): %s", configKey, gerr, stderr))
+ }
+ } else {
+ panic(fmt.Sprintf("Failed to get git %s(%s): %s", configKey, err, stderr))
+ }
+ }
+ }
+
+ // Set git some configurations.
+ if _, stderr, err := process.GetManager().Exec("git.Init(git config --global core.quotepath false)",
+ GitExecutable, "config", "--global", "core.quotepath", "false"); err != nil {
+ panic(fmt.Sprintf("Failed to execute 'git config --global core.quotepath false': %s", stderr))
+ }
}
// Fsck verifies the connectivity and validity of the objects in the database