Skip to content

Commit 3b11b67

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 3b11b67

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

pygit2/remote.py

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,23 @@ 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+
Remote._set_proxy(proxy_opts, proxy)
97108
with git_remote_callbacks(callbacks) as payload:
98109
err = C.git_remote_connect(self._remote, direction,
99-
payload.remote_callbacks, ffi.NULL,
110+
payload.remote_callbacks, proxy_opts,
100111
ffi.NULL)
101112
payload.check_error(err)
102113

@@ -106,7 +117,7 @@ def save(self):
106117
err = C.git_remote_save(self._remote)
107118
check_error(err)
108119

109-
def fetch(self, refspecs=None, message=None, callbacks=None, prune=C.GIT_FETCH_PRUNE_UNSPECIFIED):
120+
def fetch(self, refspecs=None, message=None, callbacks=None, prune=C.GIT_FETCH_PRUNE_UNSPECIFIED, proxy=None):
110121
"""Perform a fetch against this remote. Returns a <TransferProgress>
111122
object.
112123
@@ -118,24 +129,38 @@ def fetch(self, refspecs=None, message=None, callbacks=None, prune=C.GIT_FETCH_P
118129
repo, the second will remove any remote branch in the local
119130
repository that does not exist in the remote and the last will
120131
always keep the remote branches
132+
133+
proxy : None or True or str
134+
Proxy configuration. Can be one of:
135+
136+
* `None` (the default) to disable proxy usage
137+
* `True` to enable automatic proxy detection
138+
* an url to a proxy (`http://proxy.example.org:3128/`)
121139
"""
122140
message = to_bytes(message)
123141
with git_fetch_options(callbacks) as payload:
124142
opts = payload.fetch_options
125143
opts.prune = prune
144+
Remote._set_proxy(opts.proxy_opts, proxy)
126145
with StrArray(refspecs) as arr:
127146
err = C.git_remote_fetch(self._remote, arr, opts, to_bytes(message))
128147
payload.check_error(err)
129148

130149
return TransferProgress(C.git_remote_stats(self._remote))
131150

132-
def ls_remotes(self, callbacks=None):
151+
def ls_remotes(self, callbacks=None, proxy=None):
133152
"""
134153
Return a list of dicts that maps to `git_remote_head` from a
135154
`ls_remotes` call.
155+
156+
Parameters:
157+
158+
callbacks : Passed to connect()
159+
160+
proxy : Passed to connect()
136161
"""
137162

138-
self.connect(callbacks=callbacks)
163+
self.connect(callbacks=callbacks, proxy=proxy)
139164

140165
refs = ffi.new('git_remote_head ***')
141166
refs_len = ffi.new('size_t *')
@@ -201,7 +226,7 @@ def push_refspecs(self):
201226

202227
return strarray_to_strings(specs)
203228

204-
def push(self, specs, callbacks=None):
229+
def push(self, specs, callbacks=None, proxy=None):
205230
"""
206231
Push the given refspec to the remote. Raises ``GitError`` on protocol
207232
error or unpack failure.
@@ -216,13 +241,33 @@ def push(self, specs, callbacks=None):
216241
217242
specs : [str]
218243
Push refspecs to use.
244+
245+
proxy : None or True or str
246+
Proxy configuration. Can be one of:
247+
248+
* `None` (the default) to disable proxy usage
249+
* `True` to enable automatic proxy detection
250+
* an url to a proxy (`http://proxy.example.org:3128/`)
219251
"""
220252
with git_push_options(callbacks) as payload:
221253
opts = payload.push_options
254+
Remote._set_proxy(opts.proxy_opts, proxy)
222255
with StrArray(specs) as refspecs:
223256
err = C.git_remote_push(self._remote, refspecs, opts)
224257
payload.check_error(err)
225258

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

227272
class RemoteCollection:
228273
"""Collection of configured remotes

0 commit comments

Comments
 (0)