33import re
44from utils import *
55from method_missing import MethodMissingMixin
6+ from errors import GitCommandError
67
78# Enables debugging of GitPython's git commands
89GIT_PYTHON_TRACE = os .environ .get ("GIT_PYTHON_TRACE" , False )
@@ -22,6 +23,7 @@ def get_dir(self):
2223 def execute (self , command ,
2324 istream = None ,
2425 with_status = False ,
26+ with_exceptions = False ,
2527 ):
2628 """
2729 Handles executing the command on the shell and consumes and returns
@@ -36,6 +38,8 @@ def execute(self, command,
3638 ``with_status``
3739 Whether to return a (status, str) tuple.
3840
41+ ``with_exceptions``
42+ Whether to raise an exception when git returns a non-zero status.
3943 Returns
4044 str(output) # with_status = False (Default)
4145 tuple(int(status), str(output)) # with_status = True
@@ -56,6 +60,10 @@ def execute(self, command,
5660 proc .stdout .close ()
5761 # Grab the exit status
5862 status = proc .poll ()
63+ if with_exceptions and status != 0 :
64+ raise GitCommandError ("%s returned exit status %d"
65+ % ( str (command ), status ))
66+
5967 # Allow access to the command's status code
6068 if with_status :
6169 return (status , stdout_value )
@@ -107,6 +115,7 @@ def method_missing(self, method, *args, **kwargs):
107115 # otherwise these'll end up in args, which is bad.
108116 istream = pop_key (kwargs , "istream" )
109117 with_status = pop_key (kwargs , "with_status" )
118+ with_exceptions = pop_key (kwargs , "with_exceptions" )
110119 # Prepare the argument list
111120 opt_args = self .transform_kwargs (** kwargs )
112121 ext_args = map (str , args )
@@ -118,4 +127,5 @@ def method_missing(self, method, *args, **kwargs):
118127 return self .execute (call ,
119128 istream = istream ,
120129 with_status = with_status ,
130+ with_exceptions = with_exceptions ,
121131 )
0 commit comments