|
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 git_fetch_options, git_push_options, git_proxy_options, git_remote_callbacks |
32 | 32 | from .enums import FetchPrune |
33 | 33 | from .errors import check_error |
34 | 34 | from .ffi import ffi, C |
@@ -107,14 +107,12 @@ def connect(self, callbacks=None, direction=C.GIT_DIRECTION_FETCH, proxy=None): |
107 | 107 | * `True` to enable automatic proxy detection |
108 | 108 | * an url to a proxy (`http://proxy.example.org:3128/`) |
109 | 109 | """ |
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) |
| 110 | + with git_proxy_options(self, proxy=proxy) as proxy_opts: |
| 111 | + with git_remote_callbacks(callbacks) as payload: |
| 112 | + err = C.git_remote_connect( |
| 113 | + self._remote, direction, payload.remote_callbacks, proxy_opts, ffi.NULL |
| 114 | + ) |
| 115 | + payload.check_error(err) |
118 | 116 |
|
119 | 117 | def fetch( |
120 | 118 | self, |
@@ -154,10 +152,10 @@ def fetch( |
154 | 152 | opts = payload.fetch_options |
155 | 153 | opts.prune = prune |
156 | 154 | 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) |
| 155 | + with git_proxy_options(self, payload.fetch_options.proxy_opts, proxy): |
| 156 | + with StrArray(refspecs) as arr: |
| 157 | + err = C.git_remote_fetch(self._remote, arr.ptr, opts, to_bytes(message)) |
| 158 | + payload.check_error(err) |
161 | 159 |
|
162 | 160 | return TransferProgress(C.git_remote_stats(self._remote)) |
163 | 161 |
|
@@ -276,24 +274,11 @@ def push(self, specs, callbacks=None, proxy=None, push_options=None, threads=1): |
276 | 274 | with git_push_options(callbacks) as payload: |
277 | 275 | opts = payload.push_options |
278 | 276 | 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') |
| 277 | + with git_proxy_options(self, payload.push_options.proxy_opts, proxy): |
| 278 | + with StrArray(specs) as refspecs, StrArray(push_options) as pushopts: |
| 279 | + pushopts.assign_to(opts.remote_push_options) |
| 280 | + err = C.git_remote_push(self._remote, refspecs.ptr, opts) |
| 281 | + payload.check_error(err) |
297 | 282 |
|
298 | 283 |
|
299 | 284 | class RemoteCollection: |
|
0 commit comments