2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
class CreateBranchService < BaseService
|
2019-05-18 00:54:41 +05:30
|
|
|
def execute(branch_name, ref, create_master_if_empty: true)
|
|
|
|
create_master_branch if create_master_if_empty && project.empty_repo?
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
result = ValidateNewBranchService.new(project, current_user)
|
|
|
|
.execute(branch_name)
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
return result if result[:status] == :error
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
new_branch = repository.add_branch(current_user, branch_name, ref)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
if new_branch
|
2015-04-26 12:48:37 +05:30
|
|
|
success(new_branch)
|
|
|
|
else
|
|
|
|
error('Invalid reference name')
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
rescue Gitlab::Git::PreReceiveError => ex
|
2016-08-24 12:49:21 +05:30
|
|
|
error(ex.message)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def success(branch)
|
2016-08-24 12:49:21 +05:30
|
|
|
super().merge(branch: branch)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_master_branch
|
|
|
|
project.repository.create_file(
|
|
|
|
current_user,
|
|
|
|
'/README.md',
|
|
|
|
'',
|
|
|
|
message: 'Add README.md',
|
|
|
|
branch_name: 'master'
|
|
|
|
)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|