From 02f112af84b32b6731b6d286d3002730bfb2eabc Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Mon, 11 Nov 2013 17:57:14 +0000 Subject: gitindex: Add GitIndex.add_files_from_working_tree() This is like GitIndex.add_files_from_index_info, but uses the working tree and a list of paths. This is more convenient when the changes exist in the working tree. The comment describes its relationship with GitIndex.add_files_from_index_info, but it is faster, since it calls out to `git add` rather than calling out to `git hash-object` for every file. --- morphlib/gitindex.py | 31 +++++++++++++++++++++++++++++++ morphlib/gitindex_tests.py | 12 ++++++++++++ 2 files changed, 43 insertions(+) diff --git a/morphlib/gitindex.py b/morphlib/gitindex.py index 62e6838f..a119389e 100644 --- a/morphlib/gitindex.py +++ b/morphlib/gitindex.py @@ -18,6 +18,8 @@ import collections +import morphlib + STATUS_UNTRACKED = '??' STATUS_IGNORED = '!!' @@ -122,3 +124,32 @@ class GitIndex(object): for mode, sha1, path in infos) + '\0' self._run_git('update-index', '--add', '-z', '--index-info', feed_stdin=feed_stdin) + + def add_files_from_working_tree(self, paths): + '''Add existing files to the index. + + Given an iterable of paths to files in the working tree, + relative to the git repository's top-level directory, + add the contents of the files to git's object store, + and the index. + + This is similar to the following: + + gd = GitDirectory(...) + idx = gd.get_index() + for path in paths: + fullpath = os.path.join(gd,dirname, path) + with open(fullpath, 'r') as f: + sha1 = gd.store_blob(f) + idx.add_files_from_index_info([(os.stat(fullpath).st_mode, + sha1, path)]) + + ''' + + if self._gd.is_bare(): + raise morphlib.gitdir.NoWorkingTreeError(self._gd) + # Handle paths in smaller chunks, so that the runcmd + # cannot fail from exceeding command line length + # 50 is an arbitrary limit + for paths in morphlib.util.iter_trickle(paths, 50): + self._run_git('add', *paths) diff --git a/morphlib/gitindex_tests.py b/morphlib/gitindex_tests.py index af0e0444..94444091 100644 --- a/morphlib/gitindex_tests.py +++ b/morphlib/gitindex_tests.py @@ -73,3 +73,15 @@ class GitIndexTests(unittest.TestCase): idx.add_files_from_index_info( [(os.stat(filepath).st_mode, sha1, 'foo')]) self.assertEqual(list(idx.get_uncommitted_changes()),[]) + + def test_add_files_from_working_tree(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + idx = gd.get_index() + idx.add_files_from_working_tree(['foo']) + self.assertEqual(list(idx.get_uncommitted_changes()),[]) + + def test_add_files_from_working_tree_fails_in_bare(self): + gd = morphlib.gitdir.GitDirectory(self.mirror) + idx = gd.get_index() + self.assertRaises(morphlib.gitdir.NoWorkingTreeError, + idx.add_files_from_working_tree, ['foo']) -- cgit v1.2.1