3
3
import re
4
4
from utils import *
5
5
from method_missing import MethodMissingMixin
6
+ from errors import GitCommandError
6
7
7
8
# Enables debugging of GitPython's git commands
8
9
GIT_PYTHON_TRACE = os .environ .get ("GIT_PYTHON_TRACE" , False )
@@ -22,6 +23,7 @@ def get_dir(self):
22
23
def execute (self , command ,
23
24
istream = None ,
24
25
with_status = False ,
26
+ with_exceptions = False ,
25
27
):
26
28
"""
27
29
Handles executing the command on the shell and consumes and returns
@@ -36,6 +38,8 @@ def execute(self, command,
36
38
``with_status``
37
39
Whether to return a (status, str) tuple.
38
40
41
+ ``with_exceptions``
42
+ Whether to raise an exception when git returns a non-zero status.
39
43
Returns
40
44
str(output) # with_status = False (Default)
41
45
tuple(int(status), str(output)) # with_status = True
@@ -56,6 +60,10 @@ def execute(self, command,
56
60
proc .stdout .close ()
57
61
# Grab the exit status
58
62
status = proc .poll ()
63
+ if with_exceptions and status != 0 :
64
+ raise GitCommandError ("%s returned exit status %d"
65
+ % ( str (command ), status ))
66
+
59
67
# Allow access to the command's status code
60
68
if with_status :
61
69
return (status , stdout_value )
@@ -107,6 +115,7 @@ def method_missing(self, method, *args, **kwargs):
107
115
# otherwise these'll end up in args, which is bad.
108
116
istream = pop_key (kwargs , "istream" )
109
117
with_status = pop_key (kwargs , "with_status" )
118
+ with_exceptions = pop_key (kwargs , "with_exceptions" )
110
119
# Prepare the argument list
111
120
opt_args = self .transform_kwargs (** kwargs )
112
121
ext_args = map (str , args )
@@ -118,4 +127,5 @@ def method_missing(self, method, *args, **kwargs):
118
127
return self .execute (call ,
119
128
istream = istream ,
120
129
with_status = with_status ,
130
+ with_exceptions = with_exceptions ,
121
131
)
0 commit comments