Skip to content

Fix compare dropdown for branches without common history#37470

Merged
bircni merged 13 commits intogo-gitea:mainfrom
bircni:bugfix-37469
Apr 28, 2026
Merged

Fix compare dropdown for branches without common history#37470
bircni merged 13 commits intogo-gitea:mainfrom
bircni:bugfix-37469

Conversation

@bircni
Copy link
Copy Markdown
Member

@bircni bircni commented Apr 28, 2026

Summary

  • handle compare requests where base and head refs have no common merge base without returning 500
  • keep the compare branch selectors usable and show a clear warning message
  • add regression coverage for unrelated-history compare selection and merge-base error detection
  • refactor legacy code, remove dead code and fix incorrect logic

Fixes #37469

…sons

- Introduced `ErrNoMergeBase` type to handle cases where two branches do not share a common merge base.
- Updated `MergeBase` function to return this error type when applicable.
- Enhanced the compare functionality to display a warning message in the UI when no common merge base is found.
- Added tests to verify behavior when comparing branches with no common history.
- Updated locale file to include a message for the no common history scenario.
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Apr 28, 2026
Comment thread modules/gitrepo/merge.go Outdated
Comment thread modules/gitrepo/merge.go Outdated
Comment thread templates/repo/diff/compare.tmpl Outdated
Comment thread routers/web/repo/compare.go Outdated
Comment thread routers/web/repo/compare.go Outdated
Co-Authored-By: Codex <codex@openai.com>
Comment thread templates/repo/diff/compare.tmpl Outdated
Comment thread routers/web/repo/compare.go Outdated
@bircni bircni requested a review from wxiaoguang April 28, 2026 16:39
@bircni bircni added the backport/v1.26 This PR should be backported to Gitea 1.26 label Apr 28, 2026
@wxiaoguang
Copy link
Copy Markdown
Contributor

Took a deep look, I found that the legacy code is not quite right.

For example: after GetHeadOwnerAndRepo gets introduced, has := headRepo != nil and related logic doesn't make sense anymore.

Will take a deeper look and maybe fix the legacy problems together.

@wxiaoguang wxiaoguang self-assigned this Apr 28, 2026
@bircni
Copy link
Copy Markdown
Member Author

bircni commented Apr 28, 2026

Took a deep look, I found that the legacy code is not quite right.

For example: after GetHeadOwnerAndRepo gets introduced, has := headRepo != nil and related logic doesn't make sense anymore.

Will take a deeper look and maybe fix the legacy problems together.

Thank you !!!

@wxiaoguang
Copy link
Copy Markdown
Contributor

wxiaoguang commented Apr 28, 2026

Much more changes than I thought (more problems in legacy code than it looks ....). I think it's worth to do the deep clean up changes.

So I guess no need to backport it (the problem has been there for many years, and it doesn't really block daily work, just bad user experience).

Comment thread routers/web/repo/compare.go
Comment thread routers/web/repo/pull.go
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes compare UI/server handling when base and head refs have no common merge base (unrelated histories), avoiding 500s and keeping branch selectors usable with a clear warning.

Changes:

  • Treat “no merge base” as a non-fatal condition during compare info generation and render a warning instead of erroring.
  • Refactor compare info parsing and compare diff preparation, and wire flash alerts into the compare template.
  • Add regression tests for merge-base “unrelated history” detection and compare page behavior.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
tests/integration/compare_test.go Adds integration coverage for compare selection with unrelated histories.
templates/repo/diff/compare.tmpl Renders flash alerts on compare page (for the new warning).
services/git/compare.go Updates compare info generation to tolerate missing merge base.
routers/web/repo/pull.go Switches PR-creation handler to new compare parsing and error handling.
routers/web/repo/compare.go Refactors compare parsing/diff prep and adds no-merge-base warning behavior.
options/locale/locale_en-US.json Adds localized warning text for no common merge base.
modules/gitrepo/merge.go Classifies merge-base “no common history” as not-exist.
modules/gitrepo/compare_test.go Adds unit test for MergeBase with unrelated histories.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread modules/gitrepo/compare_test.go
Comment thread services/git/compare.go
Comment thread routers/web/repo/compare.go Outdated
Comment thread routers/web/repo/compare.go
Comment thread routers/web/repo/pull.go
Comment thread tests/integration/compare_test.go
@bircni
Copy link
Copy Markdown
Member Author

bircni commented Apr 28, 2026

@wxiaoguang would really Like to backport as this bothered us very much today and will still do in the next few weeks/months

@wxiaoguang
Copy link
Copy Markdown
Contributor

OK, maybe you can use the early commits as backport.

@wxiaoguang wxiaoguang added the backport/manual No power to the bots! Create your backport yourself! label Apr 28, 2026
@wxiaoguang wxiaoguang marked this pull request as ready for review April 28, 2026 18:36
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Apr 28, 2026
The permission-denied path in parseCompareInfo returns
util.NewNotExistErrorf("") so the empty message wouldn't leak why the
404 happened. CompareAndPullRequestPost was passing err.Error() into
ctx.JSONError, which surfaced an empty errorMessage to the client.
Route ErrNotExist through ctx.JSONErrorNotFound() so it sends a
generic localized 404 message regardless of the underlying error,
matching the GET path's ctx.NotFound(nil) behavior. ErrInvalidArgument
keeps its descriptive message via JSONError.

Co-Authored-By: Claude (Opus 4.7) <noreply@anthropic.com>
@silverwind
Copy link
Copy Markdown
Member

Small error-message fixup added in 109fef5.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Apr 28, 2026
@bircni bircni merged commit deec2b0 into go-gitea:main Apr 28, 2026
26 checks passed
@GiteaBot GiteaBot added this to the 1.27.0 milestone Apr 28, 2026
@bircni bircni deleted the bugfix-37469 branch April 28, 2026 21:34
zjjhot added a commit to zjjhot/gitea that referenced this pull request Apr 29, 2026
* main:
  Add DEFAULT_TITLE_SOURCE setting for pull request title default behavior (go-gitea#37465)
  Fix compare dropdown for branches without common history (go-gitea#37470)
  FIX: URL sanitization to handle schemeless credentials (go-gitea#37440)
  Refactor pull request view (4) (go-gitea#37451)
  Fix scheduled action panic with null event payload (go-gitea#37459)
  Fix attachment Content-Security-Policy (go-gitea#37455)
  [skip ci] Updated translations via Crowdin
  Rename CurrentRefPath to CurrentRefSubURL (go-gitea#37453)
  Clean up org pages layout (go-gitea#37445)
  Fix script error alert (go-gitea#37458)
  Fix inconsistent disabled styling on logged-out repo header buttons (go-gitea#37406)
  Add API endpoint to reply to pull request review comments (go-gitea#36683)
  Add CurrentURL template variable back (go-gitea#37444)
@wxiaoguang
Copy link
Copy Markdown
Contributor

-> Refactor compare diff/pull page (1) #37481

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/manual No power to the bots! Create your backport yourself! backport/v1.26 This PR should be backported to Gitea 1.26 lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compare dropdown fails when selecting branch with no common merge-base (unrelated histories)

5 participants