diff options
-rw-r--r-- | TODO | 13 | ||||
-rw-r--r-- | lib/git/index.py | 53 | ||||
-rw-r--r-- | test/git/test_index.py | 7 |
3 files changed, 55 insertions, 18 deletions
@@ -37,12 +37,13 @@ Overhaul docs - check examples, check looks, improve existing docs Index ----- -* Index class required for special handling ? Probably considering what I want - to do ! Dulwich can already write the index, and read trees, although it - could be improved as well and could possibly be made faster unless we want - to use the c modules ( not for now ) -* Index Merge ( merge two trees into the index to quickly see conflicts ). - Its possible to write it into a separate index file that can be read separately. +* write_tree should write a tree directly, which would require ability to create + objects in the first place. Should be rather simple as it is + "tree" bytes datablock | sha1sum and zipped. + Currently we use some file swapping and the git command to do it which probably + is much slower. The thing is that properly writing a tree from an index involves + creating several tree objects, so in the end it might be slower. + Hmm, probably its okay to use the command unless we go c(++) Repo ---- diff --git a/lib/git/index.py b/lib/git/index.py index e5b2d009..5bf654fe 100644 --- a/lib/git/index.py +++ b/lib/git/index.py @@ -14,7 +14,24 @@ import objects import tempfile import os import stat -from git.objects import Blob +from git.objects import Blob, Tree + +class _TemporaryFileSwap(object): + """ + Utility class moving a file to a temporary location within the same directory + and moving it back on to where on object deletion. + """ + __slots__ = ("file_path", "tmp_file_path") + + def __init__(self, file_path): + self.file_path = file_path + self.tmp_file_path = self.file_path + tempfile.mktemp('','','') + os.rename(self.file_path, self.tmp_file_path) + + def __del__(self): + if os.path.isfile(self.tmp_file_path): + os.rename(self.tmp_file_path, self.file_path) + class IndexEntry(tuple): """ @@ -335,17 +352,13 @@ class Index(object): # move current index out of the way - otherwise the merge may fail # as it considers existing entries. moving it essentially clears the index. - # Unfortunately there is no 'soft' way to do it - cur_index = os.path.join(repo.path, 'index') - moved_index = os.path.join(repo.path, 'index_moved'+tempfile.mktemp('','','')) + # Unfortunately there is no 'soft' way to do it. + # The _TemporaryFileSwap assure the original file get put back + index_handler = _TemporaryFileSwap(os.path.join(repo.path, 'index')) try: - os.rename(cur_index, moved_index) repo.git.read_tree(*arg_list, **kwargs) index = cls.from_file(repo, tmp_index) finally: - # put back the original index first ! - if os.path.exists(moved_index): - os.rename(moved_index, cur_index) if os.path.exists(tmp_index): os.remove(tmp_index) # END index merge handling @@ -354,8 +367,10 @@ class Index(object): @classmethod def _index_mode_to_tree_index_mode(cls, index_mode): - """Cleanup a index_mode value. + """ + Cleanup a index_mode value. This will return a index_mode that can be stored in a tree object. + ``index_mode`` Index_mode to clean up. """ @@ -381,15 +396,29 @@ class Index(object): for entry in self.entries.itervalues(): mode = self._index_mode_to_tree_index_mode(entry.mode) blob = Blob(self.repo, entry.sha, mode, entry.path) + blob.size = entry.size output = (entry.stage, blob) if predicate(output): yield output # END for each entry - def write_tree(self, stream): + def write_tree(self): """ - Writes the + Writes the Index in self to a corresponding Tree file into the repository + object database and returns it as corresponding Tree object. + + Returns + Tree object representing this index """ - raise NotImplementedError("TODO") + index_path = os.path.join(self.repo.path, "index") + tmp_index_mover = _TemporaryFileSwap(index_path) + + self.to_file(self, index_path) + tree_sha = self.repo.git.write_tree() + + # remove our index file so that the original index can move back into place + # On linux it will silently overwrite, on windows it won't + os.remove(index_path) + return Tree(self.repo, tree_sha, 0, '') diff --git a/test/git/test_index.py b/test/git/test_index.py index 524f0778..a4e01054 100644 --- a/test/git/test_index.py +++ b/test/git/test_index.py @@ -87,5 +87,12 @@ class TestTree(TestCase): assert isinstance(merge_blobs[0][1], Blob) + # writing a tree should fail with an unmerged index + self.failUnlessRaises(GitCommandError, tree_way_index.write_tree) + + # removed unmerged entries + self.fail("remove unmerged") + + def test_custom_commit(self): self.fail("Custom commit:write tree, make commit with custom parents") |