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
# 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
  @all_diffs = !!options.fetch(:all_diffs, false)

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

Instance Method Details

#decorate!Object



87
88
89
90
91
# File 'lib/gitlab_git/diff_collection.rb', line 87

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

#eachObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gitlab_git/diff_collection.rb', line 21

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)

    # If a diff is too large we still want to display some information
    # about it (e.g. the file path) without keeping the raw data around
    # (as this would be a waste of memory usage).
    #
    # This also removes the line count (from the diff itself) so it
    # doesn't add up to the total amount of lines.
    if diff.too_large?
      diff.prune_large_diff!
    end

    @line_count += diff.line_count
    @byte_count += diff.diff.bytesize

    if !@all_diffs && (@line_count >= @max_lines || @byte_count >= @max_bytes)
      # 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

#empty?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/gitlab_git/diff_collection.rb', line 64

def empty?
  !@iterator.any?
end

#overflow?Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/gitlab_git/diff_collection.rb', line 68

def overflow?
  populate!
  !!@overflow
end

#real_sizeObject



77
78
79
80
81
82
83
84
85
# File 'lib/gitlab_git/diff_collection.rb', line 77

def real_size
  populate!

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

#sizeObject



73
74
75
# File 'lib/gitlab_git/diff_collection.rb', line 73

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