Skip to content

Do not show 500 error when default branch doesn't exist #34096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2733,6 +2733,7 @@ branch.restore_success = Branch "%s" has been restored.
branch.restore_failed = Failed to restore branch "%s".
branch.protected_deletion_failed = Branch "%s" is protected. It cannot be deleted.
branch.default_deletion_failed = Branch "%s" is the default branch. It cannot be deleted.
branch.default_branch_not_exist = Default branch "%s" does not exist.
branch.restore = Restore Branch "%s"
branch.download = Download Branch "%s"
branch.rename = Rename Branch "%s"
Expand Down
7 changes: 6 additions & 1 deletion routers/web/repo/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package actions
import (
"bytes"
stdCtx "context"
"errors"
"net/http"
"slices"
"strings"
Expand Down Expand Up @@ -67,7 +68,11 @@ func List(ctx *context.Context) {
ctx.Data["PageIsActions"] = true

commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
if err != nil {
if errors.Is(err, util.ErrNotExist) {
ctx.Data["NotFoundPrompt"] = ctx.Tr("repo.branch.default_branch_not_exist", ctx.Repo.Repository.DefaultBranch)
ctx.NotFound(nil)
return
} else if err != nil {
ctx.ServerError("GetBranchCommit", err)
return
}
Expand Down
7 changes: 7 additions & 0 deletions routers/web/repo/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

activities_model "code.gitea.io/gitea/models/activities"
"code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/services/context"
Expand Down Expand Up @@ -52,6 +53,12 @@ func Activity(ctx *context.Context) {
ctx.Data["DateUntil"] = timeUntil
ctx.Data["PeriodText"] = ctx.Tr("repo.activity.period." + ctx.Data["Period"].(string))

branchExist, _ := git.IsBranchExist(ctx, ctx.Repo.Repository.ID, ctx.Repo.Repository.DefaultBranch)
if !branchExist {
ctx.Data["NotFoundPrompt"] = ctx.Tr("repo.branch.default_branch_not_exist", ctx.Repo.Repository.DefaultBranch)
ctx.NotFound(nil)
return
}
var err error
if ctx.Data["Activity"], err = activities_model.GetActivityStats(ctx, ctx.Repo.Repository, timeFrom,
ctx.Repo.CanRead(unit.TypeReleases),
Expand Down
2 changes: 1 addition & 1 deletion services/context/context_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (ctx *Context) notFoundInternal(logMsg string, logErr error) {

ctx.Data["IsRepo"] = ctx.Repo.Repository != nil
ctx.Data["Title"] = "Page Not Found"
ctx.HTML(http.StatusNotFound, templates.TplName("status/404"))
ctx.HTML(http.StatusNotFound, "status/404")
}

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