Skip to content

Commit 8874575

Browse files
committed
Enable setting automatic or manual proxy
On methods: - Remote.connect() - Remote.ls_remotes() - Remote.fetch() - Remote.push() See #642
1 parent dbdacd9 commit 8874575

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

pygit2/remote.py

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,24 @@ def push_url(self):
9191

9292
return maybe_string(C.git_remote_pushurl(self._remote))
9393

94-
def connect(self, callbacks=None, direction=C.GIT_DIRECTION_FETCH):
94+
def connect(self, callbacks=None, direction=C.GIT_DIRECTION_FETCH, proxy=None):
9595
"""Connect to the remote.
96+
97+
Parameters:
98+
99+
proxy : None or True or str
100+
Proxy configuration. Can be one of:
101+
102+
* `None` (the default) to disable proxy usage
103+
* `True` to enable automatic proxy detection
104+
* an url to a proxy (`http://proxy.example.org:3128/`)
96105
"""
106+
proxy_opts = ffi.new('git_proxy_options *')
107+
C.git_proxy_init_options(proxy_opts, C.GIT_PROXY_OPTIONS_VERSION)
108+
Remote._set_proxy(proxy_opts, proxy)
97109
with git_remote_callbacks(callbacks) as payload:
98110
err = C.git_remote_connect(self._remote, direction,
99-
payload.remote_callbacks, ffi.NULL,
111+
payload.remote_callbacks, proxy_opts,
100112
ffi.NULL)
101113
payload.check_error(err)
102114

@@ -106,7 +118,7 @@ def save(self):
106118
err = C.git_remote_save(self._remote)
107119
check_error(err)
108120

109-
def fetch(self, refspecs=None, message=None, callbacks=None, prune=C.GIT_FETCH_PRUNE_UNSPECIFIED):
121+
def fetch(self, refspecs=None, message=None, callbacks=None, prune=C.GIT_FETCH_PRUNE_UNSPECIFIED, proxy=None):
110122
"""Perform a fetch against this remote. Returns a <TransferProgress>
111123
object.
112124
@@ -118,24 +130,38 @@ def fetch(self, refspecs=None, message=None, callbacks=None, prune=C.GIT_FETCH_P
118130
repo, the second will remove any remote branch in the local
119131
repository that does not exist in the remote and the last will
120132
always keep the remote branches
133+
134+
proxy : None or True or str
135+
Proxy configuration. Can be one of:
136+
137+
* `None` (the default) to disable proxy usage
138+
* `True` to enable automatic proxy detection
139+
* an url to a proxy (`http://proxy.example.org:3128/`)
121140
"""
122141
message = to_bytes(message)
123142
with git_fetch_options(callbacks) as payload:
124143
opts = payload.fetch_options
125144
opts.prune = prune
145+
Remote._set_proxy(opts.proxy_opts, proxy)
126146
with StrArray(refspecs) as arr:
127147
err = C.git_remote_fetch(self._remote, arr, opts, to_bytes(message))
128148
payload.check_error(err)
129149

130150
return TransferProgress(C.git_remote_stats(self._remote))
131151

132-
def ls_remotes(self, callbacks=None):
152+
def ls_remotes(self, callbacks=None, proxy=None):
133153
"""
134154
Return a list of dicts that maps to `git_remote_head` from a
135155
`ls_remotes` call.
156+
157+
Parameters:
158+
159+
callbacks : Passed to connect()
160+
161+
proxy : Passed to connect()
136162
"""
137163

138-
self.connect(callbacks=callbacks)
164+
self.connect(callbacks=callbacks, proxy=proxy)
139165

140166
refs = ffi.new('git_remote_head ***')
141167
refs_len = ffi.new('size_t *')
@@ -201,7 +227,7 @@ def push_refspecs(self):
201227

202228
return strarray_to_strings(specs)
203229

204-
def push(self, specs, callbacks=None):
230+
def push(self, specs, callbacks=None, proxy=None):
205231
"""
206232
Push the given refspec to the remote. Raises ``GitError`` on protocol
207233
error or unpack failure.
@@ -216,13 +242,33 @@ def push(self, specs, callbacks=None):
216242
217243
specs : [str]
218244
Push refspecs to use.
245+
246+
proxy : None or True or str
247+
Proxy configuration. Can be one of:
248+
249+
* `None` (the default) to disable proxy usage
250+
* `True` to enable automatic proxy detection
251+
* an url to a proxy (`http://proxy.example.org:3128/`)
219252
"""
220253
with git_push_options(callbacks) as payload:
221254
opts = payload.push_options
255+
Remote._set_proxy(opts.proxy_opts, proxy)
222256
with StrArray(specs) as refspecs:
223257
err = C.git_remote_push(self._remote, refspecs, opts)
224258
payload.check_error(err)
225259

260+
@staticmethod
261+
def _set_proxy(proxy_opts, proxy):
262+
if proxy is None:
263+
proxy_opts.type = C.GIT_PROXY_NONE
264+
elif proxy is True:
265+
proxy_opts.type = C.GIT_PROXY_AUTO
266+
elif type(proxy) is str:
267+
proxy_opts.type = C.GIT_PROXY_SPECIFIED
268+
proxy_opts.url = to_bytes(proxy)
269+
else:
270+
raise TypeError("Proxy must be None, True, or a string")
271+
226272

227273
class RemoteCollection:
228274
"""Collection of configured remotes

0 commit comments

Comments
 (0)