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
|
2016-03-03 12:38:44 -05:00
|
|
|
# and return Gitlab::Git::Compare object that responds to commits and diffs
|
2014-07-29 05:11:16 -04:00
|
|
|
class CompareService
|
2017-01-06 10:29:13 -05:00
|
|
|
attr_reader :start_project, :start_branch_name
|
2015-09-18 15:02:01 -04:00
|
|
|
|
2017-01-06 10:29:13 -05:00
|
|
|
def initialize(new_start_project, new_start_branch_name)
|
|
|
|
@start_project = new_start_project
|
|
|
|
@start_branch_name = new_start_branch_name
|
2016-12-08 04:57:52 -05:00
|
|
|
end
|
2015-08-11 08:33:31 -04:00
|
|
|
|
2016-12-08 04:57:52 -05:00
|
|
|
def execute(target_project, target_branch, straight: false)
|
|
|
|
# If compare with other project we need to fetch ref first
|
2017-01-04 12:52:21 -05:00
|
|
|
target_project.repository.with_repo_branch_commit(
|
2017-01-06 10:29:13 -05:00
|
|
|
start_project.repository,
|
|
|
|
start_branch_name) do |commit|
|
2017-01-04 12:52:21 -05:00
|
|
|
break unless commit
|
|
|
|
|
|
|
|
compare(commit.sha, target_project, target_branch, straight)
|
2015-08-11 04:28:42 -04:00
|
|
|
end
|
2016-12-08 04:57:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2015-08-11 08:33:31 -04:00
|
|
|
|
2016-12-08 06:11:52 -05:00
|
|
|
def compare(source_sha, target_project, target_branch, straight)
|
2016-07-27 07:09:52 -04:00
|
|
|
raw_compare = Gitlab::Git::Compare.new(
|
2016-03-03 12:38:44 -05:00
|
|
|
target_project.repository.raw_repository,
|
|
|
|
target_branch,
|
2016-09-29 07:04:50 -04:00
|
|
|
source_sha,
|
|
|
|
straight
|
2015-08-11 08:33:31 -04:00
|
|
|
)
|
2016-07-27 07:09:52 -04:00
|
|
|
|
2016-10-12 06:27:59 -04:00
|
|
|
Compare.new(raw_compare, target_project, straight: straight)
|
2014-07-29 05:11:16 -04:00
|
|
|
end
|
|
|
|
end
|