Method: PackfileReader::PackfileEntry.next_entry

Defined in:
lib/packfile_reader/packfile_entry.rb

.next_entry(packfile_io, objects_to_find = :any, log_verbose = false, window_size = 10_000) ⇒ Object

Accepts a block that will receive the compressed data, uncompressed data and the computed object id. Window size is the amount of bytes to read at once while searching for the compressed data



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
# File 'lib/packfile_reader/packfile_entry.rb', line 21

def self.next_entry(packfile_io, objects_to_find=:any, log_verbose=false, window_size=10_000)
  raise 'Object id must be a valid sha1' unless objects_to_find == :any || objects_to_find.all? {|id| /^[0-9a-f]{40}$/.match? id }

  loop do
    return nil if packfile_io.eof?

    hunk = PackfileReader::Hunk.new_with_type(packfile_io)

    type = hunk.type
    size = hunk.size
    offset = hunk.offset_size

    # Clean the current line before printing the message
    $stderr.puts "\u001b[0K>>>> Processing new entry [#{type}]" if log_verbose
    while hunk.continuation?
      hunk = PackfileReader::Hunk.new_without_type(packfile_io)
      size = (hunk.size << offset) | size # Data size is a combination of all hunk sizes
      offset += hunk.offset_size
    end

    compressed_data, uncompressed_data = find_data(packfile_io, log_verbose, window_size)
    object_id = compute_id(type, size, uncompressed_data)

    type = "#{type} [CORRUPTED] " if uncompressed_data.nil?

    if objects_to_find == :any || objects_to_find.member?(object_id)
      yield compressed_data, uncompressed_data, object_id if block_given?
      return PackfileEntry.new(type, size, object_id)
    end
  end
end