From 29a59f6dcfa6ac000596d713e5e17a2e9368694c Mon Sep 17 00:00:00 2001 From: Artem Lytkin <146867384+4RH1T3CT0R7@users.noreply.github.com> Date: Wed, 21 Oct 2026 07:12:18 +0300 Subject: [PATCH] fix(compare): show the diff when a comparison has no commits (#39247) The compare page hid the diff when a comparison had no commits, even if the trees differ. A direct comparison whose head is an ancestor of its base hits this, which is where the Compare link on a force-push comment leads after commits were dropped. The diff box now shows whenever there are commits or changed files, with the header reading "0 Commits". Fixes: https://github.com/go-gitea/gitea/issues/36390 Co-authored-by: silverwind --- templates/repo/commits_table.tmpl | 4 ++-- templates/repo/diff/compare.tmpl | 4 ++-- tests/integration/compare_test.go | 10 ++++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/templates/repo/commits_table.tmpl b/templates/repo/commits_table.tmpl index 59e87d10c03..96f7bb084e7 100644 --- a/templates/repo/commits_table.tmpl +++ b/templates/repo/commits_table.tmpl @@ -1,7 +1,7 @@
- {{if .Commits}} - {{if gt .CommitCount 0}}{{.CommitCount}}{{end}}{{/* CommitCount is -1 when "follow rename" */}} + {{if or .Commits .IsDiffCompare}} + {{if ge .CommitCount 0}}{{.CommitCount}}{{end}}{{/* CommitCount is -1 when "follow rename" */}} {{ctx.Locale.Tr "repo.commits.commits"}} {{else if .IsNothingToCompare}} {{ctx.Locale.Tr "repo.commits.nothing_to_compare"}} diff --git a/templates/repo/diff/compare.tmpl b/templates/repo/diff/compare.tmpl index 482c8a43460..dd4bcd4e274 100644 --- a/templates/repo/diff/compare.tmpl +++ b/templates/repo/diff/compare.tmpl @@ -168,7 +168,7 @@
- {{$showDiffBox := and .CommitCount (not .IsNothingToCompare)}} + {{$showDiffBox := or .CommitCount .DiffShortStat.NumFiles}} {{if and .IsSigned .PageIsComparePull}} {{$allowCreatePR := and ($.CompareInfo.BaseRef.IsBranch) ($.CompareInfo.HeadRef.IsBranch) (not $.CompareInfo.DirectComparison) (or $.AllowEmptyPr (not .IsNothingToCompare))}} {{if .IsNothingToCompare}} @@ -211,7 +211,7 @@ {{end}} {{else}}{{/* not signed-in or not for pull-request */}} - {{if and (not .CommitCount) $.CompareInfo.CompareBase}} + {{if and (not $showDiffBox) $.CompareInfo.CompareBase}}
{{ctx.Locale.Tr "repo.commits.nothing_to_compare"}}
{{end}} {{end}} diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index 3bff6772535..8fd28dd3235 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -16,6 +16,7 @@ import ( "gitea.dev/modules/git" "gitea.dev/modules/git/gitcmd" "gitea.dev/modules/test" + "gitea.dev/modules/translation" "gitea.dev/modules/util" "gitea.dev/routers/common" repo_service "gitea.dev/services/repository" @@ -136,6 +137,15 @@ func TestCompareBranches(t *testing.T) { diffChanges = []string{"test.txt"} inspectCompare(t, htmlDoc, diffCount, diffChanges) + + req = NewRequest(t, "GET", "/user2/repo20/compare/8babce967f21b9dfa6987f943b91093dac58a4f0..c8e31bc7688741a5287fcde4fbb8fc129ca07027") + resp = session.MakeRequest(t, req, http.StatusOK) + htmlDoc = NewHTMLParser(t, resp.Body) + + inspectCompare(t, htmlDoc, 3, []string{"link_hi", "test.csv", "test.txt"}) + assert.NotContains(t, htmlDoc.doc.Text(), translation.NewLocale("en-US").TrString("repo.commits.nothing_to_compare")) + commitsHeader := strings.Join(strings.Fields(htmlDoc.doc.Find(".ui.top.attached.header .flex-text-block").First().Text()), " ") + assert.Equal(t, "0 "+translation.NewLocale("en-US").TrString("repo.commits.commits"), commitsHeader) } func TestCompareWithRefSuffix(t *testing.T) {