]> source.dussan.org Git - gitea.git/commitdiff
Fix incorrect "blob excerpt" link when comparing files (#31013) (#31026)
authorGiteabot <teabot@gitea.io>
Mon, 20 May 2024 07:40:14 +0000 (15:40 +0800)
committerGitHub <noreply@github.com>
Mon, 20 May 2024 07:40:14 +0000 (07:40 +0000)
Backport #31013 by wxiaoguang

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
templates/repo/diff/section_split.tmpl
templates/repo/diff/section_unified.tmpl
tests/integration/compare_test.go

index 67e2b195de3460d6ec356ea80ce0cc3242e0ab58..349f0c3dfcc9cde9cc828a2e4dc06d2d17eb62c3 100644 (file)
@@ -1,4 +1,5 @@
 {{$file := .file}}
+{{$blobExcerptRepoLink := or ctx.RootData.CommitRepoLink ctx.RootData.RepoLink}}
 <colgroup>
        <col width="50">
        <col width="10">
                                        <td class="lines-num lines-num-old">
                                                <div class="tw-flex">
                                                {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5)}}
-                                                       <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}}">
+                                                       <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}}">
                                                                {{svg "octicon-fold-down"}}
                                                        </button>
                                                {{end}}
                                                {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 4)}}
-                                                       <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}}">
+                                                       <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}}">
                                                                {{svg "octicon-fold-up"}}
                                                        </button>
                                                {{end}}
                                                {{if eq $line.GetExpandDirection 2}}
-                                                       <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}}">
+                                                       <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}}">
                                                                {{svg "octicon-fold"}}
                                                        </button>
                                                {{end}}
index 411115970963a0c7076f842f155aa16f55541ebc..ec59f4d42e0c75cacc9c97245941ca49190c40aa 100644 (file)
@@ -1,4 +1,5 @@
 {{$file := .file}}
+{{$blobExcerptRepoLink := or ctx.RootData.CommitRepoLink ctx.RootData.RepoLink}}
 <colgroup>
        <col width="50">
        <col width="50">
                                        <td colspan="2" class="lines-num">
                                                <div class="tw-flex">
                                                        {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5)}}
-                                                               <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}}">
+                                                               <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}}">
                                                                        {{svg "octicon-fold-down"}}
                                                                </button>
                                                        {{end}}
                                                        {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 4)}}
-                                                               <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}}">
+                                                               <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}}">
                                                                        {{svg "octicon-fold-up"}}
                                                                </button>
                                                        {{end}}
                                                        {{if eq $line.GetExpandDirection 2}}
-                                                               <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}}">
+                                                               <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}}">
                                                                        {{svg "octicon-fold"}}
                                                                </button>
                                                        {{end}}
index 27b2920cc16ee4899ee8b05cf97b70c94fbc42a7..7fb8dbc3327ab69550612d32781afa9d4765f402 100644 (file)
@@ -6,9 +6,14 @@ package integration
 import (
        "fmt"
        "net/http"
+       "net/url"
        "strings"
        "testing"
 
+       "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/models/unittest"
+       user_model "code.gitea.io/gitea/models/user"
+       repo_service "code.gitea.io/gitea/services/repository"
        "code.gitea.io/gitea/tests"
 
        "github.com/stretchr/testify/assert"
@@ -118,3 +123,37 @@ func TestCompareBranches(t *testing.T) {
 
        inspectCompare(t, htmlDoc, diffCount, diffChanges)
 }
+
+func TestCompareCodeExpand(t *testing.T) {
+       onGiteaRun(t, func(t *testing.T, u *url.URL) {
+               user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
+               repo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user1, user1, repo_service.CreateRepoOptions{
+                       Name:          "test_blob_excerpt",
+                       Readme:        "Default",
+                       AutoInit:      true,
+                       DefaultBranch: "main",
+               })
+               assert.NoError(t, err)
+
+               session := loginUser(t, user1.Name)
+               testEditFile(t, session, user1.Name, repo.Name, "main", "README.md", strings.Repeat("a\n", 30))
+
+               user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
+               session = loginUser(t, user2.Name)
+               testRepoFork(t, session, user1.Name, repo.Name, user2.Name, "test_blob_excerpt-fork")
+               testCreateBranch(t, session, user2.Name, "test_blob_excerpt-fork", "branch/main", "forked-branch", http.StatusSeeOther)
+               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))
+
+               req := NewRequest(t, "GET", "/user1/test_blob_excerpt/compare/main...user2/test_blob_excerpt-fork:forked-branch")
+               resp := session.MakeRequest(t, req, http.StatusOK)
+               htmlDoc := NewHTMLParser(t, resp.Body)
+               els := htmlDoc.Find(`button.code-expander-button[hx-get]`)
+
+               // all the links in the comparison should be to the forked repo&branch
+               assert.NotZero(t, els.Length())
+               for i := 0; i < els.Length(); i++ {
+                       link := els.Eq(i).AttrOr("hx-get", "")
+                       assert.True(t, strings.HasPrefix(link, "/user2/test_blob_excerpt-fork/blob_excerpt/"))
+               }
+       })
+}