Skip to content
Snippets Groups Projects
Commit 7638aee7 authored by James Ramsay's avatar James Ramsay
Browse files

Add ability to overwrite a branch via Commits API

For convenience, rather than deleting a branch and recreating the
branch, the `force` option emulates a Git force push overwriting the
existing branch.
parent 9087964c
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -11,6 +11,7 @@ module Commits
@start_project = params[:start_project] || @project
@start_branch = params[:start_branch]
@branch_name = params[:branch_name]
@force = params[:force] || false
end
 
def execute
Loading
Loading
@@ -60,13 +61,13 @@ module Commits
end
 
def validate_branch_existance!
if !project.empty_repo? && different_branch? && repository.branch_exists?(@branch_name)
if !project.empty_repo? && different_branch? && repository.branch_exists?(@branch_name) && @force == false
raise_error("A branch called '#{@branch_name}' already exists. Switch to that branch in order to make changes")
end
end
 
def validate_new_branch_name!
result = ValidateNewBranchService.new(project, current_user).execute(@branch_name)
result = ValidateNewBranchService.new(project, current_user).execute(@branch_name, @force)
 
if result[:status] == :error
raise_error("Something went wrong when we tried to create '#{@branch_name}' for you: #{result[:message]}")
Loading
Loading
Loading
Loading
@@ -3,14 +3,14 @@
require_relative 'base_service'
 
class ValidateNewBranchService < BaseService
def execute(branch_name)
def execute(branch_name, force)
valid_branch = Gitlab::GitRefValidator.validate(branch_name)
 
unless valid_branch
return error('Branch name is invalid')
end
 
if project.repository.branch_exists?(branch_name)
if project.repository.branch_exists?(branch_name) && force == false
return error('Branch already exists')
end
 
Loading
Loading
Loading
Loading
@@ -98,6 +98,7 @@ module API
optional :start_branch, type: String, desc: 'Name of the branch to start the new commit from'
optional :author_email, type: String, desc: 'Author email for commit'
optional :author_name, type: String, desc: 'Author name for commit'
optional :force, type: Boolean, default: false, desc: 'When `true` overwrite the target branch with a new branch starting from the `start_branch`'
end
post ':id/repository/commits' do
authorize_push_to_branch!(params[:branch])
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment