debian-mirror-gitlab/lib/gitlab/diff/highlight.rb

167 lines
5.5 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2016-01-29 22:53:50 +05:30
module Gitlab
module Diff
class Highlight
2021-06-08 01:23:25 +05:30
PREFIX_REGEXP = /\A(.)/.freeze
2021-04-29 21:17:54 +05:30
attr_reader :diff_file, :diff_lines, :repository, :project
2016-01-29 22:53:50 +05:30
2017-09-10 17:25:29 +05:30
delegate :old_path, :new_path, :old_sha, :new_sha, to: :diff_file, prefix: :diff
2016-01-29 22:53:50 +05:30
2016-08-24 12:49:21 +05:30
def initialize(diff_lines, repository: nil)
@repository = repository
2021-03-11 19:13:27 +05:30
@project = repository&.project
2016-08-24 12:49:21 +05:30
2016-01-29 22:53:50 +05:30
if diff_lines.is_a?(Gitlab::Diff::File)
@diff_file = diff_lines
@diff_lines = @diff_file.diff_lines
else
@diff_lines = diff_lines
end
2018-03-17 18:26:18 +05:30
2016-01-29 22:53:50 +05:30
@raw_lines = @diff_lines.map(&:text)
end
def highlight
2021-04-29 21:17:54 +05:30
populate_marker_ranges if Feature.enabled?(:use_marker_ranges, project, default_enabled: :yaml)
@diff_lines.map.with_index do |diff_line, index|
2016-01-29 22:53:50 +05:30
diff_line = diff_line.dup
# ignore highlighting for "match" lines
2016-08-24 12:49:21 +05:30
next diff_line if diff_line.meta?
2016-01-29 22:53:50 +05:30
2021-04-29 21:17:54 +05:30
rich_line = apply_syntax_highlight(diff_line)
rich_line = apply_marker_ranges_highlight(diff_line, rich_line, index)
2016-01-29 22:53:50 +05:30
2018-11-08 19:23:39 +05:30
diff_line.rich_text = rich_line
2016-01-29 22:53:50 +05:30
diff_line
end
end
private
2021-04-29 21:17:54 +05:30
def populate_marker_ranges
pair_selector = Gitlab::Diff::PairSelector.new(@raw_lines)
pair_selector.each do |old_index, new_index|
old_line = diff_lines[old_index]
new_line = diff_lines[new_index]
old_diffs, new_diffs = Gitlab::Diff::InlineDiff.new(old_line.text, new_line.text, offset: 1).inline_diffs
old_line.set_marker_ranges(old_diffs)
new_line.set_marker_ranges(new_diffs)
end
end
def apply_syntax_highlight(diff_line)
highlight_line(diff_line) || ERB::Util.html_escape(diff_line.text)
end
def apply_marker_ranges_highlight(diff_line, rich_line, index)
marker_ranges = if Feature.enabled?(:use_marker_ranges, project, default_enabled: :yaml)
diff_line.marker_ranges
else
inline_diffs[index]
end
return rich_line if marker_ranges.blank?
begin
InlineDiffMarker.new(diff_line.text, rich_line).mark(marker_ranges)
# 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
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e, issue_url: 'https://gitlab.com/gitlab-org/gitlab-foss/issues/45441')
end
end
2016-04-02 18:10:28 +05:30
def highlight_line(diff_line)
return unless diff_file && diff_file.diff_refs
2021-06-08 01:23:25 +05:30
return diff_line_highlighting(diff_line, plain: true) if blobs_too_large?
2016-01-29 22:53:50 +05:30
2021-04-29 21:17:54 +05:30
if Feature.enabled?(:diff_line_syntax_highlighting, project, default_enabled: :yaml)
diff_line_highlighting(diff_line)
else
blob_highlighting(diff_line)
end
end
2021-06-08 01:23:25 +05:30
def diff_line_highlighting(diff_line, plain: false)
2021-04-29 21:17:54 +05:30
rich_line = syntax_highlighter(diff_line).highlight(
diff_line.text(prefix: false),
2021-06-08 01:23:25 +05:30
plain: plain,
2021-04-29 21:17:54 +05:30
context: { line_number: diff_line.line }
2021-06-08 01:23:25 +05:30
)
2021-04-29 21:17:54 +05:30
# Only update text if line is found. This will prevent
# issues with submodules given the line only exists in diff content.
if rich_line
2021-06-08 01:23:25 +05:30
line_prefix = diff_line.text =~ PREFIX_REGEXP ? Regexp.last_match(1) : ' '
2021-04-29 21:17:54 +05:30
rich_line.prepend(line_prefix).concat("\n")
end
end
def syntax_highlighter(diff_line)
path = diff_line.removed? ? diff_file.old_path : diff_file.new_path
@syntax_highlighter ||= {}
@syntax_highlighter[path] ||= Gitlab::Highlight.new(
path,
@raw_lines,
language: repository&.gitattribute(path, 'gitlab-language')
)
end
# Deprecated: https://gitlab.com/gitlab-org/gitlab/-/issues/324159
# ------------------------------------------------------------------------
def blob_highlighting(diff_line)
2016-08-24 12:49:21 +05:30
rich_line =
if diff_line.unchanged? || diff_line.added?
2017-09-10 17:25:29 +05:30
new_lines[diff_line.new_pos - 1]&.html_safe
2016-08-24 12:49:21 +05:30
elsif diff_line.removed?
2017-09-10 17:25:29 +05:30
old_lines[diff_line.old_pos - 1]&.html_safe
2016-08-24 12:49:21 +05:30
end
2016-01-29 22:53:50 +05:30
# Only update text if line is found. This will prevent
# issues with submodules given the line only exists in diff content.
2016-09-13 17:45:13 +05:30
if rich_line
2021-06-08 01:23:25 +05:30
line_prefix = diff_line.text =~ PREFIX_REGEXP ? Regexp.last_match(1) : ' '
2016-09-13 17:45:13 +05:30
"#{line_prefix}#{rich_line}".html_safe
end
2016-01-29 22:53:50 +05:30
end
2021-04-29 21:17:54 +05:30
# Deprecated: https://gitlab.com/gitlab-org/gitlab/-/issues/324638
# ------------------------------------------------------------------------
2016-01-29 22:53:50 +05:30
def inline_diffs
2021-04-17 20:07:23 +05:30
@inline_diffs ||= InlineDiff.for_lines(@raw_lines)
2016-01-29 22:53:50 +05:30
end
def old_lines
2017-09-10 17:25:29 +05:30
@old_lines ||= highlighted_blob_lines(diff_file.old_blob)
2016-01-29 22:53:50 +05:30
end
def new_lines
2017-09-10 17:25:29 +05:30
@new_lines ||= highlighted_blob_lines(diff_file.new_blob)
end
def highlighted_blob_lines(blob)
return [] unless blob
blob.load_all_data!
2018-12-13 13:39:08 +05:30
blob.present.highlight.lines
2016-01-29 22:53:50 +05:30
end
2021-06-08 01:23:25 +05:30
def blobs_too_large?
return false unless Feature.enabled?(:limited_diff_highlighting, project, default_enabled: :yaml)
return true if Gitlab::Highlight.too_large?(diff_file.old_blob&.size)
Gitlab::Highlight.too_large?(diff_file.new_blob&.size)
end
2016-01-29 22:53:50 +05:30
end
end
end