Skip to content

Commit 2399b4d

Browse files
GiteaBotwxiaoguang
andauthored
Avoid unnecessary 500 panic when a commit doesn't exist (#28719) (#28721)
Backport #28719 by wxiaoguang In #26851, it assumed that `Commit` always exists when `PageIsDiff==true`. But for a 404 page, the `Commit` doesn't exist, so the following code would cause panic because nil value can't be passed as string parameter to `IsMultilineCommitMessage(string)` (or the StringUtils.Cut in later PRs) Co-authored-by: wxiaoguang <[email protected]>
1 parent ad2cb98 commit 2399b4d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

templates/base/head_opengraph.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
{{else if or .PageIsDiff .IsViewFile}}
1818
<meta property="og:title" content="{{.Title}}">
1919
<meta property="og:url" content="{{AppUrl}}{{.Link}}">
20-
{{if .PageIsDiff}}
20+
{{if and .PageIsDiff .Commit}}
2121
{{- $commitMessageParts := StringUtils.Cut .Commit.Message "\n" -}}
2222
{{- $commitMessageBody := index $commitMessageParts 1 -}}
2323
{{- if $commitMessageBody -}}

tests/integration/repo_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"code.gitea.io/gitea/modules/setting"
15+
"code.gitea.io/gitea/modules/test"
1516
"code.gitea.io/gitea/tests"
1617

1718
"github.com/PuerkitoBio/goquery"
@@ -448,3 +449,12 @@ func TestGeneratedSourceLink(t *testing.T) {
448449
assert.Equal(t, "/user27/repo49/src/commit/aacbdfe9e1c4b47f60abe81849045fa4e96f1d75/test/test.txt", dataURL)
449450
})
450451
}
452+
453+
func TestViewCommit(t *testing.T) {
454+
defer tests.PrepareTestEnv(t)()
455+
456+
req := NewRequest(t, "GET", "/user2/repo1/commit/0123456789012345678901234567890123456789")
457+
req.Header.Add("Accept", "text/html")
458+
resp := MakeRequest(t, req, http.StatusNotFound)
459+
assert.True(t, test.IsNormalPageCompleted(resp.Body.String()), "non-existing commit should render 404 page")
460+
}

0 commit comments

Comments
 (0)