| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 011d89d commit a300e32
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,7 +19,7 @@ | |||
| 19 | 19 | ||
| 20 | 20 | execute_kwargs = ('istream', 'with_keep_cwd', 'with_extended_output', | |
| 21 | 21 | 'with_exceptions', 'as_process', | |
| 22 | - 'output_stream' ) | ||
| 22 | + 'output_stream', 'output_strip' ) | ||
| 23 | 23 | ||
| 24 | 24 | __all__ = ('Git', ) | |
| 25 | 25 | ||
@@ -267,6 +267,7 @@ def execute(self, command, | |||
| 267 | 267 | with_exceptions=True, | |
| 268 | 268 | as_process=False, | |
| 269 | 269 | output_stream=None, | |
| 270 | + output_strip=True, | ||
| 270 | 271 | **subprocess_kwargs | |
| 271 | 272 | ): | |
| 272 | 273 | """Handles executing the command on the shell and consumes and returns | |
@@ -309,6 +310,11 @@ def execute(self, command, | |||
| 309 | 310 | This merely is a workaround as data will be copied from the | |
| 310 | 311 | output pipe to the given output stream directly. | |
| 311 | 312 | ||
| 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 | + | ||
| 312 | 318 | :param subprocess_kwargs: | |
| 313 | 319 | Keyword arguments to be passed to subprocess.Popen. Please note that | |
| 314 | 320 | some of the valid kwargs are already set by this method, the ones you | |
@@ -359,7 +365,7 @@ def execute(self, command, | |||
| 359 | 365 | if output_stream is None: | |
| 360 | 366 | stdout_value, stderr_value = proc.communicate() | |
| 361 | 367 | # strip trailing "\n" | |
| 362 | - if stdout_value.endswith("\n"): | ||
| 368 | + if stdout_value.endswith("\n") and output_strip: | ||
| 363 | 369 | stdout_value = stdout_value[:-1] | |
| 364 | 370 | if stderr_value.endswith("\n"): | |
| 365 | 371 | stderr_value = stderr_value[:-1] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -108,3 +108,25 @@ def test_cmd_override(self): | |||
| 108 | 108 | finally: | |
| 109 | 109 | type(self.git).GIT_PYTHON_GIT_EXECUTABLE = prev_cmd | |
| 110 | 110 | #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) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments