23
23
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
24
24
# Boston, MA 02110-1301, USA.
25
25
26
+ from __future__ import annotations
27
+
28
+ from typing import TYPE_CHECKING , cast
29
+
26
30
# Import from pygit2
27
31
from ._pygit2 import Oid , Signature
28
32
from .ffi import C , ffi
29
- from .utils import GenericIterator
33
+ from .utils import GenericIterator , buffer_to_bytes , maybe_string
34
+
35
+ if TYPE_CHECKING :
36
+ from _cffi_backend import _CDataBase as CData
30
37
38
+ from ._ctyping import _CHunk , _CSignature
39
+ from .repository import BaseRepository
31
40
32
- def wrap_signature (csig ):
41
+
42
+ def wrap_signature (csig : _CSignature ):
33
43
if not csig :
34
44
return None
35
45
36
46
return Signature (
37
- ffi . string ( csig .name ). decode ( 'utf-8' ),
38
- ffi . string ( csig .email ). decode ( 'utf-8' ),
47
+ cast ( str , maybe_string ( csig .name )),
48
+ cast ( str , maybe_string ( csig .email )),
39
49
csig .when .time ,
40
50
csig .when .offset ,
41
51
'utf-8' ,
42
52
)
43
53
44
54
45
55
class BlameHunk :
56
+ if TYPE_CHECKING :
57
+ _blame : Blame
58
+ _hunk : _CHunk
59
+
46
60
@classmethod
47
- def _from_c (cls , blame , ptr ):
61
+ def _from_c (cls , blame : Blame , ptr : _CHunk ):
48
62
hunk = cls .__new__ (cls )
49
63
hunk ._blame = blame
50
64
hunk ._hunk = ptr
@@ -73,9 +87,7 @@ def final_committer(self):
73
87
74
88
@property
75
89
def final_commit_id (self ):
76
- return Oid (
77
- raw = bytes (ffi .buffer (ffi .addressof (self ._hunk , 'final_commit_id' ))[:])
78
- )
90
+ return Oid (raw = buffer_to_bytes (ffi .addressof (self ._hunk , 'final_commit_id' )))
79
91
80
92
@property
81
93
def orig_start_line_number (self ):
@@ -89,23 +101,17 @@ def orig_committer(self):
89
101
90
102
@property
91
103
def orig_commit_id (self ):
92
- return Oid (
93
- raw = bytes (ffi .buffer (ffi .addressof (self ._hunk , 'orig_commit_id' ))[:])
94
- )
104
+ return Oid (raw = buffer_to_bytes (ffi .addressof (self ._hunk , 'orig_commit_id' )))
95
105
96
106
@property
97
107
def orig_path (self ):
98
108
"""Original path"""
99
- path = self ._hunk .orig_path
100
- if not path :
101
- return None
102
-
103
- return ffi .string (path ).decode ('utf-8' )
109
+ return maybe_string (self ._hunk .orig_path )
104
110
105
111
106
112
class Blame :
107
113
@classmethod
108
- def _from_c (cls , repo , ptr ):
114
+ def _from_c (cls , repo : BaseRepository , ptr : CData ):
109
115
blame = cls .__new__ (cls )
110
116
blame ._repo = repo
111
117
blame ._blame = ptr
@@ -117,14 +123,14 @@ def __del__(self):
117
123
def __len__ (self ):
118
124
return C .git_blame_get_hunk_count (self ._blame )
119
125
120
- def __getitem__ (self , index ):
126
+ def __getitem__ (self , index : int ):
121
127
chunk = C .git_blame_get_hunk_byindex (self ._blame , index )
122
128
if not chunk :
123
129
raise IndexError
124
130
125
131
return BlameHunk ._from_c (self , chunk )
126
132
127
- def for_line (self , line_no ) :
133
+ def for_line (self , line_no : int ) -> BlameHunk :
128
134
"""
129
135
Returns the <BlameHunk> object for a given line given its number in the
130
136
current Blame.
0 commit comments