summaryrefslogtreecommitdiff
path: root/app/services/create_branch_service.rb
diff options
context:
space:
mode:
authorLin Jen-Shin <godfat@godfat.org>2016-11-15 04:02:10 +0800
committerLin Jen-Shin <godfat@godfat.org>2016-11-15 04:02:10 +0800
commit0b5a2eef8fa5ff4976f97883b631ec28f0553f6a (patch)
treee4410baba5cb2b077b9f7257898722fbfebf20dd /app/services/create_branch_service.rb
parent3128641f7eb93fec0930ebfb83a93dfa5e0b343a (diff)
downloadgitlab-ce-0b5a2eef8fa5ff4976f97883b631ec28f0553f6a.tar.gz
Add `source_branch` option for various git operations
If `source_branch` option is passed, and target branch cannot be found, `Repository#update_branch_with_hooks` would try to create a new branch from `source_branch`. This way, we could make changes in the new branch while only firing the hooks once for the changes. Previously, we can only create a new branch first then make changes to the new branch, firing hooks twice. This behaviour is bad for CI. Fixes #7237
Diffstat (limited to 'app/services/create_branch_service.rb')
-rw-r--r--app/services/create_branch_service.rb22
1 files changed, 11 insertions, 11 deletions
diff --git a/app/services/create_branch_service.rb b/app/services/create_branch_service.rb
index 757fc35a78f..f4270a09928 100644
--- a/app/services/create_branch_service.rb
+++ b/app/services/create_branch_service.rb
@@ -2,18 +2,9 @@ require_relative 'base_service'
class CreateBranchService < BaseService
def execute(branch_name, ref, source_project: @project)
- valid_branch = Gitlab::GitRefValidator.validate(branch_name)
+ failure = validate_new_branch(branch_name)
- unless valid_branch
- return error('Branch name is invalid')
- end
-
- repository = project.repository
- existing_branch = repository.find_branch(branch_name)
-
- if existing_branch
- return error('Branch already exists')
- end
+ return failure if failure
new_branch = if source_project != @project
repository.fetch_ref(
@@ -41,4 +32,13 @@ class CreateBranchService < BaseService
def success(branch)
super().merge(branch: branch)
end
+
+ private
+
+ def validate_new_branch(branch_name)
+ result = ValidateNewBranchService.new(project, current_user).
+ execute(branch_name)
+
+ error(result[:message]) if result[:status] == :error
+ end
end