Class: Gitrb::Pack

Inherits:
Object
  • Object
show all
Defined in:
lib/gitrb/pack.rb

Constant Summary collapse

PACK_IDX_SIGNATURE =
"\377tOc"
OBJ_COMMIT =
1
OBJ_TREE =
2
OBJ_BLOB =
3
OBJ_TAG =
4
OBJ_OFS_DELTA =
6
OBJ_REF_DELTA =
7
OBJ_TYPES =
[nil, 'commit', 'tree', 'blob', 'tag'].freeze
FanOutCount =
256
SHA1Size =
20
IdxOffsetSize =
4
OffsetSize =
4
CrcSize =
4
OffsetStart =
FanOutCount * IdxOffsetSize
SHA1Start =
OffsetStart + OffsetSize
EntrySize =
OffsetSize + SHA1Size
EntrySizeV2 =
SHA1Size + CrcSize + OffsetSize

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Pack


55
56
57
58
59
# File 'lib/gitrb/pack.rb', line 55

def initialize(file)
  file = file[0...-3] + 'pack' if file =~ /\.idx$/
  @name = file
  init_pack
end

Instance Method Details

#each_objectObject


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gitrb/pack.rb', line 61

def each_object
  with_idx do |idx|
    if @version == 2
      data = read_data_v2(idx)
      data.each do |sha1, crc, offset|
        sha1 = sha1.unpack("H*").first
        yield sha1, offset
      end
    else
      pos = OffsetStart
      @size.times do
        offset = idx[pos,OffsetSize].unpack('N')[0]
        sha1 = idx[pos+OffsetSize,SHA1Size]
        pos += EntrySize
        sha1 = sha1.unpack("H*").first
        yield sha1, offset
      end
    end
  end
end

#get_object(offset) ⇒ Object


82
83
84
85
86
87
# File 'lib/gitrb/pack.rb', line 82

def get_object(offset)
  data, type = with_pack do |packfile|
    unpack_object(packfile, offset)
  end
  [data, OBJ_TYPES[type]]
end