Move mmap helper to own file and prefer ruby-mmap by default.
authorSimon 'corecode' Schubert <[email protected]>
Tue, 23 Jan 2007 14:31:36 +0000 (23 15:31 +0100)
committerSimon 'corecode' Schubert <[email protected]>
Tue, 23 Jan 2007 14:31:36 +0000 (23 15:31 +0100)
git/internal/mmap.rb [new file with mode: 0644]
git/internal/pack.rb

diff --git a/git/internal/mmap.rb b/git/internal/mmap.rb
new file mode 100644 (file)
index 0000000..3cb0558
--- /dev/null
@@ -0,0 +1,40 @@
+begin
+  require 'mmap'
+rescue LoadError
+
+module Git module Internal
+  class Mmap
+    def initialize(file)
+      @file = file
+      @offset = nil
+    end
+
+    def [](*idx)
+      idx = idx[0] if idx.length == 1
+      case idx
+      when Range
+        offset = idx.first
+        len = idx.last - idx.first + idx.exclude_end? ? 0 : 1
+      when Fixnum
+        offset = idx
+        len = nil
+      when Array
+        offset, len = idx
+      else
+        raise RuntimeError, "invalid index param: #{idx.class}"
+      end
+      if @offset != offset
+        @file.seek(offset)
+      end
+      @offset = offset + len ? len : 1
+      if not len
+        @file.read(1)[0]
+      else
+        @file.read(len)
+      end
+    end
+  end
+end end
+
+end     # rescue LoadError
+
index 4ebce78..255a1c0 100644 (file)
@@ -1,36 +1,5 @@
 require 'zlib'
-
-class MmapHelp
-  def initialize(file)
-    @file = file
-    @offset = nil
-  end
-
-  def [](*idx)
-    idx = idx[0] if idx.length == 1
-    case idx
-    when Range
-      offset = idx.first
-      len = idx.last - idx.first + idx.exclude_end? ? 0 : 1
-    when Fixnum
-      offset = idx
-      len = nil
-    when Array
-      offset, len = idx
-    else
-      raise RuntimeError, "invalid index param: #{idx.class}"
-    end
-    if @offset != offset
-      @file.seek(offset)
-    end
-    @offset = offset + len ? len : 1
-    if not len
-      @file.read(1)[0]
-    else
-      @file.read(len)
-    end
-  end
-end
+require 'git/internal/mmap'
 
 module Git module Internal
   class PackFormatError < StandardError
@@ -53,7 +22,7 @@ module Git module Internal
       @name = file
       @packfile = File.open(file)
       @idxfile = File.open(file[0...-4]+'idx')
-      @idx = MmapHelp.new(@idxfile)
+      @idx = Mmap.new(@idxfile)
 
       @offsets = [0]
       FanOutCount.times do |i|
@@ -249,3 +218,18 @@ module Git module Internal
     private :patch_delta_header_size
   end
 end end
+
+if $0 == __FILE__
+  p = Git::Internal::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