Skip to content

Commit 8f75f61

Browse files
authored
Do not show 500 error when default branch doesn't exist (go-gitea#34096) (go-gitea#34097)
Backport go-gitea#34096
1 parent 25e409e commit 8f75f61

File tree

6 files changed

+49
-4
lines changed

6 files changed

+49
-4
lines changed

models/git/branch.go

+12
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ func GetBranch(ctx context.Context, repoID int64, branchName string) (*Branch, e
173173
return &branch, nil
174174
}
175175

176+
// IsBranchExist returns true if the branch exists in the repository.
177+
func IsBranchExist(ctx context.Context, repoID int64, branchName string) (bool, error) {
178+
var branch Branch
179+
has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).And("name=?", branchName).Get(&branch)
180+
if err != nil {
181+
return false, err
182+
} else if !has {
183+
return false, nil
184+
}
185+
return !branch.IsDeleted, nil
186+
}
187+
176188
func GetBranches(ctx context.Context, repoID int64, branchNames []string, includeDeleted bool) ([]*Branch, error) {
177189
branches := make([]*Branch, 0, len(branchNames))
178190

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -2699,6 +2699,7 @@ branch.restore_success = Branch "%s" has been restored.
26992699
branch.restore_failed = Failed to restore branch "%s".
27002700
branch.protected_deletion_failed = Branch "%s" is protected. It cannot be deleted.
27012701
branch.default_deletion_failed = Branch "%s" is the default branch. It cannot be deleted.
2702+
branch.default_branch_not_exist = Default branch "%s" does not exist.
27022703
branch.restore = Restore Branch "%s"
27032704
branch.download = Download Branch "%s"
27042705
branch.rename = Rename Branch "%s"

routers/web/repo/actions/actions.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package actions
66
import (
77
"bytes"
88
stdCtx "context"
9+
"errors"
910
"fmt"
1011
"net/http"
1112
"slices"
@@ -77,7 +78,11 @@ func List(ctx *context.Context) {
7778
return
7879
} else if !empty {
7980
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
80-
if err != nil {
81+
if errors.Is(err, util.ErrNotExist) {
82+
ctx.Data["NotFoundPrompt"] = ctx.Tr("repo.branch.default_branch_not_exist", ctx.Repo.Repository.DefaultBranch)
83+
ctx.NotFound("GetBranchCommit", err)
84+
return
85+
} else if err != nil {
8186
ctx.ServerError("GetBranchCommit", err)
8287
return
8388
}

routers/web/repo/activity.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
activities_model "code.gitea.io/gitea/models/activities"
11+
"code.gitea.io/gitea/models/git"
1112
"code.gitea.io/gitea/models/unit"
1213
"code.gitea.io/gitea/modules/base"
1314
"code.gitea.io/gitea/services/context"
@@ -52,12 +53,26 @@ func Activity(ctx *context.Context) {
5253
ctx.Data["DateUntil"] = timeUntil
5354
ctx.Data["PeriodText"] = ctx.Tr("repo.activity.period." + ctx.Data["Period"].(string))
5455

56+
canReadCode := ctx.Repo.CanRead(unit.TypeCode)
57+
if canReadCode {
58+
// GetActivityStats needs to read the default branch to get some information
59+
branchExist, _ := git.IsBranchExist(ctx, ctx.Repo.Repository.ID, ctx.Repo.Repository.DefaultBranch)
60+
if !branchExist {
61+
ctx.Data["NotFoundPrompt"] = ctx.Tr("repo.branch.default_branch_not_exist", ctx.Repo.Repository.DefaultBranch)
62+
ctx.NotFound("", nil)
63+
return
64+
}
65+
}
66+
5567
var err error
56-
if ctx.Data["Activity"], err = activities_model.GetActivityStats(ctx, ctx.Repo.Repository, timeFrom,
68+
// TODO: refactor these arguments to a struct
69+
ctx.Data["Activity"], err = activities_model.GetActivityStats(ctx, ctx.Repo.Repository, timeFrom,
5770
ctx.Repo.CanRead(unit.TypeReleases),
5871
ctx.Repo.CanRead(unit.TypeIssues),
5972
ctx.Repo.CanRead(unit.TypePullRequests),
60-
ctx.Repo.CanRead(unit.TypeCode)); err != nil {
73+
canReadCode,
74+
)
75+
if err != nil {
6176
ctx.ServerError("GetActivityStats", err)
6277
return
6378
}

services/context/context_response.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (ctx *Context) notFoundInternal(logMsg string, logErr error) {
151151

152152
ctx.Data["IsRepo"] = ctx.Repo.Repository != nil
153153
ctx.Data["Title"] = "Page Not Found"
154-
ctx.HTML(http.StatusNotFound, base.TplName("status/404"))
154+
ctx.HTML(http.StatusNotFound, "status/404")
155155
}
156156

157157
// ServerError displays a 500 (Internal Server Error) page and prints the given error, if any.

tests/integration/repo_activity_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
package integration
55

66
import (
7+
"context"
78
"net/http"
89
"net/url"
910
"strings"
1011
"testing"
1112

13+
"code.gitea.io/gitea/models/db"
1214
repo_model "code.gitea.io/gitea/models/repo"
15+
"code.gitea.io/gitea/models/unittest"
1316
"code.gitea.io/gitea/modules/test"
1417

1518
"github.com/stretchr/testify/assert"
@@ -61,5 +64,14 @@ func TestRepoActivity(t *testing.T) {
6164
// Should be 3 new issues
6265
list = htmlDoc.doc.Find("#new-issues").Next().Find("p.desc")
6366
assert.Len(t, list.Nodes, 3)
67+
68+
// Non-existing default branch
69+
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: "repo1"})
70+
repo1.DefaultBranch = "no-such-branch"
71+
_, _ = db.GetEngine(context.Background()).Cols("default_branch").Update(repo1)
72+
req = NewRequest(t, "GET", "/user2/repo1/activity")
73+
req.Header.Add("Accept", "text/html")
74+
resp = session.MakeRequest(t, req, http.StatusNotFound)
75+
assert.Contains(t, resp.Body.String(), `Default branch "no-such-branch" does not exist.`)
6476
})
6577
}

0 commit comments

Comments
 (0)