From 0167c0b6093ad40db57ead418441c2798e23bca2 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 22 May 2023 21:59:52 +0800 Subject: [PATCH 1/7] fix --- custom/conf/app.example.ini | 48 +++++++++---------- .../config-cheat-sheet.en-us.md | 19 +++++--- modules/git/git.go | 8 ++++ modules/git/git_test.go | 24 +++++++--- modules/setting/git.go | 17 +++++++ modules/setting/git_test.go | 40 ++++++++++++++++ 6 files changed, 118 insertions(+), 38 deletions(-) create mode 100644 modules/setting/git_test.go diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 121ceb7152c55..d54d38f868c0f 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -682,6 +682,28 @@ LEVEL = Info ;; Disable the usage of using partial clones for git. ;DISABLE_PARTIAL_CLONE = false +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Git Operation timeout in seconds +;[git.timeout] +;DEFAULT = 360 +;MIGRATE = 600 +;MIRROR = 300 +;CLONE = 300 +;PULL = 300 +;GC = 60 + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Git Reflog timeout in days +;[git.reflog] +;ENABLED = true +;EXPIRATION = 90 + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Git config options +;[git.config] +;diff.algorithm = histogram +;other.config-key = ... + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; [service] @@ -2176,32 +2198,6 @@ LEVEL = Info ;Check at least this proportion of LFSMetaObjects per repo. (This may cause all stale LFSMetaObjects to be checked.) ;PROPORTION_TO_CHECK_PER_REPO = 0.6 - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Git Operation timeout in seconds -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;[git.timeout] -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;DEFAULT = 360 -;MIGRATE = 600 -;MIRROR = 300 -;CLONE = 300 -;PULL = 300 -;GC = 60 - - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Git Reflog timeout in days -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;[git.reflog] -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;ENABLED = true -;EXPIRATION = 90 - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;[mirror] diff --git a/docs/content/doc/administration/config-cheat-sheet.en-us.md b/docs/content/doc/administration/config-cheat-sheet.en-us.md index 349c480ae6b91..ea41d8bb9e1f2 100644 --- a/docs/content/doc/administration/config-cheat-sheet.en-us.md +++ b/docs/content/doc/administration/config-cheat-sheet.en-us.md @@ -1054,12 +1054,7 @@ Default templates for project boards: - `DISABLE_CORE_PROTECT_NTFS`: **false** Set to true to forcibly set `core.protectNTFS` to false. - `DISABLE_PARTIAL_CLONE`: **false** Disable the usage of using partial clones for git. -## Git - Reflog settings (`git.reflog`) - -- `ENABLED`: **true** Set to true to enable Git to write changes to reflogs in each repo. -- `EXPIRATION`: **90** Reflog entry lifetime, in days. Entries are removed opportunistically by Git. - -## Git - Timeout settings (`git.timeout`) +### Git - Timeout settings (`git.timeout`) - `DEFAULT`: **360**: Git operations default timeout seconds. - `MIGRATE`: **600**: Migrate external repositories timeout seconds. @@ -1068,6 +1063,18 @@ Default templates for project boards: - `PULL`: **300**: Git pull from internal repositories timeout seconds. - `GC`: **60**: Git repository GC timeout seconds. +### Git - Reflog settings (`git.reflog`) + +- `ENABLED`: **true** Set to true to enable Git to write changes to reflogs in each repo. +- `EXPIRATION`: **90** Reflog entry lifetime, in days. Entries are removed opportunistically by Git. + +### Git - Config options (`git.config`) + +The key/value pairs in this section will be used as git config: + +- `diff.algorithm`: **histogram** +- `other.config-key`: ... + ## Metrics (`metrics`) - `ENABLED`: **false**: Enables /metrics endpoint for prometheus. diff --git a/modules/git/git.go b/modules/git/git.go index a31afc077a5c1..2e0a16fb5cb87 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 25eb3085319d3..37ab669ea45fb 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 b8e7bb9cf81fe..29ec37f866e5e 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 0000000000000..1da8c87cc86b5 --- /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"]) +} From c5cbe42577f12e0b9f1aaff3b5030cd535243669 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 22 May 2023 22:58:59 +0800 Subject: [PATCH 2/7] improve document --- custom/conf/app.example.ini | 1 + docs/content/doc/administration/config-cheat-sheet.en-us.md | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index d54d38f868c0f..450495bf3deb9 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -700,6 +700,7 @@ 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 ;[git.config] ;diff.algorithm = histogram ;other.config-key = ... diff --git a/docs/content/doc/administration/config-cheat-sheet.en-us.md b/docs/content/doc/administration/config-cheat-sheet.en-us.md index ea41d8bb9e1f2..20331d9c7b50d 100644 --- a/docs/content/doc/administration/config-cheat-sheet.en-us.md +++ b/docs/content/doc/administration/config-cheat-sheet.en-us.md @@ -1070,7 +1070,8 @@ Default templates for project boards: ### Git - Config options (`git.config`) -The key/value pairs in this section will be used as git config: +The key/value pairs in this section will be used as git config. +This section only does "set" config, a removed config key from this section won't be removed from git config automatically. - `diff.algorithm`: **histogram** - `other.config-key`: ... From 1d3e111a13713aefbe442b015763d041da5dbffe Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 22 May 2023 23:14:27 +0800 Subject: [PATCH 3/7] Apply suggestions from code review Co-authored-by: silverwind --- custom/conf/app.example.ini | 3 +-- docs/content/doc/administration/config-cheat-sheet.en-us.md | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 450495bf3deb9..c497f1be672bf 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -700,10 +700,9 @@ 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 +;; 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`. ;[git.config] ;diff.algorithm = histogram -;other.config-key = ... ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/docs/content/doc/administration/config-cheat-sheet.en-us.md b/docs/content/doc/administration/config-cheat-sheet.en-us.md index 20331d9c7b50d..035c94cd27711 100644 --- a/docs/content/doc/administration/config-cheat-sheet.en-us.md +++ b/docs/content/doc/administration/config-cheat-sheet.en-us.md @@ -1071,10 +1071,9 @@ Default templates for project boards: ### Git - Config options (`git.config`) The key/value pairs in this section will be used as git config. -This section only does "set" config, a removed config key from this section won't be removed from git config automatically. +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`. - `diff.algorithm`: **histogram** -- `other.config-key`: ... ## Metrics (`metrics`) From 938d64595fd8bf783f35f172c527ed25d992e721 Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 22 May 2023 20:25:41 +0200 Subject: [PATCH 4/7] add docs for signed push --- .../administration/customizing-gitea.en-us.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/content/doc/administration/customizing-gitea.en-us.md b/docs/content/doc/administration/customizing-gitea.en-us.md index 54ce2a715f943..52cc025c8e810 100644 --- a/docs/content/doc/administration/customizing-gitea.en-us.md +++ b/docs/content/doc/administration/customizing-gitea.en-us.md @@ -282,6 +282,22 @@ Place custom files in corresponding sub-folder under `custom/options`. To add custom .gitignore, add a file with existing [.gitignore rules](https://git-scm.com/docs/gitignore) in it to `$GITEA_CUSTOM/options/gitignore` +## Customizing git config + +Starting with Gitea 1.20, you can customize the git configuration via the `git.config` section. + +### Enabling signed git pushes + +To enable signed git pushes, set these two options: + +```` +[git.config] +receive.advertisePushOptions = true +receive.certNonceSeed = +```` + +`certNonceSeed` should be set to a sufficiently random string and be kept secret. + ### Labels Starting with Gitea 1.19, you can add a file that follows the [YAML label format](https://github.com/go-gitea/gitea/blob/main/options/label/Advanced.yaml) to `$GITEA_CUSTOM/options/label`: From 282ac8c4c033a967037783f46ce37b33dfbfd40f Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 22 May 2023 20:28:21 +0200 Subject: [PATCH 5/7] reword header --- docs/content/doc/administration/customizing-gitea.en-us.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/doc/administration/customizing-gitea.en-us.md b/docs/content/doc/administration/customizing-gitea.en-us.md index 52cc025c8e810..86dc73e568307 100644 --- a/docs/content/doc/administration/customizing-gitea.en-us.md +++ b/docs/content/doc/administration/customizing-gitea.en-us.md @@ -282,7 +282,7 @@ Place custom files in corresponding sub-folder under `custom/options`. To add custom .gitignore, add a file with existing [.gitignore rules](https://git-scm.com/docs/gitignore) in it to `$GITEA_CUSTOM/options/gitignore` -## Customizing git config +## Customizing the git configuration Starting with Gitea 1.20, you can customize the git configuration via the `git.config` section. From 693677772eb1f411c236e2c21e60b20ac331be50 Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 22 May 2023 22:42:53 +0200 Subject: [PATCH 6/7] Update docs/content/doc/administration/customizing-gitea.en-us.md Co-authored-by: KN4CK3R --- docs/content/doc/administration/customizing-gitea.en-us.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/content/doc/administration/customizing-gitea.en-us.md b/docs/content/doc/administration/customizing-gitea.en-us.md index 86dc73e568307..44c597fdf3882 100644 --- a/docs/content/doc/administration/customizing-gitea.en-us.md +++ b/docs/content/doc/administration/customizing-gitea.en-us.md @@ -290,11 +290,11 @@ Starting with Gitea 1.20, you can customize the git configuration via the `git.c To enable signed git pushes, set these two options: -```` +```ini [git.config] receive.advertisePushOptions = true receive.certNonceSeed = -```` +``` `certNonceSeed` should be set to a sufficiently random string and be kept secret. From c0479ac6a759331c05319a419af850e23f6fc3ba Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 22 May 2023 22:44:05 +0200 Subject: [PATCH 7/7] Update docs/content/doc/administration/customizing-gitea.en-us.md --- docs/content/doc/administration/customizing-gitea.en-us.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/doc/administration/customizing-gitea.en-us.md b/docs/content/doc/administration/customizing-gitea.en-us.md index 44c597fdf3882..4c8b1c90d7165 100644 --- a/docs/content/doc/administration/customizing-gitea.en-us.md +++ b/docs/content/doc/administration/customizing-gitea.en-us.md @@ -296,7 +296,7 @@ receive.advertisePushOptions = true receive.certNonceSeed = ``` -`certNonceSeed` should be set to a sufficiently random string and be kept secret. +`certNonceSeed` should be set to a random string and be kept secret. ### Labels