Skip to content

Commit d5cee4a

Browse files
authored
Merge pull request #1423 from toku-sa-n/strip_newline_option
feat(cmd): add the `strip_newline` flag
2 parents 0b33576 + 17b2b12 commit d5cee4a

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ Contributors are:
4545
-Alba Mendez <me _at_ alba.sh>
4646
-Robert Westman <robert _at_ byteflux.io>
4747
-Hugo van Kemenade
48+
-Hiroki Tokunaga <tokusan441 _at_ gmail.com>
4849
Portions derived from other open source works and are clearly marked.

git/cmd.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
execute_kwargs = {'istream', 'with_extended_output',
5656
'with_exceptions', 'as_process', 'stdout_as_string',
5757
'output_stream', 'with_stdout', 'kill_after_timeout',
58-
'universal_newlines', 'shell', 'env', 'max_chunk_size'}
58+
'universal_newlines', 'shell', 'env', 'max_chunk_size', 'strip_newline_in_stdout'}
5959

6060
log = logging.getLogger(__name__)
6161
log.addHandler(logging.NullHandler())
@@ -738,6 +738,7 @@ def execute(self,
738738
shell: Union[None, bool] = None,
739739
env: Union[None, Mapping[str, str]] = None,
740740
max_chunk_size: int = io.DEFAULT_BUFFER_SIZE,
741+
strip_newline_in_stdout: bool = True,
741742
**subprocess_kwargs: Any
742743
) -> Union[str, bytes, Tuple[int, Union[str, bytes], str], AutoInterrupt]:
743744
"""Handles executing the command on the shell and consumes and returns
@@ -810,7 +811,8 @@ def execute(self,
810811
effects on a repository. For example, stale locks in case of git gc could
811812
render the repository incapable of accepting changes until the lock is manually
812813
removed.
813-
814+
:param strip_newline_in_stdout:
815+
Whether to strip the trailing `\n` of the command stdout.
814816
:return:
815817
* str(output) if extended_output = False (Default)
816818
* tuple(int(status), str(stdout), str(stderr)) if extended_output = True
@@ -944,7 +946,7 @@ def _kill_process(pid: int) -> None:
944946
if not universal_newlines:
945947
stderr_value = stderr_value.encode(defenc)
946948
# strip trailing "\n"
947-
if stdout_value.endswith(newline): # type: ignore
949+
if stdout_value.endswith(newline) and strip_newline_in_stdout: # type: ignore
948950
stdout_value = stdout_value[:-1]
949951
if stderr_value.endswith(newline): # type: ignore
950952
stderr_value = stderr_value[:-1]

test/test_repo.py

+10
Original file line numberDiff line numberDiff line change
@@ -1098,3 +1098,13 @@ def test_rebasing(self, rw_dir):
10981098
except GitCommandError:
10991099
pass
11001100
self.assertEqual(r.currently_rebasing_on(), commitSpanish)
1101+
1102+
@with_rw_directory
1103+
def test_do_not_strip_newline_in_stdout(self, rw_dir):
1104+
r = Repo.init(rw_dir)
1105+
fp = osp.join(rw_dir, 'hello.txt')
1106+
with open(fp, 'w') as fs:
1107+
fs.write("hello\n")
1108+
r.git.add(Git.polish_url(fp))
1109+
r.git.commit(message="init")
1110+
self.assertEqual(r.git.show("HEAD:hello.txt", strip_newline_in_stdout=False), 'hello\n')

0 commit comments

Comments
 (0)