gitlab-org--gitlab-foss/app/services/compare_service.rb
Lin Jen-Shin 99ac093527 Introduce Repository#with_repo_branch_commit
We merge repository checks inside it so we don't have to
check it on the call site, and we could also load the commit
for the caller. This greatly reduce code duplication.

Feedback:
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/7237#note_20572919
2017-01-05 01:52:21 +08:00

36 lines
1 KiB
Ruby

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
attr_reader :source_project, :source_branch_name
def initialize(new_source_project, new_source_branch_name)
@source_project = new_source_project
@source_branch_name = new_source_branch_name
end
def execute(target_project, target_branch, straight: false)
# If compare with other project we need to fetch ref first
target_project.repository.with_repo_branch_commit(
source_project.repository,
source_branch_name) do |commit|
break unless commit
compare(commit.sha, target_project, target_branch, straight)
end
end
private
def compare(source_sha, target_project, target_branch, straight)
raw_compare = Gitlab::Git::Compare.new(
target_project.repository.raw_repository,
target_branch,
source_sha,
straight
)
Compare.new(raw_compare, target_project, straight: straight)
end
end