2016-07-26 03:21:42 -04:00
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
module FileCollection
|
2016-08-15 10:57:19 -04:00
|
|
|
class MergeRequestDiff < Base
|
|
|
|
def initialize(merge_request_diff, diff_options:)
|
|
|
|
@merge_request_diff = merge_request_diff
|
2016-07-26 03:21:42 -04:00
|
|
|
|
2016-08-15 10:57:19 -04:00
|
|
|
super(merge_request_diff,
|
|
|
|
project: merge_request_diff.project,
|
2016-07-26 03:21:42 -04:00
|
|
|
diff_options: diff_options,
|
2016-08-15 10:57:19 -04:00
|
|
|
diff_refs: merge_request_diff.diff_refs)
|
2016-07-26 03:21:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def diff_files
|
|
|
|
super.tap { |_| store_highlight_cache }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-07-27 13:00:34 -04:00
|
|
|
# Extracted method to highlight in the same iteration to the diff_collection.
|
2016-07-26 03:21:42 -04:00
|
|
|
def decorate_diff!(diff)
|
2016-07-27 13:00:34 -04:00
|
|
|
diff_file = super
|
2016-11-23 12:18:34 -05:00
|
|
|
cache_highlight!(diff_file) if cacheable?(diff_file)
|
2016-07-27 13:00:34 -04:00
|
|
|
diff_file
|
2016-07-26 03:21:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def highlight_diff_file_from_cache!(diff_file, cache_diff_lines)
|
|
|
|
diff_file.highlighted_diff_lines = cache_diff_lines.map do |line|
|
|
|
|
Gitlab::Diff::Line.init_from_hash(line)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# If we find the highlighted diff files lines on the cache we replace existing diff_files lines (no highlighted)
|
|
|
|
# for the highlighted ones, so we just skip their execution.
|
|
|
|
# If the highlighted diff files lines are not cached we calculate and cache them.
|
|
|
|
#
|
2016-10-12 09:34:47 -04:00
|
|
|
# The content of the cache is a Hash where the key identifies the file and the values are Arrays of
|
2016-07-26 03:21:42 -04:00
|
|
|
# hashes that represent serialized diff lines.
|
|
|
|
#
|
|
|
|
def cache_highlight!(diff_file)
|
2016-11-10 12:24:12 -05:00
|
|
|
item_key = diff_file.file_identifier
|
2016-07-26 03:21:42 -04:00
|
|
|
|
2016-10-12 09:34:47 -04:00
|
|
|
if highlight_cache[item_key]
|
|
|
|
highlight_diff_file_from_cache!(diff_file, highlight_cache[item_key])
|
2016-07-26 03:21:42 -04:00
|
|
|
else
|
2016-10-12 09:34:47 -04:00
|
|
|
highlight_cache[item_key] = diff_file.highlighted_diff_lines.map(&:to_hash)
|
2016-07-26 03:21:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def highlight_cache
|
|
|
|
return @highlight_cache if defined?(@highlight_cache)
|
|
|
|
|
|
|
|
@highlight_cache = Rails.cache.read(cache_key) || {}
|
2016-07-27 13:00:34 -04:00
|
|
|
@highlight_cache_was_empty = @highlight_cache.empty?
|
2016-07-26 03:21:42 -04:00
|
|
|
@highlight_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
def store_highlight_cache
|
|
|
|
Rails.cache.write(cache_key, highlight_cache) if @highlight_cache_was_empty
|
|
|
|
end
|
|
|
|
|
2016-11-23 12:18:34 -05:00
|
|
|
def cacheable?(diff_file)
|
2016-12-26 10:03:05 -05:00
|
|
|
@merge_request_diff.present? &&
|
|
|
|
diff_file.blob &&
|
|
|
|
diff_file.blob.text? &&
|
|
|
|
@project.repository.diffable?(diff_file.blob)
|
2016-07-26 03:21:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cache_key
|
2016-08-15 10:57:19 -04:00
|
|
|
[@merge_request_diff, 'highlighted-diff-files', diff_options]
|
2016-07-26 03:21:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|