--- /dev/null
+require 'digest/sha1'
+
+module Git module Internal
+ OBJ_NONE = 0
+ 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,
+ nil, nil, :delta, nil].freeze
+
+ class RawObject
+ attr_accessor :type, :content
+ def initialize(type, content)
+ @type = type
+ @content = content
+ end
+
+ def sha1
+ Digest::SHA1.digest("%s %d\0" % [@type, @content.length] + @content)
+ end
+ end
+end end
@@ -32,7 +32,7 @@ class MmapHelp
end
end
-module Git
+module Git module Internal
class PackFormatError < StandardError
end
@@ -45,14 +45,6 @@ module Git
SHA1Start = OffsetStart + OffsetSize
EntrySize = OffsetSize + SHA1Size
- OBJ_NONE = 0
- OBJ_COMMIT = 1
- OBJ_TREE = 2
- OBJ_BLOB = 3
- OBJ_TAG = 4
- OBJ_OFS_DELTA = 6
- OBJ_REF_DELTA = 7
-
def initialize(file)
if file =~ /\.idx$/
file = file[0...-3] + 'pack'
@@ -75,8 +67,9 @@ module Git
@size = @offsets[-1]
end
- def [](sha)
- offset = find_object(sha)
+ def [](sha1)
+ offset = find_object(sha1)
+ return nil if !offset
return parse_object(offset)
end
@@ -101,13 +94,13 @@ module Git
end
end
- def find_object(sha)
- slot = sha[0]
+ def find_object(sha1)
+ slot = sha1[0]
first, last = @offsets[slot,2]
while first < last
mid = (first + last) / 2
- midsha = @idx[SHA1Start + mid * EntrySize,SHA1Size]
- cmp = midsha <=> sha
+ midsha1 = @idx[SHA1Start + mid * EntrySize,SHA1Size]
+ cmp = midsha1 <=> sha1
if cmp < 0
first = mid + 1
@@ -125,8 +118,8 @@ module Git
private :find_object
def parse_object(offset)
- # XXX convert to object
- unpack_object(offset)
+ data, type = unpack_object(offset)
+ RawObject.new(OBJ_TYPES[type], data)
end
protected :parse_object
@@ -255,19 +248,4 @@ module Git
end
private :patch_delta_header_size
end
-end
-
-if $0 == __FILE__
- p = Git::PackStorage.new(ARGV[0])
-
- if ARGV.length == 1
- p.each_sha1 do |sha1|
- puts sha1.unpack('H*')
- end
- end
-
- ARGV[1..-1].each do |sha1|
- sha1 = [sha1].pack('H*')
- puts p[sha1]
- end
-end
+end end