62
62
API.
63
63
"""
64
64
65
+ from __future__ import annotations
66
+
65
67
# Standard Library
66
68
from contextlib import contextmanager
67
69
from functools import wraps
68
- from typing import Optional , Union
70
+ from typing import TYPE_CHECKING , Any , Callable , Optional , Union
69
71
70
72
# pygit2
71
73
from ._pygit2 import DiffFile , Oid
74
76
from .ffi import C , ffi
75
77
from .utils import StrArray , maybe_string , ptr_to_bytes , to_bytes
76
78
79
+ if TYPE_CHECKING :
80
+ from .remotes import Remote , TransferProgress
81
+ from .repository import Repository
82
+
77
83
#
78
84
# The payload is the way to pass information from the pygit2 API, through
79
85
# libgit2, to the Python callbacks. And back.
80
86
#
81
87
82
88
83
89
class Payload :
84
- def __init__ (self , ** kw ):
90
+ def __init__ (self , ** kw : Any ):
85
91
for key , value in kw .items ():
86
92
setattr (self , key , value )
87
- self ._stored_exception = None
93
+ self ._stored_exception : BaseException | None = None
88
94
89
- def check_error (self , error_code ):
95
+ def check_error (self , error_code : int ):
90
96
if error_code == C .GIT_EUSER :
91
97
assert self ._stored_exception is not None
92
98
raise self ._stored_exception
@@ -112,14 +118,18 @@ class RemoteCallbacks(Payload):
112
118
RemoteCallbacks(certificate=certificate).
113
119
"""
114
120
121
+ if TYPE_CHECKING :
122
+ repository : Callable [[str , bool ], Repository ]
123
+ remote : Callable [[Repository , str , str ], Remote ]
124
+
115
125
def __init__ (self , credentials = None , certificate_check = None ):
116
126
super ().__init__ ()
117
127
if credentials is not None :
118
128
self .credentials = credentials
119
129
if certificate_check is not None :
120
130
self .certificate_check = certificate_check
121
131
122
- def sideband_progress (self , string ):
132
+ def sideband_progress (self , string : str ):
123
133
"""
124
134
Progress output callback. Override this function with your own
125
135
progress reporting function
@@ -158,7 +168,7 @@ def credentials(
158
168
"""
159
169
raise Passthrough
160
170
161
- def certificate_check (self , certificate , valid , host ):
171
+ def certificate_check (self , certificate : None , valid : bool , host : str ):
162
172
"""
163
173
Certificate callback. Override with your own function to determine
164
174
whether to accept the server's certificate.
@@ -180,7 +190,7 @@ def certificate_check(self, certificate, valid, host):
180
190
181
191
raise Passthrough
182
192
183
- def transfer_progress (self , stats ):
193
+ def transfer_progress (self , stats : TransferProgress ):
184
194
"""
185
195
Transfer progress callback. Override with your own function to report
186
196
transfer progress.
@@ -191,7 +201,7 @@ def transfer_progress(self, stats):
191
201
The progress up to now.
192
202
"""
193
203
194
- def update_tips (self , refname , old , new ):
204
+ def update_tips (self , refname : str , old : Oid , new : Oid ):
195
205
"""
196
206
Update tips callback. Override with your own function to report
197
207
reference updates.
@@ -208,7 +218,7 @@ def update_tips(self, refname, old, new):
208
218
The reference's new value.
209
219
"""
210
220
211
- def push_update_reference (self , refname , message ):
221
+ def push_update_reference (self , refname : str , message : str | None ):
212
222
"""
213
223
Push update reference callback. Override with your own function to
214
224
report the remote's acceptance or rejection of reference updates.
@@ -415,9 +425,9 @@ def git_remote_callbacks(payload):
415
425
#
416
426
417
427
418
- def libgit2_callback (f ):
428
+ def libgit2_callback (f : Callable [..., int ] ):
419
429
@wraps (f )
420
- def wrapper (* args ) :
430
+ def wrapper (* args : Any ) -> int :
421
431
data = ffi .from_handle (args [- 1 ])
422
432
args = args [:- 1 ] + (data ,)
423
433
try :
@@ -436,10 +446,10 @@ def wrapper(*args):
436
446
return ffi .def_extern ()(wrapper )
437
447
438
448
439
- def libgit2_callback_void (f ):
449
+ def libgit2_callback_void (f : Callable [..., object ] ):
440
450
@wraps (f )
441
- def wrapper (* args ):
442
- data = ffi .from_handle (args [- 1 ])
451
+ def wrapper (* args : Any ):
452
+ data : Payload = ffi .from_handle (args [- 1 ])
443
453
args = args [:- 1 ] + (data ,)
444
454
try :
445
455
f (* args )
@@ -457,7 +467,7 @@ def wrapper(*args):
457
467
458
468
459
469
@libgit2_callback
460
- def _certificate_check_cb (cert_i , valid , host , data ):
470
+ def _certificate_check_cb (cert_i , valid : int , host , data : RemoteCallbacks ):
461
471
# We want to simulate what should happen if libgit2 supported pass-through
462
472
# for this callback. For SSH, 'valid' is always False, because it doesn't
463
473
# look at known_hosts, but we do want to let it through in order to do what
@@ -479,7 +489,7 @@ def _certificate_check_cb(cert_i, valid, host, data):
479
489
480
490
481
491
@libgit2_callback
482
- def _credentials_cb (cred_out , url , username , allowed , data ):
492
+ def _credentials_cb (cred_out , url , username , allowed : int , data : RemoteCallbacks ):
483
493
credentials = getattr (data , 'credentials' , None )
484
494
if not credentials :
485
495
return 0
@@ -493,7 +503,7 @@ def _credentials_cb(cred_out, url, username, allowed, data):
493
503
494
504
495
505
@libgit2_callback
496
- def _push_update_reference_cb (ref , msg , data ):
506
+ def _push_update_reference_cb (ref , msg , data : RemoteCallbacks ):
497
507
push_update_reference = getattr (data , 'push_update_reference' , None )
498
508
if not push_update_reference :
499
509
return 0
@@ -519,7 +529,7 @@ def _remote_create_cb(remote_out, repo, name, url, data):
519
529
520
530
521
531
@libgit2_callback
522
- def _repository_create_cb (repo_out , path , bare , data ):
532
+ def _repository_create_cb (repo_out , path , bare : int , data : RemoteCallbacks ):
523
533
repository = data .repository (ffi .string (path ), bare != 0 )
524
534
# we no longer own the C object
525
535
repository ._disown ()
@@ -529,7 +539,7 @@ def _repository_create_cb(repo_out, path, bare, data):
529
539
530
540
531
541
@libgit2_callback
532
- def _sideband_progress_cb (string , length , data ):
542
+ def _sideband_progress_cb (string , length : int , data : RemoteCallbacks ):
533
543
sideband_progress = getattr (data , 'sideband_progress' , None )
534
544
if not sideband_progress :
535
545
return 0
@@ -540,7 +550,7 @@ def _sideband_progress_cb(string, length, data):
540
550
541
551
542
552
@libgit2_callback
543
- def _transfer_progress_cb (stats_ptr , data ):
553
+ def _transfer_progress_cb (stats_ptr , data : RemoteCallbacks ):
544
554
from .remotes import TransferProgress
545
555
546
556
transfer_progress = getattr (data , 'transfer_progress' , None )
@@ -552,7 +562,7 @@ def _transfer_progress_cb(stats_ptr, data):
552
562
553
563
554
564
@libgit2_callback
555
- def _update_tips_cb (refname , a , b , data ):
565
+ def _update_tips_cb (refname , a , b , data : RemoteCallbacks ):
556
566
update_tips = getattr (data , 'update_tips' , None )
557
567
if not update_tips :
558
568
return 0
@@ -569,7 +579,7 @@ def _update_tips_cb(refname, a, b, data):
569
579
#
570
580
571
581
572
- def get_credentials (fn , url , username , allowed ):
582
+ def get_credentials (fn , url , username , allowed : CredentialType ):
573
583
"""Call fn and return the credentials object."""
574
584
url_str = maybe_string (url )
575
585
username_str = maybe_string (username )
@@ -633,7 +643,7 @@ def get_credentials(fn, url, username, allowed):
633
643
634
644
@libgit2_callback
635
645
def _checkout_notify_cb (
636
- why , path_cstr , baseline , target , workdir , data : CheckoutCallbacks
646
+ why : int , path_cstr , baseline , target , workdir , data : CheckoutCallbacks
637
647
):
638
648
pypath = maybe_string (path_cstr )
639
649
pybaseline = DiffFile .from_c (ptr_to_bytes (baseline ))
@@ -660,8 +670,8 @@ def _checkout_progress_cb(path, completed_steps, total_steps, data: CheckoutCall
660
670
661
671
662
672
def _git_checkout_options (
663
- callbacks = None ,
664
- strategy = None ,
673
+ callbacks : CheckoutCallbacks | None = None ,
674
+ strategy : CheckoutStrategy | None = None ,
665
675
directory = None ,
666
676
paths = None ,
667
677
c_checkout_options_ptr = None ,
@@ -693,7 +703,7 @@ def _git_checkout_options(
693
703
694
704
if paths :
695
705
strarray = StrArray (paths )
696
- refs .append (strarray )
706
+ refs .append (strarray ) # type: ignore
697
707
opts .paths = strarray .ptr [0 ]
698
708
699
709
# If we want to receive any notifications, set up notify_cb in the options
@@ -717,7 +727,12 @@ def _git_checkout_options(
717
727
718
728
719
729
@contextmanager
720
- def git_checkout_options (callbacks = None , strategy = None , directory = None , paths = None ):
730
+ def git_checkout_options (
731
+ callbacks : CheckoutCallbacks | None = None ,
732
+ strategy = None ,
733
+ directory = None ,
734
+ paths = None ,
735
+ ):
721
736
yield _git_checkout_options (
722
737
callbacks = callbacks , strategy = strategy , directory = directory , paths = paths
723
738
)
@@ -746,7 +761,11 @@ def _stash_apply_progress_cb(progress: StashApplyProgress, data: StashApplyCallb
746
761
747
762
@contextmanager
748
763
def git_stash_apply_options (
749
- callbacks = None , reinstate_index = False , strategy = None , directory = None , paths = None
764
+ callbacks : StashApplyCallbacks | None = None ,
765
+ reinstate_index : bool = False ,
766
+ strategy = None ,
767
+ directory = None ,
768
+ paths = None ,
750
769
):
751
770
if callbacks is None :
752
771
callbacks = StashApplyCallbacks ()
0 commit comments