From 9195d6a318ff84d2bd8fc35e749c8883d975fb77 Mon Sep 17 00:00:00 2001 From: mirko Date: Thu, 19 Dec 2024 20:20:39 +0100 Subject: [PATCH] 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")