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 Method Summary collapse

Constructor Details

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

Returns a new instance of DiffCollection.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gitlab_git/diff_collection.rb', line 8

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])
  @max_bytes = @max_files * 5120 # Average 5 KB per file
  @safe_max_files = [@max_files, DEFAULT_LIMITS[:max_files]].min
  @safe_max_lines = [@max_lines, DEFAULT_LIMITS[:max_lines]].min
  @safe_max_bytes = @safe_max_files * 5120 # Average 5 KB per file
  @all_diffs = !!options.fetch(:all_diffs, false)
  @no_collapse = !!options.fetch(:no_collapse, true)
  @deltas_only = !!options.fetch(:deltas_only, false)

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

Instance Method Details

#decorate!Object



60
61
62
63
64
65
66
# File 'lib/gitlab_git/diff_collection.rb', line 60

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

#each(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/gitlab_git/diff_collection.rb', line 26

def each(&block)
  if @populated
    # @iterator.each is slower than just iterating the array in place
    @array.each(&block)
  elsif @deltas_only
    each_delta(&block)
  else
    each_patch(&block)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/gitlab_git/diff_collection.rb', line 37

def empty?
  !@iterator.any?
end

#overflow?Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/gitlab_git/diff_collection.rb', line 41

def overflow?
  populate!
  !!@overflow
end

#real_sizeObject



50
51
52
53
54
55
56
57
58
# File 'lib/gitlab_git/diff_collection.rb', line 50

def real_size
  populate!

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

#sizeObject



46
47
48
# File 'lib/gitlab_git/diff_collection.rb', line 46

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