Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0e90a8f

Browse files
committed
Merge pull requestgitpython-developers#79 from znerol/feature/master/cmd-output-strip
Add an output_strip kwarg to Git.execute
2 parents011d89d +a300e32 commit0e90a8f

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

‎git/cmd.py

Lines changed: 8 additions & 2 deletions
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
ifoutput_streamisNone:
360366
stdout_value,stderr_value=proc.communicate()
361367
# strip trailing "\n"
362-
ifstdout_value.endswith("\n"):
368+
ifstdout_value.endswith("\n")andoutput_strip:
363369
stdout_value=stdout_value[:-1]
364370
ifstderr_value.endswith("\n"):
365371
stderr_value=stderr_value[:-1]

‎git/test/test_cmd.py

Lines changed: 22 additions & 0 deletions
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+
deftest_output_strip(self):
113+
importsubprocessassp
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)

[8]ページ先頭

©2009-2025 Movatter.jp