Skip to content

Commit a4bcfb8

Browse files
wolfogrewxiaoguangGiteaBot
authored
Detect broken git hooks (#29494)
Detect broken git hooks by checking if the commit id of branches in DB is the same with the git repo. It can help #29338 #28277 and maybe more issues. Users could complain about actions, webhooks, and activities not working, but they were not aware that it is caused by broken git hooks unless they could see a warning. <img width="1348" alt="image" src="https://github.com/go-gitea/gitea/assets/9418365/2b92a46d-7f1d-4115-bef4-9f970bd695da"> It should be merged after #29493. Otherwise, users could see a ephemeral warning after committing and opening the repo home page immediately. And it also waits for #29495, since the doc link (the anchor part) will be updated. --------- Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: Giteabot <[email protected]>
1 parent 5bdf805 commit a4bcfb8

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,6 +2643,7 @@ find_file.no_matching = No matching file found
26432643
error.csv.too_large = Can't render this file because it is too large.
26442644
error.csv.unexpected = Can't render this file because it contains an unexpected character in line %d and column %d.
26452645
error.csv.invalid_field_count = Can't render this file because it has a wrong number of fields in line %d.
2646+
error.broken_git_hook = Git hooks of this repository seem to be broken. Please follow the <a target="_blank" rel="noreferrer" href="%s">documentation</a> to fix them, then push some commits to refresh the status.
26462647
26472648
[graphs]
26482649
component_loading = Loading %s...

routers/web/repo/view.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,8 @@ func renderHomeCode(ctx *context.Context) {
998998
return
999999
}
10001000

1001+
checkOutdatedBranch(ctx)
1002+
10011003
checkCitationFile(ctx, entry)
10021004
if ctx.Written() {
10031005
return
@@ -1064,6 +1066,31 @@ func renderHomeCode(ctx *context.Context) {
10641066
ctx.HTML(http.StatusOK, tplRepoHome)
10651067
}
10661068

1069+
func checkOutdatedBranch(ctx *context.Context) {
1070+
if !(ctx.Repo.IsAdmin() || ctx.Repo.IsOwner()) {
1071+
return
1072+
}
1073+
1074+
// get the head commit of the branch since ctx.Repo.CommitID is not always the head commit of `ctx.Repo.BranchName`
1075+
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.BranchName)
1076+
if err != nil {
1077+
log.Error("GetBranchCommitID: %v", err)
1078+
// Don't return an error page, as it can be rechecked the next time the user opens the page.
1079+
return
1080+
}
1081+
1082+
dbBranch, err := git_model.GetBranch(ctx, ctx.Repo.Repository.ID, ctx.Repo.BranchName)
1083+
if err != nil {
1084+
log.Error("GetBranch: %v", err)
1085+
// Don't return an error page, as it can be rechecked the next time the user opens the page.
1086+
return
1087+
}
1088+
1089+
if dbBranch.CommitID != commit.ID.String() {
1090+
ctx.Flash.Warning(ctx.Tr("repo.error.broken_git_hook", "https://docs.gitea.com/help/faq#push-hook--webhook--actions-arent-running"), true)
1091+
}
1092+
}
1093+
10671094
// RenderUserCards render a page show users according the input template
10681095
func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOptions) ([]*user_model.User, error), tpl base.TplName) {
10691096
page := ctx.FormInt("page")

0 commit comments

Comments
 (0)