RawObject class, minor modifications in pack.rb, pack.rb moved
authorMatthias Lederhofer <[email protected]>
Tue, 23 Jan 2007 13:57:13 +0000 (23 14:57 +0100)
committerMatthias Lederhofer <[email protected]>
Tue, 23 Jan 2007 13:57:13 +0000 (23 14:57 +0100)
git/internal/object.rb [new file with mode: 0644]
git/internal/pack.rb [moved from ruby/pack.rb with 90% similarity]

diff --git a/git/internal/object.rb b/git/internal/object.rb
new file mode 100644 (file)
index 0000000..1ff7950
--- /dev/null
@@ -0,0 +1,26 @@
+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
similarity index 90%
rename from ruby/pack.rb
rename to git/internal/pack.rb
index d2e24a2..4ebce78 100644 (file)
@@ -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