Changeset 634b2cd1f061…
Parent 4f730ba2f61d…
by
Changes to one file · Browse files at 634b2cd1f061 Showing diff from parent 4f730ba2f61d Diff from another changeset...
|
|
@@ -237,21 +237,10 @@ return self.iter_shas(self.find_missing_objects(have, want))
-class DiskObjectStore(BaseObjectStore):
- """Git-style object store that exists on disk."""
-
- def __init__(self, path):
- """Open an object store.
-
- :param path: Path of the object store.
- """
- self.path = path
+class PackBasedObjectStore(BaseObjectStore):
+
+ def __init__(self):
self._pack_cache = None
- self.pack_dir = os.path.join(self.path, PACKDIR)
-
- def contains_loose(self, sha):
- """Check if a particular object is present by SHA1 and is loose."""
- return self._get_shafile(sha) is not None
def contains_packed(self, sha):
"""Check if a particular object is present by SHA1 and is packed."""
@@ -260,10 +249,15 @@ return True
return False
- def __iter__(self):
- """Iterate over the SHAs that are present in this store."""
- iterables = self.packs + [self._iter_shafile_shas()]
- return itertools.chain(*iterables)
+ def _load_packs(self):
+ raise NotImplementedError(self._load_packs)
+
+ def _add_known_pack(self, pack):
+ """Add a newly appeared pack to the cache by path.
+
+ """
+ if self._pack_cache is not None:
+ self._pack_cache.append(pack)
@property
def packs(self):
@@ -272,44 +266,20 @@ self._pack_cache = self._load_packs()
return self._pack_cache
- def _load_packs(self):
- if not os.path.exists(self.pack_dir):
- return []
- pack_files = []
- for name in os.listdir(self.pack_dir):
- # TODO: verify that idx exists first
- if name.startswith("pack-") and name.endswith(".pack"):
- filename = os.path.join(self.pack_dir, name)
- pack_files.append((os.stat(filename).st_mtime, filename))
- pack_files.sort(reverse=True)
- suffix_len = len(".pack")
- return [Pack(f[:-suffix_len]) for _, f in pack_files]
-
- def _add_known_pack(self, path):
- """Add a newly appeared pack to the cache by path.
-
- """
- if self._pack_cache is not None:
- self._pack_cache.append(Pack(path))
-
- def _get_shafile_path(self, sha):
- dir = sha[:2]
- file = sha[2:]
- # Check from object dir
- return os.path.join(self.path, dir, file)
-
- def _iter_shafile_shas(self):
- for base in os.listdir(self.path):
- if len(base) != 2:
- continue
- for rest in os.listdir(os.path.join(self.path, base)):
- yield base+rest
-
- def _get_shafile(self, sha):
- path = self._get_shafile_path(sha)
- if os.path.exists(path):
- return ShaFile.from_file(path)
- return None
+ def _iter_loose_objects(self):
+ raise NotImplementedError(self._iter_loose_objects)
+
+ def _get_loose_object(self, sha):
+ raise NotImplementedError(self._get_loose_object)
+
+ def __iter__(self):
+ """Iterate over the SHAs that are present in this store."""
+ iterables = self.packs + [self._iter_loose_objects()]
+ return itertools.chain(*iterables)
+
+ def contains_loose(self, sha):
+ """Check if a particular object is present by SHA1 and is loose."""
+ return self._get_loose_object(sha) is not None
def get_raw(self, name):
"""Obtain the raw text for an object.
@@ -332,10 +302,67 @@ pass
if hexsha is None:
hexsha = sha_to_hex(name)
- ret = self._get_shafile(hexsha)
+ ret = self._get_loose_object(hexsha)
if ret is not None:
return ret.type, ret.as_raw_string()
raise KeyError(hexsha)
+
+ def add_objects(self, objects):
+ """Add a set of objects to this object store.
+
+ :param objects: Iterable over objects, should support __len__.
+ """
+ if len(objects) == 0:
+ # Don't bother writing an empty pack file
+ return
+ f, commit = self.add_pack()
+ write_pack_data(f, objects, len(objects))
+ commit()
+
+
+class DiskObjectStore(PackBasedObjectStore):
+ """Git-style object store that exists on disk."""
+
+ def __init__(self, path):
+ """Open an object store.
+
+ :param path: Path of the object store.
+ """
+ super(DiskObjectStore, self).__init__()
+ self.path = path
+ self.pack_dir = os.path.join(self.path, PACKDIR)
+
+ def _load_packs(self):
+ if not os.path.exists(self.pack_dir):
+ return []
+ pack_files = []
+ for name in os.listdir(self.pack_dir):
+ # TODO: verify that idx exists first
+ if name.startswith("pack-") and name.endswith(".pack"):
+ filename = os.path.join(self.pack_dir, name)
+ pack_files.append((os.stat(filename).st_mtime, filename))
+ pack_files.sort(reverse=True)
+ suffix_len = len(".pack")
+ return [Pack(f[:-suffix_len]) for _, f in pack_files]
+
+ def _get_shafile_path(self, sha):
+ dir = sha[:2]
+ file = sha[2:]
+ # Check from object dir
+ return os.path.join(self.path, dir, file)
+
+ def _iter_loose_objects(self):
+ for base in os.listdir(self.path):
+ if len(base) != 2:
+ continue
+ for rest in os.listdir(os.path.join(self.path, base)):
+ yield base+rest
+
+ def _get_loose_object(self, sha):
+ path = self._get_shafile_path(sha)
+ if os.path.exists(path):
+ return ShaFile.from_file(path)
+ return None
def move_in_thin_pack(self, path):
"""Move a specific file containing a pack into the pack directory.
@@ -362,7 +389,7 @@ newbasename = os.path.join(self.pack_dir, "pack-%s" % pack_sha)
os.rename(temppath+".pack", newbasename+".pack")
os.rename(temppath+".idx", newbasename+".idx")
- self._add_known_pack(newbasename)
+ self._add_known_pack(Pack(newbasename))
def move_in_pack(self, path):
"""Move a specific file containing a pack into the pack directory.
@@ -379,7 +406,7 @@ write_pack_index_v2(basename+".idx", entries, p.get_stored_checksum())
p.close()
os.rename(path, basename + ".pack")
- self._add_known_pack(basename)
+ self._add_known_pack(Pack(basename))
def add_thin_pack(self):
"""Add a new thin pack to this object store.
@@ -427,18 +454,6 @@ f.write(obj.as_legacy_object())
finally:
f.close()
-
- def add_objects(self, objects):
- """Add a set of objects to this object store.
-
- :param objects: Iterable over objects, should support __len__.
- """
- if len(objects) == 0:
- # Don't bother writing an empty pack file
- return
- f, commit = self.add_pack()
- write_pack_data(f, objects, len(objects))
- commit()
class MemoryObjectStore(BaseObjectStore):
|
Loading...