2015-09-25 12:07:36 +05:30
|
|
|
require 'securerandom'
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
# Compare 2 branches for one repo or between repositories
|
|
|
|
# and return Gitlab::CompareResult object that responds to commits and diffs
|
|
|
|
class CompareService
|
2015-11-26 14:37:03 +05:30
|
|
|
def execute(source_project, source_branch, target_project, target_branch, diff_options = {})
|
2015-09-25 12:07:36 +05:30
|
|
|
source_commit = source_project.commit(source_branch)
|
|
|
|
return unless source_commit
|
|
|
|
|
|
|
|
source_sha = source_commit.sha
|
|
|
|
|
|
|
|
# If compare with other project we need to fetch ref first
|
|
|
|
unless target_project == source_project
|
|
|
|
random_string = SecureRandom.hex
|
|
|
|
|
|
|
|
target_project.repository.fetch_ref(
|
|
|
|
source_project.repository.path_to_repo,
|
|
|
|
"refs/heads/#{source_branch}",
|
|
|
|
"refs/tmp/#{random_string}/head"
|
2014-09-02 18:07:02 +05:30
|
|
|
)
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
Gitlab::CompareResult.new(
|
|
|
|
Gitlab::Git::Compare.new(
|
|
|
|
target_project.repository.raw_repository,
|
|
|
|
target_branch,
|
|
|
|
source_sha,
|
2015-11-26 14:37:03 +05:30
|
|
|
), diff_options
|
2015-09-25 12:07:36 +05:30
|
|
|
)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|