From 220c3fe3b34a97a9c5baf52d3566e7a86e17943a Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Tue, 14 May 2024 09:45:23 +0200 Subject: [PATCH] Expand code diffs against the commits repo When expanding code diffs, the expansion should search for more context in the commits repo, rather than in the repo in context, because the commit may not be available in the base repo. For example, when previewing a pull request, the commit is not in the target repo yet - it's in the fork. Fixes #3746. Signed-off-by: Gergely Nagy --- templates/repo/diff/section_split.tmpl | 6 ++-- templates/repo/diff/section_unified.tmpl | 6 ++-- tests/integration/compare_test.go | 43 ++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/templates/repo/diff/section_split.tmpl b/templates/repo/diff/section_split.tmpl index 182fa737e1..7b1f72939e 100644 --- a/templates/repo/diff/section_split.tmpl +++ b/templates/repo/diff/section_split.tmpl @@ -18,17 +18,17 @@
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5)}} - {{end}} {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 4)}} - {{end}} {{if eq $line.GetExpandDirection 2}} - {{end}} diff --git a/templates/repo/diff/section_unified.tmpl b/templates/repo/diff/section_unified.tmpl index 018c169dc3..d7fe75895b 100644 --- a/templates/repo/diff/section_unified.tmpl +++ b/templates/repo/diff/section_unified.tmpl @@ -14,17 +14,17 @@
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5)}} - {{end}} {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 4)}} - {{end}} {{if eq $line.GetExpandDirection 2}} - {{end}} diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index 88a7c761dc..ffcdd2f23d 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -15,8 +15,11 @@ import ( repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unittest" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/gitrepo" + "code.gitea.io/gitea/modules/optional" repo_service "code.gitea.io/gitea/services/repository" + files_service "code.gitea.io/gitea/services/repository/files" "code.gitea.io/gitea/tests" "github.com/stretchr/testify/assert" @@ -213,3 +216,43 @@ func TestCompareCrossRepo(t *testing.T) { }) }) } + +func TestCompareCodeExpand(t *testing.T) { + onGiteaRun(t, func(t *testing.T, u *url.URL) { + owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) + + // Create a new repository, with a file that has many lines + repo, _, f := CreateDeclarativeRepoWithOptions(t, owner, DeclarativeRepoOptions{ + Files: optional.Some([]*files_service.ChangeRepoFile{ + { + Operation: "create", + TreePath: "docs.md", + ContentReader: strings.NewReader("01\n02\n03\n04\n05\n06\n07\n08\n09\n0a\n0b\n0c\n0d\n0e\n0f\n10\n11\n12\n12\n13\n14\n15\n16\n17\n18\n19\n1a\n1b\n1c\n1d\n1e\n1f\n20\n"), + }, + }), + }) + defer f() + + // Fork the repository + forker := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) + session := loginUser(t, forker.Name) + testRepoFork(t, session, owner.Name, repo.Name, forker.Name, repo.Name+"-copy") + testCreateBranch(t, session, forker.Name, repo.Name+"-copy", "branch/main", "code-expand", http.StatusSeeOther) + + // Edit the file, insert a line somewhere in the middle + testEditFile(t, session, forker.Name, repo.Name+"-copy", "code-expand", "docs.md", + "01\n02\n03\n04\n05\n06\n07\n08\n09\n0a\n0b\n0c\n0d\n0e\n0f\n10\n11\nHELLO WORLD!\n12\n12\n13\n14\n15\n16\n17\n18\n19\n1a\n1b\n1c\n1d\n1e\n1f\n20\n", + ) + + t.Run("code expander targets the fork", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + req := NewRequestf(t, "GET", "%s/%s/compare/main...%s/%s:code-expand", + owner.Name, repo.Name, forker.Name, repo.Name+"-copy") + resp := session.MakeRequest(t, req, http.StatusOK) + htmlDoc := NewHTMLParser(t, resp.Body) + htmlDoc.AssertElement(t, fmt.Sprintf("button.code-expander-button[hx-get^='/%s/%s/blob_excerpt/'] svg.octicon-fold-up", forker.Name, repo.Name+"-copy"), true) + htmlDoc.AssertElement(t, fmt.Sprintf("button.code-expander-button[hx-get^='/%s/%s/blob_excerpt/'] svg.octicon-fold-down", forker.Name, repo.Name+"-copy"), true) + }) + }) +}