Skip to content

Commit 34b6875

Browse files
committed
Add support for push options
Add a new keyword argument to Remote.push() that is a counterpart of `git push --push-option=<string>`. Fixes #1126
1 parent 7fa10a9 commit 34b6875

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

pygit2/remotes.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def push_refspecs(self):
236236
check_error(err)
237237
return strarray_to_strings(specs)
238238

239-
def push(self, specs, callbacks=None, proxy=None):
239+
def push(self, specs, callbacks=None, proxy=None, push_options=None):
240240
"""
241241
Push the given refspec to the remote. Raises ``GitError`` on protocol
242242
error or unpack failure.
@@ -258,11 +258,16 @@ def push(self, specs, callbacks=None, proxy=None):
258258
* `None` (the default) to disable proxy usage
259259
* `True` to enable automatic proxy detection
260260
* an url to a proxy (`http://proxy.example.org:3128/`)
261+
262+
push_options : [str]
263+
Push options to send to the server, which passes them to the
264+
pre-receive as well as the post-receive hook.
261265
"""
262266
with git_push_options(callbacks) as payload:
263267
opts = payload.push_options
264268
self.__set_proxy(opts.proxy_opts, proxy)
265-
with StrArray(specs) as refspecs:
269+
with StrArray(specs) as refspecs, StrArray(push_options) as pushopts:
270+
pushopts.assign_to(opts.remote_push_options)
266271
err = C.git_remote_push(self._remote, refspecs.ptr, opts)
267272
payload.check_error(err)
268273

test/test_remote.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525

2626
"""Tests for Remote objects."""
2727

28+
from unittest.mock import patch
2829
import sys
2930

3031
import pytest
3132

3233
import pygit2
3334
from pygit2 import Oid
35+
from pygit2.ffi import ffi
3436
from . import utils
3537

3638

@@ -356,3 +358,25 @@ def test_push_non_fast_forward_commits_to_remote_fails(origin, clone, remote):
356358

357359
with pytest.raises(pygit2.GitError):
358360
remote.push(['refs/heads/master'])
361+
362+
363+
@patch.object(pygit2.callbacks, 'RemoteCallbacks')
364+
def test_push_options(mock_callbacks, origin, clone, remote):
365+
remote.push(['refs/heads/master'])
366+
remote_push_options = mock_callbacks.return_value.push_options.remote_push_options
367+
assert remote_push_options.count == 0
368+
369+
remote.push(['refs/heads/master'], push_options=[])
370+
remote_push_options = mock_callbacks.return_value.push_options.remote_push_options
371+
assert remote_push_options.count == 0
372+
373+
remote.push(['refs/heads/master'], push_options=['foo'])
374+
remote_push_options = mock_callbacks.return_value.push_options.remote_push_options
375+
assert remote_push_options.count == 1
376+
assert ffi.string(remote_push_options.strings[0]) == b'foo'
377+
378+
remote.push(['refs/heads/master'], push_options=['Option A', 'Option B'])
379+
remote_push_options = mock_callbacks.return_value.push_options.remote_push_options
380+
assert remote_push_options.count == 2
381+
assert ffi.string(remote_push_options.strings[0]) == b'Option A'
382+
assert ffi.string(remote_push_options.strings[1]) == b'Option B'

0 commit comments

Comments
 (0)