"""Repository access."""
-
+from cStringIO import StringIO
import errno
import os
)
from dulwich.object_store import (
DiskObjectStore,
+ MemoryObjectStore,
)
from dulwich.objects import (
Blob,
return ret
create = init_bare
+
+
+class MemoryRepo(BaseRepo):
+ """Repo that stores refs, objects, and named files in memory.
+
+ MemoryRepos are always bare: they have no working tree and no index, since
+ those have a stronger dependency on the filesystem.
+ """
+
+ def __init__(self):
+ BaseRepo.__init__(self, MemoryObjectStore(), DictRefsContainer({}))
+ self._named_files = {}
+ self.bare = True
+
+ def _put_named_file(self, path, contents):
+ """Write a file to the control dir with the given name and contents.
+
+ :param path: The path to the file, relative to the control dir.
+ :contents: A string to write to the file.
+ """
+ self._named_files[path] = contents
+
+ def get_named_file(self, path):
+ """Get a file from the control dir with a specific name.
+
+ Although the filename should be interpreted as a filename relative to
+ the control dir in a disk-baked Repo, the object returned need not be
+ pointing to a file in that location.
+
+ :param path: The path to the file, relative to the control dir.
+ :return: An open file object, or None if the file does not exist.
+ """
+ contents = self._named_files.get(path, None)
+ if contents is None:
+ return None
+ return StringIO(contents)
+
+ def open_index(self):
+ """Fail to open index for this repo, since it is bare."""
+ raise NoIndexPresent()
+
+ @classmethod
+ def init_bare(cls, objects, refs):
+ ret = cls()
+ for obj in objects:
+ ret.object_store.add_object(obj)
+ for refname, sha in refs.iteritems():
+ ret.refs[refname] = sha
+ ret._init_files()
+ return ret