Skip to content

Conversation

robgonnella
Copy link
Contributor

@robgonnella robgonnella commented Oct 6, 2025

Allows users to specify a "force" option in API /contents routes when modifying files in a new branch. When "force" is true, and the branch already exists, a force push will occur provided the branch does not have a branch protection rule that disables force pushing.

This is useful as a way to manage a branch remotely through only the API. For example in an automated release tool you can pull commits, analyze, and update a release PR branch all remotely without needing to clone or perform any local git operations.

Resolve #35538

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Oct 6, 2025
@github-actions github-actions bot added modifies/api This PR adds API routes or modifies them modifies/go Pull requests that update Go code labels Oct 6, 2025
Rob Gonnella added 2 commits October 6, 2025 10:38
Allows users to specify a "force" option in API /contents routes when
modifying files in a new branch. When "force" is true, and the branch
already exists, a force push will occur provided the branch does not
have a branch protection rule that disables force pushing.

This is useful as a way to manage a branch remotely through only the
API. For example in an automated release tool you can pull commits,
analyze, and update a release PR branch all remotely without needing to
clone or perform any local git operations.

[go-gitea#35538](go-gitea#35538)
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Oct 6, 2025

To be honest, I don't think the design is clear. It doesn't seem related to "git force push" when the "force" option is in a "file operation API".

Does the "force" mean "ignore creating branch if it exists and continue update the files on it", or "discarding the existing branch and create a new one from the old branch then update the files"?

For example in an automated release tool you can pull commits, analyze, and update a release PR branch all remotely without needing to clone or perform any local git operations.

I still have the question why "force" is necessary. You can also "try to create a new branch and ignore the errors if it exists, then update the files", or "delete the existing branch and ignore the errors, then create a new branch".

@wxiaoguang
Copy link
Contributor

If I understand correctly, you'd like to make the file operation API work this way: "discarding the existing branch and create a new one from the old branch (like git force push) then update the files"?

@robgonnella
Copy link
Contributor Author

If I understand correctly, you'd like to make the file operation API work this way: "discarding the existing branch and create a new one from the old branch (like git force push) then update the files"?

Yes I believe that would be the flow.

If you are starting from a "base branch", building your file changes from that point and then targeting a "new branch" as the final destination (exactly what the file updates API exposes), you have no option other than "force" if the "new branch" already exists.

In my case deleting the branch first and recreating would not be an option as the branch will have an open PR to the "base" branch and deleting the branch will auto close the existing PR. You would then have to recreate the PR as well. I've tried this as a workaround and I suppose it's doable but there are timing issues with waiting for the PR is be fully registered as closed before opening a new PR from the same branch. Overall the experience in that flow is sub-optimal.

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Oct 6, 2025

Made some changes and simplified the code:

  1. We don't need to check "branch protection" in the API logic, each "git push" will trigger git hook, and the protection is fully done there.
    • Also added some more test asserts to make sure we get the error messages we want.
  2. All file operation APIs share the same logic, so we only need to test the "ForcePush" logic in one of them.

@robgonnella
Copy link
Contributor Author

Ahh yes much nicer! Thank you for your thoughtful and detailed review and guidance!

@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 Oct 6, 2025
@wxiaoguang wxiaoguang added this to the 1.26.0 milestone Oct 6, 2025
@wxiaoguang wxiaoguang added the type/feature Completely new functionality. Can only be merged if feature freeze is not active. label Oct 6, 2025
@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 Oct 6, 2025
@lunny lunny added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Oct 6, 2025
@lunny lunny merged commit c9e7fde into go-gitea:main Oct 7, 2025
26 checks passed
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Oct 7, 2025
@robgonnella robgonnella deleted the i-35538 branch October 7, 2025 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/api This PR adds API routes or modifies them modifies/go Pull requests that update Go code type/feature Completely new functionality. Can only be merged if feature freeze is not active.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option to force update when posting to /contents and new_branch already exists
4 participants