Replace GitAdd by GitRepository.add_files
authorGuido Günther <agx@sigxcpu.org>
Fri, 21 Oct 2011 19:20:24 +0000 (21:20 +0200)
committerGuido Günther <agx@sigxcpu.org>
Sat, 22 Oct 2011 13:33:40 +0000 (15:33 +0200)
Git-Dch: Ignore

examples/gbp-add-patch
gbp/command_wrappers.py
gbp/git.py
git-buildpackage
tests/03_test_gbp_branch.py
tests/04_test_gbp_submodules.py

index 357e31c..8a10eb5 100755 (executable)
@@ -40,7 +40,6 @@ import subprocess
 import tempfile
 from gbp.command_wrappers import (Command,
                                   CommandExecFailed,
-                                  GitAdd,
                                   GitCommand)
 from gbp.config import (GbpOptionParser, GbpOptionGroup)
 from gbp.errors import GbpError
@@ -144,7 +143,7 @@ def main(argv):
 
         patch = PatchInfo(patchfile)
 
-        GitAdd()([patchfile])
+        repo.add_files(patchfile)
         msg = build_commit_msg(repo, patch, options)
         GitCommit()(edit=options.edit, msg=msg)
         # FIXME: handle the series file
index 3488575..f182e98 100644 (file)
@@ -293,14 +293,6 @@ class GitTag(GitCommand):
         GitCommand.__call__(self, cmd)
 
 
-# FIXME: move to gbp.git.add
-class GitAdd(GitCommand):
-    """Wrap git add to add new files"""
-    def __init__(self, extra_env=None):
-        GitCommand.__init__(self, 'add', extra_env=extra_env)
-        self.run_error = "Couldn't add files"
-
-
 def copy_from(orig_dir, filters=[]):
     """
     copy a source tree over via tar
index f6e12ae..ebc0297 100644 (file)
@@ -19,7 +19,7 @@
 import re
 import subprocess
 import os.path
-from command_wrappers import (GitCommand, GitAdd, GitBranch, copy_from)
+from command_wrappers import (GitCommand, GitBranch, copy_from)
 from errors import GbpError
 import log
 import dateutil.parser
@@ -372,10 +372,14 @@ class GitRepository(object):
             raise GitRepositoryError, "revision '%s' not found" % name
         return sha[0].strip()
 
-    def write_tree(self, index=None):
-        """write out the current index, return the SHA1"""
-        if index:
-            extra_env = {'GIT_INDEX_FILE': index }
+    def write_tree(self, index_file=None):
+        """
+        Write out the current index, return the SHA1
+
+        @param index: alternate index file to write the current index to
+        """
+        if index_file:
+            extra_env = {'GIT_INDEX_FILE': index_file }
         else:
             extra_env = None
 
@@ -434,9 +438,8 @@ class GitRepository(object):
             os.unlink(git_index_file)
         except OSError:
             pass
-        extra_env = { 'GIT_INDEX_FILE': git_index_file,
-                      'GIT_WORK_TREE': unpack_dir}
-        GitAdd(extra_env=extra_env)(['-f', '.'])
+        self.add_files('.', force=True, index_file=git_index_file,
+                       work_tree=unpack_dir)
         tree = self.write_tree(git_index_file)
 
         if branch:
@@ -500,6 +503,33 @@ class GitRepository(object):
         else:
             return False
 
+
+    def add_files(self, paths, force=False, index_file=None, work_tree=None):
+        """
+        Add files to a git repository
+
+        @param paths: list of files to add
+        @param paths: list or string
+        @param force: add files even if they would be ignores by .gitignore
+        @param force: bool
+        @param index_file: alternative index file to use
+        @param work_tree: alternative working tree to use
+        """
+        extra_env = {}
+
+        if type(paths) in [type(''), type(u'')]:
+            paths = [ paths ]
+
+        args = [ '-f' ] if force else []
+
+        if index_file:
+            extra_env['GIT_INDEX_FILE'] =  index_file
+
+        if work_tree:
+            extra_env['GIT_WORK_TREE'] = work_tree
+
+        self._git_command("add", args + paths, extra_env)
+
     def format_patches(self, start, end, output_dir):
         """
         Output the commits between start and end as patches in output_dir
index d18fa93..7b5dca3 100755 (executable)
@@ -30,7 +30,7 @@ import gbp.deb as du
 from gbp.git import (GitRepositoryError, GitRepository, build_tag)
 from gbp.command_wrappers import (GitTag, Command,
                                   RunAtCommand, CommandExecFailed, PristineTar,
-                                  RemoveTree, GitAdd, CatenateTarArchive)
+                                  RemoveTree, CatenateTarArchive)
 from gbp.config import (GbpOptionParser, GbpOptionGroup)
 from gbp.errors import GbpError
 from glob import glob
@@ -229,11 +229,8 @@ def git_archive_build_orig(repo, cp, output_dir, options):
 
 def write_wc(repo):
     """write out the current working copy as a treeish object"""
-    tree = None
-    os.putenv("GIT_INDEX_FILE", wc_index)
-    GitAdd()(['-f', '.'])
-    tree = repo.write_tree()
-    os.unsetenv("GIT_INDEX_FILE")
+    repo.add_files(repo.path, force=True, index_file=wc_index)
+    tree = repo.write_tree(index_file=wc_index)
     return tree
 
 def drop_index():
index a305697..a99391d 100644 (file)
@@ -39,7 +39,7 @@ def test_is_empty():
 def test_add_files():
     """Add some dummy data"""
     shutil.copy(".git/HEAD", "testfile")
-    gbp.command_wrappers.GitAdd()(['-f', '.'])
+    repo.add_files('.', force=True)
     gbp.command_wrappers.GitCommand("commit", ["-mfoo", "-a"])()
     assert True
 
index af44c07..a6b66bb 100644 (file)
@@ -55,7 +55,7 @@ def test_empty_has_submodules():
 
 def _add_dummy_data(msg):
     shutil.copy(".git/HEAD", testfile_name)
-    gbp.command_wrappers.GitAdd()(['-f', '.'])
+    repo.add_files('.', force=True)
     gbp.command_wrappers.GitCommand("commit", ["-m%s" % msg, "-a"])()