Skip to content

Commit 8663a35

Browse files
GiteaBotwxiaoguang
andauthored
Fix incorrect "blob excerpt" link when comparing files (#31013) (#31026)
Backport #31013 by wxiaoguang Co-authored-by: wxiaoguang <[email protected]>
1 parent 3d3123d commit 8663a35

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

templates/repo/diff/section_split.tmpl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{{$file := .file}}
2+
{{$blobExcerptRepoLink := or ctx.RootData.CommitRepoLink ctx.RootData.RepoLink}}
23
<colgroup>
34
<col width="50">
45
<col width="10">
@@ -18,17 +19,17 @@
1819
<td class="lines-num lines-num-old">
1920
<div class="tw-flex">
2021
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5)}}
21-
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$.root.RepoLink}}/blob_excerpt/{{PathEscape $.root.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=split&direction=down&wiki={{$.root.PageIsWiki}}&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
22+
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptRepoLink}}/blob_excerpt/{{PathEscape $.root.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=split&direction=down&wiki={{$.root.PageIsWiki}}&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
2223
{{svg "octicon-fold-down"}}
2324
</button>
2425
{{end}}
2526
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 4)}}
26-
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$.root.RepoLink}}/blob_excerpt/{{PathEscape $.root.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=split&direction=up&wiki={{$.root.PageIsWiki}}&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
27+
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptRepoLink}}/blob_excerpt/{{PathEscape $.root.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=split&direction=up&wiki={{$.root.PageIsWiki}}&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
2728
{{svg "octicon-fold-up"}}
2829
</button>
2930
{{end}}
3031
{{if eq $line.GetExpandDirection 2}}
31-
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$.root.RepoLink}}/blob_excerpt/{{PathEscape $.root.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=split&direction=&wiki={{$.root.PageIsWiki}}&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
32+
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptRepoLink}}/blob_excerpt/{{PathEscape $.root.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=split&direction=&wiki={{$.root.PageIsWiki}}&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
3233
{{svg "octicon-fold"}}
3334
</button>
3435
{{end}}

templates/repo/diff/section_unified.tmpl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{{$file := .file}}
2+
{{$blobExcerptRepoLink := or ctx.RootData.CommitRepoLink ctx.RootData.RepoLink}}
23
<colgroup>
34
<col width="50">
45
<col width="50">
@@ -14,17 +15,17 @@
1415
<td colspan="2" class="lines-num">
1516
<div class="tw-flex">
1617
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5)}}
17-
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$.root.RepoLink}}/blob_excerpt/{{PathEscape $.root.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=unified&direction=down&wiki={{$.root.PageIsWiki}}&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
18+
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptRepoLink}}/blob_excerpt/{{PathEscape $.root.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=unified&direction=down&wiki={{$.root.PageIsWiki}}&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
1819
{{svg "octicon-fold-down"}}
1920
</button>
2021
{{end}}
2122
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 4)}}
22-
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$.root.RepoLink}}/blob_excerpt/{{PathEscape $.root.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=unified&direction=up&wiki={{$.root.PageIsWiki}}&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
23+
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptRepoLink}}/blob_excerpt/{{PathEscape $.root.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=unified&direction=up&wiki={{$.root.PageIsWiki}}&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
2324
{{svg "octicon-fold-up"}}
2425
</button>
2526
{{end}}
2627
{{if eq $line.GetExpandDirection 2}}
27-
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$.root.RepoLink}}/blob_excerpt/{{PathEscape $.root.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=unified&direction=&wiki={{$.root.PageIsWiki}}&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
28+
<button class="code-expander-button" hx-target="closest tr" hx-get="{{$blobExcerptRepoLink}}/blob_excerpt/{{PathEscape $.root.AfterCommitID}}?{{$line.GetBlobExcerptQuery}}&style=unified&direction=&wiki={{$.root.PageIsWiki}}&anchor=diff-{{$file.NameHash}}K{{$line.SectionInfo.RightIdx}}">
2829
{{svg "octicon-fold"}}
2930
</button>
3031
{{end}}

tests/integration/compare_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ package integration
66
import (
77
"fmt"
88
"net/http"
9+
"net/url"
910
"strings"
1011
"testing"
1112

13+
"code.gitea.io/gitea/models/db"
14+
"code.gitea.io/gitea/models/unittest"
15+
user_model "code.gitea.io/gitea/models/user"
16+
repo_service "code.gitea.io/gitea/services/repository"
1217
"code.gitea.io/gitea/tests"
1318

1419
"github.com/stretchr/testify/assert"
@@ -118,3 +123,37 @@ func TestCompareBranches(t *testing.T) {
118123

119124
inspectCompare(t, htmlDoc, diffCount, diffChanges)
120125
}
126+
127+
func TestCompareCodeExpand(t *testing.T) {
128+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
129+
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
130+
repo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user1, user1, repo_service.CreateRepoOptions{
131+
Name: "test_blob_excerpt",
132+
Readme: "Default",
133+
AutoInit: true,
134+
DefaultBranch: "main",
135+
})
136+
assert.NoError(t, err)
137+
138+
session := loginUser(t, user1.Name)
139+
testEditFile(t, session, user1.Name, repo.Name, "main", "README.md", strings.Repeat("a\n", 30))
140+
141+
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
142+
session = loginUser(t, user2.Name)
143+
testRepoFork(t, session, user1.Name, repo.Name, user2.Name, "test_blob_excerpt-fork")
144+
testCreateBranch(t, session, user2.Name, "test_blob_excerpt-fork", "branch/main", "forked-branch", http.StatusSeeOther)
145+
testEditFile(t, session, user2.Name, "test_blob_excerpt-fork", "forked-branch", "README.md", strings.Repeat("a\n", 15)+"CHANGED\n"+strings.Repeat("a\n", 15))
146+
147+
req := NewRequest(t, "GET", "/user1/test_blob_excerpt/compare/main...user2/test_blob_excerpt-fork:forked-branch")
148+
resp := session.MakeRequest(t, req, http.StatusOK)
149+
htmlDoc := NewHTMLParser(t, resp.Body)
150+
els := htmlDoc.Find(`button.code-expander-button[hx-get]`)
151+
152+
// all the links in the comparison should be to the forked repo&branch
153+
assert.NotZero(t, els.Length())
154+
for i := 0; i < els.Length(); i++ {
155+
link := els.Eq(i).AttrOr("hx-get", "")
156+
assert.True(t, strings.HasPrefix(link, "/user2/test_blob_excerpt-fork/blob_excerpt/"))
157+
}
158+
})
159+
}

0 commit comments

Comments
 (0)