From 84bbcf9844b895583eb90155b54efd55606f7445 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 26 Jan 2026 23:56:20 -0800 Subject: [PATCH 1/5] Fix bug when comparing there is diff but no commit --- routers/web/repo/compare.go | 31 +++++++++++++++++-------------- templates/repo/diff/compare.tmpl | 9 +++++---- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 150a8583c8..eea91eaa84 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -495,6 +495,7 @@ func PrepareCompareDiff( ctx.ServerError("GetDiffShortStat", err) return false } + ctx.Data["IsNothingToCompare"] = diffShortStat.NumFiles == 0 ctx.Data["DiffShortStat"] = diffShortStat ctx.Data["Diff"] = diff ctx.Data["DiffBlobExcerptData"] = &gitdiff.DiffBlobExcerptData{ @@ -532,24 +533,26 @@ func PrepareCompareDiff( return false } - commits, err := processGitCommits(ctx, ci.Commits) - if err != nil { - ctx.ServerError("processGitCommits", err) - return false - } - ctx.Data["Commits"] = commits - ctx.Data["CommitCount"] = len(commits) - title := ci.HeadRef.ShortName() - if len(commits) == 1 { - c := commits[0] - title = strings.TrimSpace(c.UserCommit.Summary()) - body := strings.Split(strings.TrimSpace(c.UserCommit.Message()), "\n") - if len(body) > 1 { - ctx.Data["content"] = strings.Join(body[1:], "\n") + if len(ci.Commits) > 0 { + commits, err := processGitCommits(ctx, ci.Commits) + if err != nil { + ctx.ServerError("processGitCommits", err) + return false + } + ctx.Data["Commits"] = commits + if len(commits) == 1 { + c := commits[0] + title = strings.TrimSpace(c.UserCommit.Summary()) + + body := strings.Split(strings.TrimSpace(c.UserCommit.Message()), "\n") + if len(body) > 1 { + ctx.Data["content"] = strings.Join(body[1:], "\n") + } } } + ctx.Data["CommitCount"] = len(ci.Commits) if len(title) > 255 { var trailer string diff --git a/templates/repo/diff/compare.tmpl b/templates/repo/diff/compare.tmpl index 41d0a198f4..8dd21dd480 100644 --- a/templates/repo/diff/compare.tmpl +++ b/templates/repo/diff/compare.tmpl @@ -173,7 +173,6 @@ - {{$showDiffBox := and .CommitCount (not .IsNothingToCompare)}} {{if and .IsSigned .PageIsComparePull}} {{$allowCreatePR := and ($.CompareInfo.BaseRef.IsBranch) ($.CompareInfo.HeadRef.IsBranch) (not $.CompareInfo.DirectComparison) (or $.AllowEmptyPr (not .IsNothingToCompare))}} {{if .IsNothingToCompare}} @@ -216,15 +215,17 @@ {{end}} {{else}}{{/* not singed-in or not for pull-request */}} - {{if not .CommitCount}} + {{if .IsNothingToCompare}}
{{ctx.Locale.Tr "repo.commits.nothing_to_compare"}}
{{end}} {{end}} - {{if $showDiffBox}} + {{if not .IsNothingToCompare}}
- {{template "repo/commits_table" .}} + {{if .CommitCount}} + {{template "repo/commits_table" .}} + {{end}} {{template "repo/diff/box" .}}
{{end}} From f717d4f2e56db09447eeaf62e62f0e5ba826175d Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 27 Jan 2026 07:59:37 -0800 Subject: [PATCH 2/5] revert unnecessary change --- routers/web/repo/compare.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index eea91eaa84..6a5feb322a 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -533,26 +533,24 @@ func PrepareCompareDiff( return false } + commits, err := processGitCommits(ctx, ci.Commits) + if err != nil { + ctx.ServerError("processGitCommits", err) + return false + } + ctx.Data["Commits"] = commits + ctx.Data["CommitCount"] = len(commits) + title := ci.HeadRef.ShortName() + if len(commits) == 1 { + c := commits[0] + title = strings.TrimSpace(c.UserCommit.Summary()) - if len(ci.Commits) > 0 { - commits, err := processGitCommits(ctx, ci.Commits) - if err != nil { - ctx.ServerError("processGitCommits", err) - return false - } - ctx.Data["Commits"] = commits - if len(commits) == 1 { - c := commits[0] - title = strings.TrimSpace(c.UserCommit.Summary()) - - body := strings.Split(strings.TrimSpace(c.UserCommit.Message()), "\n") - if len(body) > 1 { - ctx.Data["content"] = strings.Join(body[1:], "\n") - } + body := strings.Split(strings.TrimSpace(c.UserCommit.Message()), "\n") + if len(body) > 1 { + ctx.Data["content"] = strings.Join(body[1:], "\n") } } - ctx.Data["CommitCount"] = len(ci.Commits) if len(title) > 255 { var trailer string From c566b49e021f6c33089c01153512f03d2f6402e2 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 27 Jan 2026 09:25:18 -0800 Subject: [PATCH 3/5] Fix bug --- routers/web/repo/compare.go | 4 ++-- templates/repo/diff/compare.tmpl | 12 +++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 6a5feb322a..366637e544 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -448,7 +448,8 @@ func PrepareCompareDiff( if (headCommitID == ci.MergeBase && !ci.DirectComparison()) || headCommitID == ci.BaseCommitID { - ctx.Data["IsNothingToCompare"] = true + ctx.Data["CommitCount"] = 0 + ctx.Data["DiffNotAvailable"] = true if unit, err := repo.GetUnit(ctx, unit.TypePullRequests); err == nil { config := unit.PullRequestsConfig() @@ -495,7 +496,6 @@ func PrepareCompareDiff( ctx.ServerError("GetDiffShortStat", err) return false } - ctx.Data["IsNothingToCompare"] = diffShortStat.NumFiles == 0 ctx.Data["DiffShortStat"] = diffShortStat ctx.Data["Diff"] = diff ctx.Data["DiffBlobExcerptData"] = &gitdiff.DiffBlobExcerptData{ diff --git a/templates/repo/diff/compare.tmpl b/templates/repo/diff/compare.tmpl index 8dd21dd480..9f0e68d387 100644 --- a/templates/repo/diff/compare.tmpl +++ b/templates/repo/diff/compare.tmpl @@ -174,8 +174,8 @@ {{if and .IsSigned .PageIsComparePull}} - {{$allowCreatePR := and ($.CompareInfo.BaseRef.IsBranch) ($.CompareInfo.HeadRef.IsBranch) (not $.CompareInfo.DirectComparison) (or $.AllowEmptyPr (not .IsNothingToCompare))}} - {{if .IsNothingToCompare}} + {{$allowCreatePR := and ($.CompareInfo.BaseRef.IsBranch) ($.CompareInfo.HeadRef.IsBranch) (not $.CompareInfo.DirectComparison) (or $.AllowEmptyPr (not .DiffNotAvailable))}} + {{if .DiffNotAvailable}}
{{if $allowCreatePR}} {{ctx.Locale.Tr "repo.pulls.nothing_to_compare_and_allow_empty_pr"}} @@ -215,18 +215,20 @@
{{end}} {{else}}{{/* not singed-in or not for pull-request */}} - {{if .IsNothingToCompare}} + {{if .DiffNotAvailable}}
{{ctx.Locale.Tr "repo.commits.nothing_to_compare"}}
{{end}} {{end}} - {{if not .IsNothingToCompare}} + {{if or .CommitCount (not .DiffNotAvailable)}}
{{if .CommitCount}} {{template "repo/commits_table" .}} {{end}} - {{template "repo/diff/box" .}} + {{if not .DiffNotAvailable}} + {{template "repo/diff/box" .}} + {{end}}
{{end}} From dcffdb1a42912b7067be04eb9188ded9db063950 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 27 Jan 2026 09:53:04 -0800 Subject: [PATCH 4/5] fix bug --- routers/web/repo/pull.go | 2 +- templates/repo/commits_table.tmpl | 2 +- tests/integration/pull_compare_test.go | 71 ++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index cff501ad71..2d865843f2 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -592,7 +592,7 @@ func prepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git_s } if compareInfo.HeadCommitID == compareInfo.MergeBase { - ctx.Data["IsNothingToCompare"] = true + ctx.Data["DiffNotAvailable"] = true } if pull.IsWorkInProgress(ctx) { diff --git a/templates/repo/commits_table.tmpl b/templates/repo/commits_table.tmpl index c8ae535a18..6f369ee403 100644 --- a/templates/repo/commits_table.tmpl +++ b/templates/repo/commits_table.tmpl @@ -2,7 +2,7 @@
{{if or .PageIsCommits (gt .CommitCount 0)}} {{.CommitCount}} {{ctx.Locale.Tr "repo.commits.commits"}} - {{else if .IsNothingToCompare}} + {{else if .DiffNotAvailable}} {{ctx.Locale.Tr "repo.commits.nothing_to_compare"}} {{else}} {{ctx.Locale.Tr "repo.commits.no_commits" $.BaseBranch $.HeadBranch}} diff --git a/tests/integration/pull_compare_test.go b/tests/integration/pull_compare_test.go index da00b3fd56..78922538df 100644 --- a/tests/integration/pull_compare_test.go +++ b/tests/integration/pull_compare_test.go @@ -7,16 +7,22 @@ import ( "fmt" "net/http" "net/url" + "path" + "strconv" "testing" + "time" issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" + user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/git/gitcmd" "code.gitea.io/gitea/modules/test" repo_service "code.gitea.io/gitea/services/repository" "code.gitea.io/gitea/tests" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestPullCompare(t *testing.T) { @@ -170,3 +176,68 @@ func TestPullCompare_EnableAllowEditsFromMaintainer(t *testing.T) { } }) } + +func TestPullCompareForcePushDroppedCommit(t *testing.T) { + onGiteaRun(t, func(t *testing.T, u *url.URL) { + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) + repo, err := repo_service.CreateRepositoryDirectly(t.Context(), user, user, repo_service.CreateRepoOptions{ + Name: "compare-force-push", + AutoInit: true, + DefaultBranch: "master", + Readme: "Default", + }, true) + require.NoError(t, err) + + session := loginUser(t, user.Name) + u.Path = fmt.Sprintf("/%s/%s.git", user.Name, repo.Name) + u.User = url.UserPassword(user.Name, userPassword) + + dstPath := t.TempDir() + doGitClone(dstPath, u)(t) + doGitCreateBranch(dstPath, "feature/drop-commit")(t) + + doGitCheckoutWriteFileCommit(localGitAddCommitOptions{ + LocalRepoPath: dstPath, + CheckoutBranch: "feature/drop-commit", + TreeFilePath: "README.md", + TreeFileContent: "first\n", + })(t) + doGitCheckoutWriteFileCommit(localGitAddCommitOptions{ + LocalRepoPath: dstPath, + CheckoutBranch: "feature/drop-commit", + TreeFilePath: "README.md", + TreeFileContent: "first\nsecond\n", + })(t) + doGitPushTestRepository(dstPath, "origin", "feature/drop-commit")(t) + + resp := testPullCreate(t, session, user.Name, repo.Name, true, "master", "feature/drop-commit", "Force push compare") + prURL := test.RedirectURL(resp) + issueIndex, err := strconv.ParseInt(path.Base(prURL), 10, 64) + require.NoError(t, err) + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID, Index: issueIndex}) + require.NoError(t, issue.LoadPullRequest(t.Context())) + + err = gitcmd.NewCommand("reset", "--hard", "HEAD~1").WithDir(dstPath).Run(t.Context()) + require.NoError(t, err) + err = gitcmd.NewCommand("push", "--force", "origin", "feature/drop-commit").WithDir(dstPath).Run(t.Context()) + require.NoError(t, err) + + var compareURL string + require.Eventually(t, func() bool { + req := NewRequest(t, "GET", prURL) + resp := session.MakeRequest(t, req, http.StatusOK) + htmlDoc := NewHTMLParser(t, resp.Body) + selection := htmlDoc.doc.Find("a.comment-text-label[href*='/compare/']") + if selection.Length() == 0 { + return false + } + compareURL = selection.Last().AttrOr("href", "") + return compareURL != "" + }, 5*time.Second, 20*time.Millisecond) + + req := NewRequest(t, "GET", compareURL) + resp = session.MakeRequest(t, req, http.StatusOK) + htmlDoc := NewHTMLParser(t, resp.Body) + assert.Positive(t, htmlDoc.doc.Find("#diff-file-boxes [data-new-filename=\"README.md\"]").Length()) + }) +} From 9d53e729ad980f428739ac4d6f8fceaab0c94e53 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 28 Jan 2026 16:24:59 -0800 Subject: [PATCH 5/5] improvement --- templates/repo/diff/compare.tmpl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/templates/repo/diff/compare.tmpl b/templates/repo/diff/compare.tmpl index 9f0e68d387..ed62e69e93 100644 --- a/templates/repo/diff/compare.tmpl +++ b/templates/repo/diff/compare.tmpl @@ -221,9 +221,10 @@ {{end}}
- {{if or .CommitCount (not .DiffNotAvailable)}} + {{$hasCommits := and .Commits (gt .CommitCount 0)}} + {{if or $hasCommits (not .DiffNotAvailable)}}
- {{if .CommitCount}} + {{if $hasCommits}} {{template "repo/commits_table" .}} {{end}} {{if not .DiffNotAvailable}}