Skip to content

Commit ef1c9c6

Browse files
Support reflogs
Signed-off-by: Philip Peterson <philip.c.peterson@gmail.com>
1 parent 6886706 commit ef1c9c6

File tree

10 files changed

+167
-3
lines changed

10 files changed

+167
-3
lines changed

custom/conf/app.example.ini

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,18 @@ ROUTER = console
21452145
;NOTICE_ON_SUCCESS = false
21462146
;SCHEDULE = @every 72h
21472147

2148+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2149+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2150+
;; Resynchronize Git configurations of all repositories.
2151+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2152+
;[cron.resync_all_configs]
2153+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2154+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2155+
;ENABLED = false
2156+
;RUN_AT_START = false
2157+
;NOTICE_ON_SUCCESS = false
2158+
;SCHEDULE = @every 72h
2159+
21482160
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21492161
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21502162
;; Reinitialize all missing Git repositories for which records exist
@@ -2256,6 +2268,17 @@ ROUTER = console
22562268
;PULL = 300
22572269
;GC = 60
22582270

2271+
2272+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2273+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2274+
;; Git Reflog timeout in days
2275+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2276+
;[git.reflog]
2277+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2278+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2279+
;ENABLED = true
2280+
;EXPIRATION = 90
2281+
22592282
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22602283
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22612284
;[mirror]

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,13 @@ Default templates for project boards:
10111011
- `NOTICE_ON_SUCCESS`: **false**: Set to true to switch on success notices.
10121012
- `SCHEDULE`: **@every 72h**: Cron syntax for scheduling repository archive cleanup, e.g. `@every 1h`.
10131013

1014+
#### Cron - Resynchronize Git configs of all repositories ('cron.resync_all_configs')
1015+
1016+
- `ENABLED`: **false**: Enable service.
1017+
- `RUN_AT_START`: **false**: Run tasks at start up time (if ENABLED).
1018+
- `NOTICE_ON_SUCCESS`: **false**: Set to true to switch on success notices.
1019+
- `SCHEDULE`: **@every 72h**: Cron syntax for scheduling repository archive cleanup, e.g. `@every 1h`.
1020+
10141021
#### Cron - Reinitialize all missing Git repositories for which records exist ('cron.reinit_missing_repos')
10151022

10161023
- `ENABLED`: **false**: Enable service.

modules/git/repo.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919

2020
"code.gitea.io/gitea/modules/proxy"
21+
"code.gitea.io/gitea/modules/setting"
2122
"code.gitea.io/gitea/modules/util"
2223
)
2324

@@ -302,6 +303,47 @@ func GetDivergingCommits(ctx context.Context, repoPath, baseBranch, targetBranch
302303
return DivergeObject{ahead, behind}, nil
303304
}
304305

306+
// CreateConfig creates a git config for the repo
307+
func CreateConfig(repoPath string) error {
308+
return createConfig(repoPath)
309+
}
310+
311+
// createConfig creates a git config for the repo
312+
func createConfig(repoPath string) (err error) {
313+
gitConfigPath := filepath.Join(repoPath, "config")
314+
315+
reflogEnabled := setting.Git.Reflog.Enabled
316+
reflogExpirationDays := setting.Git.Reflog.Expiration
317+
318+
configStr := strings.TrimSpace(`
319+
[core]
320+
repositoryformatversion = 0
321+
filemode = true
322+
bare = true
323+
ignorecase = true
324+
precomposeunicode = true
325+
`)
326+
327+
if reflogEnabled {
328+
configStr += "\n logAllRefUpdates = true"
329+
if reflogExpirationDays != 90 {
330+
configStr += "\n[gc]"
331+
configStr += fmt.Sprintf("\n reflogExpire = %d", reflogExpirationDays)
332+
}
333+
}
334+
335+
configStr += "\n"
336+
337+
if err = util.Remove(gitConfigPath); err != nil && !os.IsNotExist(err) {
338+
return fmt.Errorf("unable to pre-remove config file '%s' prior to rewriting: %w ", gitConfigPath, err)
339+
}
340+
if err = os.WriteFile(gitConfigPath, []byte(configStr), 0o644); err != nil {
341+
return fmt.Errorf("write config file '%s': %w", gitConfigPath, err)
342+
}
343+
344+
return nil
345+
}
346+
305347
// CreateBundle create bundle content to the target path
306348
func (repo *Repository) CreateBundle(ctx context.Context, commit string, out io.Writer) error {
307349
tmp, err := os.MkdirTemp(os.TempDir(), "gitea-bundle")

modules/repository/init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ func checkInitRepository(ctx context.Context, owner, name string) (err error) {
277277
// Init git bare new repository.
278278
if err = git.InitRepository(ctx, repoPath, true); err != nil {
279279
return fmt.Errorf("git.InitRepository: %w", err)
280+
} else if err = git.CreateConfig(repoPath); err != nil {
281+
return fmt.Errorf("git.CreateConfig: %v", err)
280282
} else if err = createDelegateHooks(repoPath); err != nil {
281283
return fmt.Errorf("createDelegateHooks: %w", err)
282284
}

modules/setting/git.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ import (
1212

1313
// Git settings
1414
var Git = struct {
15-
Path string
16-
HomePath string
17-
DisableDiffHighlight bool
15+
Path string
16+
HomePath string
17+
DisableDiffHighlight bool
18+
Reflog struct {
19+
Enabled bool
20+
Expiration int
21+
} `ini:"git.reflog"`
1822
MaxGitDiffLines int
1923
MaxGitDiffLineCharacters int
2024
MaxGitDiffFiles int
@@ -37,6 +41,13 @@ var Git = struct {
3741
GC int `ini:"GC"`
3842
} `ini:"git.timeout"`
3943
}{
44+
Reflog: struct {
45+
Enabled bool
46+
Expiration int
47+
}{
48+
Enabled: false,
49+
Expiration: 90,
50+
},
4051
DisableDiffHighlight: false,
4152
MaxGitDiffLines: 1000,
4253
MaxGitDiffLineCharacters: 5000,

options/locale/locale_en-US.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,6 +2543,7 @@ dashboard.resync_all_sshkeys.desc = (Not needed for the built-in SSH server.)
25432543
dashboard.resync_all_sshprincipals = Update the '.ssh/authorized_principals' file with Gitea SSH principals.
25442544
dashboard.resync_all_sshprincipals.desc = (Not needed for the built-in SSH server.)
25452545
dashboard.resync_all_hooks = Resynchronize pre-receive, update and post-receive hooks of all repositories.
2546+
dashboard.resync_all_configs = Resynchronize Git configuration of all repositories.
25462547
dashboard.reinit_missing_repos = Reinitialize all missing Git repositories for which records exist
25472548
dashboard.sync_external_users = Synchronize external user data
25482549
dashboard.cleanup_hook_task_table = Cleanup hook_task table
@@ -2928,6 +2929,8 @@ config.git_disable_diff_highlight = Disable Diff Syntax Highlight
29282929
config.git_max_diff_lines = Max Diff Lines (for a single file)
29292930
config.git_max_diff_line_characters = Max Diff Characters (for a single line)
29302931
config.git_max_diff_files = Max Diff Files (to be shown)
2932+
config.git_enable_reflogs = Enable Reflogs
2933+
config.git_reflog_expiry_time = Expiry Time
29312934
config.git_gc_args = GC Arguments
29322935
config.git_migrate_timeout = Migration Timeout
29332936
config.git_mirror_timeout = Mirror Update Timeout

services/cron/tasks_extended.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ func registerRepositoryUpdateHook() {
9595
})
9696
}
9797

98+
func registerRepositoryUpdateConfig() {
99+
RegisterTaskFatal("resync_all_configs", &BaseConfig{
100+
Enabled: false,
101+
RunAtStart: false,
102+
Schedule: "@every 72h",
103+
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
104+
return repo_service.SyncRepositoryConfig(ctx)
105+
})
106+
}
107+
98108
func registerReinitMissingRepositories() {
99109
RegisterTaskFatal("reinit_missing_repos", &BaseConfig{
100110
Enabled: false,
@@ -220,6 +230,7 @@ func initExtendedTasks() {
220230
registerRewriteAllPublicKeys()
221231
registerRewriteAllPrincipalKeys()
222232
registerRepositoryUpdateHook()
233+
registerRepositoryUpdateConfig()
223234
registerReinitMissingRepositories()
224235
registerDeleteMissingRepositories()
225236
registerRemoveRandomAvatars()

services/repository/config.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package repository
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"code.gitea.io/gitea/models/db"
11+
repo_model "code.gitea.io/gitea/models/repo"
12+
"code.gitea.io/gitea/modules/git"
13+
"code.gitea.io/gitea/modules/log"
14+
15+
"xorm.io/builder"
16+
)
17+
18+
// SyncRepositoryConfig rewrites all repositories' pre-receive, update and post-receive hooks
19+
// to make sure the binary and custom conf path are up-to-date.
20+
func SyncRepositoryConfig(ctx context.Context) error {
21+
log.Trace("Doing: SyncRepositoryConfig")
22+
23+
if err := db.Iterate(
24+
ctx,
25+
builder.Gt{"id": 0},
26+
func(ctx context.Context, repo *repo_model.Repository) error {
27+
select {
28+
case <-ctx.Done():
29+
return db.ErrCancelledf("before sync repository config for %s", repo.FullName())
30+
default:
31+
}
32+
33+
if err := git.CreateConfig(repo.RepoPath()); err != nil {
34+
return fmt.Errorf("SyncRepositoryConfig: %w", err)
35+
}
36+
if repo.HasWiki() {
37+
if err := git.CreateConfig(repo.WikiPath()); err != nil {
38+
return fmt.Errorf("SyncRepositoryConfig: %w", err)
39+
}
40+
}
41+
return nil
42+
},
43+
); err != nil {
44+
return err
45+
}
46+
47+
log.Trace("Finished: SyncRepositoryConfig")
48+
return nil
49+
}

templates/admin/config.tmpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,19 @@
331331
<dd>{{.Git.MaxGitDiffFiles}}</dd>
332332
<dt>{{.locale.Tr "admin.config.git_gc_args"}}</dt>
333333
<dd><code>{{.Git.GCArgs}}</code></dd>
334+
334335
<div class="ui divider"></div>
336+
337+
<dt>{{.locale.Tr "admin.config.git_enable_reflogs"}}</dt>
338+
<dd>{{if .Git.Reflog.Enabled}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</dd>
339+
340+
{{if .Git.Reflog.Enabled }}
341+
<dt>{{.locale.Tr "admin.config.git_reflog_expiry_time"}}</dt>
342+
<dd>{{.locale.Tr "tool.days" .Git.Reflog.Expiration}}</dd>
343+
{{end}}
344+
345+
<div class="ui divider"></div>
346+
335347
<dt>{{.locale.Tr "admin.config.git_migrate_timeout"}}</dt>
336348
<dd>{{.Git.Timeout.Migrate}} {{.locale.Tr "tool.raw_seconds"}}</dd>
337349
<dt>{{.locale.Tr "admin.config.git_mirror_timeout"}}</dt>

templates/admin/dashboard.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
<td>{{.locale.Tr "admin.dashboard.resync_all_hooks"}}</td>
5757
<td><button type="submit" class="ui green button" name="op" value="resync_all_hooks">{{svg "octicon-play"}} {{.locale.Tr "admin.dashboard.operation_run"}}</button></td>
5858
</tr>
59+
<tr>
60+
<td>{{.locale.Tr "admin.dashboard.resync_all_configs"}}</td>
61+
<td><button type="submit" class="ui green button" name="op" value="resync_all_configs">{{svg "octicon-play"}} {{.locale.Tr "admin.dashboard.operation_run"}}</button></td>
62+
</tr>
5963
<tr>
6064
<td>{{.locale.Tr "admin.dashboard.reinit_missing_repos"}}</td>
6165
<td><button type="submit" class="ui green button" name="op" value="reinit_missing_repos">{{svg "octicon-play"}} {{.locale.Tr "admin.dashboard.operation_run"}}</button></td>

0 commit comments

Comments
 (0)