From 4b69aa5c1067495532b2cce8a42703dc63a256cc Mon Sep 17 00:00:00 2001 From: mirko Date: Tue, 24 Sep 2024 16:03:51 +0200 Subject: [PATCH 01/10] Add integration test for downloading a patch/diff file in compare page --- tests/integration/compare_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index 2d27a430ac..36c4d03e90 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -23,6 +23,7 @@ import ( files_service "code.gitea.io/gitea/services/repository/files" "code.gitea.io/gitea/tests" + "github.com/PuerkitoBio/goquery" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -67,6 +68,32 @@ func inspectCompare(t *testing.T, htmlDoc *HTMLDoc, diffCount int, diffChanges [ } } +func TestComparePatchAndDiffMenuEntries(t *testing.T) { + defer tests.PrepareTestEnv(t)() + + session := loginUser(t, "user2") + req := NewRequest(t, "GET", "/user2/repo-release/compare/v1.0...v2.0") + resp := session.MakeRequest(t, req, http.StatusOK) + htmlDoc := NewHTMLParser(t, resp.Body) + diffOptions := htmlDoc.doc.Find("[data-tooltip-content=\"Diff options\"]") + + var patchDownloadEntryPresent bool + var diffDownloadEntryPresent bool + diffOptions.Children().Each(func (idx int, c *goquery.Selection) { + value, exists := c.Attr("download") + if exists && strings.HasSuffix(value, ".patch") { + patchDownloadEntryPresent = true + } + + if exists && strings.HasSuffix(value, ".diff") { + diffDownloadEntryPresent = true + } + }) + + assert.True(t, patchDownloadEntryPresent, "Patch file download entry should be present") + assert.True(t, diffDownloadEntryPresent, "Diff file download entry should be present") +} + // Git commit graph for repo20 // * 8babce9 (origin/remove-files-b) Add a dummy file // * b67e43a Delete test.csv and link_hi From 513285460356084daf27c97efddcc585b2531c16 Mon Sep 17 00:00:00 2001 From: mirko Date: Sun, 15 Dec 2024 19:46:21 +0100 Subject: [PATCH 02/10] Add support for diff and patch compare --- routers/web/repo/compare.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 03d49fa692..27b1da13c2 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -231,6 +231,10 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { if infoPath == "" { infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} } else { + infoPath, isDiff := strings.CutSuffix(infoPath, ".diff") + ctx.Data["DownloadDiff"] = isDiff + infoPath, isPatch := strings.CutSuffix(infoPath, ".patch") + ctx.Data["DownloadPatch"] = isPatch infos = strings.SplitN(infoPath, "...", 2) if len(infos) != 2 { if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 { @@ -717,6 +721,22 @@ func CompareDiff(ctx *context.Context) { return } + if ctx.Data["DownloadDiff"].(bool) { + err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch,git.RawDiffNormal,"", ctx.Resp) + if err != nil { + ctx.ServerError("GetDiff", err) + return + } + } + + if ctx.Data["DownloadPatch"].(bool) { + err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch,git.RawDiffPatch,"", ctx.Resp) + if err != nil { + ctx.ServerError("GetPatch", err) + return + } + } + ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes ctx.Data["DirectComparison"] = ci.DirectComparison ctx.Data["OtherCompareSeparator"] = ".." From 9195d6a318ff84d2bd8fc35e749c8883d975fb77 Mon Sep 17 00:00:00 2001 From: mirko Date: Thu, 19 Dec 2024 20:20:39 +0100 Subject: [PATCH 03/10] Add patch/diff compare download --- routers/web/repo/compare.go | 15 ++++++++------- templates/repo/diff/options_dropdown.tmpl | 3 +++ tests/integration/compare_test.go | 7 ++++--- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 27b1da13c2..cddbdf82b1 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -232,9 +232,9 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} } else { infoPath, isDiff := strings.CutSuffix(infoPath, ".diff") - ctx.Data["DownloadDiff"] = isDiff + ctx.Data["ComparingDiff"] = isDiff infoPath, isPatch := strings.CutSuffix(infoPath, ".patch") - ctx.Data["DownloadPatch"] = isPatch + ctx.Data["ComparingPatch"] = isPatch infos = strings.SplitN(infoPath, "...", 2) if len(infos) != 2 { if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 { @@ -721,18 +721,18 @@ func CompareDiff(ctx *context.Context) { return } - if ctx.Data["DownloadDiff"].(bool) { + if ctx.Data["ComparingDiff"].(bool) { err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch,git.RawDiffNormal,"", ctx.Resp) if err != nil { - ctx.ServerError("GetDiff", err) + ctx.ServerError("ComparingDiff", err) return } } - if ctx.Data["DownloadPatch"].(bool) { + if ctx.Data["ComparingPatch"].(bool) { err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch,git.RawDiffPatch,"", ctx.Resp) if err != nil { - ctx.ServerError("GetPatch", err) + ctx.ServerError("ComparingPatch", err) return } } @@ -822,7 +822,8 @@ func CompareDiff(ctx *context.Context) { if ci.DirectComparison { separator = ".." } - ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + separator + base.ShortSha(afterCommitID) + ctx.Data["Comparing"] = base.ShortSha(beforeCommitID) + separator + base.ShortSha(afterCommitID) + ctx.Data["Title"] = "Comparing " + ctx.Data["Comparing"].(string) ctx.Data["IsDiffCompare"] = true _, templateErrs := setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates) diff --git a/templates/repo/diff/options_dropdown.tmpl b/templates/repo/diff/options_dropdown.tmpl index 09b7b80e41..97dc3ba940 100644 --- a/templates/repo/diff/options_dropdown.tmpl +++ b/templates/repo/diff/options_dropdown.tmpl @@ -11,6 +11,9 @@ {{else if .Commit.ID.String}} {{ctx.Locale.Tr "repo.diff.download_patch"}} {{ctx.Locale.Tr "repo.diff.download_diff"}} + {{else if .Diff}} + {{ctx.Locale.Tr "repo.diff.download_patch"}} + {{ctx.Locale.Tr "repo.diff.download_diff"}} {{end}} {{ctx.Locale.Tr "repo.pulls.expand_files"}} {{ctx.Locale.Tr "repo.pulls.collapse_files"}} diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index 36c4d03e90..fbeeb31e1d 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -75,11 +75,11 @@ func TestComparePatchAndDiffMenuEntries(t *testing.T) { req := NewRequest(t, "GET", "/user2/repo-release/compare/v1.0...v2.0") resp := session.MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) - diffOptions := htmlDoc.doc.Find("[data-tooltip-content=\"Diff options\"]") - + downloadOptions := htmlDoc.doc.Find("a.item[download]") + var patchDownloadEntryPresent bool var diffDownloadEntryPresent bool - diffOptions.Children().Each(func (idx int, c *goquery.Selection) { + downloadOptions.Each(func (idx int, c *goquery.Selection) { value, exists := c.Attr("download") if exists && strings.HasSuffix(value, ".patch") { patchDownloadEntryPresent = true @@ -88,6 +88,7 @@ func TestComparePatchAndDiffMenuEntries(t *testing.T) { if exists && strings.HasSuffix(value, ".diff") { diffDownloadEntryPresent = true } + }) assert.True(t, patchDownloadEntryPresent, "Patch file download entry should be present") From 4a7ec0f9a888e9142009e9517e48b260a4b7cdba Mon Sep 17 00:00:00 2001 From: mirko Date: Thu, 19 Dec 2024 20:38:36 +0100 Subject: [PATCH 04/10] Fix formatting --- routers/web/repo/compare.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index cddbdf82b1..2afc432e35 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -722,7 +722,7 @@ func CompareDiff(ctx *context.Context) { } if ctx.Data["ComparingDiff"].(bool) { - err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch,git.RawDiffNormal,"", ctx.Resp) + err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch,git.RawDiffNormal, "", ctx.Resp) if err != nil { ctx.ServerError("ComparingDiff", err) return @@ -730,7 +730,7 @@ func CompareDiff(ctx *context.Context) { } if ctx.Data["ComparingPatch"].(bool) { - err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch,git.RawDiffPatch,"", ctx.Resp) + err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch,git.RawDiffPatch, "", ctx.Resp) if err != nil { ctx.ServerError("ComparingPatch", err) return From d490738d5bf8e777b519d90216f61b55d94c5e2f Mon Sep 17 00:00:00 2001 From: mirko Date: Thu, 19 Dec 2024 20:41:55 +0100 Subject: [PATCH 05/10] Fix formatting --- routers/web/repo/compare.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 2afc432e35..bd13cf4ed9 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -722,7 +722,7 @@ func CompareDiff(ctx *context.Context) { } if ctx.Data["ComparingDiff"].(bool) { - err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch,git.RawDiffNormal, "", ctx.Resp) + err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch, git.RawDiffNormal, "", ctx.Resp) if err != nil { ctx.ServerError("ComparingDiff", err) return @@ -730,7 +730,7 @@ func CompareDiff(ctx *context.Context) { } if ctx.Data["ComparingPatch"].(bool) { - err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch,git.RawDiffPatch, "", ctx.Resp) + err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch, git.RawDiffPatch, "", ctx.Resp) if err != nil { ctx.ServerError("ComparingPatch", err) return From 4e819f1b3c598a9132a213f927e08691ea297930 Mon Sep 17 00:00:00 2001 From: mirko Date: Tue, 24 Dec 2024 18:36:05 +0100 Subject: [PATCH 06/10] Test compare patch download --- tests/integration/compare_test.go | 45 +++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index fbeeb31e1d..083324c691 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -95,6 +95,51 @@ func TestComparePatchAndDiffMenuEntries(t *testing.T) { assert.True(t, diffDownloadEntryPresent, "Diff file download entry should be present") } +func TestComparePatchDownload(t *testing.T) { + defer tests.PrepareTestEnv(t)() + + session := loginUser(t, "user2") + req := NewRequest(t, "GET", "/user2/repo-release/compare/v1.0...v2.0.patch") + attendedResponse := ` + diff --git a/README.md b/README.md +index 6dfe48a..bc7068d 100644 +--- a/README.md ++++ b/README.md +@@ -1,3 +1,3 @@ + # Releases test repo + +-With a v1.0 ++With a v1.0 and a v2.0 +diff --git a/bugfix b/bugfix +new file mode 100644 +index 0000000..e69de29 +diff --git a/feature b/feature +new file mode 100644 +index 0000000..e69de29` + + resp := session.MakeRequest(t, req, http.StatusOK) + assert.Equal(t, attendedResponse, resp.Body.String()) + // htmlDoc := NewHTMLParser(t, resp.Body) + // downloadOptions := htmlDoc.doc.Find("a.item[download]") + + // var patchDownloadEntryPresent bool + // var diffDownloadEntryPresent bool + // downloadOptions.Each(func (idx int, c *goquery.Selection) { + // value, exists := c.Attr("download") + // if exists && strings.HasSuffix(value, ".patch") { + // patchDownloadEntryPresent = true + // } + + // if exists && strings.HasSuffix(value, ".diff") { + // diffDownloadEntryPresent = true + // } + //}) + + // assert.True(t, patchDownloadEntryPresent, "Patch file download entry should be present") + // assert.True(t, diffDownloadEntryPresent, "Diff file download entry should be present") +} + + // Git commit graph for repo20 // * 8babce9 (origin/remove-files-b) Add a dummy file // * b67e43a Delete test.csv and link_hi From b13ac5fcbfd621f4eb167135a66005d2c2ecc378 Mon Sep 17 00:00:00 2001 From: mirko Date: Tue, 24 Dec 2024 18:53:09 +0100 Subject: [PATCH 07/10] Fix formatting --- tests/integration/compare_test.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index 083324c691..05f24dafbe 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -76,10 +76,10 @@ func TestComparePatchAndDiffMenuEntries(t *testing.T) { resp := session.MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) downloadOptions := htmlDoc.doc.Find("a.item[download]") - + var patchDownloadEntryPresent bool var diffDownloadEntryPresent bool - downloadOptions.Each(func (idx int, c *goquery.Selection) { + downloadOptions.Each(func(idx int, c *goquery.Selection) { value, exists := c.Attr("download") if exists && strings.HasSuffix(value, ".patch") { patchDownloadEntryPresent = true @@ -88,7 +88,6 @@ func TestComparePatchAndDiffMenuEntries(t *testing.T) { if exists && strings.HasSuffix(value, ".diff") { diffDownloadEntryPresent = true } - }) assert.True(t, patchDownloadEntryPresent, "Patch file download entry should be present") @@ -121,7 +120,7 @@ index 0000000..e69de29` assert.Equal(t, attendedResponse, resp.Body.String()) // htmlDoc := NewHTMLParser(t, resp.Body) // downloadOptions := htmlDoc.doc.Find("a.item[download]") - + // var patchDownloadEntryPresent bool // var diffDownloadEntryPresent bool // downloadOptions.Each(func (idx int, c *goquery.Selection) { @@ -139,7 +138,6 @@ index 0000000..e69de29` // assert.True(t, diffDownloadEntryPresent, "Diff file download entry should be present") } - // Git commit graph for repo20 // * 8babce9 (origin/remove-files-b) Add a dummy file // * b67e43a Delete test.csv and link_hi From f1bf48920349b9f40c6d9dd9d569b604a22c867b Mon Sep 17 00:00:00 2001 From: mirko Date: Tue, 24 Dec 2024 19:09:37 +0100 Subject: [PATCH 08/10] Fix formatting --- tests/integration/compare_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index 05f24dafbe..d06ea572c3 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -76,7 +76,6 @@ func TestComparePatchAndDiffMenuEntries(t *testing.T) { resp := session.MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) downloadOptions := htmlDoc.doc.Find("a.item[download]") - var patchDownloadEntryPresent bool var diffDownloadEntryPresent bool downloadOptions.Each(func(idx int, c *goquery.Selection) { From 11a433ed1c7b5981ca0dcf4fb18fcecfa810449d Mon Sep 17 00:00:00 2001 From: mirko Date: Tue, 24 Dec 2024 20:41:17 +0100 Subject: [PATCH 09/10] Fix tests --- tests/integration/compare_test.go | 81 +++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 21 deletions(-) diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index d06ea572c3..d134ebf40a 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -98,8 +98,64 @@ func TestComparePatchDownload(t *testing.T) { session := loginUser(t, "user2") req := NewRequest(t, "GET", "/user2/repo-release/compare/v1.0...v2.0.patch") - attendedResponse := ` - diff --git a/README.md b/README.md + attendedResponse := `From 4380f99290b2b3922733ff82c57afad915ace907 Mon Sep 17 00:00:00 2001 +From: user1 +Date: Mon, 17 Apr 2023 14:39:35 +0200 +Subject: [PATCH 1/3] feature v2 + +--- + feature | 0 + 1 file changed, 0 insertions(+), 0 deletions(-) + create mode 100644 feature + +diff --git a/feature b/feature +new file mode 100644 +index 0000000..e69de29 + +From 79f9d88f1b054d650f88da0bd658e21f7b0cf6ec Mon Sep 17 00:00:00 2001 +From: user1 +Date: Mon, 17 Apr 2023 14:38:53 +0200 +Subject: [PATCH 2/3] bugfix + +--- + bugfix | 0 + 1 file changed, 0 insertions(+), 0 deletions(-) + create mode 100644 bugfix + +diff --git a/bugfix b/bugfix +new file mode 100644 +index 0000000..e69de29 + +From 7197b56fdc75b453f47c9110938cb46a303579fd Mon Sep 17 00:00:00 2001 +From: user1 +Date: Mon, 17 Apr 2023 14:42:34 +0200 +Subject: [PATCH 3/3] readme: v2 + +--- + README.md | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/README.md b/README.md +index 6dfe48a..bc7068d 100644 +--- a/README.md ++++ b/README.md +@@ -1,3 +1,3 @@ + # Releases test repo + +-With a v1.0 ++With a v1.0 and a v2.0 +` + + resp := session.MakeRequest(t, req, http.StatusOK) + assert.Equal(t, attendedResponse, resp.Body.String()) +} + +func TestCompareDiffDownload(t *testing.T) { + defer tests.PrepareTestEnv(t)() + + session := loginUser(t, "user2") + req := NewRequest(t, "GET", "/user2/repo-release/compare/v1.0...v2.0.diff") + attendedResponse := `diff --git a/README.md b/README.md index 6dfe48a..bc7068d 100644 --- a/README.md +++ b/README.md @@ -113,28 +169,11 @@ new file mode 100644 index 0000000..e69de29 diff --git a/feature b/feature new file mode 100644 -index 0000000..e69de29` +index 0000000..e69de29 +` resp := session.MakeRequest(t, req, http.StatusOK) assert.Equal(t, attendedResponse, resp.Body.String()) - // htmlDoc := NewHTMLParser(t, resp.Body) - // downloadOptions := htmlDoc.doc.Find("a.item[download]") - - // var patchDownloadEntryPresent bool - // var diffDownloadEntryPresent bool - // downloadOptions.Each(func (idx int, c *goquery.Selection) { - // value, exists := c.Attr("download") - // if exists && strings.HasSuffix(value, ".patch") { - // patchDownloadEntryPresent = true - // } - - // if exists && strings.HasSuffix(value, ".diff") { - // diffDownloadEntryPresent = true - // } - //}) - - // assert.True(t, patchDownloadEntryPresent, "Patch file download entry should be present") - // assert.True(t, diffDownloadEntryPresent, "Diff file download entry should be present") } // Git commit graph for repo20 From 10067db6b03cd75d4f771bacdbb70ad54c8ae08c Mon Sep 17 00:00:00 2001 From: mirko Date: Wed, 1 Jan 2025 08:07:33 +0100 Subject: [PATCH 10/10] Improve code --- routers/web/repo/compare.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index bd13cf4ed9..24785d867e 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -233,8 +233,11 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { } else { infoPath, isDiff := strings.CutSuffix(infoPath, ".diff") ctx.Data["ComparingDiff"] = isDiff - infoPath, isPatch := strings.CutSuffix(infoPath, ".patch") - ctx.Data["ComparingPatch"] = isPatch + if !isDiff { + var isPatch bool + infoPath, isPatch = strings.CutSuffix(infoPath, ".patch") + ctx.Data["ComparingPatch"] = isPatch + } infos = strings.SplitN(infoPath, "...", 2) if len(infos) != 2 { if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 { @@ -721,7 +724,7 @@ func CompareDiff(ctx *context.Context) { return } - if ctx.Data["ComparingDiff"].(bool) { + if ctx.Data["ComparingDiff"] != nil && ctx.Data["ComparingDiff"].(bool) { err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch, git.RawDiffNormal, "", ctx.Resp) if err != nil { ctx.ServerError("ComparingDiff", err) @@ -729,7 +732,7 @@ func CompareDiff(ctx *context.Context) { } } - if ctx.Data["ComparingPatch"].(bool) { + if ctx.Data["ComparingPatch"] != nil && ctx.Data["ComparingPatch"].(bool) { err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch, git.RawDiffPatch, "", ctx.Resp) if err != nil { ctx.ServerError("ComparingPatch", err)