summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--morphlib/gitindex.py4
-rw-r--r--morphlib/gitindex_tests.py9
2 files changed, 13 insertions, 0 deletions
diff --git a/morphlib/gitindex.py b/morphlib/gitindex.py
index d8de8824..7cac2311 100644
--- a/morphlib/gitindex.py
+++ b/morphlib/gitindex.py
@@ -97,3 +97,7 @@ class GitIndex(object):
for code, to_path, from_path in self._get_status():
if code not in (STATUS_UNTRACKED, STATUS_IGNORED):
yield code, to_path, from_path
+
+ def set_to_tree(self, treeish):
+ '''Modify the index to contain the contents of the treeish.'''
+ self._run_git('read-tree', treeish)
diff --git a/morphlib/gitindex_tests.py b/morphlib/gitindex_tests.py
index db1e0e9b..2dcb5003 100644
--- a/morphlib/gitindex_tests.py
+++ b/morphlib/gitindex_tests.py
@@ -54,3 +54,12 @@ class GitIndexTests(unittest.TestCase):
self.assertEqual(sorted(idx.get_uncommitted_changes()),
[('D ', 'foo', None)])
# 'D ' means not in the index, but in the working tree
+
+ def test_set_to_tree_alt_index(self):
+ gd = morphlib.gitdir.GitDirectory(self.dirname)
+ idx = gd.get_index(os.path.join(self.tempdir, 'index'))
+ # Read the HEAD commit into the index, which is the same as the
+ # working tree, so there are no uncommitted changes reported
+ # by status
+ idx.set_to_tree(gd.HEAD)
+ self.assertEqual(list(idx.get_uncommitted_changes()),[])