From de2b4e5374669b72deda714b0b64f71ee54c4869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20K=C4=99pie=C5=84?= Date: Mon, 17 Feb 2025 14:40:45 +0100 Subject: [PATCH] Enable multi-threaded pack file creation libgit2 supports creating pack files using multiple threads, which can significantly speed up a push operation. Enable Remote.push() to take advantage of that capability by adding a relevant keyword argument whose value gets passed to git_remote_push(). --- pygit2/remotes.py | 12 +++++++++++- test/test_remote.py | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pygit2/remotes.py b/pygit2/remotes.py index 30c2e5bd..2322e7f7 100644 --- a/pygit2/remotes.py +++ b/pygit2/remotes.py @@ -237,7 +237,7 @@ def push_refspecs(self): check_error(err) return strarray_to_strings(specs) - def push(self, specs, callbacks=None, proxy=None, push_options=None): + def push(self, specs, callbacks=None, proxy=None, push_options=None, threads=1): """ Push the given refspec to the remote. Raises ``GitError`` on protocol error or unpack failure. @@ -263,9 +263,19 @@ def push(self, specs, callbacks=None, proxy=None, push_options=None): push_options : [str] Push options to send to the server, which passes them to the pre-receive as well as the post-receive hook. + + threads : int + If the transport being used to push to the remote requires the + creation of a pack file, this controls the number of worker threads + used by the packbuilder when creating that pack file to be sent to + the remote. + + If set to 0, the packbuilder will auto-detect the number of threads + to create. The default value is 1. """ with git_push_options(callbacks) as payload: opts = payload.push_options + opts.pb_parallelism = threads self.__set_proxy(opts.proxy_opts, proxy) with StrArray(specs) as refspecs, StrArray(push_options) as pushopts: pushopts.assign_to(opts.remote_push_options) diff --git a/test/test_remote.py b/test/test_remote.py index 1d214733..1d414479 100644 --- a/test/test_remote.py +++ b/test/test_remote.py @@ -466,3 +466,19 @@ def test_push_options(origin, clone, remote): remote_push_options = callbacks.push_options.remote_push_options assert remote_push_options.count == 2 # strings pointed to by remote_push_options.strings[] are already freed + + +def test_push_threads(origin, clone, remote): + from pygit2 import RemoteCallbacks + + callbacks = RemoteCallbacks() + remote.push(['refs/heads/master'], callbacks) + assert callbacks.push_options.pb_parallelism == 1 + + callbacks = RemoteCallbacks() + remote.push(['refs/heads/master'], callbacks, threads=0) + assert callbacks.push_options.pb_parallelism == 0 + + callbacks = RemoteCallbacks() + remote.push(['refs/heads/master'], callbacks, threads=1) + assert callbacks.push_options.pb_parallelism == 1