2015-08-11 12:33:31 +00:00
|
|
|
require 'securerandom'
|
|
|
|
|
2014-07-29 09:11:16 +00:00
|
|
|
# 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
|
2014-07-29 09:11:16 +00:00
|
|
|
class CompareService
|
2016-10-12 10:27:59 +00:00
|
|
|
def execute(source_project, source_branch, target_project, target_branch, straight: false)
|
2015-09-18 19:02:01 +00:00
|
|
|
source_commit = source_project.commit(source_branch)
|
|
|
|
return unless source_commit
|
|
|
|
|
|
|
|
source_sha = source_commit.sha
|
2015-08-11 12:33:31 +00: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 14:45:38 +00:00
|
|
|
)
|
2015-08-11 08:28:42 +00:00
|
|
|
end
|
2015-08-11 12:33:31 +00:00
|
|
|
|
2016-07-27 11:09:52 +00:00
|
|
|
raw_compare = Gitlab::Git::Compare.new(
|
2016-03-03 17:38:44 +00:00
|
|
|
target_project.repository.raw_repository,
|
|
|
|
target_branch,
|
2016-09-29 11:04:50 +00:00
|
|
|
source_sha,
|
|
|
|
straight
|
2015-08-11 12:33:31 +00:00
|
|
|
)
|
2016-07-27 11:09:52 +00:00
|
|
|
|
2016-10-12 10:27:59 +00:00
|
|
|
Compare.new(raw_compare, target_project, straight: straight)
|
2014-07-29 09:11:16 +00:00
|
|
|
end
|
|
|
|
end
|