Skip to content

Commit 15c6bb5

Browse files
aswildlunny
authored andcommitted
Add repository setting to enable/disable health checks (#3607)
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 321cc2a commit 15c6bb5

File tree

7 files changed

+73
-2
lines changed

7 files changed

+73
-2
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ var migrations = []Migration{
172172
NewMigration("add label descriptions", addLabelsDescriptions),
173173
// v59 -> v60
174174
NewMigration("add merge whitelist for protected branches", addProtectedBranchMergeWhitelist),
175+
// v60 -> v61
176+
NewMigration("add is_fsck_enabled column for repos", addFsckEnabledToRepo),
175177
}
176178

177179
// Migrate database to current version

models/migrations/v60.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: 3 additions & 2 deletions
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"`
@@ -2173,12 +2174,12 @@ func GitFsck() {
21732174
log.Trace("Doing: GitFsck")
21742175

21752176
if err := x.
2176-
Where("id>0").BufferSize(setting.IterateBufferSize).
2177+
Where("id>0 AND is_fsck_enabled=?", true).BufferSize(setting.IterateBufferSize).
21772178
Iterate(new(Repository),
21782179
func(idx int, bean interface{}) error {
21792180
repo := bean.(*Repository)
21802181
repoPath := repo.RepoPath()
2181-
log.Trace(fmt.Sprintf("Running health check for repository %s", repoPath))
2182+
log.Trace("Running health check on repository %s", repoPath)
21822183
if err := git.Fsck(repoPath, setting.Cron.RepoHealthCheck.Timeout, setting.Cron.RepoHealthCheck.Args...); err != nil {
21832184
desc := fmt.Sprintf("Failed to health check repository (%s): %v", repoPath, err)
21842185
log.Warn(desc)

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
@@ -910,6 +910,8 @@ settings.pulls.ignore_whitespace = Ignore changes in whitespace when checking co
910910
settings.pulls.allow_merge_commits = Allow merge commits
911911
settings.pulls.allow_rebase_merge = Allow rebase to merge commits
912912
settings.pulls.allow_squash_commits = Allow to squash commits before merging
913+
settings.admin_settings = Admin Settings
914+
settings.admin_enable_health_check = Enable health checks (git fsck) for this repo
913915
settings.danger_zone = Danger Zone
914916
settings.new_owner_has_same_repo = The new owner already has a repository with same name. Please choose another name.
915917
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)