diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2013-11-11 17:57:14 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2013-11-22 13:49:26 +0000 |
commit | 02f112af84b32b6731b6d286d3002730bfb2eabc (patch) | |
tree | 00972b0821d1bd5f74c4fdf42c8100a15627f731 /morphlib/gitindex.py | |
parent | 05cf106943ce737392d507ab1e34a3487849bd9f (diff) | |
download | morph-02f112af84b32b6731b6d286d3002730bfb2eabc.tar.gz |
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.
Diffstat (limited to 'morphlib/gitindex.py')
-rw-r--r-- | morphlib/gitindex.py | 31 |
1 files changed, 31 insertions, 0 deletions
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) |