2018-11-06 04:45:35 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-26 07:21:42 +00:00
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
module FileCollection
|
|
|
|
class Base
|
2018-09-19 12:26:28 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2018-09-04 20:06:34 +00:00
|
|
|
attr_reader :project, :diff_options, :diff_refs, :fallback_diff_refs, :diffable
|
2016-07-26 07:21:42 +00:00
|
|
|
|
2019-12-16 21:08:00 +00:00
|
|
|
delegate :count, :size, :real_size, to: :raw_diff_files
|
2016-07-26 07:21:42 +00:00
|
|
|
|
2016-07-27 17:00:34 +00:00
|
|
|
def self.default_options
|
2018-09-19 12:26:28 +00:00
|
|
|
::Commit.max_diff_options.merge(ignore_whitespace_change: false, expanded: false, include_stats: true)
|
2016-07-27 17:00:34 +00:00
|
|
|
end
|
|
|
|
|
2017-05-15 18:19:49 +00:00
|
|
|
def initialize(diffable, project:, diff_options: nil, diff_refs: nil, fallback_diff_refs: nil)
|
2016-07-27 17:00:34 +00:00
|
|
|
diff_options = self.class.default_options.merge(diff_options || {})
|
|
|
|
|
2017-05-15 18:19:49 +00:00
|
|
|
@diffable = diffable
|
2018-09-19 12:26:28 +00:00
|
|
|
@include_stats = diff_options.delete(:include_stats)
|
2021-06-14 18:10:28 +00:00
|
|
|
@pagination_data = diff_options.delete(:pagination_data)
|
2017-05-15 18:19:49 +00:00
|
|
|
@project = project
|
2016-07-26 07:21:42 +00:00
|
|
|
@diff_options = diff_options
|
2017-05-15 18:19:49 +00:00
|
|
|
@diff_refs = diff_refs
|
|
|
|
@fallback_diff_refs = fallback_diff_refs
|
2018-09-19 12:26:28 +00:00
|
|
|
@repository = project.repository
|
2016-07-26 07:21:42 +00:00
|
|
|
end
|
|
|
|
|
2018-11-01 14:50:39 +00:00
|
|
|
def diffs
|
|
|
|
@diffs ||= diffable.raw_diffs(diff_options)
|
|
|
|
end
|
|
|
|
|
2020-12-15 09:10:00 +00:00
|
|
|
def diff_files(sorted: false)
|
|
|
|
raw_diff_files(sorted: sorted)
|
2019-12-16 12:07:43 +00:00
|
|
|
end
|
|
|
|
|
2020-12-15 09:10:00 +00:00
|
|
|
def raw_diff_files(sorted: false)
|
|
|
|
strong_memoize(:"raw_diff_files_#{sorted}") do
|
|
|
|
collection = diffs.decorate! { |diff| decorate_diff!(diff) }
|
|
|
|
collection = sort_diffs(collection) if sorted
|
|
|
|
collection
|
|
|
|
end
|
2016-07-26 07:21:42 +00:00
|
|
|
end
|
|
|
|
|
2021-08-03 06:08:50 +00:00
|
|
|
# This is either the new path, otherwise the old path for the diff_file
|
2019-12-11 15:07:38 +00:00
|
|
|
def diff_file_paths
|
|
|
|
diff_files.map(&:file_path)
|
|
|
|
end
|
|
|
|
|
2021-08-03 06:08:50 +00:00
|
|
|
# This is both the new and old paths for the diff_file
|
|
|
|
def diff_paths
|
|
|
|
diff_files.map(&:paths).flatten.uniq
|
|
|
|
end
|
|
|
|
|
2019-12-11 15:07:38 +00:00
|
|
|
def pagination_data
|
2021-06-14 18:10:28 +00:00
|
|
|
@pagination_data || empty_pagination_data
|
2019-12-11 15:07:38 +00:00
|
|
|
end
|
|
|
|
|
2018-12-04 13:15:19 +00: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
|
|
|
|
|
2020-06-19 21:08:32 +00:00
|
|
|
def diff_file_with_old_path(old_path, a_mode = nil)
|
|
|
|
if Feature.enabled?(:file_identifier_hash) && a_mode.present?
|
|
|
|
diff_files.find { |diff_file| diff_file.old_path == old_path && diff_file.a_mode == a_mode }
|
|
|
|
else
|
|
|
|
diff_files.find { |diff_file| diff_file.old_path == old_path }
|
|
|
|
end
|
2017-05-21 20:38:33 +00:00
|
|
|
end
|
|
|
|
|
2020-06-19 21:08:32 +00:00
|
|
|
def diff_file_with_new_path(new_path, b_mode = nil)
|
|
|
|
if Feature.enabled?(:file_identifier_hash) && b_mode.present?
|
|
|
|
diff_files.find { |diff_file| diff_file.new_path == new_path && diff_file.b_mode == b_mode }
|
|
|
|
else
|
|
|
|
diff_files.find { |diff_file| diff_file.new_path == new_path }
|
|
|
|
end
|
2017-05-21 20:38:33 +00:00
|
|
|
end
|
|
|
|
|
2018-09-04 20:06:34 +00:00
|
|
|
def clear_cache
|
|
|
|
# No-op
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_cache
|
|
|
|
# No-op
|
|
|
|
end
|
|
|
|
|
2021-06-28 09:08:19 +00:00
|
|
|
def overflow?
|
|
|
|
raw_diff_files.overflow?
|
|
|
|
end
|
|
|
|
|
2016-07-26 07:21:42 +00:00
|
|
|
private
|
|
|
|
|
2021-06-14 18:10:28 +00:00
|
|
|
def empty_pagination_data
|
2021-06-28 06:07:45 +00:00
|
|
|
{ total_pages: nil }
|
2021-06-14 18:10:28 +00:00
|
|
|
end
|
|
|
|
|
2018-09-19 12:26:28 +00:00
|
|
|
def diff_stats_collection
|
|
|
|
strong_memoize(:diff_stats) do
|
2020-06-24 15:08:50 +00:00
|
|
|
next unless fetch_diff_stats?
|
2018-09-19 12:26:28 +00:00
|
|
|
|
|
|
|
@repository.diff_stats(diff_refs.base_sha, diff_refs.head_sha)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
def fetch_diff_stats?
|
|
|
|
# There are scenarios where we don't need to request Diff Stats,
|
|
|
|
# when caching for instance.
|
|
|
|
@include_stats && diff_refs
|
|
|
|
end
|
|
|
|
|
2016-07-26 07:21:42 +00:00
|
|
|
def decorate_diff!(diff)
|
2018-04-30 21:44:37 +00:00
|
|
|
return diff if diff.is_a?(File)
|
|
|
|
|
2018-09-19 12:26:28 +00: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 07:21:42 +00:00
|
|
|
end
|
2020-12-15 09:10:00 +00:00
|
|
|
|
|
|
|
def sort_diffs(diffs)
|
|
|
|
Gitlab::Diff::FileCollectionSorter.new(diffs).sort
|
|
|
|
end
|
2016-07-26 07:21:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|