From 757b4c17e900f1d11a81bc9467d90e6c245ee8f2 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Mon, 13 Mar 2023 00:51:07 -0700 Subject: [PATCH] Support reflogs (#22451) This PR adds support for reflogs on all repositories. It does this by adding a global configuration entry. Implements #14865 --------- Signed-off-by: Philip Peterson Co-authored-by: Lunny Xiao --- custom/conf/app.example.ini | 11 ++++++++ .../doc/advanced/config-cheat-sheet.en-us.md | 5 ++++ modules/git/git.go | 27 +++++++++++++++++++ modules/setting/git.go | 17 +++++++++--- options/locale/locale_en-US.ini | 2 ++ templates/admin/config.tmpl | 12 +++++++++ 6 files changed, 71 insertions(+), 3 deletions(-) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index c3c20a216c..b2b5af0af8 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -2272,6 +2272,17 @@ ROUTER = console ;PULL = 300 ;GC = 60 + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Git Reflog timeout in days +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;[git.reflog] +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;ENABLED = true +;EXPIRATION = 90 + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;[mirror] diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index a5ef977f15..b67d6cdf5f 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -1093,6 +1093,11 @@ 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`) - `DEFAULT`: **360**: Git operations default timeout seconds. diff --git a/modules/git/git.go b/modules/git/git.go index 24cfea8c7f..a31afc077a 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -201,6 +201,23 @@ func InitFull(ctx context.Context) (err error) { return syncGitConfig() } +func enableReflogs() error { + if err := configSet("core.logAllRefUpdates", "true"); err != nil { + return err + } + err := configSet("gc.reflogExpire", fmt.Sprintf("%d", setting.Git.Reflog.Expiration)) + return err +} + +func disableReflogs() error { + if err := configUnsetAll("core.logAllRefUpdates", "true"); err != nil { + return err + } else if err := configUnsetAll("gc.reflogExpire", ""); err != nil { + return err + } + return nil +} + // syncGitConfig only modifies gitconfig, won't change global variables (otherwise there will be data-race problem) func syncGitConfig() (err error) { if err = os.MkdirAll(HomeDir(), os.ModePerm); err != nil { @@ -224,6 +241,16 @@ func syncGitConfig() (err error) { return err } + if setting.Git.Reflog.Enabled { + if err := enableReflogs(); err != nil { + return err + } + } else { + if err := disableReflogs(); err != nil { + return err + } + } + if CheckGitVersionAtLeast("2.10") == nil { if err := configSet("receive.advertisePushOptions", "true"); err != nil { return err diff --git a/modules/setting/git.go b/modules/setting/git.go index 457b35936e..b8e7bb9cf8 100644 --- a/modules/setting/git.go +++ b/modules/setting/git.go @@ -12,9 +12,13 @@ import ( // Git settings var Git = struct { - Path string - HomePath string - DisableDiffHighlight bool + Path string + HomePath string + DisableDiffHighlight bool + Reflog struct { + Enabled bool + Expiration int + } `ini:"git.reflog"` MaxGitDiffLines int MaxGitDiffLineCharacters int MaxGitDiffFiles int @@ -37,6 +41,13 @@ var Git = struct { GC int `ini:"GC"` } `ini:"git.timeout"` }{ + Reflog: struct { + Enabled bool + Expiration int + }{ + Enabled: true, + Expiration: 90, + }, DisableDiffHighlight: false, MaxGitDiffLines: 1000, MaxGitDiffLineCharacters: 5000, diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index dccf184335..677af1397d 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2934,6 +2934,8 @@ config.git_disable_diff_highlight = Disable Diff Syntax Highlight config.git_max_diff_lines = Max Diff Lines (for a single file) config.git_max_diff_line_characters = Max Diff Characters (for a single line) config.git_max_diff_files = Max Diff Files (to be shown) +config.git_enable_reflogs = Enable Reflogs +config.git_reflog_expiry_time = Expiry Time config.git_gc_args = GC Arguments config.git_migrate_timeout = Migration Timeout config.git_mirror_timeout = Mirror Update Timeout diff --git a/templates/admin/config.tmpl b/templates/admin/config.tmpl index 8f572c8396..cc0580cc19 100644 --- a/templates/admin/config.tmpl +++ b/templates/admin/config.tmpl @@ -331,7 +331,19 @@
{{.Git.MaxGitDiffFiles}}
{{.locale.Tr "admin.config.git_gc_args"}}
{{.Git.GCArgs}}
+
+ +
{{.locale.Tr "admin.config.git_enable_reflogs"}}
+
{{if .Git.Reflog.Enabled}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+ + {{if .Git.Reflog.Enabled}} +
{{.locale.Tr "admin.config.git_reflog_expiry_time"}}
+
{{.locale.Tr "tool.days" .Git.Reflog.Expiration}}
+ {{end}} + +
+
{{.locale.Tr "admin.config.git_migrate_timeout"}}
{{.Git.Timeout.Migrate}} {{.locale.Tr "tool.raw_seconds"}}
{{.locale.Tr "admin.config.git_mirror_timeout"}}