diff.rb 12.4 KB
Newer Older
Robert Speicher's avatar
Robert Speicher committed
1 2 3 4
# Gitlab::Git::Diff is a wrapper around native Rugged::Diff object
module Gitlab
  module Git
    class Diff
5
      TimeoutError = Class.new(StandardError)
6
      include Gitlab::EncodingHelper
Robert Speicher's avatar
Robert Speicher committed
7 8 9 10 11 12 13

      # Diff properties
      attr_accessor :old_path, :new_path, :a_mode, :b_mode, :diff

      # Stats properties
      attr_accessor :new_file, :renamed_file, :deleted_file

14 15 16 17
      alias_method :new_file?, :new_file
      alias_method :deleted_file?, :deleted_file
      alias_method :renamed_file?, :renamed_file

18
      attr_accessor :expanded
Robert Speicher's avatar
Robert Speicher committed
19

Douwe Maan's avatar
Douwe Maan committed
20 21
      alias_method :expanded?, :expanded

22 23 24
      # We need this accessor because of `to_hash` and `init_from_hash`
      attr_accessor :too_large

25 26 27
      class << self
        # The maximum size of a diff to display.
        def size_limit
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
          if RequestStore.active?
            RequestStore['gitlab_git_diff_size_limit'] ||= find_size_limit
          else
            find_size_limit
          end
        end

        # The maximum size before a diff is collapsed.
        def collapse_limit
          if RequestStore.active?
            RequestStore['gitlab_git_diff_collapse_limit'] ||= find_collapse_limit
          else
            find_collapse_limit
          end
        end

        def find_size_limit
45 46 47 48 49 50
          if Feature.enabled?('gitlab_git_diff_size_limit_increase')
            200.kilobytes
          else
            100.kilobytes
          end
        end
Robert Speicher's avatar
Robert Speicher committed
51

52
        def find_collapse_limit
53 54 55 56 57 58
          if Feature.enabled?('gitlab_git_diff_size_limit_increase')
            100.kilobytes
          else
            10.kilobytes
          end
        end
Robert Speicher's avatar
Robert Speicher committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

        def between(repo, head, base, options = {}, *paths)
          straight = options.delete(:straight) || false

          common_commit = if straight
                            base
                          else
                            # Only show what is new in the source branch
                            # compared to the target branch, not the other way
                            # around. The linex below with merge_base is
                            # equivalent to diff with three dots (git diff
                            # branch1...branch2) From the git documentation:
                            # "git diff A...B" is equivalent to "git diff
                            # $(git-merge-base A B) B"
                            repo.merge_base_commit(head, base)
                          end

          options ||= {}
          actual_options = filter_diff_options(options)
          repo.diff(common_commit, head, actual_options, *paths)
        end

        # Return a copy of the +options+ hash containing only keys that can be
        # passed to Rugged.  Allowed options are:
        #
        #  :max_size ::
        #    An integer specifying the maximum byte size of a file before a it
        #    will be treated as binary. The default value is 512MB.
        #
        #  :context_lines ::
        #    The number of unchanged lines that define the boundary of a hunk
        #    (and to display before and after the actual changes). The default is
        #    3.
        #
        #  :interhunk_lines ::
        #    The maximum number of unchanged lines between hunk boundaries before
        #    the hunks will be merged into a one. The default is 0.
        #
        #  :old_prefix ::
        #    The virtual "directory" to prefix to old filenames in hunk headers.
        #    The default is "a".
        #
        #  :new_prefix ::
        #    The virtual "directory" to prefix to new filenames in hunk headers.
        #    The default is "b".
        #
        #  :reverse ::
        #    If true, the sides of the diff will be reversed.
        #
        #  :force_text ::
        #    If true, all files will be treated as text, disabling binary
        #    attributes & detection.
        #
        #  :ignore_whitespace ::
        #    If true, all whitespace will be ignored.
        #
        #  :ignore_whitespace_change ::
        #    If true, changes in amount of whitespace will be ignored.
        #
        #  :ignore_whitespace_eol ::
        #    If true, whitespace at end of line will be ignored.
        #
        #  :ignore_submodules ::
        #    if true, submodules will be excluded from the diff completely.
        #
        #  :patience ::
        #    If true, the "patience diff" algorithm will be used (currenlty
        #    unimplemented).
        #
        #  :include_ignored ::
        #    If true, ignored files will be included in the diff.
        #
        #  :include_untracked ::
        #   If true, untracked files will be included in the diff.
        #
        #  :include_unmodified ::
        #    If true, unmodified files will be included in the diff.
        #
        #  :recurse_untracked_dirs ::
        #    Even if +:include_untracked+ is true, untracked directories will
        #    only be marked with a single entry in the diff. If this flag is set
        #    to true, all files under ignored directories will be included in the
        #    diff, too.
        #
        #  :disable_pathspec_match ::
        #    If true, the given +*paths+ will be applied as exact matches,
        #    instead of as fnmatch patterns.
        #
        #  :deltas_are_icase ::
        #    If true, filename comparisons will be made with case-insensitivity.
        #
        #  :include_untracked_content ::
        #    if true, untracked content will be contained in the the diff patch
        #    text.
        #
        #  :skip_binary_check ::
        #    If true, diff deltas will be generated without spending time on
        #    binary detection. This is useful to improve performance in cases
        #    where the actual file content difference is not needed.
        #
        #  :include_typechange ::
        #    If true, type changes for files will not be interpreted as deletion
        #    of the "old file" and addition of the "new file", but will generate
        #    typechange records.
        #
        #  :include_typechange_trees ::
        #    Even if +:include_typechange+ is true, blob -> tree changes will
        #    still usually be handled as a deletion of the blob. If this flag is
        #    set to true, blob -> tree changes will be marked as typechanges.
        #
        #  :ignore_filemode ::
        #    If true, file mode changes will be ignored.
        #
        #  :recurse_ignored_dirs ::
        #    Even if +:include_ignored+ is true, ignored directories will only be
        #    marked with a single entry in the diff. If this flag is set to true,
        #    all files under ignored directories will be included in the diff,
        #    too.
        def filter_diff_options(options, default_options = {})
          allowed_options = [:max_size, :context_lines, :interhunk_lines,
                             :old_prefix, :new_prefix, :reverse, :force_text,
                             :ignore_whitespace, :ignore_whitespace_change,
                             :ignore_whitespace_eol, :ignore_submodules,
                             :patience, :include_ignored, :include_untracked,
                             :include_unmodified, :recurse_untracked_dirs,
                             :disable_pathspec_match, :deltas_are_icase,
                             :include_untracked_content, :skip_binary_check,
                             :include_typechange, :include_typechange_trees,
                             :ignore_filemode, :recurse_ignored_dirs, :paths,
Douwe Maan's avatar
Douwe Maan committed
188
                             :max_files, :max_lines, :limits, :expanded]
Robert Speicher's avatar
Robert Speicher committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

          if default_options
            actual_defaults = default_options.dup
            actual_defaults.keep_if do |key|
              allowed_options.include?(key)
            end
          else
            actual_defaults = {}
          end

          if options
            filtered_opts = options.dup
            filtered_opts.keep_if do |key|
              allowed_options.include?(key)
            end
            filtered_opts = actual_defaults.merge(filtered_opts)
          else
            filtered_opts = actual_defaults
          end

          filtered_opts
        end
      end

213 214 215
      def initialize(raw_diff, expanded: true)
        @expanded = expanded

Robert Speicher's avatar
Robert Speicher committed
216 217
        case raw_diff
        when Hash
218
          init_from_hash(raw_diff)
219
          prune_diff_if_eligible
Robert Speicher's avatar
Robert Speicher committed
220
        when Rugged::Patch, Rugged::Diff::Delta
221
          init_from_rugged(raw_diff)
222
        when Gitlab::GitalyClient::Diff
223
          init_from_gitaly(raw_diff)
224
          prune_diff_if_eligible
225 226
        when Gitaly::CommitDelta
          init_from_gitaly(raw_diff)
Robert Speicher's avatar
Robert Speicher committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        when nil
          raise "Nil as raw diff passed"
        else
          raise "Invalid raw diff type: #{raw_diff.class}"
        end
      end

      def serialize_keys
        @serialize_keys ||= %i(diff new_path old_path a_mode b_mode new_file renamed_file deleted_file too_large)
      end

      def to_hash
        hash = {}

        keys = serialize_keys

        keys.each do |key|
          hash[key] = send(key)
        end

        hash
      end

250 251 252 253
      def mode_changed?
        a_mode && b_mode && a_mode != b_mode
      end

Robert Speicher's avatar
Robert Speicher committed
254 255 256 257 258 259 260 261 262
      def submodule?
        a_mode == '160000' || b_mode == '160000'
      end

      def line_count
        @line_count ||= Util.count_lines(@diff)
      end

      def too_large?
Douwe Maan's avatar
Douwe Maan committed
263
        if @too_large.nil?
264
          @too_large = @diff.bytesize >= self.class.size_limit
Douwe Maan's avatar
Douwe Maan committed
265 266 267
        else
          @too_large
        end
Robert Speicher's avatar
Robert Speicher committed
268 269
      end

270
      def too_large!
Robert Speicher's avatar
Robert Speicher committed
271 272 273 274 275 276 277
        @diff = ''
        @line_count = 0
        @too_large = true
      end

      def collapsed?
        return @collapsed if defined?(@collapsed)
278

279
        @collapsed = !expanded && @diff.bytesize >= self.class.collapse_limit
Robert Speicher's avatar
Robert Speicher committed
280 281
      end

282
      def collapse!
Robert Speicher's avatar
Robert Speicher committed
283 284 285 286 287 288 289
        @diff = ''
        @line_count = 0
        @collapsed = true
      end

      private

290
      def init_from_rugged(rugged)
Robert Speicher's avatar
Robert Speicher committed
291
        if rugged.is_a?(Rugged::Patch)
292
          init_from_rugged_patch(rugged)
Robert Speicher's avatar
Robert Speicher committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306
          d = rugged.delta
        else
          d = rugged
        end

        @new_path = encode!(d.new_file[:path])
        @old_path = encode!(d.old_file[:path])
        @a_mode = d.old_file[:mode].to_s(8)
        @b_mode = d.new_file[:mode].to_s(8)
        @new_file = d.added?
        @renamed_file = d.renamed?
        @deleted_file = d.deleted?
      end

307
      def init_from_rugged_patch(patch)
Robert Speicher's avatar
Robert Speicher committed
308 309
        # Don't bother initializing diffs that are too large. If a diff is
        # binary we're not going to display anything so we skip the size check.
310
        return if !patch.delta.binary? && prune_large_patch(patch)
Robert Speicher's avatar
Robert Speicher committed
311 312 313 314

        @diff = encode!(strip_diff_headers(patch.to_s))
      end

315
      def init_from_hash(hash)
Robert Speicher's avatar
Robert Speicher committed
316 317 318 319 320
        raw_diff = hash.symbolize_keys

        serialize_keys.each do |key|
          send(:"#{key}=", raw_diff[key.to_sym])
        end
321 322
      end

323 324 325 326 327 328 329 330 331
      def init_from_gitaly(diff)
        @diff = diff.patch if diff.respond_to?(:patch)
        @new_path = encode!(diff.to_path.dup)
        @old_path = encode!(diff.from_path.dup)
        @a_mode = diff.old_mode.to_s(8)
        @b_mode = diff.new_mode.to_s(8)
        @new_file = diff.from_id == BLANK_SHA
        @renamed_file = diff.from_path != diff.to_path
        @deleted_file = diff.to_id == BLANK_SHA
332
      end
Robert Speicher's avatar
Robert Speicher committed
333

334 335 336 337 338 339
      def prune_diff_if_eligible
        if too_large?
          too_large!
        elsif collapsed?
          collapse!
        end
Robert Speicher's avatar
Robert Speicher committed
340 341 342 343
      end

      # If the patch surpasses any of the diff limits it calls the appropiate
      # prune method and returns true. Otherwise returns false.
344
      def prune_large_patch(patch)
Robert Speicher's avatar
Robert Speicher committed
345 346 347 348 349 350
        size = 0

        patch.each_hunk do |hunk|
          hunk.each_line do |line|
            size += line.content.bytesize

351
            if size >= self.class.size_limit
352
              too_large!
Robert Speicher's avatar
Robert Speicher committed
353 354 355 356 357
              return true
            end
          end
        end

358
        if !expanded && size >= self.class.collapse_limit
359
          collapse!
Robert Speicher's avatar
Robert Speicher committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
          return true
        end

        false
      end

      # Strip out the information at the beginning of the patch's text to match
      # Grit's output
      def strip_diff_headers(diff_text)
        # Delete everything up to the first line that starts with '---' or
        # 'Binary'
        diff_text.sub!(/\A.*?^(---|Binary)/m, '\1')

        if diff_text.start_with?('---', 'Binary')
          diff_text
        else
          # If the diff_text did not contain a line starting with '---' or
          # 'Binary', return the empty string. No idea why; we are just
          # preserving behavior from before the refactor.
          ''
        end
      end
    end
  end
end