summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2013-11-11 17:55:08 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2013-11-22 13:49:25 +0000
commit05cf106943ce737392d507ab1e34a3487849bd9f (patch)
tree270aa1aea5fdef947aed68474bf1cc565f4ba576
parent65fc7474c12b7b0fccd57810ba4f06d2fbca895a (diff)
downloadmorph-05cf106943ce737392d507ab1e34a3487849bd9f.tar.gz
gitindex: Add GitIndex.add_files_from_index_info()
This is used to add files directly to the index, without touching the working tree. This is used in the build without commit code for creating new morphologies pointing to different branches.
-rw-r--r--morphlib/gitindex.py21
-rw-r--r--morphlib/gitindex_tests.py10
2 files changed, 31 insertions, 0 deletions
diff --git a/morphlib/gitindex.py b/morphlib/gitindex.py
index 7cac2311..62e6838f 100644
--- a/morphlib/gitindex.py
+++ b/morphlib/gitindex.py
@@ -101,3 +101,24 @@ class GitIndex(object):
def set_to_tree(self, treeish):
'''Modify the index to contain the contents of the treeish.'''
self._run_git('read-tree', treeish)
+
+ def add_files_from_index_info(self, infos):
+ '''Add files without interacting with the working tree.
+
+ `infos` is an iterable of (file mode string, object sha1, path)
+ There are no constraints on the size of the iterable
+
+ '''
+
+ # update-index may take NUL terminated input lines of the entries
+ # to add so we generate a string for the input, rather than
+ # having many command line arguments, since for a large amount
+ # of entries, this can be too many arguments to process and the
+ # exec will fail.
+ # Generating the input as a string uses more memory than using
+ # subprocess.Popen directly and using .communicate, but is much
+ # less verbose.
+ feed_stdin = '\0'.join('%o %s\t%s' % (mode, sha1, path)
+ for mode, sha1, path in infos) + '\0'
+ self._run_git('update-index', '--add', '-z', '--index-info',
+ feed_stdin=feed_stdin)
diff --git a/morphlib/gitindex_tests.py b/morphlib/gitindex_tests.py
index 2dcb5003..af0e0444 100644
--- a/morphlib/gitindex_tests.py
+++ b/morphlib/gitindex_tests.py
@@ -63,3 +63,13 @@ class GitIndexTests(unittest.TestCase):
# by status
idx.set_to_tree(gd.HEAD)
self.assertEqual(list(idx.get_uncommitted_changes()),[])
+
+ def test_add_files_from_index_info(self):
+ gd = morphlib.gitdir.GitDirectory(self.dirname)
+ idx = gd.get_index(os.path.join(self.tempdir, 'index'))
+ filepath = os.path.join(gd.dirname, 'foo')
+ with open(filepath, 'r') as f:
+ sha1 = gd.store_blob(f)
+ idx.add_files_from_index_info(
+ [(os.stat(filepath).st_mode, sha1, 'foo')])
+ self.assertEqual(list(idx.get_uncommitted_changes()),[])