Skip to content

Commit 323259c

Browse files
committed
git.py: add a with_exceptions keyword argument
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>
1 parent 80d01f0 commit 323259c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

lib/git_python/git.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44
from utils import *
55
from method_missing import MethodMissingMixin
6+
from errors import GitCommandError
67

78
# Enables debugging of GitPython's git commands
89
GIT_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

Comments
 (0)