summaryrefslogtreecommitdiff
path: root/lib/gitlab/diff/position.rb
diff options
context:
space:
mode:
authorFilipa Lacerda <filipa@gitlab.com>2017-10-10 09:05:53 +0100
committerFilipa Lacerda <filipa@gitlab.com>2017-10-10 09:05:53 +0100
commitabdfe58503d26137ee0cb3cc17dcbdedcb93d57c (patch)
tree4f1132f9aa61d5ecb53ebe7fe16b531e5968b232 /lib/gitlab/diff/position.rb
parent058381b6a5a331a85389d12e032117621bab19cc (diff)
parent43b692cb4b43a476862fb6e7bf4c09edd6035077 (diff)
downloadgitlab-ce-abdfe58503d26137ee0cb3cc17dcbdedcb93d57c.tar.gz
Merge branch 'master' into 38869-ci-global38869-ci-global
* master: (116 commits) Fix bad type checking to prevent 0 count badge to be shown fix incorrect description for advanced settings section of project settings Introduce new hook data builders for Issue and MergeRequest Don't create todos for old issue assignees Start adding Gitlab::HookData::IssuableBuilder Include the changes in issuable webhook payloads Rename the `codeclimate` job to `codequality` Don't show an "Unsubscribe" link in snippet comment notifications Add QA::Scenario::Gitlab::Group::Create Removes CommitsList from global namespace Fix wiki empty page translation namespace not being removed Fixes mini graph in commit view Fix link to new i18n index page Update i18n docs Move i18n/introduction to i18n/index Resolve "Simple documentation update - backup to restore in restore section" Remove AjaxLoadingSpinner and CreateLabelDropdown from global namespace Move cycle analytics banner into a vue file Updated Icons + Fix for Collapsed Groups Angle Don't create fork networks for root projects that are deleted ...
Diffstat (limited to 'lib/gitlab/diff/position.rb')
-rw-r--r--lib/gitlab/diff/position.rb90
1 files changed, 38 insertions, 52 deletions
diff --git a/lib/gitlab/diff/position.rb b/lib/gitlab/diff/position.rb
index b8db3adef0a..bd0a9502a5e 100644
--- a/lib/gitlab/diff/position.rb
+++ b/lib/gitlab/diff/position.rb
@@ -1,37 +1,25 @@
-# Defines a specific location, identified by paths and line numbers,
+# Defines a specific location, identified by paths line numbers and image coordinates,
# within a specific diff, identified by start, head and base commit ids.
module Gitlab
module Diff
class Position
- attr_reader :old_path
- attr_reader :new_path
- attr_reader :old_line
- attr_reader :new_line
- attr_reader :base_sha
- attr_reader :start_sha
- attr_reader :head_sha
-
+ attr_accessor :formatter
+
+ delegate :old_path,
+ :new_path,
+ :base_sha,
+ :start_sha,
+ :head_sha,
+ :old_line,
+ :new_line,
+ :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
def initialize(attrs = {})
- if diff_file = attrs[:diff_file]
- attrs[:diff_refs] = diff_file.diff_refs
- attrs[:old_path] = diff_file.old_path
- attrs[:new_path] = diff_file.new_path
- end
-
- if diff_refs = attrs[:diff_refs]
- attrs[:base_sha] = diff_refs.base_sha
- attrs[:start_sha] = diff_refs.start_sha
- attrs[:head_sha] = diff_refs.head_sha
- end
-
- @old_path = attrs[:old_path]
- @new_path = attrs[:new_path]
- @base_sha = attrs[:base_sha]
- @start_sha = attrs[:start_sha]
- @head_sha = attrs[:head_sha]
-
- @old_line = attrs[:old_line]
- @new_line = attrs[:new_line]
+ @formatter = get_formatter_class(attrs[:position_type]).new(attrs)
end
# `Gitlab::Diff::Position` objects are stored as serialized attributes in
@@ -46,7 +34,11 @@ module Gitlab
end
def encode_with(coder)
- coder['attributes'] = self.to_h
+ coder['attributes'] = formatter.to_h
+ end
+
+ def key
+ formatter.key
end
def ==(other)
@@ -54,20 +46,11 @@ module Gitlab
other.diff_refs == diff_refs &&
other.old_path == old_path &&
other.new_path == new_path &&
- other.old_line == old_line &&
- other.new_line == new_line
+ other.formatter == formatter
end
def to_h
- {
- old_path: old_path,
- new_path: new_path,
- old_line: old_line,
- new_line: new_line,
- base_sha: base_sha,
- start_sha: start_sha,
- head_sha: head_sha
- }
+ formatter.to_h
end
def inspect
@@ -75,23 +58,15 @@ module Gitlab
end
def complete?
- file_path.present? &&
- (old_line || new_line) &&
- diff_refs.complete?
+ file_path.present? && formatter.complete? && diff_refs.complete?
end
def to_json(opts = nil)
- JSON.generate(self.to_h, opts)
+ JSON.generate(formatter.to_h, opts)
end
def type
- if old_line && new_line
- nil
- elsif new_line
- 'new'
- else
- 'old'
- end
+ formatter.line_age
end
def unchanged?
@@ -150,6 +125,17 @@ module Gitlab
diff_refs.compare_in(repository.project).diffs(paths: paths, expanded: true).diff_files.first
end
+
+ def get_formatter_class(type)
+ type ||= "text"
+
+ case type
+ when 'image'
+ Gitlab::Diff::Formatters::ImageFormatter
+ else
+ Gitlab::Diff::Formatters::TextFormatter
+ end
+ end
end
end
end