Skip to content

Commit 4f2f470

Browse files
committed
Add repository setting to enable/disable health checks
New Feature: * Repository struct field for IsFsckEnabled (default true of course) * Admin Settings section on repo options page, accessible only by admin users Possible Enhancements: * There's no way to force running health checks on all repos regardless of their IsFsckEnabled setting. This would be useful if there were an admin API or dashboard button to run fsck immediately. Issue: #1712 Signed-off-by: Allen Wild <[email protected]>
1 parent 2cd3622 commit 4f2f470

File tree

7 files changed

+74
-1
lines changed

7 files changed

+74
-1
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ var migrations = []Migration{
168168
NewMigration("remove is_owner, num_teams columns from org_user", removeIsOwnerColumnFromOrgUser),
169169
// v57 -> v58
170170
NewMigration("add closed_unix column for issues", addIssueClosedTime),
171+
// v58 -> v59
172+
NewMigration("add is_fsck_enabled column for repos", addFsckEnabledToRepo),
171173
}
172174

173175
// Migrate database to current version

models/migrations/v58.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/go-xorm/xorm"
11+
)
12+
13+
func addFsckEnabledToRepo(x *xorm.Engine) error {
14+
type Repository struct {
15+
IsFsckEnabled bool `xorm:"NOT NULL DEFAULT true"`
16+
}
17+
18+
if err := x.Sync2(new(Repository)); err != nil {
19+
return fmt.Errorf("Sync2: %v", err)
20+
}
21+
return nil
22+
}

models/repo.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ type Repository struct {
198198
BaseRepo *Repository `xorm:"-"`
199199
Size int64 `xorm:"NOT NULL DEFAULT 0"`
200200
IndexerStatus *RepoIndexerStatus `xorm:"-"`
201+
IsFsckEnabled bool `xorm:"NOT NULL DEFAULT true"`
201202

202203
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
203204
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
@@ -2167,11 +2168,12 @@ func GitFsck() {
21672168
log.Trace("Doing: GitFsck")
21682169

21692170
if err := x.
2170-
Where("id>0").BufferSize(setting.IterateBufferSize).
2171+
Where("id>0 AND is_fsck_enabled=?", true).BufferSize(setting.IterateBufferSize).
21712172
Iterate(new(Repository),
21722173
func(idx int, bean interface{}) error {
21732174
repo := bean.(*Repository)
21742175
repoPath := repo.RepoPath()
2176+
log.Trace("Running health check on repository %s", repoPath)
21752177
if err := git.Fsck(repoPath, setting.Cron.RepoHealthCheck.Timeout, setting.Cron.RepoHealthCheck.Args...); err != nil {
21762178
desc := fmt.Sprintf("Failed to health check repository (%s): %v", repoPath, err)
21772179
log.Warn(desc)
@@ -2183,6 +2185,7 @@ func GitFsck() {
21832185
}); err != nil {
21842186
log.Error(4, "GitFsck: %v", err)
21852187
}
2188+
log.Trace("Finished: GitFsck")
21862189
}
21872190

21882191
// GitGcRepos calls 'git gc' to remove unnecessary files and optimize the local repository

modules/auth/repo_form.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ type RepoSettingForm struct {
113113
PullsAllowSquash bool
114114
EnableTimetracker bool
115115
AllowOnlyContributorsToTrackTime bool
116+
117+
// Admin settings
118+
EnableHealthCheck bool
116119
}
117120

118121
// Validate validates the fields

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,8 @@ settings.pulls.ignore_whitespace = Ignore changes in whitespace when checking co
905905
settings.pulls.allow_merge_commits = Allow merge commits
906906
settings.pulls.allow_rebase_merge = Allow rebase to merge commits
907907
settings.pulls.allow_squash_commits = Allow to squash commits before merging
908+
settings.admin_settings = Admin Settings
909+
settings.admin_enable_health_check = Enable health checks (git fsck) for this repo
908910
settings.danger_zone = Danger Zone
909911
settings.new_owner_has_same_repo = The new owner already has a repository with same name. Please choose another name.
910912
settings.convert = Convert To Regular Repository

routers/repo/setting.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,24 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
229229
ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
230230
ctx.Redirect(ctx.Repo.RepoLink + "/settings")
231231

232+
case "admin":
233+
if !ctx.User.IsAdmin {
234+
ctx.Error(403)
235+
return
236+
}
237+
238+
if repo.IsFsckEnabled != form.EnableHealthCheck {
239+
repo.IsFsckEnabled = form.EnableHealthCheck
240+
if err := models.UpdateRepository(repo, false); err != nil {
241+
ctx.ServerError("UpdateRepository", err)
242+
return
243+
}
244+
log.Trace("Repository admin settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
245+
}
246+
247+
ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
248+
ctx.Redirect(ctx.Repo.RepoLink + "/settings")
249+
232250
case "convert":
233251
if !ctx.Repo.IsOwner() {
234252
ctx.Error(404)

templates/repo/settings/options.tmpl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,29 @@
236236
</form>
237237
</div>
238238

239+
{{if .IsAdmin}}
240+
<h4 class="ui top attached header">
241+
{{.i18n.Tr "repo.settings.admin_settings"}}
242+
</h4>
243+
<div class="ui attached segment">
244+
<form class="ui form" method="post">
245+
{{.CsrfTokenHtml}}
246+
<input type="hidden" name="action" value="admin">
247+
<div class="field">
248+
<div class="ui checkbox">
249+
<input name="enable_health_check" type="checkbox" {{if .Repository.IsFsckEnabled}}checked{{end}}>
250+
<label>{{.i18n.Tr "repo.settings.admin_enable_health_check"}}</label>
251+
</div>
252+
</div>
253+
254+
<div class="ui divider"></div>
255+
<div class="field">
256+
<button class="ui green button">{{$.i18n.Tr "repo.settings.update_settings"}}</button>
257+
</div>
258+
</form>
259+
</div>
260+
{{end}}
261+
239262
{{if .IsRepositoryOwner}}
240263
<h4 class="ui top attached warning header">
241264
{{.i18n.Tr "repo.settings.danger_zone"}}

0 commit comments

Comments
 (0)