Add patch/diff compare download

This commit is contained in:
mirko 2024-12-19 20:20:39 +01:00
parent 5132854603
commit 9195d6a318
3 changed files with 15 additions and 10 deletions

View file

@ -232,9 +232,9 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch}
} else { } else {
infoPath, isDiff := strings.CutSuffix(infoPath, ".diff") infoPath, isDiff := strings.CutSuffix(infoPath, ".diff")
ctx.Data["DownloadDiff"] = isDiff ctx.Data["ComparingDiff"] = isDiff
infoPath, isPatch := strings.CutSuffix(infoPath, ".patch") infoPath, isPatch := strings.CutSuffix(infoPath, ".patch")
ctx.Data["DownloadPatch"] = isPatch ctx.Data["ComparingPatch"] = isPatch
infos = strings.SplitN(infoPath, "...", 2) infos = strings.SplitN(infoPath, "...", 2)
if len(infos) != 2 { if len(infos) != 2 {
if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 { if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 {
@ -721,18 +721,18 @@ func CompareDiff(ctx *context.Context) {
return return
} }
if ctx.Data["DownloadDiff"].(bool) { 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 { if err != nil {
ctx.ServerError("GetDiff", err) ctx.ServerError("ComparingDiff", err)
return return
} }
} }
if ctx.Data["DownloadPatch"].(bool) { 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 { if err != nil {
ctx.ServerError("GetPatch", err) ctx.ServerError("ComparingPatch", err)
return return
} }
} }
@ -822,7 +822,8 @@ func CompareDiff(ctx *context.Context) {
if ci.DirectComparison { if ci.DirectComparison {
separator = ".." 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 ctx.Data["IsDiffCompare"] = true
_, templateErrs := setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates) _, templateErrs := setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates)

View file

@ -11,6 +11,9 @@
{{else if .Commit.ID.String}} {{else if .Commit.ID.String}}
<a class="item" href="{{$.RepoLink}}/commit/{{PathEscape .Commit.ID.String}}.patch" download="{{ShortSha .Commit.ID.String}}.patch">{{ctx.Locale.Tr "repo.diff.download_patch"}}</a> <a class="item" href="{{$.RepoLink}}/commit/{{PathEscape .Commit.ID.String}}.patch" download="{{ShortSha .Commit.ID.String}}.patch">{{ctx.Locale.Tr "repo.diff.download_patch"}}</a>
<a class="item" href="{{$.RepoLink}}/commit/{{PathEscape .Commit.ID.String}}.diff" download="{{ShortSha .Commit.ID.String}}.diff">{{ctx.Locale.Tr "repo.diff.download_diff"}}</a> <a class="item" href="{{$.RepoLink}}/commit/{{PathEscape .Commit.ID.String}}.diff" download="{{ShortSha .Commit.ID.String}}.diff">{{ctx.Locale.Tr "repo.diff.download_diff"}}</a>
{{else if .Diff}}
<a class="item" href="{{$.RepoLink}}/compare/{{.Comparing}}.patch" download="{{.Comparing}}.patch">{{ctx.Locale.Tr "repo.diff.download_patch"}}</a>
<a class="item" href="{{$.RepoLink}}/compare/{{.Comparing}}.diff" download="{{.Comparing}}.diff">{{ctx.Locale.Tr "repo.diff.download_diff"}}</a>
{{end}} {{end}}
<a id="expand-files-btn" class="item">{{ctx.Locale.Tr "repo.pulls.expand_files"}}</a> <a id="expand-files-btn" class="item">{{ctx.Locale.Tr "repo.pulls.expand_files"}}</a>
<a id="collapse-files-btn" class="item">{{ctx.Locale.Tr "repo.pulls.collapse_files"}}</a> <a id="collapse-files-btn" class="item">{{ctx.Locale.Tr "repo.pulls.collapse_files"}}</a>

View file

@ -75,11 +75,11 @@ func TestComparePatchAndDiffMenuEntries(t *testing.T) {
req := NewRequest(t, "GET", "/user2/repo-release/compare/v1.0...v2.0") req := NewRequest(t, "GET", "/user2/repo-release/compare/v1.0...v2.0")
resp := session.MakeRequest(t, req, http.StatusOK) resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body) 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 patchDownloadEntryPresent bool
var diffDownloadEntryPresent 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") value, exists := c.Attr("download")
if exists && strings.HasSuffix(value, ".patch") { if exists && strings.HasSuffix(value, ".patch") {
patchDownloadEntryPresent = true patchDownloadEntryPresent = true
@ -88,6 +88,7 @@ func TestComparePatchAndDiffMenuEntries(t *testing.T) {
if exists && strings.HasSuffix(value, ".diff") { if exists && strings.HasSuffix(value, ".diff") {
diffDownloadEntryPresent = true diffDownloadEntryPresent = true
} }
}) })
assert.True(t, patchDownloadEntryPresent, "Patch file download entry should be present") assert.True(t, patchDownloadEntryPresent, "Patch file download entry should be present")