Browse Source

use separate store for packs.

Jelmer Vernooij 16 years ago
parent
commit
22d562ddef
2 changed files with 54 additions and 35 deletions
  1. 1 1
      bin/dul-fetch-pack
  2. 53 34
      dulwich/repo.py

+ 1 - 1
bin/dul-fetch-pack

@@ -47,7 +47,7 @@ r = Repo(".")
 # FIXME: Will just fetch everything..
 graphwalker = SimpleFetchGraphWalker(r.heads().values(), r.get_parents)
 
-f, commit = r.add_pack()
+f, commit = r.object_store.add_pack()
 try:
     client.fetch_pack(path, determine_wants, graphwalker, f.write, sys.stdout.write)
     f.close()

+ 53 - 34
dulwich/repo.py

@@ -55,7 +55,7 @@ class Repo(object):
       self._basedir = root
     self.path = controldir
     self.tags = [Tag(name, ref) for name, ref in self.get_tags().items()]
-    self._packs = None
+    self._object_store = None
 
   def basedir(self):
     return self._basedir
@@ -63,29 +63,15 @@ class Repo(object):
   def object_dir(self):
     return os.path.join(self.basedir(), OBJECTDIR)
 
+  @property
+  def object_store(self):
+    if self._object_store is None:
+        self._object_store = ObjectStore(self.object_dir())
+    return self._object_store
+
   def pack_dir(self):
     return os.path.join(self.object_dir(), PACKDIR)
 
-  def add_pack(self):
-    fd, path = tempfile.mkstemp(dir=self.pack_dir(), suffix=".pack")
-    f = os.fdopen(fd, 'w')
-    def commit():
-       if os.path.getsize(path) > 0:
-           self._move_in_pack(path)
-    return f, commit
-
-  def _move_in_pack(self, path):
-    p = PackData(path)
-    entries = p.sorted_entries()
-    basename = os.path.join(self.pack_dir(), "pack-%s" % iter_sha1(entry[0] for entry in entries))
-    write_pack_index_v2(basename+".idx", entries, p.calculate_checksum())
-    os.rename(path, basename + ".pack")
-
-  def _get_packs(self):
-    if self._packs is None:
-        self._packs = list(load_packs(self.pack_dir()))
-    return self._packs
-
   def _get_ref(self, file):
     f = open(file, 'rb')
     try:
@@ -134,19 +120,7 @@ class Repo(object):
     return self.ref('HEAD')
 
   def _get_object(self, sha, cls):
-    assert len(sha) == 40, "Incorrect length sha: %s" % str(sha)
-    dir = sha[:2]
-    file = sha[2:]
-    # Check from object dir
-    path = os.path.join(self.object_dir(), dir, file)
-    if os.path.exists(path):
-      return cls.from_file(path)
-    # Check from packs
-    for pack in self._get_packs():
-        if sha in pack:
-            return pack[sha]
-    # Should this raise instead?
-    return None
+    return self.object_store.get_object(sha, cls)
 
   def get_object(self, sha):
     return self._get_object(sha, ShaFile)
@@ -214,3 +188,48 @@ class Repo(object):
 
   create = init_bare
 
+
+class ObjectStore(object):
+
+    def __init__(self, path):
+        self.path = path
+        self._packs = None
+
+    def pack_dir(self):
+        return os.path.join(self.path, PACKDIR)
+
+    @property
+    def packs(self):
+        if self._packs is None:
+            self._packs = list(load_packs(self.pack_dir()))
+        return self._packs
+
+    def get_object(self, sha, cls):
+        assert len(sha) == 40, "Incorrect length sha: %s" % str(sha)
+        dir = sha[:2]
+        file = sha[2:]
+        # Check from object dir
+        path = os.path.join(self.path, dir, file)
+        if os.path.exists(path):
+          return cls.from_file(path)
+        # Check from packs
+        for pack in self.packs:
+            if sha in pack:
+                return pack[sha]
+        # Should this raise instead?
+        return None
+
+    def move_in_pack(self, path):
+        p = PackData(path)
+        entries = p.sorted_entries()
+        basename = os.path.join(self.pack_dir(), "pack-%s" % iter_sha1(entry[0] for entry in entries))
+        write_pack_index_v2(basename+".idx", entries, p.calculate_checksum())
+        os.rename(path, basename + ".pack")
+
+    def add_pack(self):
+        fd, path = tempfile.mkstemp(dir=self.pack_dir(), suffix=".pack")
+        f = os.fdopen(fd, 'w')
+        def commit():
+            if os.path.getsize(path) > 0:
+                self.move_in_pack(path)
+        return f, commit