enhance: allow builtin default git config options to be overridden (#38172)

This is really a follow-up to
[#38148](https://github.com/go-gitea/gitea/pull/35305) , instead of
having specific mappings of options for git configurations, just honor
any user-provided gitconfig. I include a test which points out the
specific config I have which was previously not honored, but more
generally this means that gitea now only *adds* new gitconfig and never
overwrites any config provided under `[git.config]`.

---------

Signed-off-by: Royce Remer <royceremer@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Royce Remer
2026-06-22 11:29:06 -07:00
committed by GitHub
parent 08a18d36a6
commit 736ab982c8
3 changed files with 27 additions and 15 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -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
}