2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-26 03:21:42 -04:00
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
module FileCollection
|
|
|
|
class Base
|
2018-09-19 08:26:28 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2018-09-04 16:06:34 -04:00
|
|
|
attr_reader :project, :diff_options, :diff_refs, :fallback_diff_refs, :diffable
|
2016-07-26 03:21:42 -04:00
|
|
|
|
|
|
|
delegate :count, :size, :real_size, to: :diff_files
|
|
|
|
|
2016-07-27 13:00:34 -04:00
|
|
|
def self.default_options
|
2018-09-19 08:26:28 -04:00
|
|
|
::Commit.max_diff_options.merge(ignore_whitespace_change: false, expanded: false, include_stats: true)
|
2016-07-27 13:00:34 -04:00
|
|
|
end
|
|
|
|
|
2017-05-15 14:19:49 -04:00
|
|
|
def initialize(diffable, project:, diff_options: nil, diff_refs: nil, fallback_diff_refs: nil)
|
2016-07-27 13:00:34 -04:00
|
|
|
diff_options = self.class.default_options.merge(diff_options || {})
|
|
|
|
|
2017-05-15 14:19:49 -04:00
|
|
|
@diffable = diffable
|
2018-09-19 08:26:28 -04:00
|
|
|
@include_stats = diff_options.delete(:include_stats)
|
2017-05-15 14:19:49 -04:00
|
|
|
@project = project
|
2016-07-26 03:21:42 -04:00
|
|
|
@diff_options = diff_options
|
2017-05-15 14:19:49 -04:00
|
|
|
@diff_refs = diff_refs
|
|
|
|
@fallback_diff_refs = fallback_diff_refs
|
2018-09-19 08:26:28 -04:00
|
|
|
@repository = project.repository
|
2016-07-26 03:21:42 -04:00
|
|
|
end
|
|
|
|
|
2018-11-01 10:50:39 -04:00
|
|
|
def diffs
|
|
|
|
@diffs ||= diffable.raw_diffs(diff_options)
|
|
|
|
end
|
|
|
|
|
2016-07-26 03:21:42 -04:00
|
|
|
def diff_files
|
2018-11-01 10:50:39 -04:00
|
|
|
@diff_files ||= diffs.decorate! { |diff| decorate_diff!(diff) }
|
2016-07-26 03:21:42 -04:00
|
|
|
end
|
|
|
|
|
2018-12-04 08:15:19 -05:00
|
|
|
# This mutates `diff_files` lines.
|
|
|
|
def unfold_diff_files(positions)
|
|
|
|
positions_grouped_by_path = positions.group_by { |position| position.file_path }
|
|
|
|
|
|
|
|
diff_files.each do |diff_file|
|
|
|
|
positions = positions_grouped_by_path.fetch(diff_file.file_path, [])
|
|
|
|
positions.each { |position| diff_file.unfold_diff_lines(position) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-21 16:38:33 -04:00
|
|
|
def diff_file_with_old_path(old_path)
|
|
|
|
diff_files.find { |diff_file| diff_file.old_path == old_path }
|
|
|
|
end
|
|
|
|
|
|
|
|
def diff_file_with_new_path(new_path)
|
|
|
|
diff_files.find { |diff_file| diff_file.new_path == new_path }
|
|
|
|
end
|
|
|
|
|
2018-09-04 16:06:34 -04:00
|
|
|
def clear_cache
|
|
|
|
# No-op
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_cache
|
|
|
|
# No-op
|
|
|
|
end
|
|
|
|
|
2016-07-26 03:21:42 -04:00
|
|
|
private
|
|
|
|
|
2018-09-19 08:26:28 -04:00
|
|
|
def diff_stats_collection
|
|
|
|
strong_memoize(:diff_stats) do
|
|
|
|
# There are scenarios where we don't need to request Diff Stats,
|
|
|
|
# when caching for instance.
|
|
|
|
next unless @include_stats
|
|
|
|
next unless diff_refs
|
|
|
|
|
|
|
|
@repository.diff_stats(diff_refs.base_sha, diff_refs.head_sha)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-26 03:21:42 -04:00
|
|
|
def decorate_diff!(diff)
|
2018-04-30 17:44:37 -04:00
|
|
|
return diff if diff.is_a?(File)
|
|
|
|
|
2018-09-19 08:26:28 -04:00
|
|
|
stats = diff_stats_collection&.find_by_path(diff.new_path)
|
|
|
|
|
|
|
|
Gitlab::Diff::File.new(diff,
|
|
|
|
repository: project.repository,
|
|
|
|
diff_refs: diff_refs,
|
|
|
|
fallback_diff_refs: fallback_diff_refs,
|
|
|
|
stats: stats)
|
2016-07-26 03:21:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|