Skip to content

Commit a300e32

Browse files
committed
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.
1 parent 011d89d commit a300e32

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

git/cmd.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
execute_kwargs = ('istream', 'with_keep_cwd', 'with_extended_output',
2121
'with_exceptions', 'as_process',
22-
'output_stream' )
22+
'output_stream', 'output_strip' )
2323

2424
__all__ = ('Git', )
2525

@@ -267,6 +267,7 @@ def execute(self, command,
267267
with_exceptions=True,
268268
as_process=False,
269269
output_stream=None,
270+
output_strip=True,
270271
**subprocess_kwargs
271272
):
272273
"""Handles executing the command on the shell and consumes and returns
@@ -309,6 +310,11 @@ def execute(self, command,
309310
This merely is a workaround as data will be copied from the
310311
output pipe to the given output stream directly.
311312
313+
:param output_strip:
314+
Strip the last line of the output if it is empty (default). Stripping should
315+
be disabled whenever it is important that the output is not modified in any
316+
way. For example when retrieving patch files using git-diff.
317+
312318
:param subprocess_kwargs:
313319
Keyword arguments to be passed to subprocess.Popen. Please note that
314320
some of the valid kwargs are already set by this method, the ones you
@@ -359,7 +365,7 @@ def execute(self, command,
359365
if output_stream is None:
360366
stdout_value, stderr_value = proc.communicate()
361367
# strip trailing "\n"
362-
if stdout_value.endswith("\n"):
368+
if stdout_value.endswith("\n") and output_strip:
363369
stdout_value = stdout_value[:-1]
364370
if stderr_value.endswith("\n"):
365371
stderr_value = stderr_value[:-1]

git/test/test_cmd.py

+22
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,25 @@ def test_cmd_override(self):
108108
finally:
109109
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = prev_cmd
110110
#END undo adjustment
111+
112+
def test_output_strip(self):
113+
import subprocess as sp
114+
hexsha = "b2339455342180c7cc1e9bba3e9f181f7baa5167"
115+
116+
# Verify that a trailing newline is stripped from the output of a git
117+
# command.
118+
content = self.git.cat_file('blob', hexsha)
119+
g = self.git.hash_object(istream=sp.PIPE, as_process=True, stdin=True)
120+
g.stdin.write(content)
121+
g.stdin.close()
122+
newsha = g.stdout.readline().strip()
123+
self.assertNotEquals(newsha, hexsha)
124+
125+
# Verify that output of a git command which ends with an empty
126+
# line is not modified when the output_strip flag is cleared.
127+
content = self.git.cat_file('blob', hexsha, output_strip=False)
128+
g = self.git.hash_object(istream=sp.PIPE, as_process=True, stdin=True)
129+
g.stdin.write(content)
130+
g.stdin.close()
131+
newsha = g.stdout.readline().strip()
132+
self.assertEquals(newsha, hexsha)

0 commit comments

Comments
 (0)