From 68f5e40e46e769ad2b1b956b1cd579bf3669e254 Mon Sep 17 00:00:00 2001 From: Giteabot Date: Tue, 14 Apr 2026 05:42:11 +0800 Subject: [PATCH] fix(api): handle missing base branch in PR commits API (#37193) (#37203) Backport #37193 by @Mohit25022005 fix(api): handle missing base branch in PR commits API Closes #36366 Previously, the PR commits API returned a 500 Internal Server Error when the base branch was missing due to an unhandled git "bad revision" error. This change: - Checks for base branch existence before performing git operations - Returns 404 when the base branch does not exist - Prevents git errors from surfacing as 500 responses This improves API robustness and aligns behavior with expected error handling. Tested locally by: - Creating a pull request - Deleting the base branch - Verifying that the API returns 404 instead of 500 Co-authored-by: Mohit Swarnkar Co-authored-by: wxiaoguang Co-authored-by: Lunny Xiao --- routers/api/v1/repo/pull.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index ef86f413b7..ef8cc6cd93 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -22,6 +22,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/git/gitcmd" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" @@ -1429,7 +1430,11 @@ func GetPullRequestCommits(ctx *context.APIContext) { } else { compareInfo, err = git_service.GetCompareInfo(ctx, pr.BaseRepo, pr.BaseRepo, baseGitRepo, git.RefNameFromBranch(pr.BaseBranch), git.RefName(pr.GetGitHeadRefName()), false, false) } - if err != nil { + + if gitcmd.StderrHasPrefix(err, "fatal: bad revision") { + ctx.APIError(http.StatusNotFound, "invalid base branch or revision") + return + } else if err != nil { ctx.APIErrorInternal(err) return }