diff options
Diffstat (limited to 'morphlib')
-rw-r--r-- | morphlib/gitindex.py | 21 | ||||
-rw-r--r-- | morphlib/gitindex_tests.py | 10 |
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()),[]) |