Skip to content
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

Add support for push options #1282

Merged
merged 3 commits into from
Mar 27, 2024
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
4 changes: 2 additions & 2 deletions pygit2/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def remove(self, path, level=0):
def remove_all(self, pathspecs):
"""Remove all index entries matching pathspecs."""
with StrArray(pathspecs) as arr:
err = C.git_index_remove_all(self._index, arr, ffi.NULL, ffi.NULL)
err = C.git_index_remove_all(self._index, arr.ptr, ffi.NULL, ffi.NULL)
check_error(err, io=True)

def add_all(self, pathspecs=None):
Expand All @@ -190,7 +190,7 @@ def add_all(self, pathspecs=None):
"""
pathspecs = pathspecs or []
with StrArray(pathspecs) as arr:
err = C.git_index_add_all(self._index, arr, 0, ffi.NULL, ffi.NULL)
err = C.git_index_add_all(self._index, arr.ptr, 0, ffi.NULL, ffi.NULL)
check_error(err, io=True)

def add(self, path_or_entry):
Expand Down
13 changes: 9 additions & 4 deletions pygit2/remotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def fetch(
opts.depth = depth
self.__set_proxy(opts.proxy_opts, proxy)
with StrArray(refspecs) as arr:
err = C.git_remote_fetch(self._remote, arr, opts, to_bytes(message))
err = C.git_remote_fetch(self._remote, arr.ptr, opts, to_bytes(message))
payload.check_error(err)

return TransferProgress(C.git_remote_stats(self._remote))
Expand Down Expand Up @@ -236,7 +236,7 @@ def push_refspecs(self):
check_error(err)
return strarray_to_strings(specs)

def push(self, specs, callbacks=None, proxy=None):
def push(self, specs, callbacks=None, proxy=None, push_options=None):
"""
Push the given refspec to the remote. Raises ``GitError`` on protocol
error or unpack failure.
Expand All @@ -258,12 +258,17 @@ def push(self, specs, callbacks=None, proxy=None):
* `None` (the default) to disable proxy usage
* `True` to enable automatic proxy detection
* an url to a proxy (`http://proxy.example.org:3128/`)

push_options : [str]
Push options to send to the server, which passes them to the
pre-receive as well as the post-receive hook.
"""
with git_push_options(callbacks) as payload:
opts = payload.push_options
self.__set_proxy(opts.proxy_opts, proxy)
with StrArray(specs) as refspecs:
err = C.git_remote_push(self._remote, refspecs, opts)
with StrArray(specs) as refspecs, StrArray(push_options) as pushopts:
pushopts.assign_to(opts.remote_push_options)
err = C.git_remote_push(self._remote, refspecs.ptr, opts)
payload.check_error(err)

def __set_proxy(self, proxy_opts, proxy):
Expand Down
26 changes: 24 additions & 2 deletions pygit2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,17 @@ class StrArray:
list of strings. This has a context manager, which you should use, e.g.

with StrArray(list_of_strings) as arr:
C.git_function_that_takes_strarray(arr)
C.git_function_that_takes_strarray(arr.ptr)

To make a pre-existing git_strarray point to the provided list of strings,
use the context manager's assign_to() method:

struct = ffi.new('git_strarray *', [ffi.NULL, 0])
with StrArray(list_of_strings) as arr:
arr.assign_to(struct)

The above construct is still subject to FFI scoping rules, i.e. the
contents of 'struct' only remain valid within the StrArray context.
"""

def __init__(self, l):
Expand All @@ -117,11 +127,23 @@ def __init__(self, l):
self.array = ffi.new('git_strarray *', [self._arr, len(strings)])

def __enter__(self):
return self.array
return self

def __exit__(self, type, value, traceback):
pass

@property
def ptr(self):
return self.array
kempniu marked this conversation as resolved.
Show resolved Hide resolved

def assign_to(self, git_strarray):
if self.array == ffi.NULL:
git_strarray.strings = ffi.NULL
git_strarray.count = 0
else:
git_strarray.strings = self._arr
git_strarray.count = len(self._strings)


class GenericIterator:
"""Helper to easily implement an iterator.
Expand Down
24 changes: 24 additions & 0 deletions test/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@

"""Tests for Remote objects."""

from unittest.mock import patch
import sys

import pytest

import pygit2
from pygit2 import Oid
from pygit2.ffi import ffi
from . import utils


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

with pytest.raises(pygit2.GitError):
remote.push(['refs/heads/master'])


@patch.object(pygit2.callbacks, 'RemoteCallbacks')
def test_push_options(mock_callbacks, origin, clone, remote):
remote.push(['refs/heads/master'])
remote_push_options = mock_callbacks.return_value.push_options.remote_push_options
assert remote_push_options.count == 0

remote.push(['refs/heads/master'], push_options=[])
remote_push_options = mock_callbacks.return_value.push_options.remote_push_options
assert remote_push_options.count == 0

remote.push(['refs/heads/master'], push_options=['foo'])
remote_push_options = mock_callbacks.return_value.push_options.remote_push_options
assert remote_push_options.count == 1
assert ffi.string(remote_push_options.strings[0]) == b'foo'

remote.push(['refs/heads/master'], push_options=['Option A', 'Option B'])
remote_push_options = mock_callbacks.return_value.push_options.remote_push_options
assert remote_push_options.count == 2
assert ffi.string(remote_push_options.strings[0]) == b'Option A'
assert ffi.string(remote_push_options.strings[1]) == b'Option B'