diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2015-08-14 17:23:40 +0300 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2015-08-14 17:23:40 +0300 |
commit | f4149bcddca9c0e7aac078b3e7c198f5624ea107 (patch) | |
tree | a0315fdea9e31d2694768d08c94b6361a405dccd /app/services | |
parent | 4e4866f2559262b3c858de15890eb864f18eeca8 (diff) |
Refactor how repository makes commit with pre/post receive hooks
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/commit_service.rb | 56 | ||||
-rw-r--r-- | app/services/files/base_service.rb | 2 | ||||
-rw-r--r-- | app/services/files/create_service.rb | 4 | ||||
-rw-r--r-- | app/services/files/delete_service.rb | 4 | ||||
-rw-r--r-- | app/services/files/update_service.rb | 4 |
5 files changed, 4 insertions, 66 deletions
diff --git a/app/services/commit_service.rb b/app/services/commit_service.rb index 5000e73b473..c77da061a9c 100644 --- a/app/services/commit_service.rb +++ b/app/services/commit_service.rb @@ -1,61 +1,5 @@ -require 'securerandom' class CommitService - class PreReceiveError < StandardError; end - class CommitError < StandardError; end - def self.transaction(project, current_user, branch) - repository = project.repository - path_to_repo = repository.path_to_repo - empty_repo = repository.empty? - oldrev = Gitlab::Git::BLANK_SHA - ref = Gitlab::Git::BRANCH_REF_PREFIX + branch - gl_id = Gitlab::ShellEnv.gl_id(current_user) - - # Create temporary ref - random_string = SecureRandom.hex - tmp_ref = "refs/tmp/#{random_string}/head" - - unless empty_repo - oldrev = repository.find_branch(branch).target - repository.rugged.references.create(tmp_ref, oldrev) - end - - # Make commit in tmp ref - newrev = yield(tmp_ref) - - unless newrev - raise CommitError.new('Failed to create commit') - end - - # Run GitLab pre-receive hook - pre_receive_hook = Gitlab::Git::Hook.new('pre-receive', path_to_repo) - status = pre_receive_hook.trigger(gl_id, oldrev, newrev, ref) - - if status - if empty_repo - # Create branch - repository.rugged.references.create(ref, newrev) - else - # Update head - current_head = repository.find_branch(branch).target - - # Make sure target branch was not changed during pre-receive hook - if current_head == oldrev - repository.rugged.references.update(ref, newrev) - else - raise CommitError.new('Commit was rejected because branch received new push') - end - end - - # Run GitLab post receive hook - post_receive_hook = Gitlab::Git::Hook.new('post-receive', path_to_repo) - status = post_receive_hook.trigger(gl_id, oldrev, newrev, ref) - else - # Remove tmp ref and return error to user - repository.rugged.references.delete(tmp_ref) - - raise PreReceiveError.new('Commit was rejected by pre-reveive hook') - end end end diff --git a/app/services/files/base_service.rb b/app/services/files/base_service.rb index 507e21f5818..7aecee217d8 100644 --- a/app/services/files/base_service.rb +++ b/app/services/files/base_service.rb @@ -26,7 +26,7 @@ module Files else error("Something went wrong. Your changes were not committed") end - rescue CommitService::CommitError, CommitService::PreReceiveError, ValidationError => ex + rescue Repository::CommitError, Repository::PreReceiveError, ValidationError => ex error(ex.message) end diff --git a/app/services/files/create_service.rb b/app/services/files/create_service.rb index 3e00864f00d..91d715b2d63 100644 --- a/app/services/files/create_service.rb +++ b/app/services/files/create_service.rb @@ -3,9 +3,7 @@ require_relative "base_service" module Files class CreateService < Files::BaseService def commit - CommitService.transaction(project, current_user, @target_branch) do |tmp_ref| - repository.commit_file(current_user, @file_path, @file_content, @commit_message, tmp_ref) - end + repository.commit_file(current_user, @file_path, @file_content, @commit_message, @target_branch) end def validate diff --git a/app/services/files/delete_service.rb b/app/services/files/delete_service.rb index d61ca31cf9d..27c881c3430 100644 --- a/app/services/files/delete_service.rb +++ b/app/services/files/delete_service.rb @@ -3,9 +3,7 @@ require_relative "base_service" module Files class DeleteService < Files::BaseService def commit - CommitService.transaction(project, current_user, @target_branch) do |tmp_ref| - repository.remove_file(current_user, @file_path, @commit_message, tmp_ref) - end + repository.remove_file(current_user, @file_path, @commit_message, @target_branch) end end end diff --git a/app/services/files/update_service.rb b/app/services/files/update_service.rb index 69212b36996..a20903c6f02 100644 --- a/app/services/files/update_service.rb +++ b/app/services/files/update_service.rb @@ -3,9 +3,7 @@ require_relative "base_service" module Files class UpdateService < Files::BaseService def commit - CommitService.transaction(project, current_user, @target_branch) do |tmp_ref| - repository.commit_file(current_user, @file_path, @file_content, @commit_message, tmp_ref) - end + repository.commit_file(current_user, @file_path, @file_content, @commit_message, @target_branch) end end end |