Skip to content

Commit b2dbc78

Browse files
committed
fix 500 in blob_excerpt
1 parent 7939850 commit b2dbc78

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

services/gitdiff/gitdiff.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func DiffInlineWithUnicodeEscape(s template.HTML, locale translation.Locale) Dif
268268
return DiffInline{EscapeStatus: status, Content: content}
269269
}
270270

271-
func (diffSection *DiffSection) getLineContentForRender(lineIdx int, diffLine *DiffLine, highlightLines map[int]template.HTML) template.HTML {
271+
func (diffSection *DiffSection) getLineContentForRender(lineIdx int, diffLine *DiffLine, fileLanguage string, highlightLines map[int]template.HTML) template.HTML {
272272
h, ok := highlightLines[lineIdx-1]
273273
if ok {
274274
return h
@@ -279,20 +279,27 @@ func (diffSection *DiffSection) getLineContentForRender(lineIdx int, diffLine *D
279279
if setting.Git.DisableDiffHighlight {
280280
return template.HTML(html.EscapeString(diffLine.Content[1:]))
281281
}
282-
283-
h, _ = highlight.Code(diffSection.Name, diffSection.file.Language, diffLine.Content[1:])
282+
h, _ = highlight.Code(diffSection.Name, fileLanguage, diffLine.Content[1:])
284283
return h
285284
}
286285

287286
func (diffSection *DiffSection) getDiffLineForRender(diffLineType DiffLineType, leftLine, rightLine *DiffLine, locale translation.Locale) DiffInline {
287+
var fileLanguage string
288+
var highlightedLeftLines, highlightedRightLines map[int]template.HTML
289+
// when a "diff section" is manually prepared by ExcerptBlob, it doesn't have "file" information
290+
if diffSection.file != nil {
291+
fileLanguage = diffSection.file.Language
292+
highlightedLeftLines, highlightedRightLines = diffSection.file.highlightedLeftLines, diffSection.file.highlightedRightLines
293+
}
294+
288295
hcd := newHighlightCodeDiff()
289296
var diff1, diff2, lineHTML template.HTML
290297
if leftLine != nil {
291-
diff1 = diffSection.getLineContentForRender(leftLine.LeftIdx, leftLine, diffSection.file.highlightedLeftLines)
298+
diff1 = diffSection.getLineContentForRender(leftLine.LeftIdx, leftLine, fileLanguage, highlightedLeftLines)
292299
lineHTML = util.Iif(diffLineType == DiffLinePlain, diff1, "")
293300
}
294301
if rightLine != nil {
295-
diff2 = diffSection.getLineContentForRender(rightLine.RightIdx, rightLine, diffSection.file.highlightedRightLines)
302+
diff2 = diffSection.getLineContentForRender(rightLine.RightIdx, rightLine, fileLanguage, highlightedRightLines)
296303
lineHTML = util.Iif(diffLineType == DiffLinePlain, diff2, "")
297304
}
298305
if diffLineType != DiffLinePlain {

tests/integration/pull_commit_test.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,33 @@ package integration
55

66
import (
77
"net/http"
8-
"net/url"
98
"testing"
109

1110
pull_service "code.gitea.io/gitea/services/pull"
1211

1312
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1414
)
1515

1616
func TestListPullCommits(t *testing.T) {
17-
onGiteaRun(t, func(t *testing.T, u *url.URL) {
18-
session := loginUser(t, "user5")
19-
req := NewRequest(t, "GET", "/user2/repo1/pulls/3/commits/list")
20-
resp := session.MakeRequest(t, req, http.StatusOK)
21-
22-
var pullCommitList struct {
23-
Commits []pull_service.CommitInfo `json:"commits"`
24-
LastReviewCommitSha string `json:"last_review_commit_sha"`
25-
}
26-
DecodeJSON(t, resp, &pullCommitList)
27-
28-
if assert.Len(t, pullCommitList.Commits, 2) {
29-
assert.Equal(t, "985f0301dba5e7b34be866819cd15ad3d8f508ee", pullCommitList.Commits[0].ID)
30-
assert.Equal(t, "5c050d3b6d2db231ab1f64e324f1b6b9a0b181c2", pullCommitList.Commits[1].ID)
31-
}
32-
assert.Equal(t, "4a357436d925b5c974181ff12a994538ddc5a269", pullCommitList.LastReviewCommitSha)
17+
session := loginUser(t, "user5")
18+
req := NewRequest(t, "GET", "/user2/repo1/pulls/3/commits/list")
19+
resp := session.MakeRequest(t, req, http.StatusOK)
20+
21+
var pullCommitList struct {
22+
Commits []pull_service.CommitInfo `json:"commits"`
23+
LastReviewCommitSha string `json:"last_review_commit_sha"`
24+
}
25+
DecodeJSON(t, resp, &pullCommitList)
26+
27+
require.Len(t, pullCommitList.Commits, 2)
28+
assert.Equal(t, "985f0301dba5e7b34be866819cd15ad3d8f508ee", pullCommitList.Commits[0].ID)
29+
assert.Equal(t, "5c050d3b6d2db231ab1f64e324f1b6b9a0b181c2", pullCommitList.Commits[1].ID)
30+
assert.Equal(t, "4a357436d925b5c974181ff12a994538ddc5a269", pullCommitList.LastReviewCommitSha)
31+
32+
t.Run("CommitBlobExcerpt", func(t *testing.T) {
33+
req = NewRequest(t, "GET", "/user2/repo1/blob_excerpt/985f0301dba5e7b34be866819cd15ad3d8f508ee?last_left=0&last_right=0&left=2&right=2&left_hunk_size=2&right_hunk_size=2&path=README.md&style=split&direction=up")
34+
resp = session.MakeRequest(t, req, http.StatusOK)
35+
assert.Contains(t, resp.Body.String(), `<td class="lines-code lines-code-new"><code class="code-inner"># repo1</code>`)
3336
})
3437
}

0 commit comments

Comments
 (0)