summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Ramsay <jramsay@gitlab.com>2018-10-02 09:11:32 -0400
committerJames Ramsay <jramsay@gitlab.com>2018-10-02 17:14:01 -0400
commit7638aee7f3670d3fe8699c5ccb92571697533ac4 (patch)
treec19230de685271f2a5ce3f650553c85b6e009e40
parent9087964c8c6a5d068832334b4377188fdd88d578 (diff)
downloadgitlab-ce-jr-45035-override-branch.tar.gz
Add ability to overwrite a branch via Commits APIjr-45035-override-branch
For convenience, rather than deleting a branch and recreating the branch, the `force` option emulates a Git force push overwriting the existing branch.
-rw-r--r--app/services/commits/create_service.rb5
-rw-r--r--app/services/validate_new_branch_service.rb4
-rw-r--r--lib/api/commits.rb1
3 files changed, 6 insertions, 4 deletions
diff --git a/app/services/commits/create_service.rb b/app/services/commits/create_service.rb
index 3ce9acc833c..9f94e616717 100644
--- a/app/services/commits/create_service.rb
+++ b/app/services/commits/create_service.rb
@@ -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
@@ -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]}")
diff --git a/app/services/validate_new_branch_service.rb b/app/services/validate_new_branch_service.rb
index c19e2ec2043..214c727bc63 100644
--- a/app/services/validate_new_branch_service.rb
+++ b/app/services/validate_new_branch_service.rb
@@ -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
diff --git a/lib/api/commits.rb b/lib/api/commits.rb
index f0db1318146..0eecb91ed05 100644
--- a/lib/api/commits.rb
+++ b/lib/api/commits.rb
@@ -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])