Keep storages as instance variables
authorSimon 'corecode' Schubert <[email protected]>
Wed, 24 Jan 2007 08:22:35 +0000 (24 09:22 +0100)
committerSimon 'corecode' Schubert <[email protected]>
Wed, 24 Jan 2007 08:22:35 +0000 (24 09:22 +0100)
git.rb
git/internal/mmap.rb
git/internal/pack.rb

diff --git a/git.rb b/git.rb
index d8e4185..73aec9a 100644 (file)
--- a/git.rb
+++ b/git.rb
@@ -7,6 +7,9 @@ module Git
   class Repository
     def initialize(git_dir)
       @git_dir = git_dir
+      @loose = Internal::LooseStorage.new("#@git_dir/objects")
+      @packs = []
+      initpacks
     end
 
     def get_object_by_sha1(sha1)
@@ -17,28 +20,35 @@ module Git
 
     def get_raw_object_by_sha1(sha1)
       sha1 = [sha1].pack("H*")
-      packs = Dir.glob(@git_dir + '/objects/pack/pack-*.pack')
 
       # try packs
-      packs.each do |pack|
-        storage = Git::Internal::PackStorage.new(pack)
-        o = storage[sha1]
+      @packs.each do |pack|
+        o = pack[sha1]
         return o if o
       end
 
       # try loose storage
-      storage = Git::Internal::LooseStorage.new(@git_dir+'/objects')
-      o = storage[sha1]
+      o = @loose[sha1]
       return o if o
 
       # try packs again, maybe the object got packed in the meantime
-      packs.each do |pack|
-        storage = Git::Internal::PackStorage.new(pack)
-        o = storage[sha1]
+      initpacks
+      @packs.each do |pack|
+        o = pack[sha1]
         return o if o
       end
 
-      return nil
+      nil
+    end
+
+    def initpacks
+      @packs.each do |pack|
+        pack.close
+      end
+      @packs = []
+      Dir.glob("#@git_dir/objects/pack/pack-*.pack").each do |pack|
+        @packs << Git::Internal::PackStorage.new(pack)
+      end
     end
   end
 end
index 3cb0558..c1500c1 100644 (file)
@@ -9,6 +9,10 @@ module Git module Internal
       @offset = nil
     end
 
+    def unmap
+      @file = nil
+    end
+
     def [](*idx)
       idx = idx[0] if idx.length == 1
       case idx
index 5408eed..d9c297e 100644 (file)
@@ -40,6 +40,12 @@ module Git module Internal
       @size = @offsets[-1]
     end
 
+    def close
+      @packfile.close
+      @idx.unmap
+      @idxfile.close
+    end
+
     def [](sha1)
       offset = find_object(sha1)
       return nil if !offset