gitlab-org--gitlab-foss/app/services/compare_service.rb

43 lines
1.2 KiB
Ruby
Raw Normal View History

require 'securerandom'
# Compare 2 branches for one repo or between repositories
2016-03-03 17:38:44 +00:00
# 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)
source_sha = source_project.repository.
commit(source_branch_name).try(:sha)
2016-12-08 11:11:52 +00:00
return unless source_sha
# If compare with other project we need to fetch ref first
if target_project == source_project
2016-12-08 11:11:52 +00:00
compare(source_sha, target_project, target_branch, straight)
else
target_project.repository.with_tmp_ref(
source_project.repository, source_branch_name) do
2016-12-08 11:11:52 +00:00
compare(source_sha, target_project, target_branch, straight)
end
end
end
private
2016-12-08 11:11:52 +00:00
def compare(source_sha, target_project, target_branch, straight)
raw_compare = Gitlab::Git::Compare.new(
2016-03-03 17:38:44 +00:00
target_project.repository.raw_repository,
target_branch,
source_sha,
straight
)
Compare.new(raw_compare, target_project, straight: straight)
end
end