25
25
26
26
# Import from pygit2
27
27
from ._pygit2 import Oid
28
+ from .callbacks import git_fetch_options , git_push_options , git_remote_callbacks
28
29
from .errors import check_error , Passthrough
29
30
from .ffi import ffi , C
30
31
from .refspec import Refspec
@@ -182,41 +183,6 @@ def push_update_reference(self, refname, message):
182
183
Rejection message from the remote. If None, the update was accepted.
183
184
"""
184
185
185
- def _fill_fetch_options (self , fetch_opts ):
186
- fetch_opts .callbacks .sideband_progress = C ._sideband_progress_cb
187
- fetch_opts .callbacks .transfer_progress = C ._transfer_progress_cb
188
- fetch_opts .callbacks .update_tips = C ._update_tips_cb
189
- fetch_opts .callbacks .credentials = C ._credentials_cb
190
- fetch_opts .callbacks .certificate_check = C ._certificate_cb
191
- # We need to make sure that this handle stays alive
192
- self ._self_handle = ffi .new_handle (self )
193
- fetch_opts .callbacks .payload = self ._self_handle
194
-
195
- self ._stored_exception = None
196
-
197
- def _fill_push_options (self , push_opts ):
198
- push_opts .callbacks .sideband_progress = C ._sideband_progress_cb
199
- push_opts .callbacks .transfer_progress = C ._transfer_progress_cb
200
- push_opts .callbacks .update_tips = C ._update_tips_cb
201
- push_opts .callbacks .credentials = C ._credentials_cb
202
- push_opts .callbacks .certificate_check = C ._certificate_cb
203
- push_opts .callbacks .push_update_reference = C ._push_update_reference_cb
204
- # We need to make sure that this handle stays alive
205
- self ._self_handle = ffi .new_handle (self )
206
- push_opts .callbacks .payload = self ._self_handle
207
-
208
- def _fill_prune_callbacks (self , prune_callbacks ):
209
- prune_callbacks .update_tips = C ._update_tips_cb
210
- # We need to make sure that this handle stays alive
211
- self ._self_handle = ffi .new_handle (self )
212
- prune_callbacks .payload = self ._self_handle
213
-
214
- def _fill_connect_callbacks (self , connect_callbacks ):
215
- connect_callbacks .credentials = C ._credentials_cb
216
- # We need to make sure that this handle stays alive
217
- self ._self_handle = ffi .new_handle (self )
218
- connect_callbacks .payload = self ._self_handle
219
-
220
186
221
187
# These functions exist to be called by the git_remote as callbacks. They proxy
222
188
# the call to whatever the user set
@@ -374,21 +340,15 @@ def push_url(self):
374
340
return maybe_string (C .git_remote_pushurl (self ._remote ))
375
341
376
342
def connect (self , callbacks = None , direction = C .GIT_DIRECTION_FETCH ):
377
- """Connect to the remote."""
378
-
379
- remote_callbacks = ffi .new ('git_remote_callbacks *' )
380
- C .git_remote_init_callbacks (remote_callbacks , C .GIT_REMOTE_CALLBACKS_VERSION )
381
-
382
- if callbacks is None :
383
- callbacks = RemoteCallbacks ()
384
- callbacks ._fill_connect_callbacks (remote_callbacks )
385
-
386
- err = C .git_remote_connect (self ._remote , direction , remote_callbacks , ffi .NULL , ffi .NULL );
387
- check_error (err )
343
+ """Connect to the remote.
344
+ """
345
+ with git_remote_callbacks (callbacks ) as (remote_callbacks , cb ):
346
+ err = C .git_remote_connect (self ._remote , direction , remote_callbacks , ffi .NULL , ffi .NULL )
347
+ check_error (err , cb )
388
348
389
349
def save (self ):
390
- """Save a remote to its repository's configuration."""
391
-
350
+ """Save a remote to its repository's configuration.
351
+ """
392
352
err = C .git_remote_save (self ._remote )
393
353
check_error (err )
394
354
@@ -405,26 +365,12 @@ def fetch(self, refspecs=None, message=None, callbacks=None, prune=C.GIT_FETCH_P
405
365
repository that does not exist in the remote and the last will
406
366
always keep the remote branches
407
367
"""
408
-
409
368
message = to_bytes (message )
410
-
411
- opts = ffi .new ('git_fetch_options *' )
412
- err = C .git_fetch_init_options (opts , C .GIT_FETCH_OPTIONS_VERSION )
413
-
414
- if callbacks is None :
415
- callbacks = RemoteCallbacks ()
416
- callbacks ._fill_fetch_options (opts )
417
-
418
- opts .prune = prune
419
-
420
- try :
369
+ with git_fetch_options (callbacks ) as (opts , cb ):
370
+ opts .prune = prune
421
371
with StrArray (refspecs ) as arr :
422
- err = C .git_remote_fetch (self ._remote , arr , opts , message )
423
- if callbacks ._stored_exception :
424
- raise callbacks ._stored_exception
425
- check_error (err )
426
- finally :
427
- callbacks ._self_handle = None
372
+ err = C .git_remote_fetch (self ._remote , arr , opts , to_bytes (message ))
373
+ check_error (err , cb )
428
374
429
375
return TransferProgress (C .git_remote_stats (self ._remote ))
430
376
@@ -443,11 +389,8 @@ def ls_remotes(self, callbacks=None):
443
389
check_error (err )
444
390
445
391
results = []
446
-
447
392
for i in range (int (refs_len [0 ])):
448
-
449
393
local = bool (refs [0 ][i ].local )
450
-
451
394
if local :
452
395
loid = Oid (raw = bytes (ffi .buffer (refs [0 ][i ].loid .id )[:]))
453
396
else :
@@ -466,17 +409,11 @@ def ls_remotes(self, callbacks=None):
466
409
return results
467
410
468
411
def prune (self , callbacks = None ):
469
- """Perform a prune against this remote."""
470
-
471
- remote_callbacks = ffi .new ('git_remote_callbacks *' )
472
- C .git_remote_init_callbacks (remote_callbacks , C .GIT_REMOTE_CALLBACKS_VERSION )
473
-
474
- if callbacks is None :
475
- callbacks = RemoteCallbacks ()
476
- callbacks ._fill_prune_callbacks (remote_callbacks )
477
-
478
- err = C .git_remote_prune (self ._remote , remote_callbacks )
479
- check_error (err )
412
+ """Perform a prune against this remote.
413
+ """
414
+ with git_remote_callbacks (callbacks ) as (remote_callbacks , cb ):
415
+ err = C .git_remote_prune (self ._remote , remote_callbacks )
416
+ check_error (err , cb )
480
417
481
418
@property
482
419
def refspec_count (self ):
@@ -525,20 +462,11 @@ def push(self, specs, callbacks=None):
525
462
specs : [str]
526
463
Push refspecs to use.
527
464
"""
528
- push_opts = ffi .new ('git_push_options *' )
529
- err = C .git_push_init_options (push_opts , C .GIT_PUSH_OPTIONS_VERSION )
530
-
531
- if callbacks is None :
532
- callbacks = RemoteCallbacks ()
533
- callbacks ._fill_push_options (push_opts )
534
-
535
- # Build custom callback structure
536
- try :
465
+ with git_push_options (callbacks ) as (opts , cb ):
537
466
with StrArray (specs ) as refspecs :
538
- err = C .git_remote_push (self ._remote , refspecs , push_opts )
539
- check_error (err )
540
- finally :
541
- callbacks ._self_handle = None
467
+ err = C .git_remote_push (self ._remote , refspecs , opts )
468
+ check_error (err , cb )
469
+
542
470
543
471
def get_credentials (fn , url , username , allowed ):
544
472
"""Call fn and return the credentials object"""
0 commit comments