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

D
Dmitriy Zaporozhets 已提交
3 4 5
module Gitlab
  module Diff
    class File
6 7
      include Gitlab::Utils::StrongMemoize

8
      attr_reader :diff, :repository, :diff_refs, :fallback_diff_refs, :unique_identifier
D
Dmitriy Zaporozhets 已提交
9

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

      # 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 已提交
26

27 28 29 30 31 32 33 34
      def initialize(
        diff,
        repository:,
        diff_refs: nil,
        fallback_diff_refs: nil,
        stats: nil,
        unique_identifier: nil)

D
Dmitriy Zaporozhets 已提交
35
        @diff = diff
36
        @stats = stats
37
        @repository = repository
38
        @diff_refs = diff_refs
39
        @fallback_diff_refs = fallback_diff_refs
40
        @unique_identifier = unique_identifier
41
        @unfolded = false
42 43

        # Ensure items are collected in the the batch
44 45
        new_blob_lazy
        old_blob_lazy
46 47
      end

F
Felipe Artur 已提交
48
      def position(position_marker, position_type: :text)
D
Douwe Maan 已提交
49 50
        return unless diff_refs

F
Felipe Artur 已提交
51 52 53
        data = {
          diff_refs: diff_refs,
          position_type: position_type.to_s,
D
Douwe Maan 已提交
54
          old_path: old_path,
F
Felipe Artur 已提交
55 56 57 58 59 60 61 62 63 64
          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 已提交
65 66
      end

67 68 69
      def line_code(line)
        return if line.meta?

70
        Gitlab::Git.diff_line_code(file_path, line.new_pos, line.old_pos)
71 72 73 74 75 76
      end

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

D
Douwe Maan 已提交
77
      def line_for_position(pos)
78
        return unless pos.position_type == 'text'
79

80 81 82 83 84 85 86 87 88
        # This method is normally used to find which line the diff was
        # commented on, and in this context, it's normally the raw diff persisted
        # at `note_diff_files`, which is a fraction of the entire diff
        # (it goes from the first line, to the commented line, or
        # one line below). Therefore it's more performant to fetch
        # from bottom to top instead of the other way around.
        diff_lines
          .reverse_each
          .find { |line| line.old_line == pos.old_line && line.new_line == pos.new_line }
D
Douwe Maan 已提交
89 90 91 92 93 94 95 96 97 98 99 100
      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

101 102
      # Returns the raw diff content up to the given line index
      def diff_hunk(diff_line)
103 104 105 106 107 108
        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")
109 110
      end

111 112 113 114 115 116 117 118
      def old_sha
        diff_refs&.base_sha
      end

      def new_sha
        diff_refs&.head_sha
      end

119 120 121
      def new_content_sha
        return if deleted_file?
        return @new_content_sha if defined?(@new_content_sha)
122 123

        refs = diff_refs || fallback_diff_refs
124
        @new_content_sha = refs&.head_sha
125 126 127 128 129
      end

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

131 132
        refs = diff_refs || fallback_diff_refs
        @old_content_sha = refs&.base_sha
D
Douwe Maan 已提交
133 134
      end

135
      def new_blob
136 137 138
        strong_memoize(:new_blob) do
          new_blob_lazy&.itself
        end
139 140
      end

141
      def old_blob
142 143 144
        strong_memoize(:old_blob) do
          old_blob_lazy&.itself
        end
D
Dmitriy Zaporozhets 已提交
145 146
      end

147 148 149 150 151 152 153 154 155 156
      def new_blob_lines_between(from_line, to_line)
        return [] unless new_blob

        from_index = from_line - 1
        to_index = to_line - 1

        new_blob.load_all_data!
        new_blob.data.lines[from_index..to_index]
      end

157 158 159 160 161
      def content_sha
        new_content_sha || old_content_sha
      end

      def blob
162
        new_blob || old_blob
163 164
      end

165 166 167 168
      def highlighted_diff_lines=(value)
        clear_memoization(:diff_lines_for_serializer)
        @highlighted_diff_lines = value
      end
169

170
      # Array of Gitlab::Diff::Line objects
D
Dmitriy Zaporozhets 已提交
171
      def diff_lines
F
Felipe Artur 已提交
172 173
        @diff_lines ||=
          Gitlab::Diff::Parser.new.parse(raw_diff.each_line, diff_file: self).to_a
174 175
      end

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
      # 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

194 195 196 197
      def highlight_loaded?
        @highlighted_diff_lines.present?
      end

198
      def highlighted_diff_lines
F
Felipe Artur 已提交
199 200
        @highlighted_diff_lines ||=
          Gitlab::Diff::Highlight.new(self, repository: self.repository).highlight
201 202
      end

203
      # Array[<Hash>] with right/left keys that contains Gitlab::Diff::Line objects which text is hightlighted
204
      def parallel_diff_lines
205
        @parallel_diff_lines ||= Gitlab::Diff::ParallelDiff.new(self).parallelize
206 207
      end

208
      def raw_diff
209
        diff.diff.to_s
210 211
      end

D
Dmitriy Zaporozhets 已提交
212 213 214 215 216
      def next_line(index)
        diff_lines[index + 1]
      end

      def prev_line(index)
217
        diff_lines[index - 1] if index > 0
D
Dmitriy Zaporozhets 已提交
218
      end
219

D
Douwe Maan 已提交
220 221 222 223
      def paths
        [old_path, new_path].compact
      end

224
      def file_path
225
        new_path.presence || old_path
226
      end
227 228

      def added_lines
229
        @stats&.additions || diff_lines.count(&:added?)
230 231 232
      end

      def removed_lines
233
        @stats&.deletions || diff_lines.count(&:removed?)
234
      end
D
Douwe Maan 已提交
235

236
      def file_identifier
237
        "#{file_path}-#{new_file?}-#{deleted_file?}-#{renamed_file?}"
238
      end
239 240 241 242 243

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

244 245
      def binary_in_repo?
        has_binary_notice? || try_blobs(:binary_in_repo?)
246 247
      end

248 249
      def text_in_repo?
        !binary_in_repo?
250
      end
D
Douwe Maan 已提交
251 252

      def external_storage_error?
253
        try_blobs(:external_storage_error?)
D
Douwe Maan 已提交
254 255 256
      end

      def stored_externally?
257
        try_blobs(:stored_externally?)
D
Douwe Maan 已提交
258 259 260
      end

      def external_storage
261
        try_blobs(:external_storage)
D
Douwe Maan 已提交
262 263 264
      end

      def content_changed?
265 266 267 268
        return blobs_changed? if diff_refs
        return false if new_file? || deleted_file? || renamed_file?

        text? && diff_lines.any?
D
Douwe Maan 已提交
269 270 271 272 273 274
      end

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

275
      # rubocop: disable CodeReuse/ActiveRecord
D
Douwe Maan 已提交
276
      def size
277
        valid_blobs.map(&:size).sum
D
Douwe Maan 已提交
278
      end
279
      # rubocop: enable CodeReuse/ActiveRecord
D
Douwe Maan 已提交
280

281
      # rubocop: disable CodeReuse/ActiveRecord
D
Douwe Maan 已提交
282
      def raw_size
283
        valid_blobs.map(&:raw_size).sum
D
Douwe Maan 已提交
284
      end
285
      # rubocop: enable CodeReuse/ActiveRecord
D
Douwe Maan 已提交
286

287 288 289 290
      def empty?
        valid_blobs.map(&:empty?).all?
      end

291 292 293 294
      def binary?
        strong_memoize(:is_binary) do
          try_blobs(:binary?)
        end
D
Douwe Maan 已提交
295 296
      end

297 298 299 300
      def text?
        strong_memoize(:is_text) do
          !binary? && !different_type?
        end
D
Douwe Maan 已提交
301 302
      end

303 304 305 306
      def viewer
        rich_viewer || simple_viewer
      end

D
Douwe Maan 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320
      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 已提交
321 322 323
      # 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
324 325
        strong_memoize(:diff_lines_for_serializer) do
          lines = highlighted_diff_lines
F
Felipe Artur 已提交
326

327 328
          next if lines.empty?
          next if blob.nil?
F
Felipe Artur 已提交
329

330
          last_line = lines.last
F
Felipe Artur 已提交
331

332 333 334 335
          if last_line.new_pos < total_blob_lines(blob) && !deleted_file?
            match_line = Gitlab::Diff::Line.new("", 'match', nil, last_line.old_pos, last_line.new_pos)
            lines.push(match_line)
          end
F
Felipe Artur 已提交
336

337 338
          lines
        end
F
Felipe Artur 已提交
339 340
      end

341 342 343 344 345 346 347 348 349 350
      def fully_expanded?
        return true if binary?

        lines = diff_lines_for_serializer

        return true if lines.nil?

        lines.none? { |line| line.type.to_s == 'match' }
      end

D
Douwe Maan 已提交
351 352
      private

F
Felipe Artur 已提交
353 354 355 356 357 358 359 360
      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

361 362
      # We can't use Object#try because Blob doesn't inherit from Object, but
      # from BasicObject (via SimpleDelegator).
363
      def try_blobs(meth)
364
        old_blob&.public_send(meth) || new_blob&.public_send(meth)
365 366 367
      end

      def valid_blobs
368
        [old_blob, new_blob].compact
369 370
      end

F
Felipe Artur 已提交
371 372 373 374 375 376 377 378
      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

379 380 381 382
      def blobs_changed?
        old_blob && new_blob && old_blob.id != new_blob.id
      end

383 384 385 386 387 388 389 390 391 392 393 394
      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 已提交
395 396 397 398
      def simple_viewer_class
        return DiffViewer::NotDiffable unless diffable?

        if content_changed?
399
          if text?
D
Douwe Maan 已提交
400 401 402 403 404
            DiffViewer::Text
          else
            DiffViewer::NoPreview
          end
        elsif new_file?
405
          if text?
D
Douwe Maan 已提交
406 407 408 409 410
            DiffViewer::Text
          else
            DiffViewer::Added
          end
        elsif deleted_file?
411
          if text?
D
Douwe Maan 已提交
412 413 414 415 416 417 418 419
            DiffViewer::Text
          else
            DiffViewer::Deleted
          end
        elsif renamed_file?
          DiffViewer::Renamed
        elsif mode_changed?
          DiffViewer::ModeChanged
420 421
        else
          DiffViewer::NoPreview
D
Douwe Maan 已提交
422 423 424 425 426 427 428 429 430 431
        end
      end

      def rich_viewer_class
        viewer_class_from(RICH_VIEWERS)
      end

      def viewer_class_from(classes)
        return unless diffable?
        return unless new_file? || deleted_file? || content_changed?
432
        return if different_type? || external_storage_error?
D
Douwe Maan 已提交
433 434 435 436 437

        verify_binary = !stored_externally?

        classes.find { |viewer_class| viewer_class.can_render?(self, verify_binary: verify_binary) }
      end
D
Dmitriy Zaporozhets 已提交
438 439 440
    end
  end
end