file.rb 9.8 KB
Newer Older
1 2
# frozen_string_literal: true

D
Dmitriy Zaporozhets 已提交
3 4 5
module Gitlab
  module Diff
    class File
6
      attr_reader :diff, :repository, :diff_refs, :fallback_diff_refs
D
Dmitriy Zaporozhets 已提交
7

8 9
      delegate :new_file?, :deleted_file?, :renamed_file?,
        :old_path, :new_path, :a_mode, :b_mode, :mode_changed?,
10
        :submodule?, :expanded?, :too_large?, :collapsed?, :line_count, :has_binary_notice?, to: :diff, prefix: false
D
Douwe Maan 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23

      # Finding a viewer for a diff file happens based only on extension and whether the
      # diff file blobs are binary or text, which means 1 diff file should only be matched by 1 viewer,
      # and the order of these viewers doesn't really matter.
      #
      # However, when the diff file blobs are LFS pointers, we cannot know for sure whether the
      # file being pointed to is binary or text. In this case, we match only on
      # extension, preferring binary viewers over text ones if both exist, since the
      # large files referred to in "Large File Storage" are much more likely to be
      # binary than text.
      RICH_VIEWERS = [
        DiffViewer::Image
      ].sort_by { |v| v.binary? ? 0 : 1 }.freeze
D
Dmitriy Zaporozhets 已提交
24

25
      def initialize(diff, repository:, diff_refs: nil, fallback_diff_refs: nil, stats: nil)
D
Dmitriy Zaporozhets 已提交
26
        @diff = diff
27
        @stats = stats
28
        @repository = repository
29
        @diff_refs = diff_refs
30
        @fallback_diff_refs = fallback_diff_refs
31
        @unfolded = false
32 33

        # Ensure items are collected in the the batch
34 35
        new_blob_lazy
        old_blob_lazy
36 37
      end

F
Felipe Artur 已提交
38
      def position(position_marker, position_type: :text)
D
Douwe Maan 已提交
39 40
        return unless diff_refs

F
Felipe Artur 已提交
41 42 43
        data = {
          diff_refs: diff_refs,
          position_type: position_type.to_s,
D
Douwe Maan 已提交
44
          old_path: old_path,
F
Felipe Artur 已提交
45 46 47 48 49 50 51 52 53 54
          new_path: new_path
        }

        if position_type == :text
          data.merge!(text_position_properties(position_marker))
        else
          data.merge!(image_position_properties(position_marker))
        end

        Position.new(data)
D
Douwe Maan 已提交
55 56
      end

57 58 59
      def line_code(line)
        return if line.meta?

60
        Gitlab::Git.diff_line_code(file_path, line.new_pos, line.old_pos)
61 62 63 64 65 66
      end

      def line_for_line_code(code)
        diff_lines.find { |line| line_code(line) == code }
      end

D
Douwe Maan 已提交
67
      def line_for_position(pos)
68 69 70
        return nil unless pos.position_type == 'text'

        diff_lines.find { |line| line.old_line == pos.old_line && line.new_line == pos.new_line }
D
Douwe Maan 已提交
71 72 73 74 75 76 77 78 79 80 81 82
      end

      def position_for_line_code(code)
        line = line_for_line_code(code)
        position(line) if line
      end

      def line_code_for_position(pos)
        line = line_for_position(pos)
        line_code(line) if line
      end

83 84
      # Returns the raw diff content up to the given line index
      def diff_hunk(diff_line)
85 86 87 88 89 90
        diff_line_index = diff_line.index
        # @@ (match) header is not kept if it's found in the top of the file,
        # therefore we should keep an extra line on this scenario.
        diff_line_index += 1 unless diff_lines.first.match?

        diff_lines.select { |line| line.index <= diff_line_index }.map(&:text).join("\n")
91 92
      end

93 94 95 96 97 98 99 100
      def old_sha
        diff_refs&.base_sha
      end

      def new_sha
        diff_refs&.head_sha
      end

101 102 103
      def new_content_sha
        return if deleted_file?
        return @new_content_sha if defined?(@new_content_sha)
104 105

        refs = diff_refs || fallback_diff_refs
106
        @new_content_sha = refs&.head_sha
107 108 109 110 111
      end

      def old_content_sha
        return if new_file?
        return @old_content_sha if defined?(@old_content_sha)
112

113 114
        refs = diff_refs || fallback_diff_refs
        @old_content_sha = refs&.base_sha
D
Douwe Maan 已提交
115 116
      end

117
      def new_blob
118
        new_blob_lazy&.itself
119 120
      end

121
      def old_blob
122
        old_blob_lazy&.itself
D
Dmitriy Zaporozhets 已提交
123 124
      end

125 126 127 128 129
      def content_sha
        new_content_sha || old_content_sha
      end

      def blob
130
        new_blob || old_blob
131 132
      end

133
      attr_writer :highlighted_diff_lines
134

135
      # Array of Gitlab::Diff::Line objects
D
Dmitriy Zaporozhets 已提交
136
      def diff_lines
F
Felipe Artur 已提交
137 138
        @diff_lines ||=
          Gitlab::Diff::Parser.new.parse(raw_diff.each_line, diff_file: self).to_a
139 140
      end

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      # Changes diff_lines according to the given position. That is,
      # it checks whether the position requires blob lines into the diff
      # in order to be presented.
      def unfold_diff_lines(position)
        return unless position

        unfolder = Gitlab::Diff::LinesUnfolder.new(self, position)

        if unfolder.unfold_required?
          @diff_lines = unfolder.unfolded_diff_lines
          @unfolded = true
        end
      end

      def unfolded?
        @unfolded
      end

159
      def highlighted_diff_lines
F
Felipe Artur 已提交
160 161
        @highlighted_diff_lines ||=
          Gitlab::Diff::Highlight.new(self, repository: self.repository).highlight
162 163
      end

164
      # Array[<Hash>] with right/left keys that contains Gitlab::Diff::Line objects which text is hightlighted
165
      def parallel_diff_lines
166
        @parallel_diff_lines ||= Gitlab::Diff::ParallelDiff.new(self).parallelize
167 168
      end

169
      def raw_diff
170
        diff.diff.to_s
171 172
      end

D
Dmitriy Zaporozhets 已提交
173 174 175 176 177
      def next_line(index)
        diff_lines[index + 1]
      end

      def prev_line(index)
178
        diff_lines[index - 1] if index > 0
D
Dmitriy Zaporozhets 已提交
179
      end
180

D
Douwe Maan 已提交
181 182 183 184
      def paths
        [old_path, new_path].compact
      end

185
      def file_path
186
        new_path.presence || old_path
187
      end
188 189

      def added_lines
190
        @stats&.additions || diff_lines.count(&:added?)
191 192 193
      end

      def removed_lines
194
        @stats&.deletions || diff_lines.count(&:removed?)
195
      end
D
Douwe Maan 已提交
196

197
      def file_identifier
198
        "#{file_path}-#{new_file?}-#{deleted_file?}-#{renamed_file?}"
199
      end
200 201 202 203 204 205

      def diffable?
        repository.attributes(file_path).fetch('diff') { true }
      end

      def binary?
206
        has_binary_notice? || try_blobs(:binary?)
207 208 209 210 211
      end

      def text?
        !binary?
      end
D
Douwe Maan 已提交
212 213

      def external_storage_error?
214
        try_blobs(:external_storage_error?)
D
Douwe Maan 已提交
215 216 217
      end

      def stored_externally?
218
        try_blobs(:stored_externally?)
D
Douwe Maan 已提交
219 220 221
      end

      def external_storage
222
        try_blobs(:external_storage)
D
Douwe Maan 已提交
223 224 225
      end

      def content_changed?
226 227 228 229
        return blobs_changed? if diff_refs
        return false if new_file? || deleted_file? || renamed_file?

        text? && diff_lines.any?
D
Douwe Maan 已提交
230 231 232 233 234 235
      end

      def different_type?
        old_blob && new_blob && old_blob.binary? != new_blob.binary?
      end

236
      # rubocop: disable CodeReuse/ActiveRecord
D
Douwe Maan 已提交
237
      def size
238
        valid_blobs.map(&:size).sum
D
Douwe Maan 已提交
239
      end
240
      # rubocop: enable CodeReuse/ActiveRecord
D
Douwe Maan 已提交
241

242
      # rubocop: disable CodeReuse/ActiveRecord
D
Douwe Maan 已提交
243
      def raw_size
244
        valid_blobs.map(&:raw_size).sum
D
Douwe Maan 已提交
245
      end
246
      # rubocop: enable CodeReuse/ActiveRecord
D
Douwe Maan 已提交
247 248

      def raw_binary?
249
        try_blobs(:raw_binary?)
D
Douwe Maan 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
      end

      def raw_text?
        !raw_binary? && !different_type?
      end

      def simple_viewer
        @simple_viewer ||= simple_viewer_class.new(self)
      end

      def rich_viewer
        return @rich_viewer if defined?(@rich_viewer)

        @rich_viewer = rich_viewer_class&.new(self)
      end

      def rendered_as_text?(ignore_errors: true)
        simple_viewer.is_a?(DiffViewer::Text) && (ignore_errors || simple_viewer.render_error.nil?)
      end

F
Felipe Artur 已提交
270 271 272 273 274 275
      # This adds the bottom match line to the array if needed. It contains
      # the data to load more context lines.
      def diff_lines_for_serializer
        lines = highlighted_diff_lines

        return if lines.empty?
276
        return if blob.nil?
F
Felipe Artur 已提交
277 278 279

        last_line = lines.last

280
        if last_line.new_pos < total_blob_lines(blob) && !deleted_file?
F
Felipe Artur 已提交
281 282 283 284 285 286 287
          match_line = Gitlab::Diff::Line.new("", 'match', nil, last_line.old_pos, last_line.new_pos)
          lines.push(match_line)
        end

        lines
      end

D
Douwe Maan 已提交
288 289
      private

F
Felipe Artur 已提交
290 291 292 293 294 295 296 297
      def total_blob_lines(blob)
        @total_lines ||= begin
          line_count = blob.lines.size
          line_count -= 1 if line_count > 0 && blob.lines.last.blank?
          line_count
        end
      end

298 299
      # We can't use Object#try because Blob doesn't inherit from Object, but
      # from BasicObject (via SimpleDelegator).
300
      def try_blobs(meth)
301
        old_blob&.public_send(meth) || new_blob&.public_send(meth)
302 303 304
      end

      def valid_blobs
305
        [old_blob, new_blob].compact
306 307
      end

F
Felipe Artur 已提交
308 309 310 311 312 313 314 315
      def text_position_properties(line)
        { old_line: line.old_line, new_line: line.new_line }
      end

      def image_position_properties(image_point)
        image_point.to_h
      end

316 317 318 319
      def blobs_changed?
        old_blob && new_blob && old_blob.id != new_blob.id
      end

320 321 322 323 324 325 326 327 328 329 330 331
      def new_blob_lazy
        return unless new_content_sha

        Blob.lazy(repository.project, new_content_sha, file_path)
      end

      def old_blob_lazy
        return unless old_content_sha

        Blob.lazy(repository.project, old_content_sha, old_path)
      end

D
Douwe Maan 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
      def simple_viewer_class
        return DiffViewer::NotDiffable unless diffable?

        if content_changed?
          if raw_text?
            DiffViewer::Text
          else
            DiffViewer::NoPreview
          end
        elsif new_file?
          if raw_text?
            DiffViewer::Text
          else
            DiffViewer::Added
          end
        elsif deleted_file?
          if raw_text?
            DiffViewer::Text
          else
            DiffViewer::Deleted
          end
        elsif renamed_file?
          DiffViewer::Renamed
        elsif mode_changed?
          DiffViewer::ModeChanged
357 358
        else
          DiffViewer::NoPreview
D
Douwe Maan 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
        end
      end

      def rich_viewer_class
        viewer_class_from(RICH_VIEWERS)
      end

      def viewer_class_from(classes)
        return unless diffable?
        return if different_type? || external_storage_error?
        return unless new_file? || deleted_file? || content_changed?

        verify_binary = !stored_externally?

        classes.find { |viewer_class| viewer_class.can_render?(self, verify_binary: verify_binary) }
      end
D
Dmitriy Zaporozhets 已提交
375 376 377
    end
  end
end