Skip to content

fix: Public API 500 error when creating file#8211

Open
Meldiron wants to merge 3 commits intogogs:mainfrom
Meldiron:copilot/fix-create-or-update-file-bug
Open

fix: Public API 500 error when creating file#8211
Meldiron wants to merge 3 commits intogogs:mainfrom
Meldiron:copilot/fix-create-or-update-file-bug

Conversation

@Meldiron
Copy link
Copy Markdown

@Meldiron Meldiron commented Mar 24, 2026

Agent-Logs-Url: https://github.com/Meldiron/gogs/sessions/5f2d56f2-ffda-4c56-8ed1-07f2b443ba3e

Describe the pull request

Fixes bug when updating existing file only worked for default branch, not branch provided in payload

Link to the issue: #8210

Checklist

  • I agree to follow the Code of Conduct by submitting this pull request.
  • I have read and acknowledge the Contributing guide.
  • I have added test cases to cover the new code or have provided the test plan. (if applicable)
  • I have added an entry to CHANGELOG. (if applicable)

Test plan

New test added for scenario described in issue

## Additional info

This PR was made by AI, but I am available to address PR comments manually. I self-reviewed before submitting changes.

…ranches via API

When calling PUT /repos/{owner}/{repo}/contents/{path} with a branch that
already exists (but differs from the default branch), the handler was always
passing OldBranch=DefaultBranch to UpdateRepoFile. This caused UpdateRepoFile
to interpret the operation as "create a new branch from the default branch",
which failed with "branch already exists" when the target branch was already
present.

Fix by checking if the target branch exists before choosing OldBranch:
- If the target branch exists: OldBranch = target branch (commit on top of it)
- If the target branch doesn't exist: OldBranch = DefaultBranch (create it)

Also adds TestRepository_UpdateRepoFile_ExistingBranch to cover this case.

Co-authored-by: Meldiron <19310830+Meldiron@users.noreply.github.com>
Agent-Logs-Url: https://github.com/Meldiron/gogs/sessions/5f2d56f2-ffda-4c56-8ed1-07f2b443ba3e
@Meldiron Meldiron requested a review from unknwon as a code owner March 24, 2026 09:10
@Meldiron
Copy link
Copy Markdown
Author

CleanShot 2026-03-24 at 11 09 33@2x

I dont think DeepSource warning is relevant to my changes, so I will not be addressing it. But yeah, words like fetch, list, prepare, generate might work better.
But personally, dont see it as big confisuion, golang is well typed.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants