diff options
Diffstat (limited to 'app/services/branches/validate_new_service.rb')
-rw-r--r-- | app/services/branches/validate_new_service.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/app/services/branches/validate_new_service.rb b/app/services/branches/validate_new_service.rb new file mode 100644 index 00000000000..e45183d160f --- /dev/null +++ b/app/services/branches/validate_new_service.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module Branches + class ValidateNewService < BaseService + def initialize(project) + @project = project + end + + def execute(branch_name, force: false) + return error('Branch name is invalid') unless valid_name?(branch_name) + + if branch_exist?(branch_name) && !force + return error('Branch already exists') + end + + success + rescue Gitlab::Git::PreReceiveError => ex + error(ex.message) + end + + private + + def valid_name?(branch_name) + Gitlab::GitRefValidator.validate(branch_name) + end + + def branch_exist?(branch_name) + project.repository.branch_exists?(branch_name) + end + end +end |