diff options
author | Robert Speicher <robert@gitlab.com> | 2017-03-02 23:14:02 +0000 |
---|---|---|
committer | Felipe Artur <felipefac@gmail.com> | 2017-03-06 18:32:59 -0300 |
commit | c36418aa81418e0f21ff83657afafe4532376670 (patch) | |
tree | d4e9907b75d3c108c7b90e9f00d88d9e0df70969 /app | |
parent | fb2745685caa3a9623b6bb5520be41a336886990 (diff) | |
download | gitlab-ce-c36418aa81418e0f21ff83657afafe4532376670.tar.gz |
Merge branch 'dm-fix-api-create-file-on-empty-repo' into 'master'
Fix creating a file in an empty repo using the API
Closes #28626
See merge request !9632
Diffstat (limited to 'app')
-rw-r--r-- | app/models/repository.rb | 2 | ||||
-rw-r--r-- | app/services/files/base_service.rb | 16 | ||||
-rw-r--r-- | app/services/git_operation_service.rb | 37 |
3 files changed, 15 insertions, 40 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb index 1a6418a4491..5085e5f436c 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -1075,6 +1075,8 @@ class Repository end def with_repo_branch_commit(start_repository, start_branch_name) + return yield(nil) if start_repository.empty_repo? + branch_name_or_sha = if start_repository == self start_branch_name diff --git a/app/services/files/base_service.rb b/app/services/files/base_service.rb index 0a25f56d24c..0f31f4d7cbb 100644 --- a/app/services/files/base_service.rb +++ b/app/services/files/base_service.rb @@ -58,16 +58,12 @@ module Files raise_error("You are not allowed to push into this branch") end - unless project.empty_repo? - unless @start_project.repository.branch_exists?(@start_branch) - raise_error('You can only create or edit files when you are on a branch') - end - - if different_branch? - if repository.branch_exists?(@target_branch) - raise_error('Branch with such name already exists. You need to switch to this branch in order to make changes') - end - end + if !@start_project.empty_repo? && !@start_project.repository.branch_exists?(@start_branch) + raise ValidationError, 'You can only create or edit files when you are on a branch' + end + + if !project.empty_repo? && different_branch? && repository.branch_exists?(@branch_name) + raise ValidationError, "A branch called #{@branch_name} already exists. Switch to that branch in order to make changes" end end diff --git a/app/services/git_operation_service.rb b/app/services/git_operation_service.rb index 27bcc047601..ed6ea638235 100644 --- a/app/services/git_operation_service.rb +++ b/app/services/git_operation_service.rb @@ -56,12 +56,16 @@ class GitOperationService start_project: repository.project, &block) - check_with_branch_arguments!( - branch_name, start_branch_name, start_project) + start_repository = start_project.repository + start_branch_name = nil if start_repository.empty_repo? + + if start_branch_name && !start_repository.branch_exists?(start_branch_name) + raise ArgumentError, "Cannot find branch #{start_branch_name} in #{start_repository.path_with_namespace}" + end update_branch_with_hooks(branch_name) do repository.with_repo_branch_commit( - start_project.repository, + start_repository, start_branch_name || branch_name, &block) end @@ -149,31 +153,4 @@ class GitOperationService repository.raw_repository.autocrlf = :input end end - - def check_with_branch_arguments!( - branch_name, start_branch_name, start_project) - return if repository.branch_exists?(branch_name) - - if repository.project != start_project - unless start_branch_name - raise ArgumentError, - 'Should also pass :start_branch_name if' + - ' :start_project is different from current project' - end - - unless start_project.repository.branch_exists?(start_branch_name) - raise ArgumentError, - "Cannot find branch #{branch_name} nor" \ - " #{start_branch_name} from" \ - " #{start_project.path_with_namespace}" - end - elsif start_branch_name - unless repository.branch_exists?(start_branch_name) - raise ArgumentError, - "Cannot find branch #{branch_name} nor" \ - " #{start_branch_name} from" \ - " #{repository.project.path_with_namespace}" - end - end - end end |