diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index fffd6eeb72..548c39d4b6 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -762,7 +762,12 @@ LEVEL = Info ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Git config options -;; This section only does "set" config, a removed config key from this section won't be removed from git config automatically. The format is `some.configKey = value`. +;; The format is `some.configKey = value`. +;; These options will be written into the gitconfig file under "[git] HOME_PATH" when Gitea web starts. +;; ATTENTION: +;; * It only does "set" config, a removed config key from this section won't be removed from git config automatically. +;; * Some config options might affect the behavior of git and fail Gitea's git operation, +;; make sure you know what you are doing before making changes. ;[git.config] ;diff.algorithm = histogram ;core.logAllRefUpdates = true diff --git a/modules/git/config.go b/modules/git/config.go index e6a2ac8817..b7f41251f8 100644 --- a/modules/git/config.go +++ b/modules/git/config.go @@ -21,14 +21,6 @@ func syncGitConfig(ctx context.Context) (err error) { return fmt.Errorf("unable to prepare git home directory %s, err: %w", gitcmd.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(ctx, 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. @@ -111,8 +103,18 @@ func syncGitConfig(ctx context.Context) (err error) { } err = configUnsetAll(ctx, "uploadpack.allowAnySHA1InWant", "true") } + if err != nil { + return err + } - return err + // Apply user's git config options last so they take precedence over builtin defaults + for k, v := range setting.GitConfig.Options { + if err = configSet(ctx, strings.ToLower(k), v); err != nil { + return err + } + } + + return nil } func configSet(ctx context.Context, key, value string) error { diff --git a/modules/git/config_test.go b/modules/git/config_test.go index 9e358d4a80..cdfca656e1 100644 --- a/modules/git/config_test.go +++ b/modules/git/config_test.go @@ -10,6 +10,7 @@ import ( "gitea.dev/modules/git/gitcmd" "gitea.dev/modules/setting" + "gitea.dev/modules/test" "github.com/stretchr/testify/assert" ) @@ -55,14 +56,18 @@ func TestGitConfig(t *testing.T) { assert.False(t, gitConfigContains("key-x = *")) } -func TestSyncConfig(t *testing.T) { - oldGitConfig := setting.GitConfig - defer func() { - setting.GitConfig = oldGitConfig - }() +func TestSyncGitConfig(t *testing.T) { + defer test.MockVariableValue(&setting.GitConfig)() + + assert.Empty(t, setting.GitConfig.Options) + assert.NoError(t, syncGitConfig(t.Context())) + assert.True(t, gitConfigContains("commitGraph = true")) // builtin default config setting.GitConfig.Options["sync-test.cfg-key-a"] = "CfgValA" + setting.GitConfig.Options["core.commitgraph"] = "false" assert.NoError(t, syncGitConfig(t.Context())) assert.True(t, gitConfigContains("[sync-test]")) assert.True(t, gitConfigContains("cfg-key-a = CfgValA")) + assert.False(t, gitConfigContains("commitGraph")) // builtin default config can be overridden + assert.True(t, gitConfigContains("commitgraph = false")) // git config key is case-insensitive }