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
55
56
57
58
59
60
61
62
63
# 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 = object_offset - unpack_delta_size(io)
        patch  = read_compressed(io)
        base   = objects[offset]
        base.class.new(apply_patch(base.data, patch))
      when 7 then
        # TODO
        sha = io.read(20)
        # base = lookup_by_sha(sha)
        patch = read_compressed(io)
        # base.class.new(apply_patch(base.data, patch))
        nil
    end

    objects[object_offset] = object
  end

  GitDB.log(objects.values.map { |o| o.inspect })

  io.read(20)

  objects.values.compact
end

#write(entries) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/git-db/pack.rb', line 65

def write(entries)
  buffer = ""
  signature = ["PACK", 2, entries.length].pack("a4NN")
  #GitDB.log("SIGNATURE: #{signature}")
  io.write(signature)
  buffer << signature
  
  entries.each do |entry|
    header = pack_pack_header(entry.type, entry.data.length)
    #GitDB.log("HEADER: #{header.inspect}")
    io.write(header)
    buffer << header
    compressed = Zlib::Deflate.deflate(entry.data)
    io.write(compressed)
    buffer << compressed
  end
  
  #GitDB.log("BUFFER: #{buffer.inspect}")
  signature = GitDB::hex_to_sha1(Digest::SHA1.hexdigest(buffer))
  #GitDB.log("SIGNATURE: #{signature.inspect}")
  io.write(signature)
  io.flush
end