Skip to content

Commit 80cc87b

Browse files
authored
Fix tag route and empty repo (#33253)
1 parent 10b6047 commit 80cc87b

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

Diff for: routers/web/repo/view_home.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func handleRepoEmptyOrBroken(ctx *context.Context) {
249249
} else if reallyEmpty {
250250
showEmpty = true // the repo is really empty
251251
updateContextRepoEmptyAndStatus(ctx, true, repo_model.RepositoryReady)
252-
} else if ctx.Repo.Commit == nil {
252+
} else if branches, _, _ := ctx.Repo.GitRepo.GetBranches(0, 1); len(branches) == 0 {
253253
showEmpty = true // it is not really empty, but there is no branch
254254
// at the moment, other repo units like "actions" are not able to handle such case,
255255
// so we just mark the repo as empty to prevent from displaying these units.

Diff for: routers/web/web.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1335,8 +1335,7 @@ func registerRoutes(m *web.Router) {
13351335
m.Get(".atom", feedEnabled, repo.TagsListFeedAtom)
13361336
}, ctxDataSet("EnableFeed", setting.Other.EnableFeed),
13371337
repo.MustBeNotEmpty, context.RepoRefByType(context.RepoRefTag, context.RepoRefByTypeOptions{IgnoreNotExistErr: true}))
1338-
m.Post("/tags/delete", repo.DeleteTag, reqSignIn,
1339-
repo.MustBeNotEmpty, context.RepoMustNotBeArchived(), reqRepoCodeWriter, context.RepoRef())
1338+
m.Post("/tags/delete", reqSignIn, repo.MustBeNotEmpty, context.RepoMustNotBeArchived(), reqRepoCodeWriter, repo.DeleteTag)
13401339
}, optSignIn, context.RepoAssignment, reqRepoCodeReader)
13411340
// end "/{username}/{reponame}": repo tags
13421341

Diff for: tests/integration/empty_repo_test.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"testing"
1515

1616
auth_model "code.gitea.io/gitea/models/auth"
17+
"code.gitea.io/gitea/models/db"
1718
repo_model "code.gitea.io/gitea/models/repo"
1819
"code.gitea.io/gitea/models/unittest"
1920
user_model "code.gitea.io/gitea/models/user"
@@ -24,6 +25,7 @@ import (
2425
"code.gitea.io/gitea/tests"
2526

2627
"github.com/stretchr/testify/assert"
28+
"github.com/stretchr/testify/require"
2729
)
2830

2931
func testAPINewFile(t *testing.T, session *TestSession, user, repo, branch, treePath, content string) *httptest.ResponseRecorder {
@@ -60,7 +62,9 @@ func TestEmptyRepoAddFile(t *testing.T) {
6062
session := loginUser(t, "user30")
6163
req := NewRequest(t, "GET", "/user30/empty")
6264
resp := session.MakeRequest(t, req, http.StatusOK)
63-
assert.Contains(t, resp.Body.String(), "empty-repo-guide")
65+
bodyString := resp.Body.String()
66+
assert.Contains(t, bodyString, "empty-repo-guide")
67+
assert.True(t, test.IsNormalPageCompleted(bodyString))
6468

6569
req = NewRequest(t, "GET", "/user30/empty/_new/"+setting.Repository.DefaultBranch)
6670
resp = session.MakeRequest(t, req, http.StatusOK)
@@ -80,6 +84,21 @@ func TestEmptyRepoAddFile(t *testing.T) {
8084
req = NewRequest(t, "GET", redirect)
8185
resp = session.MakeRequest(t, req, http.StatusOK)
8286
assert.Contains(t, resp.Body.String(), "newly-added-test-file")
87+
88+
// the repo is not empty anymore
89+
req = NewRequest(t, "GET", "/user30/empty")
90+
resp = session.MakeRequest(t, req, http.StatusOK)
91+
assert.Contains(t, resp.Body.String(), "test-file.md")
92+
93+
// if the repo is in incorrect state, it should be able to self-heal (recover to correct state)
94+
user30EmptyRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: 30, Name: "empty"})
95+
user30EmptyRepo.IsEmpty = true
96+
user30EmptyRepo.DefaultBranch = "no-such"
97+
_, err := db.GetEngine(db.DefaultContext).ID(user30EmptyRepo.ID).Update(user30EmptyRepo)
98+
require.NoError(t, err)
99+
req = NewRequest(t, "GET", "/user30/empty")
100+
resp = session.MakeRequest(t, req, http.StatusOK)
101+
assert.Contains(t, resp.Body.String(), "test-file.md")
83102
}
84103

85104
func TestEmptyRepoUploadFile(t *testing.T) {

0 commit comments

Comments
 (0)