2017-09-10 17:25:29 +05:30
|
|
|
# Gitaly note: JV: no RPC's here.
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Gitlab
|
|
|
|
module Git
|
|
|
|
class DiffCollection
|
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
DEFAULT_LIMITS = { max_files: 100, max_lines: 5000 }.freeze
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
attr_reader :limits
|
|
|
|
|
|
|
|
delegate :max_files, :max_lines, :max_bytes, :safe_max_files, :safe_max_lines, :safe_max_bytes, to: :limits
|
|
|
|
|
|
|
|
def self.collection_limits(options = {})
|
|
|
|
limits = {}
|
|
|
|
limits[:max_files] = options.fetch(:max_files, DEFAULT_LIMITS[:max_files])
|
|
|
|
limits[:max_lines] = options.fetch(:max_lines, DEFAULT_LIMITS[:max_lines])
|
|
|
|
limits[:max_bytes] = limits[:max_files] * 5.kilobytes # Average 5 KB per file
|
|
|
|
limits[:safe_max_files] = [limits[:max_files], DEFAULT_LIMITS[:max_files]].min
|
|
|
|
limits[:safe_max_lines] = [limits[:max_lines], DEFAULT_LIMITS[:max_lines]].min
|
|
|
|
limits[:safe_max_bytes] = limits[:safe_max_files] * 5.kilobytes # Average 5 KB per file
|
|
|
|
|
|
|
|
OpenStruct.new(limits)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def initialize(iterator, options = {})
|
|
|
|
@iterator = iterator
|
2017-09-10 17:25:29 +05:30
|
|
|
@limits = self.class.collection_limits(options)
|
|
|
|
@enforce_limits = !!options.fetch(:limits, true)
|
|
|
|
@expanded = !!options.fetch(:expanded, true)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
@line_count = 0
|
|
|
|
@byte_count = 0
|
|
|
|
@overflow = false
|
2017-09-10 17:25:29 +05:30
|
|
|
@empty = true
|
2017-08-17 22:00:37 +05:30
|
|
|
@array = Array.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(&block)
|
2017-09-10 17:25:29 +05:30
|
|
|
@array.each(&block)
|
|
|
|
|
|
|
|
return if @overflow
|
|
|
|
return if @iterator.nil?
|
|
|
|
|
|
|
|
Gitlab::GitalyClient.migrate(:commit_raw_diffs) do |is_enabled|
|
|
|
|
if is_enabled && @iterator.is_a?(Gitlab::GitalyClient::DiffStitcher)
|
|
|
|
each_gitaly_patch(&block)
|
|
|
|
else
|
|
|
|
each_rugged_patch(&block)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
@populated = true
|
|
|
|
|
|
|
|
# Allow iterator to be garbage-collected. It cannot be reused anyway.
|
|
|
|
@iterator = nil
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def empty?
|
2017-09-10 17:25:29 +05:30
|
|
|
any? # Make sure the iterator has been exercised
|
|
|
|
@empty
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def overflow?
|
|
|
|
populate!
|
|
|
|
!!@overflow
|
|
|
|
end
|
|
|
|
|
|
|
|
def size
|
|
|
|
@size ||= count # forces a loop using each method
|
|
|
|
end
|
|
|
|
|
|
|
|
def real_size
|
|
|
|
populate!
|
|
|
|
|
|
|
|
if @overflow
|
|
|
|
"#{size}+"
|
|
|
|
else
|
|
|
|
size.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def decorate!
|
|
|
|
collection = each_with_index do |element, i|
|
|
|
|
@array[i] = yield(element)
|
|
|
|
end
|
|
|
|
collection
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
alias_method :to_ary, :to_a
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def populate!
|
|
|
|
return if @populated
|
|
|
|
|
|
|
|
each { nil } # force a loop through all diffs
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def over_safe_limits?(files)
|
2017-09-10 17:25:29 +05:30
|
|
|
files >= safe_max_files || @line_count > safe_max_lines || @byte_count >= safe_max_bytes
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def each_gitaly_patch
|
|
|
|
i = @array.length
|
|
|
|
|
|
|
|
@iterator.each do |raw|
|
|
|
|
diff = Gitlab::Git::Diff.new(raw, expanded: !@enforce_limits || @expanded)
|
|
|
|
|
|
|
|
if raw.overflow_marker
|
|
|
|
@overflow = true
|
|
|
|
break
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
yield @array[i] = diff
|
2017-09-10 17:25:29 +05:30
|
|
|
i += 1
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def each_rugged_patch
|
|
|
|
i = @array.length
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
@iterator.each do |raw|
|
|
|
|
@empty = false
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
if @enforce_limits && i >= max_files
|
2017-08-17 22:00:37 +05:30
|
|
|
@overflow = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expanded = !@enforce_limits || @expanded
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
diff = Gitlab::Git::Diff.new(raw, expanded: expanded)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
if !expanded && over_safe_limits?(i) && diff.line_count > 0
|
|
|
|
diff.collapse!
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
@line_count += diff.line_count
|
|
|
|
@byte_count += diff.diff.bytesize
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
if @enforce_limits && (@line_count >= max_lines || @byte_count >= max_bytes)
|
2017-08-17 22:00:37 +05:30
|
|
|
# This last Diff instance pushes us over the lines limit. We stop and
|
|
|
|
# discard it.
|
|
|
|
@overflow = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
yield @array[i] = diff
|
2017-09-10 17:25:29 +05:30
|
|
|
i += 1
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|