Skip to content

Configurable chunk size #681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
)


execute_kwargs = {'istream', 'with_extended_output', 'with_exceptions',
'as_process', 'stdout_as_string', 'output_stream',
'with_stdout', 'kill_after_timeout', 'universal_newlines',
'shell', 'env'}
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'}

log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
Expand Down Expand Up @@ -174,8 +174,6 @@ def __setstate__(self, d):
dict_to_slots_and__excluded_are_none(self, d, excluded=self._excluded_)

# CONFIGURATION
# The size in bytes read from stdout when copying git's output to another stream
max_chunk_size = io.DEFAULT_BUFFER_SIZE

git_exec_name = "git" # default that should work on linux and windows

Expand Down Expand Up @@ -597,6 +595,7 @@ def execute(self, command,
universal_newlines=False,
shell=None,
env=None,
max_chunk_size=io.DEFAULT_BUFFER_SIZE,
**subprocess_kwargs
):
"""Handles executing the command on the shell and consumes and returns
Expand Down Expand Up @@ -642,6 +641,11 @@ def execute(self, command,

:param env:
A dictionary of environment variables to be passed to `subprocess.Popen`.

:param max_chunk_size:
Maximum number of bytes in one chunk of data passed to the output_stream in
one invocation of write() method. If the given number is not positive then
the default value is used.

:param subprocess_kwargs:
Keyword arguments to be passed to subprocess.Popen. Please note that
Expand Down Expand Up @@ -788,7 +792,8 @@ def _kill_process(pid):
stderr_value = stderr_value[:-1]
status = proc.returncode
else:
stream_copy(proc.stdout, output_stream, self.max_chunk_size)
max_chunk_size = max_chunk_size if max_chunk_size and max_chunk_size > 0 else io.DEFAULT_BUFFER_SIZE
stream_copy(proc.stdout, output_stream, max_chunk_size)
stdout_value = output_stream
stderr_value = proc.stderr.read()
# strip trailing "\n"
Expand Down
17 changes: 17 additions & 0 deletions git/test/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
import glob
import io
from io import BytesIO
import itertools
import os
Expand Down Expand Up @@ -220,6 +221,22 @@ def test_clone_from_pathlib(self, rw_dir):

Repo.clone_from(original_repo.git_dir, pathlib.Path(rw_dir) / "clone_pathlib")

@with_rw_repo('HEAD')
def test_max_chunk_size(self, repo):
class TestOutputStream(object):
def __init__(self, max_chunk_size):
self.max_chunk_size = max_chunk_size

def write(self, b):
assert_true(len(b) <= self.max_chunk_size)

for chunk_size in [16, 128, 1024]:
repo.git.status(output_stream=TestOutputStream(chunk_size), max_chunk_size=chunk_size)

repo.git.log(n=100, output_stream=TestOutputStream(io.DEFAULT_BUFFER_SIZE), max_chunk_size=None)
repo.git.log(n=100, output_stream=TestOutputStream(io.DEFAULT_BUFFER_SIZE), max_chunk_size=-10)
repo.git.log(n=100, output_stream=TestOutputStream(io.DEFAULT_BUFFER_SIZE))

def test_init(self):
prev_cwd = os.getcwd()
os.chdir(tempfile.gettempdir())
Expand Down