diff options
author | David Aguilar <davvid@gmail.com> | 2008-05-29 02:13:27 -0700 |
---|---|---|
committer | David Aguilar <davvid@gmail.com> | 2008-05-29 02:13:27 -0700 |
commit | 323259cc3d977235f4e7e8980219e5e96d66086e (patch) | |
tree | 817aa0c870bb686999be14d4675bae17d1aafb0a /lib/git_python | |
parent | 80d01f0ef89e287184ba6d07be010ee3f16a74d9 (diff) | |
download | gitpython-323259cc3d977235f4e7e8980219e5e96d66086e.tar.gz |
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>
Diffstat (limited to 'lib/git_python')
-rw-r--r-- | lib/git_python/git.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/git_python/git.py b/lib/git_python/git.py index de588b36..6d5fd1ed 100644 --- a/lib/git_python/git.py +++ b/lib/git_python/git.py @@ -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, ) |