file.rb 7.1 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2 3
module Gitlab
  module Diff
    class File
4
      attr_reader :diff, :repository, :diff_refs, :fallback_diff_refs
D
Dmitriy Zaporozhets 已提交
5

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

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

23
      def initialize(diff, repository:, diff_refs: nil, fallback_diff_refs: nil)
D
Dmitriy Zaporozhets 已提交
24
        @diff = diff
25
        @repository = repository
26
        @diff_refs = diff_refs
27
        @fallback_diff_refs = fallback_diff_refs
28 29
      end

D
Douwe Maan 已提交
30 31 32 33 34 35 36 37
      def position(line)
        return unless diff_refs

        Position.new(
          old_path: old_path,
          new_path: new_path,
          old_line: line.old_line,
          new_line: line.new_line,
38
          diff_refs: diff_refs
D
Douwe Maan 已提交
39 40 41
        )
      end

42 43 44 45 46 47 48 49 50 51
      def line_code(line)
        return if line.meta?

        Gitlab::Diff::LineCode.generate(file_path, line.new_pos, line.old_pos)
      end

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

D
Douwe Maan 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65
      def line_for_position(pos)
        diff_lines.find { |line| position(line) == pos }
      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

66 67 68 69 70 71 72 73
      def old_sha
        diff_refs&.base_sha
      end

      def new_sha
        diff_refs&.head_sha
      end

74 75 76
      def new_content_sha
        return if deleted_file?
        return @new_content_sha if defined?(@new_content_sha)
77 78

        refs = diff_refs || fallback_diff_refs
79
        @new_content_sha = refs&.head_sha
80 81 82 83 84
      end

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

86 87
        refs = diff_refs || fallback_diff_refs
        @old_content_sha = refs&.base_sha
D
Douwe Maan 已提交
88 89
      end

90 91
      def new_blob
        return @new_blob if defined?(@new_blob)
92

93 94
        sha = new_content_sha
        return @new_blob = nil unless sha
95

96
        @new_blob = repository.blob_at(sha, file_path)
97 98
      end

99 100 101 102 103 104 105
      def old_blob
        return @old_blob if defined?(@old_blob)

        sha = old_content_sha
        return @old_blob = nil unless sha

        @old_blob = repository.blob_at(sha, old_path)
D
Dmitriy Zaporozhets 已提交
106 107
      end

108 109 110 111 112 113 114 115
      def content_sha
        new_content_sha || old_content_sha
      end

      def blob
        new_blob || old_blob
      end

116
      attr_writer :highlighted_diff_lines
117

118
      # Array of Gitlab::Diff::Line objects
D
Dmitriy Zaporozhets 已提交
119
      def diff_lines
120
        @diff_lines ||= Gitlab::Diff::Parser.new.parse(raw_diff.each_line).to_a
121 122
      end

123
      def highlighted_diff_lines
124
        @highlighted_diff_lines ||= Gitlab::Diff::Highlight.new(self, repository: self.repository).highlight
125 126
      end

127
      # Array[<Hash>] with right/left keys that contains Gitlab::Diff::Line objects which text is hightlighted
128
      def parallel_diff_lines
129
        @parallel_diff_lines ||= Gitlab::Diff::ParallelDiff.new(self).parallelize
130 131
      end

132
      def raw_diff
133
        diff.diff.to_s
134 135
      end

D
Dmitriy Zaporozhets 已提交
136 137 138 139 140
      def next_line(index)
        diff_lines[index + 1]
      end

      def prev_line(index)
141
        diff_lines[index - 1] if index > 0
D
Dmitriy Zaporozhets 已提交
142
      end
143

D
Douwe Maan 已提交
144 145 146 147
      def paths
        [old_path, new_path].compact
      end

148
      def file_path
149
        new_path.presence || old_path
150
      end
151 152

      def added_lines
G
Gabriel Mazetto 已提交
153
        diff_lines.count(&:added?)
154 155 156
      end

      def removed_lines
G
Gabriel Mazetto 已提交
157
        diff_lines.count(&:removed?)
158
      end
D
Douwe Maan 已提交
159

160
      def file_identifier
161
        "#{file_path}-#{new_file?}-#{deleted_file?}-#{renamed_file?}"
162
      end
163 164 165 166 167 168 169 170 171 172 173 174

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

      def binary?
        old_blob&.binary? || new_blob&.binary?
      end

      def text?
        !binary?
      end
D
Douwe Maan 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188

      def external_storage_error?
        old_blob&.external_storage_error? || new_blob&.external_storage_error?
      end

      def stored_externally?
        old_blob&.stored_externally? || new_blob&.stored_externally?
      end

      def external_storage
        old_blob&.external_storage || new_blob&.external_storage
      end

      def content_changed?
189 190 191 192
        return blobs_changed? if diff_refs
        return false if new_file? || deleted_file? || renamed_file?

        text? && diff_lines.any?
D
Douwe Maan 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
      end

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

      def size
        [old_blob&.size, new_blob&.size].compact.sum
      end

      def raw_size
        [old_blob&.raw_size, new_blob&.raw_size].compact.sum
      end

      def raw_binary?
        old_blob&.raw_binary? || new_blob&.raw_binary?
      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

      private

231 232 233 234
      def blobs_changed?
        old_blob && new_blob && old_blob.id != new_blob.id
      end

D
Douwe Maan 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
      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
260 261
        else
          DiffViewer::NoPreview
D
Douwe Maan 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
        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 已提交
278 279 280
    end
  end
end