summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Schori <lo@znerol.ch>2012-10-18 21:15:06 +0200
committerLorenz Schori <lo@znerol.ch>2012-10-18 21:15:06 +0200
commita300e32dc8d965c8552a0cfdfb5f8734347b582e (patch)
tree0bb12d8bd5e7c7e42ca1dfb0142db97677f2d90d
parent011d89d3187f4436383f7e7159c105b96a83bb80 (diff)
downloadgitpython-a300e32dc8d965c8552a0cfdfb5f8734347b582e.tar.gz
Add an output_strip kwarg to Git.execute
Strip the last line of the output if it is empty (default). Stripping should be disabled whenever it is important that the output is not modified in any way. For example when retrieving patch files using git-diff.
-rw-r--r--git/cmd.py10
-rw-r--r--git/test/test_cmd.py22
2 files changed, 30 insertions, 2 deletions
diff --git a/git/cmd.py b/git/cmd.py
index 46a350e8..7abfb611 100644
--- a/git/cmd.py
+++ b/git/cmd.py
@@ -19,7 +19,7 @@ from subprocess import (
execute_kwargs = ('istream', 'with_keep_cwd', 'with_extended_output',
'with_exceptions', 'as_process',
- 'output_stream' )
+ 'output_stream', 'output_strip' )
__all__ = ('Git', )
@@ -267,6 +267,7 @@ class Git(LazyMixin):
with_exceptions=True,
as_process=False,
output_stream=None,
+ output_strip=True,
**subprocess_kwargs
):
"""Handles executing the command on the shell and consumes and returns
@@ -309,6 +310,11 @@ class Git(LazyMixin):
This merely is a workaround as data will be copied from the
output pipe to the given output stream directly.
+ :param output_strip:
+ Strip the last line of the output if it is empty (default). Stripping should
+ be disabled whenever it is important that the output is not modified in any
+ way. For example when retrieving patch files using git-diff.
+
:param subprocess_kwargs:
Keyword arguments to be passed to subprocess.Popen. Please note that
some of the valid kwargs are already set by this method, the ones you
@@ -359,7 +365,7 @@ class Git(LazyMixin):
if output_stream is None:
stdout_value, stderr_value = proc.communicate()
# strip trailing "\n"
- if stdout_value.endswith("\n"):
+ if stdout_value.endswith("\n") and output_strip:
stdout_value = stdout_value[:-1]
if stderr_value.endswith("\n"):
stderr_value = stderr_value[:-1]
diff --git a/git/test/test_cmd.py b/git/test/test_cmd.py
index 2d38e0a8..b5732339 100644
--- a/git/test/test_cmd.py
+++ b/git/test/test_cmd.py
@@ -108,3 +108,25 @@ class TestGit(TestBase):
finally:
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = prev_cmd
#END undo adjustment
+
+ def test_output_strip(self):
+ import subprocess as sp
+ hexsha = "b2339455342180c7cc1e9bba3e9f181f7baa5167"
+
+ # Verify that a trailing newline is stripped from the output of a git
+ # command.
+ content = self.git.cat_file('blob', hexsha)
+ g = self.git.hash_object(istream=sp.PIPE, as_process=True, stdin=True)
+ g.stdin.write(content)
+ g.stdin.close()
+ newsha = g.stdout.readline().strip()
+ self.assertNotEquals(newsha, hexsha)
+
+ # Verify that output of a git command which ends with an empty
+ # line is not modified when the output_strip flag is cleared.
+ content = self.git.cat_file('blob', hexsha, output_strip=False)
+ g = self.git.hash_object(istream=sp.PIPE, as_process=True, stdin=True)
+ g.stdin.write(content)
+ g.stdin.close()
+ newsha = g.stdout.readline().strip()
+ self.assertEquals(newsha, hexsha)