2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-30 00:52:50 -05:00
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class Highlight
|
2016-06-20 12:51:48 -04:00
|
|
|
attr_reader :diff_file, :diff_lines, :raw_lines, :repository
|
2016-01-07 22:37:01 -05:00
|
|
|
|
2017-05-15 13:10:51 -04:00
|
|
|
delegate :old_path, :new_path, :old_sha, :new_sha, to: :diff_file, prefix: :diff
|
2016-01-07 22:37:01 -05:00
|
|
|
|
2016-06-20 12:51:48 -04:00
|
|
|
def initialize(diff_lines, repository: nil)
|
|
|
|
@repository = repository
|
|
|
|
|
2016-01-21 19:43:06 -05:00
|
|
|
if diff_lines.is_a?(Gitlab::Diff::File)
|
2016-01-21 19:53:01 -05:00
|
|
|
@diff_file = diff_lines
|
|
|
|
@diff_lines = @diff_file.diff_lines
|
2016-01-21 19:43:06 -05:00
|
|
|
else
|
|
|
|
@diff_lines = diff_lines
|
|
|
|
end
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2016-01-14 16:28:07 -05:00
|
|
|
@raw_lines = @diff_lines.map(&:text)
|
2015-12-30 00:52:50 -05:00
|
|
|
end
|
|
|
|
|
2016-01-14 16:28:07 -05:00
|
|
|
def highlight
|
2016-01-21 20:10:58 -05:00
|
|
|
@diff_lines.map.with_index do |diff_line, i|
|
|
|
|
diff_line = diff_line.dup
|
2015-12-30 21:44:12 -05:00
|
|
|
# ignore highlighting for "match" lines
|
2016-06-20 12:51:48 -04:00
|
|
|
next diff_line if diff_line.meta?
|
2016-01-14 16:28:07 -05:00
|
|
|
|
2018-10-01 12:44:30 -04:00
|
|
|
rich_line = highlight_line(diff_line) || ERB::Util.html_escape(diff_line.text)
|
2016-01-15 08:39:43 -05:00
|
|
|
|
|
|
|
if line_inline_diffs = inline_diffs[i]
|
2018-02-21 08:42:11 -05:00
|
|
|
begin
|
|
|
|
rich_line = InlineDiffMarker.new(diff_line.text, rich_line).mark(line_inline_diffs)
|
|
|
|
# This should only happen when the encoding of the diff doesn't
|
|
|
|
# match the blob, which is a bug. But we shouldn't fail to render
|
|
|
|
# completely in that case, even though we want to report the error.
|
|
|
|
rescue RangeError => e
|
2018-04-13 12:32:54 -04:00
|
|
|
Gitlab::Sentry.track_exception(e, issue_url: 'https://gitlab.com/gitlab-org/gitlab-ce/issues/45441')
|
2018-02-21 08:42:11 -05:00
|
|
|
end
|
2016-01-15 08:39:43 -05:00
|
|
|
end
|
|
|
|
|
2018-08-11 16:33:15 -04:00
|
|
|
diff_line.rich_text = rich_line
|
2016-01-15 08:39:43 -05:00
|
|
|
|
2016-01-21 20:10:58 -05:00
|
|
|
diff_line
|
|
|
|
end
|
2016-01-14 16:28:07 -05:00
|
|
|
end
|
|
|
|
|
2016-01-15 08:39:43 -05:00
|
|
|
private
|
|
|
|
|
2016-01-29 13:37:17 -05:00
|
|
|
def highlight_line(diff_line)
|
|
|
|
return unless diff_file && diff_file.diff_refs
|
2016-01-20 12:44:27 -05:00
|
|
|
|
2016-07-06 19:29:41 -04:00
|
|
|
rich_line =
|
|
|
|
if diff_line.unchanged? || diff_line.added?
|
2017-06-06 17:21:29 -04:00
|
|
|
new_lines[diff_line.new_pos - 1]&.html_safe
|
2016-07-06 19:29:41 -04:00
|
|
|
elsif diff_line.removed?
|
2017-06-06 17:21:29 -04:00
|
|
|
old_lines[diff_line.old_pos - 1]&.html_safe
|
2016-07-06 19:29:41 -04:00
|
|
|
end
|
2016-01-14 16:28:07 -05:00
|
|
|
|
|
|
|
# Only update text if line is found. This will prevent
|
|
|
|
# issues with submodules given the line only exists in diff content.
|
2016-07-20 12:25:36 -04:00
|
|
|
if rich_line
|
2017-02-21 19:16:48 -05:00
|
|
|
line_prefix = diff_line.text =~ /\A(.)/ ? $1 : ' '
|
2016-07-20 12:25:36 -04:00
|
|
|
"#{line_prefix}#{rich_line}".html_safe
|
|
|
|
end
|
2016-01-14 16:28:07 -05:00
|
|
|
end
|
|
|
|
|
2016-01-15 08:39:43 -05:00
|
|
|
def inline_diffs
|
2016-01-29 13:37:17 -05:00
|
|
|
@inline_diffs ||= InlineDiff.for_lines(@raw_lines)
|
2015-12-30 00:52:50 -05:00
|
|
|
end
|
|
|
|
|
2016-01-07 22:37:01 -05:00
|
|
|
def old_lines
|
2017-06-06 17:21:29 -04:00
|
|
|
@old_lines ||= highlighted_blob_lines(diff_file.old_blob)
|
2016-01-07 22:37:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_lines
|
2017-06-06 17:21:29 -04:00
|
|
|
@new_lines ||= highlighted_blob_lines(diff_file.new_blob)
|
|
|
|
end
|
|
|
|
|
|
|
|
def highlighted_blob_lines(blob)
|
|
|
|
return [] unless blob
|
|
|
|
|
|
|
|
blob.load_all_data!
|
2018-09-06 00:34:25 -04:00
|
|
|
blob.present.highlight.lines
|
2016-01-14 16:38:37 -05:00
|
|
|
end
|
2015-12-30 00:52:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|