Sha256: 97dfc2ee149fbe3e4660b3bdb29e0e21637e823289b1e897cf0f5ca2150a3b55

Contents?: true

Size: 1.98 KB

Versions: 4

Compression:

Stored size: 1.98 KB

Contents

module Gitlab
  module Git
    class DiffCollection
      include Enumerable

      DEFAULT_LIMITS = { max_files: 100, max_lines: 5000 }.freeze

      def initialize(iterator, options={})
        @iterator = iterator
        @max_files = options.fetch(:max_files, DEFAULT_LIMITS[:max_files])
        @max_lines = options.fetch(:max_lines, DEFAULT_LIMITS[:max_lines])
        @all_diffs = !!options.fetch(:all_diffs, false)

        @line_count = 0
        @overflow = false
        @array = Array.new
      end

      def each
        @iterator.each_with_index do |raw, i|
          # First yield cached Diff instances from @array
          if @array[i]
            yield @array[i]
            next
          end

          # We have exhausted @array, time to create new Diff instances or stop.
          break if @overflow

          if !@all_diffs && i >= @max_files
            @overflow = true
            break
          end

          # Going by the number of files alone it is OK to create a new Diff instance.
          diff = Gitlab::Git::Diff.new(raw)

          @line_count += diff.line_count
          if !@all_diffs && @line_count >= @max_lines
            # This last Diff instance pushes us over the lines limit. We stop and
            # discard it.
            @overflow = true
            break
          end

          yield @array[i] = diff
        end
      end

      def empty?
        !@iterator.any?
      end

      def overflow?
        populate!
        !!@overflow
      end

      def size
        @size ||= count # forces a loop through @iterator
      end

      def real_size
        populate!

        if @overflow
          "#{size}+"
        else
          size.to_s
        end
      end

      def decorate!
        each_with_index do |element, i|
          @array[i] = yield(element)
        end
      end

      private

      def populate!
        return if @populated

        each { nil } # force a loop through all diffs
        @populated = true
        nil
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gitlab_git-9.0.3 lib/gitlab_git/diff_collection.rb
gitlab_git-9.0.2 lib/gitlab_git/diff_collection.rb
gitlab_git-9.0.1 lib/gitlab_git/diff_collection.rb
gitlab_git-9.0.0 lib/gitlab_git/diff_collection.rb