summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--morphlib/gitindex.py31
-rw-r--r--morphlib/gitindex_tests.py12
2 files changed, 43 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)
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'])