2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-25 09:46:04 -04:00
|
|
|
require 'set'
|
|
|
|
|
2016-07-26 03:21:42 -04:00
|
|
|
class Compare
|
2018-03-07 20:29:12 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2016-07-27 07:09:52 -04:00
|
|
|
delegate :same, :head, :base, to: :@compare
|
2016-07-26 03:21:42 -04:00
|
|
|
|
2016-07-27 13:00:34 -04:00
|
|
|
attr_reader :project
|
|
|
|
|
2016-07-26 03:21:42 -04:00
|
|
|
def self.decorate(compare, project)
|
|
|
|
if compare.is_a?(Compare)
|
|
|
|
compare
|
|
|
|
else
|
|
|
|
self.new(compare, project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-07 20:29:12 -05:00
|
|
|
def initialize(compare, project, base_sha: nil, straight: false)
|
2016-07-26 03:21:42 -04:00
|
|
|
@compare = compare
|
|
|
|
@project = project
|
2018-03-07 20:29:12 -05:00
|
|
|
@base_sha = base_sha
|
2016-09-29 07:04:50 -04:00
|
|
|
@straight = straight
|
2016-07-26 03:21:42 -04:00
|
|
|
end
|
|
|
|
|
2016-07-27 07:09:52 -04:00
|
|
|
def commits
|
2016-07-27 13:00:34 -04:00
|
|
|
@commits ||= Commit.decorate(@compare.commits, project)
|
2016-07-27 07:09:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def start_commit
|
2018-03-07 20:29:12 -05:00
|
|
|
strong_memoize(:start_commit) do
|
|
|
|
commit = @compare.base
|
2016-07-27 07:09:52 -04:00
|
|
|
|
2018-03-07 20:29:12 -05:00
|
|
|
::Commit.new(commit, project) if commit
|
|
|
|
end
|
2016-07-27 07:09:52 -04:00
|
|
|
end
|
|
|
|
|
2016-07-27 13:00:34 -04:00
|
|
|
def head_commit
|
2018-03-07 20:29:12 -05:00
|
|
|
strong_memoize(:head_commit) do
|
|
|
|
commit = @compare.head
|
2016-07-27 07:09:52 -04:00
|
|
|
|
2018-03-07 20:29:12 -05:00
|
|
|
::Commit.new(commit, project) if commit
|
|
|
|
end
|
2016-07-27 07:09:52 -04:00
|
|
|
end
|
2016-07-27 13:00:34 -04:00
|
|
|
alias_method :commit, :head_commit
|
2016-07-27 07:09:52 -04:00
|
|
|
|
2016-10-12 06:27:59 -04:00
|
|
|
def start_commit_sha
|
2018-03-07 20:29:12 -05:00
|
|
|
start_commit&.sha
|
2016-10-12 06:27:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def base_commit_sha
|
2018-03-08 09:39:50 -05:00
|
|
|
strong_memoize(:base_commit) do
|
|
|
|
next unless start_commit && head_commit
|
|
|
|
|
|
|
|
@base_sha || project.merge_base_commit(start_commit.id, head_commit.id)&.sha
|
|
|
|
end
|
2016-10-12 06:27:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def head_commit_sha
|
2018-03-07 20:29:12 -05:00
|
|
|
commit&.sha
|
2016-10-12 06:27:59 -04:00
|
|
|
end
|
|
|
|
|
2016-07-27 13:00:34 -04:00
|
|
|
def raw_diffs(*args)
|
|
|
|
@compare.diffs(*args)
|
|
|
|
end
|
|
|
|
|
2016-08-03 12:32:01 -04:00
|
|
|
def diffs(diff_options = nil)
|
2016-07-27 13:00:34 -04:00
|
|
|
Gitlab::Diff::FileCollection::Compare.new(self,
|
|
|
|
project: project,
|
2016-07-26 03:21:42 -04:00
|
|
|
diff_options: diff_options,
|
|
|
|
diff_refs: diff_refs)
|
|
|
|
end
|
2016-07-27 07:09:52 -04:00
|
|
|
|
|
|
|
def diff_refs
|
2016-07-27 13:00:34 -04:00
|
|
|
Gitlab::Diff::DiffRefs.new(
|
2016-10-12 06:27:59 -04:00
|
|
|
base_sha: @straight ? start_commit_sha : base_commit_sha,
|
|
|
|
start_sha: start_commit_sha,
|
|
|
|
head_sha: head_commit_sha
|
2016-07-27 07:09:52 -04:00
|
|
|
)
|
|
|
|
end
|
2018-10-25 09:46:04 -04:00
|
|
|
|
|
|
|
def modified_paths
|
|
|
|
paths = Set.new
|
|
|
|
diffs.diff_files.each do |diff|
|
|
|
|
paths.add diff.old_path
|
|
|
|
paths.add diff.new_path
|
|
|
|
end
|
|
|
|
paths.to_a
|
|
|
|
end
|
2016-07-26 03:21:42 -04:00
|
|
|
end
|