summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiroki Tokunaga <tokusan441@gmail.com>2022-04-06 23:08:36 +0900
committerHiroki Tokunaga <tokusan441@gmail.com>2022-04-06 23:08:36 +0900
commite4360aea32aad11bf3c54b0dc0a6cabb21b5e687 (patch)
tree5d8092a2fbfeef22e70953ed70753ff4bb603435
parent0b33576f8e7add5671f8927dff228e7f92eec076 (diff)
downloadgitpython-e4360aea32aad11bf3c54b0dc0a6cabb21b5e687.tar.gz
feat(cmd): add the `strip_newline` flag
This commit adds the `strip_newline` flag to the `Git.execute` method. When this flag is set to `True`, it will trim the trailing `\n`. The default value is `True` for backward compatibility. Setting it to `False` is helpful for, e.g., the `git show` output, especially with the binary file, as the missing `\n` may invalidate the file.
-rw-r--r--git/cmd.py7
-rw-r--r--test/test_repo.py10
2 files changed, 15 insertions, 2 deletions
diff --git a/git/cmd.py b/git/cmd.py
index 4f056987..9c5da89d 100644
--- a/git/cmd.py
+++ b/git/cmd.py
@@ -55,7 +55,7 @@ if TYPE_CHECKING:
execute_kwargs = {'istream', 'with_extended_output',
'with_exceptions', 'as_process', 'stdout_as_string',
'output_stream', 'with_stdout', 'kill_after_timeout',
- 'universal_newlines', 'shell', 'env', 'max_chunk_size'}
+ 'universal_newlines', 'shell', 'env', 'max_chunk_size', 'strip_newline'}
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
@@ -738,6 +738,7 @@ class Git(LazyMixin):
shell: Union[None, bool] = None,
env: Union[None, Mapping[str, str]] = None,
max_chunk_size: int = io.DEFAULT_BUFFER_SIZE,
+ strip_newline: bool = True,
**subprocess_kwargs: Any
) -> Union[str, bytes, Tuple[int, Union[str, bytes], str], AutoInterrupt]:
"""Handles executing the command on the shell and consumes and returns
@@ -810,6 +811,8 @@ class Git(LazyMixin):
effects on a repository. For example, stale locks in case of git gc could
render the repository incapable of accepting changes until the lock is manually
removed.
+ :param strip_newline:
+ Whether to strip the trailing '\n' of the command output.
:return:
* str(output) if extended_output = False (Default)
@@ -944,7 +947,7 @@ class Git(LazyMixin):
if not universal_newlines:
stderr_value = stderr_value.encode(defenc)
# strip trailing "\n"
- if stdout_value.endswith(newline): # type: ignore
+ if stdout_value.endswith(newline) and strip_newline: # type: ignore
stdout_value = stdout_value[:-1]
if stderr_value.endswith(newline): # type: ignore
stderr_value = stderr_value[:-1]
diff --git a/test/test_repo.py b/test/test_repo.py
index 6d617609..14339f57 100644
--- a/test/test_repo.py
+++ b/test/test_repo.py
@@ -1098,3 +1098,13 @@ class TestRepo(TestBase):
except GitCommandError:
pass
self.assertEqual(r.currently_rebasing_on(), commitSpanish)
+
+ @with_rw_directory
+ def test_do_not_strip_newline(self, rw_dir):
+ r = Repo.init(rw_dir)
+ fp = osp.join(rw_dir, 'hello.txt')
+ with open(fp, 'w') as fs:
+ fs.write("hello\n")
+ r.git.add(Git.polish_url(fp))
+ r.git.commit(message="init")
+ self.assertEqual(r.git.show("HEAD:hello.txt", strip_newline=False), 'hello\n')