2019-10-09 21:06:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
module FileCollection
|
|
|
|
class MergeRequestDiffBase < Base
|
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
2021-07-15 06:09:39 +00:00
|
|
|
delegate :real_size, :overflow?, :cache_key, to: :@merge_request_diff
|
|
|
|
|
2019-10-09 21:06:24 +00:00
|
|
|
def initialize(merge_request_diff, diff_options:)
|
|
|
|
@merge_request_diff = merge_request_diff
|
|
|
|
|
|
|
|
super(merge_request_diff,
|
|
|
|
project: merge_request_diff.project,
|
2020-09-21 15:09:44 +00:00
|
|
|
diff_options: merged_diff_options(diff_options),
|
2019-10-09 21:06:24 +00:00
|
|
|
diff_refs: merge_request_diff.diff_refs,
|
|
|
|
fallback_diff_refs: merge_request_diff.fallback_diff_refs)
|
|
|
|
end
|
2019-12-06 00:07:48 +00:00
|
|
|
|
2020-12-15 09:10:00 +00:00
|
|
|
def diff_files(sorted: false)
|
2019-12-06 00:07:48 +00:00
|
|
|
strong_memoize(:diff_files) do
|
|
|
|
diff_files = super
|
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
diff_files.each { |diff_file| highlight_cache.decorate(diff_file) }
|
2019-12-06 00:07:48 +00:00
|
|
|
|
|
|
|
diff_files
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-15 09:10:00 +00:00
|
|
|
def raw_diff_files(sorted: false)
|
|
|
|
# We force `sorted` to `false` as we don't need to sort the diffs when
|
|
|
|
# dealing with `MergeRequestDiff` since we sort its files on create.
|
|
|
|
super(sorted: false)
|
|
|
|
end
|
|
|
|
|
2019-12-06 00:07:48 +00:00
|
|
|
override :write_cache
|
|
|
|
def write_cache
|
2020-06-24 15:08:50 +00:00
|
|
|
highlight_cache.write_if_empty
|
2020-07-14 12:09:14 +00:00
|
|
|
diff_stats_cache.write_if_empty(diff_stats_collection)
|
2019-12-06 00:07:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
override :clear_cache
|
|
|
|
def clear_cache
|
2020-06-24 15:08:50 +00:00
|
|
|
highlight_cache.clear
|
2020-07-14 12:09:14 +00:00
|
|
|
diff_stats_cache.clear
|
2019-12-06 00:07:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
def highlight_cache
|
|
|
|
strong_memoize(:highlight_cache) do
|
|
|
|
Gitlab::Diff::HighlightCache.new(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def diff_stats_cache
|
|
|
|
strong_memoize(:diff_stats_cache) do
|
2021-07-15 06:09:39 +00:00
|
|
|
Gitlab::Diff::StatsCache.new(cachable_key: cache_key)
|
2020-06-24 15:08:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
override :diff_stats_collection
|
|
|
|
def diff_stats_collection
|
|
|
|
strong_memoize(:diff_stats) do
|
|
|
|
next unless fetch_diff_stats?
|
|
|
|
|
2020-07-14 12:09:14 +00:00
|
|
|
diff_stats_cache.read || super
|
2020-06-24 15:08:50 +00:00
|
|
|
end
|
2019-12-06 00:07:48 +00:00
|
|
|
end
|
2020-09-21 15:09:44 +00:00
|
|
|
|
|
|
|
def merged_diff_options(diff_options)
|
2020-10-13 12:08:41 +00:00
|
|
|
project = @merge_request_diff.project
|
|
|
|
max_diff_options = ::Commit.max_diff_options(project: project).merge(project: project)
|
|
|
|
|
2020-09-21 15:09:44 +00:00
|
|
|
diff_options.present? ? diff_options.merge(max_diff_options) : max_diff_options
|
|
|
|
end
|
2019-10-09 21:06:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|