summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/git/index.py20
-rw-r--r--test/git/test_index.py28
2 files changed, 43 insertions, 5 deletions
diff --git a/lib/git/index.py b/lib/git/index.py
index 754c9f15..111dc8d2 100644
--- a/lib/git/index.py
+++ b/lib/git/index.py
@@ -528,13 +528,13 @@ class IndexFile(LazyMixin, diff.Diffable):
Note:
This is a possibly dangerious operations as it will discard your changes
- to index.endtries
+ to index.entries
Returns
self
"""
del(self.entries)
- self.entries
+ # allows to lazily reread on demand
return self
def write_tree(self):
@@ -623,7 +623,7 @@ class IndexFile(LazyMixin, diff.Diffable):
raise NotImplementedError("todo")
@default_index
- def reset(self, commit='HEAD', working_tree=False, **kwargs):
+ def reset(self, commit='HEAD', working_tree=False, paths=None, **kwargs):
"""
Reset the index to reflect the tree at the given commit. This will not
adjust our HEAD reference as opposed to HEAD.reset.
@@ -636,11 +636,23 @@ class IndexFile(LazyMixin, diff.Diffable):
``working_tree``
If True, the files in the working tree will reflect the changed index.
If False, the working tree will not be touched
+ Please note that changes to the working copy will be discarded without
+ warning !
``**kwargs``
Additional keyword arguments passed to git-reset
+
+ Returns
+ self
"""
- raise NotImplementedError("todo: use git-read-tree if there is no working tree to update")
+ head = self.repo.head
+ prev_commit = head.commit
+
+ # reset to get the tree/working copy
+ head.reset(commit, index=True, working_tree=working_tree, paths=paths, **kwargs)
+ # put the head back
+ head.reset(prev_commit, index=False, working_tree=False)
+ return self
@default_index
def diff(self, other=diff.Diffable.Index, paths=None, create_patch=False, **kwargs):
diff --git a/test/git/test_index.py b/test/git/test_index.py
index 36e57d5c..7236aad9 100644
--- a/test/git/test_index.py
+++ b/test/git/test_index.py
@@ -146,7 +146,33 @@ class TestTree(TestBase):
# against something unusual
self.failUnlessRaises(ValueError, index.diff, int)
- self.fail( "Test IndexFile.reset" )
+ # adjust the index to match an old revision
+ cur_branch = rw_repo.active_branch
+ cur_commit = cur_branch.commit
+ rev_head_parent = 'HEAD~1'
+ assert index.reset(rev_head_parent) is index
+
+ assert cur_branch == rw_repo.active_branch
+ assert cur_commit == rw_repo.head.commit
+
+ # there must be differences towards the working tree which is in the 'future'
+ assert index.diff(None)
+
+ # reset the working copy as well to current head,to pull 'back' as well
+ new_data = "will be reverted"
+ file_path = os.path.join(rw_repo.git.git_dir, "CHANGES")
+ fp = open(file_path, "w")
+ fp.write(new_data)
+ fp.close()
+ index.reset(rev_head_parent, working_tree=True)
+ assert not index.diff(None)
+ assert cur_branch == rw_repo.active_branch
+ assert cur_commit == rw_repo.head.commit
+ fp = open(file_path)
+ try:
+ assert fp.read() != new_data
+ finally:
+ fp.close()
@with_rw_repo('0.1.6')
def test_index_mutation(self, rw_repo):