From 8384d0d8d528ffdd60c9ba9e3c0c9f688cb560ef Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Thu, 8 Dec 2016 17:57:52 +0800 Subject: Introduce Repository#with_tmp_ref which we need commits from the other repository. We'll cleanup the tmp ref after we're done with our business. --- app/services/compare_service.rb | 30 +++++++++++++++++----------- app/services/git_operation_service.rb | 15 +++++++------- app/services/merge_requests/build_service.rb | 5 +++-- 3 files changed, 28 insertions(+), 22 deletions(-) (limited to 'app/services') diff --git a/app/services/compare_service.rb b/app/services/compare_service.rb index 5e8fafca98c..4367cb5f615 100644 --- a/app/services/compare_service.rb +++ b/app/services/compare_service.rb @@ -3,23 +3,29 @@ require 'securerandom' # Compare 2 branches for one repo or between repositories # and return Gitlab::Git::Compare object that responds to commits and diffs class CompareService - def execute(source_project, source_branch, target_project, target_branch, straight: false) - source_commit = source_project.commit(source_branch) - return unless source_commit + attr_reader :source_project, :source_sha - source_sha = source_commit.sha + def initialize(new_source_project, source_branch) + @source_project = new_source_project + @source_sha = new_source_project.commit(source_branch).try(:sha) + end - # If compare with other project we need to fetch ref first - unless target_project == source_project - random_string = SecureRandom.hex + def execute(target_project, target_branch, straight: false) + return unless source_sha - target_project.repository.fetch_ref( - source_project.repository.path_to_repo, - "refs/heads/#{source_branch}", - "refs/tmp/#{random_string}/head" - ) + # If compare with other project we need to fetch ref first + if target_project == source_project + compare(target_project, target_branch, straight) + else + target_project.repository.with_tmp_ref(source_project, source_branch) do + compare(target_project, target_branch, straight) + end end + end + + private + def compare(target_project, target_branch, straight) raw_compare = Gitlab::Git::Compare.new( target_project.repository.raw_repository, target_branch, diff --git a/app/services/git_operation_service.rb b/app/services/git_operation_service.rb index 36c8b8ff575..a7d267cd6b4 100644 --- a/app/services/git_operation_service.rb +++ b/app/services/git_operation_service.rb @@ -38,15 +38,14 @@ GitOperationService = Struct.new(:user, :repository) do branch_name, source_branch_name, source_project) update_branch_with_hooks(branch_name) do |ref| - if repository.project != source_project - repository.fetch_ref( - source_project.repository.path_to_repo, - "#{Gitlab::Git::BRANCH_REF_PREFIX}#{source_branch_name}", - "#{Gitlab::Git::BRANCH_REF_PREFIX}#{branch_name}" - ) + if repository.project == source_project + yield(ref) + else + repository.with_tmp_ref( + source_project.repository, source_branch_name) do + yield(ref) + end end - - yield(ref) end end diff --git a/app/services/merge_requests/build_service.rb b/app/services/merge_requests/build_service.rb index bebfca7537b..a52a94c5ffa 100644 --- a/app/services/merge_requests/build_service.rb +++ b/app/services/merge_requests/build_service.rb @@ -16,9 +16,10 @@ module MergeRequests messages = validate_branches(merge_request) return build_failed(merge_request, messages) unless messages.empty? - compare = CompareService.new.execute( + compare = CompareService.new( merge_request.source_project, - merge_request.source_branch, + merge_request.source_branch + ).execute( merge_request.target_project, merge_request.target_branch, ) -- cgit v1.2.1