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 <me@silverwind.io>
This commit is contained in:
Artem Lytkinandsilverwind authored and GitHub committed 2026-10-21 04:12:18 +00:00
1 parent 1244e06d76
commit 29a59f6dcf
3 files changed
+14 -4

No files matched your search

+2 -2
View File
@@ -1,7 +1,7 @@
<div class="ui top attached header flex-left-right">
<div class="flex-text-block">
{{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"}}
+2 -2
View File
@@ -168,7 +168,7 @@
</div>
</div>
{{$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 @@
</div>
{{end}}
{{else}}{{/* not signed-in or not for pull-request */}}
{{if and (not .CommitCount) $.CompareInfo.CompareBase}}
{{if and (not $showDiffBox) $.CompareInfo.CompareBase}}
<div class="ui segment">{{ctx.Locale.Tr "repo.commits.nothing_to_compare"}}</div>
{{end}}
{{end}}
+10
View File
@@ -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) {