diff --git a/templates/base/head_opengraph.tmpl b/templates/base/head_opengraph.tmpl
index 19d924610cd54..f1f38999d2ef4 100644
--- a/templates/base/head_opengraph.tmpl
+++ b/templates/base/head_opengraph.tmpl
@@ -17,7 +17,7 @@
{{else if or .PageIsDiff .IsViewFile}}
- {{if .PageIsDiff}}
+ {{if and .PageIsDiff .Commit}}
{{- $commitMessageParts := StringUtils.Cut .Commit.Message "\n" -}}
{{- $commitMessageBody := index $commitMessageParts 1 -}}
{{- if $commitMessageBody -}}
diff --git a/tests/integration/repo_test.go b/tests/integration/repo_test.go
index f5417b100cfef..f141b6dcb1e20 100644
--- a/tests/integration/repo_test.go
+++ b/tests/integration/repo_test.go
@@ -12,6 +12,7 @@ import (
"time"
"code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/tests"
"github.com/PuerkitoBio/goquery"
@@ -448,3 +449,12 @@ func TestGeneratedSourceLink(t *testing.T) {
assert.Equal(t, "/user27/repo49/src/commit/aacbdfe9e1c4b47f60abe81849045fa4e96f1d75/test/test.txt", dataURL)
})
}
+
+func TestViewCommit(t *testing.T) {
+ defer tests.PrepareTestEnv(t)()
+
+ req := NewRequest(t, "GET", "/user2/repo1/commit/0123456789012345678901234567890123456789")
+ req.Header.Add("Accept", "text/html")
+ resp := MakeRequest(t, req, http.StatusNotFound)
+ assert.True(t, test.IsNormalPageCompleted(resp.Body.String()), "non-existing commit should render 404 page")
+}