Skip to content

Commit 47ee84d

Browse files
GiteaBotwxiaoguang
andauthored
Fix repo broken check (#34444) (#34452)
Backport #34444 by wxiaoguang Fix #34424 Co-authored-by: wxiaoguang <[email protected]>
1 parent 89f1df0 commit 47ee84d

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

routers/web/repo/view_home.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
user_model "code.gitea.io/gitea/models/user"
2222
"code.gitea.io/gitea/modules/git"
2323
giturl "code.gitea.io/gitea/modules/git/url"
24+
"code.gitea.io/gitea/modules/gitrepo"
2425
"code.gitea.io/gitea/modules/httplib"
2526
"code.gitea.io/gitea/modules/log"
2627
repo_module "code.gitea.io/gitea/modules/repository"
@@ -261,6 +262,10 @@ func updateContextRepoEmptyAndStatus(ctx *context.Context, empty bool, status re
261262

262263
func handleRepoEmptyOrBroken(ctx *context.Context) {
263264
showEmpty := true
265+
if ctx.Repo.GitRepo == nil {
266+
// in case the repo really exists and works, but the status was incorrectly marked as "broken", we need to open and check it again
267+
ctx.Repo.GitRepo, _ = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository)
268+
}
264269
if ctx.Repo.GitRepo != nil {
265270
reallyEmpty, err := ctx.Repo.GitRepo.IsEmpty()
266271
if err != nil {
@@ -396,10 +401,8 @@ func Home(ctx *context.Context) {
396401
return
397402
}
398403

399-
prepareHomeTreeSideBarSwitch(ctx)
400-
401404
title := ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
402-
if len(ctx.Repo.Repository.Description) > 0 {
405+
if ctx.Repo.Repository.Description != "" {
403406
title += ": " + ctx.Repo.Repository.Description
404407
}
405408
ctx.Data["Title"] = title
@@ -412,6 +415,8 @@ func Home(ctx *context.Context) {
412415
return
413416
}
414417

418+
prepareHomeTreeSideBarSwitch(ctx)
419+
415420
// get the current git entry which doer user is currently looking at.
416421
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
417422
if err != nil {

services/context/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,8 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
795795
return func(ctx *Context) {
796796
var err error
797797
refType := detectRefType
798-
if ctx.Repo.Repository.IsBeingCreated() {
799-
return // no git repo, so do nothing, users will see a "migrating" UI provided by "migrate/migrating.tmpl"
798+
if ctx.Repo.Repository.IsBeingCreated() || ctx.Repo.Repository.IsBroken() {
799+
return // no git repo, so do nothing, users will see a "migrating" UI provided by "migrate/migrating.tmpl", or empty repo guide
800800
}
801801
// Empty repository does not have reference information.
802802
if ctx.Repo.Repository.IsEmpty {

tests/integration/empty_repo_test.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"code.gitea.io/gitea/modules/setting"
2424
api "code.gitea.io/gitea/modules/structs"
2525
"code.gitea.io/gitea/modules/test"
26+
"code.gitea.io/gitea/modules/util"
2627
"code.gitea.io/gitea/tests"
2728

2829
"github.com/stretchr/testify/assert"
@@ -100,22 +101,29 @@ func TestEmptyRepoAddFile(t *testing.T) {
100101
assert.Contains(t, resp.Body.String(), "test-file.md")
101102

102103
// if the repo is in incorrect state, it should be able to self-heal (recover to correct state)
103-
user30EmptyRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: 30, Name: "empty"})
104-
user30EmptyRepo.IsEmpty = true
105-
user30EmptyRepo.DefaultBranch = "no-such"
106-
_, err := db.GetEngine(db.DefaultContext).ID(user30EmptyRepo.ID).Cols("is_empty", "default_branch").Update(user30EmptyRepo)
107-
require.NoError(t, err)
108-
user30EmptyRepo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: 30, Name: "empty"})
109-
assert.True(t, user30EmptyRepo.IsEmpty)
110-
111-
req = NewRequest(t, "GET", "/user30/empty")
112-
resp = session.MakeRequest(t, req, http.StatusSeeOther)
113-
redirect = test.RedirectURL(resp)
114-
assert.Equal(t, "/user30/empty", redirect)
115-
116-
req = NewRequest(t, "GET", "/user30/empty")
117-
resp = session.MakeRequest(t, req, http.StatusOK)
118-
assert.Contains(t, resp.Body.String(), "test-file.md")
104+
testEmptyOrBrokenRecover := func(t *testing.T, isEmpty, isBroken bool) {
105+
user30EmptyRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: 30, Name: "empty"})
106+
user30EmptyRepo.IsEmpty = isEmpty
107+
user30EmptyRepo.Status = util.Iif(isBroken, repo_model.RepositoryBroken, repo_model.RepositoryReady)
108+
user30EmptyRepo.DefaultBranch = "no-such"
109+
_, err := db.GetEngine(db.DefaultContext).ID(user30EmptyRepo.ID).Cols("is_empty", "status", "default_branch").Update(user30EmptyRepo)
110+
require.NoError(t, err)
111+
user30EmptyRepo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: 30, Name: "empty"})
112+
assert.Equal(t, isEmpty, user30EmptyRepo.IsEmpty)
113+
assert.Equal(t, isBroken, user30EmptyRepo.Status == repo_model.RepositoryBroken)
114+
115+
req = NewRequest(t, "GET", "/user30/empty")
116+
resp = session.MakeRequest(t, req, http.StatusSeeOther)
117+
redirect = test.RedirectURL(resp)
118+
assert.Equal(t, "/user30/empty", redirect)
119+
120+
req = NewRequest(t, "GET", "/user30/empty")
121+
resp = session.MakeRequest(t, req, http.StatusOK)
122+
assert.Contains(t, resp.Body.String(), "test-file.md")
123+
}
124+
testEmptyOrBrokenRecover(t, true, false)
125+
testEmptyOrBrokenRecover(t, false, true)
126+
testEmptyOrBrokenRecover(t, true, true)
119127
}
120128

121129
func TestEmptyRepoUploadFile(t *testing.T) {

0 commit comments

Comments
 (0)