diff options
author | David Aguilar <davvid@gmail.com> | 2008-05-29 01:59:38 -0700 |
---|---|---|
committer | David Aguilar <davvid@gmail.com> | 2008-05-29 01:59:38 -0700 |
commit | 2405cf54ac06140a0821f55b34c1d54f699e8e73 (patch) | |
tree | 4d21880097532f025e8dd9772751b20b3964dd0a | |
parent | 579d1b8174c9be915c0fa17a67fc38cc6de36ad7 (diff) | |
download | gitpython-2405cf54ac06140a0821f55b34c1d54f699e8e73.tar.gz |
git.py: add support for passing in a stdin input stream
execute/method_missing now support:
fh = open('filename', 'r')
print git.foo( istream=fh )
fh.close()
The name "istream" was chosen so that it would not conflict
with any of the builtin git flags. "stdin", for instance,
is used by git.
Signed-off-by: David Aguilar <davvid@gmail.com>
-rw-r--r-- | lib/git_python/git.py | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/lib/git_python/git.py b/lib/git_python/git.py index c66d9461..01548d4a 100644 --- a/lib/git_python/git.py +++ b/lib/git_python/git.py @@ -19,24 +19,32 @@ class Git(MethodMissingMixin): def get_dir(self): return self.git_dir - def execute(self, command): + def execute(self, command, + istream = None, + ): """ Handles executing the command on the shell and consumes and returns the returned information (stdout) ``command`` - The command to execute + The command argument list to execute + + ``istream`` + Standard input filehandle passed to subprocess.Popen. """ if GIT_PYTHON_TRACE: print command + # Start the process proc = subprocess.Popen(command, cwd = self.git_dir, - stdout=subprocess.PIPE + stdin = istream, + stdout = subprocess.PIPE ) - proc.wait() - stdout_value = proc.stdout.read() + + # Wait for the process to return + stdout_value, err = proc.communicate() proc.stdout.close() return stdout_value @@ -70,7 +78,9 @@ class Git(MethodMissingMixin): is the list of arguments ``kwargs`` - is a dict of keyword arguments + is a dict of keyword arguments. + This function accepts the same optional keyword arguments + as execute(). Examples git.rev_list('master', max_count=10, header=True) @@ -78,12 +88,18 @@ class Git(MethodMissingMixin): Returns str """ + + # Handle optional arguments prior to calling transform_kwargs + # otherwise these'll end up in args, which is bad. + istream = pop_key(kwargs, "istream") + # Prepare the argument list opt_args = self.transform_kwargs(**kwargs) - ext_args = map(lambda a: (a == '--') and a or "%s" % a, args) + ext_args = map(str, args) args = opt_args + ext_args call = ["git", dashify(method)] call.extend(args) - stdout_value = self.execute(call) - return stdout_value + return self.execute(call, + istream = istream, + ) |