Skip to content

fix(api): handle fork-only commits in compare API#37185

Merged
lunny merged 7 commits into
go-gitea:mainfrom
Mohit25022005:fix-compare-fork
Apr 13, 2026
Merged

fix(api): handle fork-only commits in compare API#37185
lunny merged 7 commits into
go-gitea:mainfrom
Mohit25022005:fix-compare-fork

Conversation

@Mohit25022005
Copy link
Copy Markdown
Contributor

Fix 500 error when comparing branches across fork repositories

Problem

The compare API returns a 500 Internal Server Error when comparing branches where the head commit exists only in the fork repository.

Cause

The API was using the base repository's GitRepo and repository context when converting commits. This fails when the commit does not exist in the base repository, resulting in a "fatal: bad object" error.

Solution

Use the head repository and HeadGitRepo when available to ensure commits are resolved in the correct repository context.

Result

  • Fixes "fatal: bad object" error
  • Enables proper comparison between base and fork repositories
  • Prevents 500 Internal Server Error

Fixes #37168

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Apr 12, 2026
Comment thread routers/api/v1/repo/compare.go Outdated
Comment thread routers/api/v1/repo/compare.go Outdated
Comment thread routers/api/v1/repo/compare.go Outdated
@GiteaBot GiteaBot added lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Apr 12, 2026
@bircni
Copy link
Copy Markdown
Member

bircni commented Apr 12, 2026

i think a test would also be good

@Mohit25022005
Copy link
Copy Markdown
Contributor Author

Thanks for the suggestion!

I've added an integration test covering the fork-only commit scenario to ensure the compare API handles it correctly without errors.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged labels Apr 12, 2026
Comment thread tests/integration/api_repo_compare_test.go Outdated
@lunny lunny added the backport/v1.26 This PR should be backported to Gitea 1.26 label Apr 12, 2026
@wxiaoguang
Copy link
Copy Markdown
Contributor

Not right.

Even if you revert your change, the test still passes.

@wxiaoguang wxiaoguang marked this pull request as draft April 12, 2026 17:26
@bircni
Copy link
Copy Markdown
Member

bircni commented Apr 12, 2026

Not right.

Even if you revert your change, the test still passes.

could you elaborate further?

@wxiaoguang
Copy link
Copy Markdown
Contributor

Not right.
Even if you revert your change, the test still passes.

could you elaborate further?

f74011f

@wxiaoguang wxiaoguang marked this pull request as ready for review April 12, 2026 17:41
@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 12, 2026
@bircni bircni added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Apr 12, 2026
@lunny lunny merged commit 80585ad into go-gitea:main Apr 13, 2026
26 checks passed
@GiteaBot GiteaBot added this to the 1.27.0 milestone Apr 13, 2026
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Apr 13, 2026
GiteaBot pushed a commit to GiteaBot/gitea that referenced this pull request Apr 13, 2026
Fix 500 error when comparing branches across fork repositories

## Problem

The compare API returns a 500 Internal Server Error when comparing
branches where the head commit exists only in the fork repository.

## Cause

The API was using the base repository's GitRepo and repository context
when converting commits. This fails when the commit does not exist in
the base repository, resulting in a "fatal: bad object" error.

## Solution

Use the head repository and HeadGitRepo when available to ensure commits
are resolved in the correct repository context.

## Result

* Fixes "fatal: bad object" error
* Enables proper comparison between base and fork repositories
* Prevents 500 Internal Server Error

Fixes go-gitea#37168

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
@GiteaBot GiteaBot added the backport/done All backports for this PR have been created label Apr 13, 2026
lunny pushed a commit that referenced this pull request Apr 13, 2026
Backport #37185 by @Mohit25022005

Fix 500 error when comparing branches across fork repositories

## Problem

The compare API returns a 500 Internal Server Error when comparing
branches where the head commit exists only in the fork repository.

## Cause

The API was using the base repository's GitRepo and repository context
when converting commits. This fails when the commit does not exist in
the base repository, resulting in a "fatal: bad object" error.

## Solution

Use the head repository and HeadGitRepo when available to ensure commits
are resolved in the correct repository context.

## Result

* Fixes "fatal: bad object" error
* Enables proper comparison between base and fork repositories
* Prevents 500 Internal Server Error

Fixes #37168

Co-authored-by: Mohit Swarnkar <mohitswarnkar13@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
zjjhot added a commit to zjjhot/gitea that referenced this pull request Apr 14, 2026
* main:
  Add comment for the design of "user activity time" (go-gitea#37195)
  fix(api): handle missing base branch in PR commits API (go-gitea#37193)
  Refactor htmx and fetch-action related code (go-gitea#37186)
  Fix encoding for Matrix Webhooks (go-gitea#37190)
  Always show owner/repo name in compare page dropdowns (go-gitea#37172)
  fix(api): handle fork-only commits in compare API (go-gitea#37185)
  Improve Contributing docs and set a release schedule (go-gitea#37109)
  Update Nix flake (go-gitea#37183)
  Remove outdated RunUser logic (go-gitea#37180)
  Refactor flash message and remove SanitizeHTML template func (go-gitea#37179)
  Indicate form field readonly via background (go-gitea#37175)
  Remove dead CSS rules (go-gitea#37173)
  Fix flaky `TestCatFileBatch/QueryTerminated` test (go-gitea#37159)
  Implement logout redirection for reverse proxy auth setups (go-gitea#36085)
  Add missing `//nolint:depguard` (go-gitea#37162)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/done All backports for this PR have been created 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.

GITEA API repository compare result in ISE 500

5 participants