gitlab-org--gitlab-foss/lib/gitlab/diff/position.rb

177 lines
4.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-10-07 00:25:17 -04:00
# Defines a specific location, identified by paths line numbers and image coordinates,
2016-06-20 13:17:25 -04:00
# within a specific diff, identified by start, head and base commit ids.
module Gitlab
module Diff
class Position
2017-10-07 00:25:17 -04:00
attr_accessor :formatter
delegate :old_path,
:new_path,
:base_sha,
:start_sha,
:head_sha,
:old_line,
:new_line,
:width,
:height,
:x,
:y,
2017-10-07 00:25:17 -04:00
:position_type, to: :formatter
# A position can belong to a text line or to an image coordinate
# it depends of the position_type argument.
# Text position will have: new_line and old_line
# Image position will have: width, height, x, y
2016-06-20 13:17:25 -04:00
def initialize(attrs = {})
2017-10-07 00:25:17 -04:00
@formatter = get_formatter_class(attrs[:position_type]).new(attrs)
2016-06-20 13:17:25 -04:00
end
2016-07-06 19:28:13 -04:00
# `Gitlab::Diff::Position` objects are stored as serialized attributes in
# `DiffNote`, which use YAML to encode and decode objects.
# `#init_with` and `#encode_with` can be used to customize the en/decoding
# behavior. In this case, we override these to prevent memoized instance
# variables like `@diff_file` and `@diff_line` from being serialized.
2016-06-20 13:17:25 -04:00
def init_with(coder)
initialize(coder['attributes'])
self
end
def encode_with(coder)
2017-10-07 00:25:17 -04:00
coder['attributes'] = formatter.to_h
end
def key
formatter.key
2016-06-20 13:17:25 -04:00
end
def ==(other)
other.is_a?(self.class) &&
other.diff_refs == diff_refs &&
other.old_path == old_path &&
other.new_path == new_path &&
2017-10-07 00:25:17 -04:00
other.formatter == formatter
2016-06-20 13:17:25 -04:00
end
def to_h
2017-10-07 00:25:17 -04:00
formatter.to_h
2016-06-20 13:17:25 -04:00
end
def inspect
%(#<#{self.class}:#{object_id} #{to_h}>)
end
def complete?
2017-10-07 00:25:17 -04:00
file_path.present? && formatter.complete? && diff_refs.complete?
2016-06-20 13:17:25 -04:00
end
def to_json(opts = nil)
2017-10-07 00:25:17 -04:00
JSON.generate(formatter.to_h, opts)
2016-06-20 13:17:25 -04:00
end
def as_json(opts = nil)
to_h.as_json(opts)
end
2016-06-20 13:17:25 -04:00
def type
2017-10-07 00:25:17 -04:00
formatter.line_age
2016-06-20 13:17:25 -04:00
end
def unchanged?
type.nil?
end
def added?
type == 'new'
end
def removed?
type == 'old'
end
def paths
[old_path, new_path].compact.uniq
end
def file_path
new_path.presence || old_path
end
def diff_refs
@diff_refs ||= DiffRefs.new(base_sha: base_sha, start_sha: start_sha, head_sha: head_sha)
end
def unfolded_diff?(repository)
diff_file(repository)&.unfolded?
end
2016-06-20 13:17:25 -04:00
def diff_file(repository)
2017-10-30 08:30:31 -04:00
return @diff_file if defined?(@diff_file)
@diff_file = begin
key = {
project_id: repository.project.id,
start_sha: start_sha,
head_sha: head_sha,
path: file_path
}
Gitlab::SafeRequestStore.fetch(key) { find_diff_file(repository) }
2016-06-20 13:17:25 -04:00
end
end
def diff_options
{ paths: paths, expanded: true, include_stats: false }
end
2016-06-20 13:17:25 -04:00
def diff_line(repository)
@diff_line ||= diff_file(repository)&.line_for_position(self)
2016-06-20 13:17:25 -04:00
end
def line_code(repository)
@line_code ||= diff_file(repository)&.line_code_for_position(self)
2016-06-20 13:17:25 -04:00
end
def file_hash
@file_hash ||= Digest::SHA1.hexdigest(file_path)
end
def on_image?
position_type == 'image'
end
def on_text?
position_type == 'text'
end
2016-06-20 13:17:25 -04:00
private
def find_diff_file(repository)
return unless diff_refs.complete?
2017-10-30 08:30:31 -04:00
return unless comparison = diff_refs.compare_in(repository.project)
file = comparison.diffs(diff_options).diff_files.first
# We need to unfold diff lines according to the position in order
# to correctly calculate the line code and trace position changes.
file&.unfold_diff_lines(self)
file
2016-06-20 13:17:25 -04:00
end
2017-10-07 00:25:17 -04:00
def get_formatter_class(type)
type ||= "text"
case type
when 'image'
Gitlab::Diff::Formatters::ImageFormatter
else
Gitlab::Diff::Formatters::TextFormatter
end
end
2016-06-20 13:17:25 -04:00
end
end
end