diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-05-24 00:30:19 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-23 16:30:19 +0000 |
commit | 8080ace6fcf73a5fbe4a0dd71881228abd0c68b9 (patch) | |
tree | f9d05eb8d638ecf45c9c00e5234e472542f94c99 /modules | |
parent | 910bf3154606107616ed7648004d80d323edf426 (diff) | |
download | gitea-8080ace6fcf73a5fbe4a0dd71881228abd0c68b9.tar.gz gitea-8080ace6fcf73a5fbe4a0dd71881228abd0c68b9.zip |
Support changing git config through `app.ini`, use `diff.algorithm=histogram` by default (#24860)
Close #13454 , Close #23255, Close #14697 (and maybe more related
issues)
Many users have the requirement to customize the git config. This PR
introduces an easy way: put the options in Gitea's app.ini
`[git.config]`, then the config options will be applied to git config.
And it can support more flexible default config values, eg: now
`diff.algorithm=histogram` by default. According to:
https://stackoverflow.com/a/32367597/4754037 , `histogram diff` is
efficient and doesn't like to cause server-side problems.
---------
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Giteabot <teabot@gitea.io>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/git/git.go | 8 | ||||
-rw-r--r-- | modules/git/git_test.go | 24 | ||||
-rw-r--r-- | modules/setting/git.go | 17 | ||||
-rw-r--r-- | modules/setting/git_test.go | 40 |
4 files changed, 83 insertions, 6 deletions
diff --git a/modules/git/git.go b/modules/git/git.go index a31afc077a..2e0a16fb5c 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -224,6 +224,14 @@ func syncGitConfig() (err error) { return fmt.Errorf("unable to prepare git home directory %s, err: %w", HomeDir(), err) } + // first, write user's git config options to git config file + // user config options could be overwritten by builtin values later, because if a value is builtin, it must have some special purposes + for k, v := range setting.GitConfig.Options { + if err = configSet(strings.ToLower(k), v); err != nil { + return err + } + } + // Git requires setting user.name and user.email in order to commit changes - old comment: "if they're not set just add some defaults" // TODO: need to confirm whether users really need to change these values manually. It seems that these values are dummy only and not really used. // If these values are not really used, then they can be set (overwritten) directly without considering about existence. diff --git a/modules/git/git_test.go b/modules/git/git_test.go index 25eb308531..37ab669ea4 100644 --- a/modules/git/git_test.go +++ b/modules/git/git_test.go @@ -42,14 +42,14 @@ func TestMain(m *testing.M) { } } -func TestGitConfig(t *testing.T) { - gitConfigContains := func(sub string) bool { - if b, err := os.ReadFile(HomeDir() + "/.gitconfig"); err == nil { - return strings.Contains(string(b), sub) - } - return false +func gitConfigContains(sub string) bool { + if b, err := os.ReadFile(HomeDir() + "/.gitconfig"); err == nil { + return strings.Contains(string(b), sub) } + return false +} +func TestGitConfig(t *testing.T) { assert.False(t, gitConfigContains("key-a")) assert.NoError(t, configSetNonExist("test.key-a", "val-a")) @@ -81,3 +81,15 @@ func TestGitConfig(t *testing.T) { assert.NoError(t, configUnsetAll("test.key-x", "*")) assert.False(t, gitConfigContains("key-x = *")) } + +func TestSyncConfig(t *testing.T) { + oldGitConfig := setting.GitConfig + defer func() { + setting.GitConfig = oldGitConfig + }() + + setting.GitConfig.Options["sync-test.cfg-key-a"] = "CfgValA" + assert.NoError(t, syncGitConfig()) + assert.True(t, gitConfigContains("[sync-test]")) + assert.True(t, gitConfigContains("cfg-key-a = CfgValA")) +} diff --git a/modules/setting/git.go b/modules/setting/git.go index b8e7bb9cf8..29ec37f866 100644 --- a/modules/setting/git.go +++ b/modules/setting/git.go @@ -5,6 +5,7 @@ package setting import ( "path/filepath" + "strings" "time" "code.gitea.io/gitea/modules/log" @@ -78,12 +79,28 @@ var Git = struct { }, } +var GitConfig = struct { + Options map[string]string +}{ + Options: make(map[string]string), +} + func loadGitFrom(rootCfg ConfigProvider) { sec := rootCfg.Section("git") if err := sec.MapTo(&Git); err != nil { log.Fatal("Failed to map Git settings: %v", err) } + secGitConfig := rootCfg.Section("git.config") + GitConfig.Options = make(map[string]string) + for _, key := range secGitConfig.Keys() { + // git config key is case-insensitive, so always use lower-case + GitConfig.Options[strings.ToLower(key.Name())] = key.String() + } + if _, ok := GitConfig.Options["diff.algorithm"]; !ok { + GitConfig.Options["diff.algorithm"] = "histogram" + } + Git.HomePath = sec.Key("HOME_PATH").MustString("home") if !filepath.IsAbs(Git.HomePath) { Git.HomePath = filepath.Join(AppDataPath, Git.HomePath) diff --git a/modules/setting/git_test.go b/modules/setting/git_test.go new file mode 100644 index 0000000000..1da8c87cc8 --- /dev/null +++ b/modules/setting/git_test.go @@ -0,0 +1,40 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package setting + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGitConfig(t *testing.T) { + oldGit := Git + oldGitConfig := GitConfig + defer func() { + Git = oldGit + GitConfig = oldGitConfig + }() + + cfg, err := NewConfigProviderFromData(` +[git.config] +a.b = 1 +`) + assert.NoError(t, err) + loadGitFrom(cfg) + + assert.Len(t, GitConfig.Options, 2) + assert.EqualValues(t, "1", GitConfig.Options["a.b"]) + assert.EqualValues(t, "histogram", GitConfig.Options["diff.algorithm"]) + + cfg, err = NewConfigProviderFromData(` +[git.config] +diff.algorithm = other +`) + assert.NoError(t, err) + loadGitFrom(cfg) + + assert.Len(t, GitConfig.Options, 1) + assert.EqualValues(t, "other", GitConfig.Options["diff.algorithm"]) +} |