Class: Gitlab::Git::DiffCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gitlab/git/diff_collection.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iterator, options = {}) ⇒ DiffCollection

Returns a new instance of DiffCollection.


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gitlab/git/diff_collection.rb', line 29

def initialize(iterator, options = {})
  @iterator = iterator
  @limits = self.class.limits(options)
  @enforce_limits = !!options.fetch(:limits, true)
  @expanded = !!options.fetch(:expanded, true)
  @offset_index = options.fetch(:offset_index, 0)

  @line_count = 0
  @byte_count = 0
  @overflow = false
  @empty = true
  @array = []
end

Instance Attribute Details

#limitsObject (readonly)

Returns the value of attribute limits


12
13
14
# File 'lib/gitlab/git/diff_collection.rb', line 12

def limits
  @limits
end

Class Method Details

.limits(options = {}) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gitlab/git/diff_collection.rb', line 16

def self.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
  limits[:max_patch_bytes] = Gitlab::Git::Diff.patch_hard_limit_bytes

  OpenStruct.new(limits)
end

Instance Method Details

#decorate!Object


91
92
93
94
95
96
# File 'lib/gitlab/git/diff_collection.rb', line 91

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

#each(&block) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gitlab/git/diff_collection.rb', line 43

def each(&block)
  @array.each(&block)

  return if @overflow
  return if @iterator.nil?

  if @iterator.is_a?(Gitlab::GitalyClient::DiffStitcher)
    each_gitaly_patch(&block)
  else
    each_serialized_patch(&block)
  end

  @populated = true

  # Allow iterator to be garbage-collected. It cannot be reused anyway.
  @iterator = nil
end

#empty?Boolean

Returns:

  • (Boolean)

61
62
63
64
# File 'lib/gitlab/git/diff_collection.rb', line 61

def empty?
  any? # Make sure the iterator has been exercised
  @empty
end

#line_countObject


85
86
87
88
89
# File 'lib/gitlab/git/diff_collection.rb', line 85

def line_count
  populate!

  @line_count
end

#overflow?Boolean

Returns:

  • (Boolean)

66
67
68
69
# File 'lib/gitlab/git/diff_collection.rb', line 66

def overflow?
  populate!
  !!@overflow
end

#real_sizeObject


75
76
77
78
79
80
81
82
83
# File 'lib/gitlab/git/diff_collection.rb', line 75

def real_size
  populate!

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

#sizeObject


71
72
73
# File 'lib/gitlab/git/diff_collection.rb', line 71

def size
  @size ||= count # forces a loop using each method
end