diff options
author | David Aguilar <davvid@gmail.com> | 2008-05-29 02:07:41 -0700 |
---|---|---|
committer | David Aguilar <davvid@gmail.com> | 2008-05-29 02:07:41 -0700 |
commit | 58fa2c401856f93712ae6cc45d4dc3458ee4b4f4 (patch) | |
tree | d8058c55e6aca64c4d6fd9831af0ac5c3bed29d4 /lib/git_python | |
parent | dc922f3678a1b01cac2b3f1ec74b831ffec52a71 (diff) | |
download | gitpython-58fa2c401856f93712ae6cc45d4dc3458ee4b4f4.tar.gz |
git.py: add support for git.foo( with_status=True )
Passing with_status to an arbitrary git command causes execute
to return (status_code, output) instead of the typical
returned output.
This is useful when callers need access to the exit status code.
Signed-off-by: David Aguilar <davvid@gmail.com>
Diffstat (limited to 'lib/git_python')
-rw-r--r-- | lib/git_python/git.py | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/git_python/git.py b/lib/git_python/git.py index 01548d4a..de588b36 100644 --- a/lib/git_python/git.py +++ b/lib/git_python/git.py @@ -21,6 +21,7 @@ class Git(MethodMissingMixin): def execute(self, command, istream = None, + with_status = False, ): """ Handles executing the command on the shell and consumes and returns @@ -31,6 +32,13 @@ class Git(MethodMissingMixin): ``istream`` Standard input filehandle passed to subprocess.Popen. + + ``with_status`` + Whether to return a (status, str) tuple. + + Returns + str(output) # with_status = False (Default) + tuple(int(status), str(output)) # with_status = True """ if GIT_PYTHON_TRACE: @@ -46,7 +54,13 @@ class Git(MethodMissingMixin): # Wait for the process to return stdout_value, err = proc.communicate() proc.stdout.close() - return stdout_value + # Grab the exit status + status = proc.poll() + # Allow access to the command's status code + if with_status: + return (status, stdout_value) + else: + return stdout_value def transform_kwargs(self, **kwargs): """ @@ -86,12 +100,13 @@ class Git(MethodMissingMixin): git.rev_list('master', max_count=10, header=True) Returns - str + Same as execute() """ # Handle optional arguments prior to calling transform_kwargs # otherwise these'll end up in args, which is bad. istream = pop_key(kwargs, "istream") + with_status = pop_key(kwargs, "with_status") # Prepare the argument list opt_args = self.transform_kwargs(**kwargs) ext_args = map(str, args) @@ -102,4 +117,5 @@ class Git(MethodMissingMixin): return self.execute(call, istream = istream, + with_status = with_status, ) |