Class: Gitlab::Diff::Position

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/diff/position.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Position

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


29
30
31
# File 'lib/gitlab/diff/position.rb', line 29

def initialize(attrs = {})
  @formatter = get_formatter_class(attrs[:position_type]).new(attrs)
end

Instance Attribute Details

#formatterObject

Returns the value of attribute formatter.


8
9
10
# File 'lib/gitlab/diff/position.rb', line 8

def formatter
  @formatter
end

Instance Method Details

#==(other) ⇒ Object


52
53
54
55
56
57
58
# File 'lib/gitlab/diff/position.rb', line 52

def ==(other)
  other.is_a?(self.class) &&
    other.diff_refs == diff_refs &&
    other.old_path == old_path &&
    other.new_path == new_path &&
    other.formatter == formatter
end

#added?Boolean

Returns:

  • (Boolean)

92
93
94
# File 'lib/gitlab/diff/position.rb', line 92

def added?
  type == 'new'
end

#as_json(opts = nil) ⇒ Object


76
77
78
# File 'lib/gitlab/diff/position.rb', line 76

def as_json(opts = nil)
  to_h.as_json(opts)
end

#complete?Boolean

Returns:

  • (Boolean)

68
69
70
# File 'lib/gitlab/diff/position.rb', line 68

def complete?
  file_path.present? && formatter.complete? && diff_refs.complete?
end

#diff_file(repository) ⇒ Object


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/gitlab/diff/position.rb', line 116

def diff_file(repository)
  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
    }

    # Takes action when creating diff notes (multiple calls are
    # submitted to this method).
    Gitlab::SafeRequestStore.fetch(key) { find_diff_file(repository) }
  end

  # We need to unfold diff lines according to the position in order
  # to correctly calculate the line code and trace position changes.
  @diff_file&.tap { |file| file.unfold_diff_lines(self) }
end

#diff_line(repository) ⇒ Object


141
142
143
# File 'lib/gitlab/diff/position.rb', line 141

def diff_line(repository)
  @diff_line ||= diff_file(repository)&.line_for_position(self)
end

#diff_optionsObject


137
138
139
# File 'lib/gitlab/diff/position.rb', line 137

def diff_options
  { paths: paths, expanded: true, include_stats: false }
end

#diff_refsObject


108
109
110
# File 'lib/gitlab/diff/position.rb', line 108

def diff_refs
  @diff_refs ||= DiffRefs.new(base_sha: base_sha, start_sha: start_sha, head_sha: head_sha)
end

#encode_with(coder) ⇒ Object


44
45
46
# File 'lib/gitlab/diff/position.rb', line 44

def encode_with(coder)
  coder['attributes'] = formatter.to_h
end

#file_hashObject


149
150
151
# File 'lib/gitlab/diff/position.rb', line 149

def file_hash
  @file_hash ||= Digest::SHA1.hexdigest(file_path)
end

#file_pathObject


104
105
106
# File 'lib/gitlab/diff/position.rb', line 104

def file_path
  new_path.presence || old_path
end

#find_diff_file_from(diffable) ⇒ Object


161
162
163
164
165
166
167
168
169
# File 'lib/gitlab/diff/position.rb', line 161

def find_diff_file_from(diffable)
  diff_files = diffable.diffs(diff_options).diff_files

  if Feature.enabled?(:file_identifier_hash) && file_identifier_hash.present?
    diff_files.find { |df| df.file_identifier_hash == file_identifier_hash }
  else
    diff_files.first
  end
end

#init_with(coder) ⇒ Object

`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.


38
39
40
41
42
# File 'lib/gitlab/diff/position.rb', line 38

def init_with(coder)
  initialize(coder['attributes'])

  self
end

#inspectObject


64
65
66
# File 'lib/gitlab/diff/position.rb', line 64

def inspect
  %(#<#{self.class}:#{object_id} #{to_h}>)
end

#keyObject


48
49
50
# File 'lib/gitlab/diff/position.rb', line 48

def key
  formatter.key
end

#line_code(repository) ⇒ Object


145
146
147
# File 'lib/gitlab/diff/position.rb', line 145

def line_code(repository)
  @line_code ||= diff_file(repository)&.line_code_for_position(self)
end

#multiline?Boolean

Returns:

  • (Boolean)

171
172
173
174
175
# File 'lib/gitlab/diff/position.rb', line 171

def multiline?
  return unless on_text? && line_range

  line_range['start'] != line_range['end']
end

#on_image?Boolean

Returns:

  • (Boolean)

153
154
155
# File 'lib/gitlab/diff/position.rb', line 153

def on_image?
  position_type == 'image'
end

#on_text?Boolean

Returns:

  • (Boolean)

157
158
159
# File 'lib/gitlab/diff/position.rb', line 157

def on_text?
  position_type == 'text'
end

#pathsObject


100
101
102
# File 'lib/gitlab/diff/position.rb', line 100

def paths
  [old_path, new_path].compact.uniq
end

#removed?Boolean

Returns:

  • (Boolean)

96
97
98
# File 'lib/gitlab/diff/position.rb', line 96

def removed?
  type == 'old'
end

#to_hObject


60
61
62
# File 'lib/gitlab/diff/position.rb', line 60

def to_h
  formatter.to_h
end

#to_json(opts = nil) ⇒ Object


72
73
74
# File 'lib/gitlab/diff/position.rb', line 72

def to_json(opts = nil)
  Gitlab::Json.generate(formatter.to_h, opts)
end

#typeObject


80
81
82
# File 'lib/gitlab/diff/position.rb', line 80

def type
  formatter.line_age
end

#unchanged?Boolean

Returns:

  • (Boolean)

88
89
90
# File 'lib/gitlab/diff/position.rb', line 88

def unchanged?
  type.nil?
end

#unfoldable?Boolean

Returns:

  • (Boolean)

84
85
86
# File 'lib/gitlab/diff/position.rb', line 84

def unfoldable?
  on_text? && unchanged?
end

#unfolded_diff?(repository) ⇒ Boolean

Returns:

  • (Boolean)

112
113
114
# File 'lib/gitlab/diff/position.rb', line 112

def unfolded_diff?(repository)
  diff_file(repository)&.unfolded?
end