|
28 | 28 |
|
29 | 29 | # Import from pygit2 |
30 | 30 | from ._pygit2 import Oid |
31 | | -from .callbacks import git_fetch_options, git_push_options, git_remote_callbacks |
| 31 | +from .callbacks import ( |
| 32 | + git_fetch_options, |
| 33 | + git_push_options, |
| 34 | + git_proxy_options, |
| 35 | + git_remote_callbacks, |
| 36 | +) |
32 | 37 | from .enums import FetchPrune |
33 | 38 | from .errors import check_error |
34 | 39 | from .ffi import ffi, C |
@@ -107,14 +112,16 @@ def connect(self, callbacks=None, direction=C.GIT_DIRECTION_FETCH, proxy=None): |
107 | 112 | * `True` to enable automatic proxy detection |
108 | 113 | * an url to a proxy (`http://proxy.example.org:3128/`) |
109 | 114 | """ |
110 | | - proxy_opts = ffi.new('git_proxy_options *') |
111 | | - C.git_proxy_options_init(proxy_opts, C.GIT_PROXY_OPTIONS_VERSION) |
112 | | - self.__set_proxy(proxy_opts, proxy) |
113 | | - with git_remote_callbacks(callbacks) as payload: |
114 | | - err = C.git_remote_connect( |
115 | | - self._remote, direction, payload.remote_callbacks, proxy_opts, ffi.NULL |
116 | | - ) |
117 | | - payload.check_error(err) |
| 115 | + with git_proxy_options(self, proxy=proxy) as proxy_opts: |
| 116 | + with git_remote_callbacks(callbacks) as payload: |
| 117 | + err = C.git_remote_connect( |
| 118 | + self._remote, |
| 119 | + direction, |
| 120 | + payload.remote_callbacks, |
| 121 | + proxy_opts, |
| 122 | + ffi.NULL, |
| 123 | + ) |
| 124 | + payload.check_error(err) |
118 | 125 |
|
119 | 126 | def fetch( |
120 | 127 | self, |
@@ -154,10 +161,12 @@ def fetch( |
154 | 161 | opts = payload.fetch_options |
155 | 162 | opts.prune = prune |
156 | 163 | opts.depth = depth |
157 | | - self.__set_proxy(opts.proxy_opts, proxy) |
158 | | - with StrArray(refspecs) as arr: |
159 | | - err = C.git_remote_fetch(self._remote, arr.ptr, opts, to_bytes(message)) |
160 | | - payload.check_error(err) |
| 164 | + with git_proxy_options(self, payload.fetch_options.proxy_opts, proxy): |
| 165 | + with StrArray(refspecs) as arr: |
| 166 | + err = C.git_remote_fetch( |
| 167 | + self._remote, arr.ptr, opts, to_bytes(message) |
| 168 | + ) |
| 169 | + payload.check_error(err) |
161 | 170 |
|
162 | 171 | return TransferProgress(C.git_remote_stats(self._remote)) |
163 | 172 |
|
@@ -276,24 +285,11 @@ def push(self, specs, callbacks=None, proxy=None, push_options=None, threads=1): |
276 | 285 | with git_push_options(callbacks) as payload: |
277 | 286 | opts = payload.push_options |
278 | 287 | opts.pb_parallelism = threads |
279 | | - self.__set_proxy(opts.proxy_opts, proxy) |
280 | | - with StrArray(specs) as refspecs, StrArray(push_options) as pushopts: |
281 | | - pushopts.assign_to(opts.remote_push_options) |
282 | | - err = C.git_remote_push(self._remote, refspecs.ptr, opts) |
283 | | - payload.check_error(err) |
284 | | - |
285 | | - def __set_proxy(self, proxy_opts, proxy): |
286 | | - if proxy is None: |
287 | | - proxy_opts.type = C.GIT_PROXY_NONE |
288 | | - elif proxy is True: |
289 | | - proxy_opts.type = C.GIT_PROXY_AUTO |
290 | | - elif type(proxy) is str: |
291 | | - proxy_opts.type = C.GIT_PROXY_SPECIFIED |
292 | | - # Keep url in memory, otherwise memory is freed and bad things happen |
293 | | - self.__url = ffi.new('char[]', to_bytes(proxy)) |
294 | | - proxy_opts.url = self.__url |
295 | | - else: |
296 | | - raise TypeError('Proxy must be None, True, or a string') |
| 288 | + with git_proxy_options(self, payload.push_options.proxy_opts, proxy): |
| 289 | + with StrArray(specs) as refspecs, StrArray(push_options) as pushopts: |
| 290 | + pushopts.assign_to(opts.remote_push_options) |
| 291 | + err = C.git_remote_push(self._remote, refspecs.ptr, opts) |
| 292 | + payload.check_error(err) |
297 | 293 |
|
298 | 294 |
|
299 | 295 | class RemoteCollection: |
|
0 commit comments