From e4360aea32aad11bf3c54b0dc0a6cabb21b5e687 Mon Sep 17 00:00:00 2001 From: Hiroki Tokunaga Date: Wed, 6 Apr 2022 23:08:36 +0900 Subject: 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. --- git/cmd.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'git/cmd.py') 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] -- cgit v1.2.1