git.py: add a with_exceptions keyword argument
authorDavid Aguilar <davvid@gmail.com>
Thu, 29 May 2008 09:13:27 +0000 (02:13 -0700)
committerDavid Aguilar <davvid@gmail.com>
Thu, 29 May 2008 09:13:27 +0000 (02:13 -0700)
When git.foo( with_exceptions=True ) is called a
GitCommandError is raised when the exit status is non-zero.

Signed-off-by: David Aguilar <davvid@gmail.com>
lib/git_python/git.py

index de588b36565477ce137fe2b8ac6307d43cc304ad..6d5fd1edc69477e7a1f290d49aab2613c64ccb79 100644 (file)
@@ -3,6 +3,7 @@ import subprocess
 import re
 from utils import *
 from method_missing import MethodMissingMixin
+from errors import GitCommandError
 
 # Enables debugging of GitPython's git commands
 GIT_PYTHON_TRACE = os.environ.get("GIT_PYTHON_TRACE", False)
@@ -22,6 +23,7 @@ class Git(MethodMissingMixin):
     def execute(self, command,
                 istream = None,
                 with_status = False,
+                with_exceptions = False,
                 ):
         """
         Handles executing the command on the shell and consumes and returns
@@ -36,6 +38,8 @@ class Git(MethodMissingMixin):
         ``with_status``
             Whether to return a (status, str) tuple.
 
+        ``with_exceptions``
+            Whether to raise an exception when git returns a non-zero status.
         Returns
             str(output)                     # with_status = False (Default)
             tuple(int(status), str(output)) # with_status = True
@@ -56,6 +60,10 @@ class Git(MethodMissingMixin):
         proc.stdout.close()
         # Grab the exit status
         status = proc.poll()
+        if with_exceptions and status != 0:
+            raise GitCommandError("%s returned exit status %d"
+                                  % ( str(command), status ))
+
         # Allow access to the command's status code
         if with_status:
             return (status, stdout_value)
@@ -107,6 +115,7 @@ class Git(MethodMissingMixin):
         # otherwise these'll end up in args, which is bad.
         istream = pop_key(kwargs, "istream")
         with_status = pop_key(kwargs, "with_status")
+        with_exceptions = pop_key(kwargs, "with_exceptions")
         # Prepare the argument list
         opt_args = self.transform_kwargs(**kwargs)
         ext_args = map(str, args)
@@ -118,4 +127,5 @@ class Git(MethodMissingMixin):
         return self.execute(call,
                             istream = istream,
                             with_status = with_status,
+                            with_exceptions = with_exceptions,
                             )