Skip to content

Commit 2f149c5

Browse files
authored
Use [git.config] for reflog cleaning up (#24958)
Follow #24860 (comment) Use `[git.config]` for reflog cleaning up, the new options are more flexible. * https://git-scm.com/docs/git-config#Documentation/git-config.txt-corelogAllRefUpdates * https://git-scm.com/docs/git-config#Documentation/git-config.txt-gcreflogExpire ## ⚠️ BREAKING The section `[git.reflog]` is now obsolete and its keys have been moved to the following replacements: - `[git.reflog].ENABLED` → `[git.config].core.logAllRefUpdates` - `[git.reflog].EXPIRATION` → `[git.config].gc.reflogExpire`
1 parent 0d54395 commit 2f149c5

File tree

5 files changed

+62
-61
lines changed

5 files changed

+62
-61
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -693,17 +693,13 @@ LEVEL = Info
693693
;PULL = 300
694694
;GC = 60
695695

696-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
697-
;; Git Reflog timeout in days
698-
;[git.reflog]
699-
;ENABLED = true
700-
;EXPIRATION = 90
701-
702696
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
703697
;; Git config options
704698
;; 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`.
705699
;[git.config]
706700
;diff.algorithm = histogram
701+
;core.logAllRefUpdates = true
702+
;gc.reflogExpire = 90
707703

708704
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
709705
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/doc/administration/config-cheat-sheet.en-us.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,17 +1065,14 @@ Default templates for project boards:
10651065
- `PULL`: **300**: Git pull from internal repositories timeout seconds.
10661066
- `GC`: **60**: Git repository GC timeout seconds.
10671067

1068-
### Git - Reflog settings (`git.reflog`)
1069-
1070-
- `ENABLED`: **true** Set to true to enable Git to write changes to reflogs in each repo.
1071-
- `EXPIRATION`: **90** Reflog entry lifetime, in days. Entries are removed opportunistically by Git.
1072-
10731068
### Git - Config options (`git.config`)
10741069

10751070
The key/value pairs in this section will be used as git config.
10761071
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`.
10771072

10781073
- `diff.algorithm`: **histogram**
1074+
- `core.logAllRefUpdates`: **true**
1075+
- `gc.reflogExpire`: **90**
10791076

10801077
## Metrics (`metrics`)
10811078

modules/git/git.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -201,23 +201,6 @@ func InitFull(ctx context.Context) (err error) {
201201
return syncGitConfig()
202202
}
203203

204-
func enableReflogs() error {
205-
if err := configSet("core.logAllRefUpdates", "true"); err != nil {
206-
return err
207-
}
208-
err := configSet("gc.reflogExpire", fmt.Sprintf("%d", setting.Git.Reflog.Expiration))
209-
return err
210-
}
211-
212-
func disableReflogs() error {
213-
if err := configUnsetAll("core.logAllRefUpdates", "true"); err != nil {
214-
return err
215-
} else if err := configUnsetAll("gc.reflogExpire", ""); err != nil {
216-
return err
217-
}
218-
return nil
219-
}
220-
221204
// syncGitConfig only modifies gitconfig, won't change global variables (otherwise there will be data-race problem)
222205
func syncGitConfig() (err error) {
223206
if err = os.MkdirAll(HomeDir(), os.ModePerm); err != nil {
@@ -249,16 +232,6 @@ func syncGitConfig() (err error) {
249232
return err
250233
}
251234

252-
if setting.Git.Reflog.Enabled {
253-
if err := enableReflogs(); err != nil {
254-
return err
255-
}
256-
} else {
257-
if err := disableReflogs(); err != nil {
258-
return err
259-
}
260-
}
261-
262235
if CheckGitVersionAtLeast("2.10") == nil {
263236
if err := configSet("receive.advertisePushOptions", "true"); err != nil {
264237
return err

modules/setting/git.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ var Git = struct {
1616
Path string
1717
HomePath string
1818
DisableDiffHighlight bool
19-
Reflog struct {
20-
Enabled bool
21-
Expiration int
22-
} `ini:"git.reflog"`
19+
2320
MaxGitDiffLines int
2421
MaxGitDiffLineCharacters int
2522
MaxGitDiffFiles int
@@ -42,13 +39,6 @@ var Git = struct {
4239
GC int `ini:"GC"`
4340
} `ini:"git.timeout"`
4441
}{
45-
Reflog: struct {
46-
Enabled bool
47-
Expiration int
48-
}{
49-
Enabled: true,
50-
Expiration: 90,
51-
},
5242
DisableDiffHighlight: false,
5343
MaxGitDiffLines: 1000,
5444
MaxGitDiffLineCharacters: 5000,
@@ -79,9 +69,19 @@ var Git = struct {
7969
},
8070
}
8171

82-
var GitConfig = struct {
83-
Options map[string]string
84-
}{
72+
type GitConfigType struct {
73+
Options map[string]string // git config key is case-insensitive, always use lower-case
74+
}
75+
76+
func (c *GitConfigType) SetOption(key, val string) {
77+
c.Options[strings.ToLower(key)] = val
78+
}
79+
80+
func (c *GitConfigType) GetOption(key string) string {
81+
return c.Options[strings.ToLower(key)]
82+
}
83+
84+
var GitConfig = GitConfigType{
8585
Options: make(map[string]string),
8686
}
8787

@@ -93,12 +93,22 @@ func loadGitFrom(rootCfg ConfigProvider) {
9393

9494
secGitConfig := rootCfg.Section("git.config")
9595
GitConfig.Options = make(map[string]string)
96-
for _, key := range secGitConfig.Keys() {
97-
// git config key is case-insensitive, so always use lower-case
98-
GitConfig.Options[strings.ToLower(key.Name())] = key.String()
96+
GitConfig.SetOption("diff.algorithm", "histogram")
97+
GitConfig.SetOption("core.logAllRefUpdates", "true")
98+
GitConfig.SetOption("gc.reflogExpire", "90")
99+
100+
secGitReflog := rootCfg.Section("git.reflog")
101+
if secGitReflog.HasKey("ENABLED") {
102+
deprecatedSetting(rootCfg, "git.reflog", "ENABLED", "git.config", "core.logAllRefUpdates", "1.21")
103+
GitConfig.SetOption("core.logAllRefUpdates", secGitReflog.Key("ENABLED").In("true", []string{"true", "false"}))
99104
}
100-
if _, ok := GitConfig.Options["diff.algorithm"]; !ok {
101-
GitConfig.Options["diff.algorithm"] = "histogram"
105+
if secGitReflog.HasKey("EXPIRATION") {
106+
deprecatedSetting(rootCfg, "git.reflog", "EXPIRATION", "git.config", "core.reflogExpire", "1.21")
107+
GitConfig.SetOption("gc.reflogExpire", secGitReflog.Key("EXPIRATION").String())
108+
}
109+
110+
for _, key := range secGitConfig.Keys() {
111+
GitConfig.SetOption(key.Name(), key.String())
102112
}
103113

104114
Git.HomePath = sec.Key("HOME_PATH").MustString("home")

modules/setting/git_test.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ a.b = 1
2323
`)
2424
assert.NoError(t, err)
2525
loadGitFrom(cfg)
26-
27-
assert.Len(t, GitConfig.Options, 2)
2826
assert.EqualValues(t, "1", GitConfig.Options["a.b"])
2927
assert.EqualValues(t, "histogram", GitConfig.Options["diff.algorithm"])
3028

@@ -34,7 +32,34 @@ diff.algorithm = other
3432
`)
3533
assert.NoError(t, err)
3634
loadGitFrom(cfg)
37-
38-
assert.Len(t, GitConfig.Options, 1)
3935
assert.EqualValues(t, "other", GitConfig.Options["diff.algorithm"])
4036
}
37+
38+
func TestGitReflog(t *testing.T) {
39+
oldGit := Git
40+
oldGitConfig := GitConfig
41+
defer func() {
42+
Git = oldGit
43+
GitConfig = oldGitConfig
44+
}()
45+
46+
// default reflog config without legacy options
47+
cfg, err := NewConfigProviderFromData(``)
48+
assert.NoError(t, err)
49+
loadGitFrom(cfg)
50+
51+
assert.EqualValues(t, "true", GitConfig.GetOption("core.logAllRefUpdates"))
52+
assert.EqualValues(t, "90", GitConfig.GetOption("gc.reflogExpire"))
53+
54+
// custom reflog config by legacy options
55+
cfg, err = NewConfigProviderFromData(`
56+
[git.reflog]
57+
ENABLED = false
58+
EXPIRATION = 123
59+
`)
60+
assert.NoError(t, err)
61+
loadGitFrom(cfg)
62+
63+
assert.EqualValues(t, "false", GitConfig.GetOption("core.logAllRefUpdates"))
64+
assert.EqualValues(t, "123", GitConfig.GetOption("gc.reflogExpire"))
65+
}

0 commit comments

Comments
 (0)