diff options
-rw-r--r-- | lib/git/index.py | 27 | ||||
-rw-r--r-- | lib/git/utils.py | 4 | ||||
-rw-r--r-- | test/git/test_index.py | 10 |
3 files changed, 32 insertions, 9 deletions
diff --git a/lib/git/index.py b/lib/git/index.py index fd5081ce..4eabab15 100644 --- a/lib/git/index.py +++ b/lib/git/index.py @@ -15,7 +15,7 @@ import tempfile import os import stat from git.objects import Blob, Tree -from git.utils import SHA1Writer, LazyMixin +from git.utils import SHA1Writer, LazyMixin, ConcurrentWriteOperation from git.diff import Diffable class _TemporaryFileSwap(object): @@ -175,7 +175,7 @@ class Index(LazyMixin): def _set_cache_(self, attr): if attr == "entries": # read the current index - fp = open(os.path.join(self.repo.path, "index"), "r") + fp = open(self._index_path(), "r") try: self._read_from_stream(fp) finally: @@ -184,6 +184,9 @@ class Index(LazyMixin): else: super(Index, self)._set_cache_(attr) + def _index_path(self): + return os.path.join(self.repo.path, "index") + @classmethod def _read_entry(cls, stream): """Return: One entry of the given stream""" @@ -306,12 +309,14 @@ class Index(LazyMixin): real_size = ((stream.tell() - beginoffset + 8) & ~7) stream.write("\0" * ((beginoffset + real_size) - stream.tell())) - def write(self, stream): + def write(self, stream=None): """ - Write the current state to the given stream + Write the current state to the given stream or to the default repository + index. ``stream`` - File-like object. + File-like object or None. + If None, the default repository index will be overwritten. Returns self @@ -319,6 +324,13 @@ class Index(LazyMixin): Note Index writing based on the dulwich implementation """ + write_op = None + if stream is None: + write_op = ConcurrentWriteOperation(self._index_path()) + stream = write_op._begin_writing() + # stream = open(self._index_path() + # END stream handling + stream = SHA1Writer(stream) # header @@ -338,6 +350,9 @@ class Index(LazyMixin): # write the sha over the content stream.write_sha() + if write_op is not None: + write_op._end_writing() + @classmethod def from_tree(cls, repo, *treeish, **kwargs): @@ -504,7 +519,7 @@ class Index(LazyMixin): Returns Tree object representing this index """ - index_path = os.path.join(self.repo.path, "index") + index_path = self._index_path() tmp_index_mover = _TemporaryFileSwap(index_path) self.to_file(self, index_path) diff --git a/lib/git/utils.py b/lib/git/utils.py index f188a7a4..8cdb4804 100644 --- a/lib/git/utils.py +++ b/lib/git/utils.py @@ -201,7 +201,9 @@ class ConcurrentWriteOperation(LockFile): if self._temp_write_fp is None: return - self._temp_write_fp.close() + if not self._temp_write_fp.closed: + self._temp_write_fp.close() + if successful: # on windows, rename does not silently overwrite the existing one if sys.platform == "win32": diff --git a/test/git/test_index.py b/test/git/test_index.py index 8baf408c..5c643a67 100644 --- a/test/git/test_index.py +++ b/test/git/test_index.py @@ -101,7 +101,13 @@ class TestTree(TestBase): # END for each blob assert num_blobs == len(three_way_index.entries) - def test_from_index(self): + @with_rw_repo('0.1.6') + def test_from_index(self, rw_repo): # default Index instance points to our index - index = Index(self.rorepo) + index = Index(rw_repo) assert len(index.entries) + + # write the file back + index.write() + + # could sha it, or check stats |