[go: up one dir, main page]

Skip to content

Enhance API function for creating a new branch with 'force' parameter

Problem to solve

The POST /projects/:id/repository/branches can create new branch on specified commit. When the branch already exists, then this endpoint returns "Branch already exists" error message. Git command git branch has an optional --force option for this purpose.

Currently using Gitlab API we need to make several steps to simulate this feature:

  • Check if the branch already exists
  • If so, then delete it.
  • Create the branch again on desired commit.

The downside of this procedure is that it is not an atomic operation. When the procedure crash or is interrupted in the middle, then the branch can be already deleted but the new one is not created yet. The branch is lost instead of being moved to the new commit.

Proposal

  1. Add a new optional 'force' parameter to the API function for creating a new branch.
  2. If the 'force' parameter is provided, a new branch should be created even if a branch with the same name already exists.
  3. In such a case, the existing branch should be moved to the new commit.

References

The git branch --force command allows the creation of a new branch even if a branch with the same name already exists and moves the existing branch to the new commit. Implementing this feature would bring GitLab's functionality closer to the behavior of the git branch --force command.