2015-08-11 08:33:31 -04:00
|
|
|
require 'securerandom'
|
|
|
|
|
2014-07-29 05:11:16 -04:00
|
|
|
# Compare 2 branches for one repo or between repositories
|
|
|
|
# and return Gitlab::CompareResult object that responds to commits and diffs
|
|
|
|
class CompareService
|
2015-08-11 08:33:31 -04:00
|
|
|
def execute(source_project, source_branch, target_project, target_branch)
|
2015-09-18 15:02:01 -04:00
|
|
|
source_commit = source_project.commit(source_branch)
|
|
|
|
return unless source_commit
|
|
|
|
|
|
|
|
source_sha = source_commit.sha
|
2015-08-11 08:33:31 -04:00
|
|
|
|
|
|
|
# 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"
|
2015-07-15 10:45:38 -04:00
|
|
|
)
|
2015-08-11 04:28:42 -04:00
|
|
|
end
|
2015-08-11 08:33:31 -04:00
|
|
|
|
|
|
|
Gitlab::CompareResult.new(
|
|
|
|
Gitlab::Git::Compare.new(
|
|
|
|
target_project.repository.raw_repository,
|
|
|
|
target_branch,
|
|
|
|
source_sha,
|
|
|
|
)
|
|
|
|
)
|
2014-07-29 05:11:16 -04:00
|
|
|
end
|
|
|
|
end
|