Class: GitDB::Pack

Inherits:
Object
  • Object
show all
Defined in:
lib/git-db/pack.rb

Defined Under Namespace

Classes: PackObject

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Pack

Returns a new instance of Pack



11
12
13
# File 'lib/git-db/pack.rb', line 11

def initialize(io)
  @io = GitDB::Utility::CountingIO.new(io)
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io



9
10
11
# File 'lib/git-db/pack.rb', line 9

def io
  @io
end

Instance Method Details

#readObject



15
16
17
18
19
20
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
# File 'lib/git-db/pack.rb', line 15

def read
  header = io.read(12)
  return nil unless header

  signature, version, entries = header.unpack("a4NN")
  raise 'invalid pack signature' unless signature == 'PACK'
  raise 'invalid version'        unless version   == 2

  objects = {}

  1.upto(entries) do
    object_offset = io.offset
    type, size    = unpack_pack_header(io)

    object = case type
      when 1 then GitDB::Objects::Commit.new(read_compressed(io))
      when 2 then GitDB::Objects::Tree.new(read_compressed(io))
      when 3 then GitDB::Objects::Blob.new(read_compressed(io))
      when 4 then GitDB::Objects::Tag.new(read_compressed(io))
      when 5 then raise 'Invalid Type: 5'
      when 6 then
        # offset delta, find the referred-to pack and apply as a patch
        offset = object_offset - unpack_delta_size(io)
        patch  = read_compressed(io)
        base   = objects[offset]
        data   = apply_patch(base.data, patch)
        base.class.new(data)
      when 7 then
        # TODO: patch against sha
        raise "Type 7 unimplemented, please report"
    end

    objects[object_offset] = object
  end

  # read the checksum, TODO: check the checksum
  checksum = io.read(20)

  objects.values.compact
end

#write(entries) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/git-db/pack.rb', line 56

def write(entries)
  # build the pack header
  buffer = ["PACK", 2, entries.length].pack("a4NN")

  # deflate the entries
  entries.each do |entry|
    buffer << pack_pack_header(entry.type, entry.data.length)
    buffer << Zlib::Deflate.deflate(entry.data)
  end

  # calculate a checksum
  checksum = GitDB::hex_to_sha1(Digest::SHA1.hexdigest(buffer))

  # write and flush the buffer
  io.write(buffer)
  io.write(checksum)
  io.flush
end