Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork966
Expand file tree
/
Copy pathtyp.py
More file actions
165 lines (124 loc) · 5.39 KB
/
typ.py
File metadata and controls
165 lines (124 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""Module with additional types used by the index"""
frombinasciiimportb2a_hex
from .utilimport (
pack,
unpack
)
fromgit.objectsimportBlob
# typing ----------------------------------------------------------------------
fromtypingimport (NamedTuple,Sequence,TYPE_CHECKING,Tuple,Union,cast)
fromgit.typesimportPathLike
ifTYPE_CHECKING:
fromgit.repoimportRepo
# ---------------------------------------------------------------------------------
__all__= ('BlobFilter','BaseIndexEntry','IndexEntry')
#{ Invariants
CE_NAMEMASK=0x0fff
CE_STAGEMASK=0x3000
CE_EXTENDED=0x4000
CE_VALID=0x8000
CE_STAGESHIFT=12
#} END invariants
classBlobFilter(object):
"""
Predicate to be used by iter_blobs allowing to filter only return blobs which
match the given list of directories or files.
The given paths are given relative to the repository.
"""
__slots__='paths'
def__init__(self,paths:Sequence[PathLike])->None:
"""
:param paths:
tuple or list of paths which are either pointing to directories or
to files relative to the current repository
"""
self.paths=paths
def__call__(self,stage_blob:Blob)->bool:
path=stage_blob[1].path
forpinself.paths:
ifpath.startswith(p):
returnTrue
# END for each path in filter paths
returnFalse
classBaseIndexEntryHelper(NamedTuple):
"""Typed namedtuple to provide named attribute access for BaseIndexEntry.
Needed to allow overriding __new__ in child class to preserve backwards compat."""
mode:int
binsha:bytes
flags:int
path:PathLike
ctime_bytes:bytes=pack(">LL",0,0)
mtime_bytes:bytes=pack(">LL",0,0)
dev:int=0
inode:int=0
uid:int=0
gid:int=0
size:int=0
classBaseIndexEntry(BaseIndexEntryHelper):
"""Small Brother of an index entry which can be created to describe changes
done to the index in which case plenty of additional information is not required.
As the first 4 data members match exactly to the IndexEntry type, methods
expecting a BaseIndexEntry can also handle full IndexEntries even if they
use numeric indices for performance reasons.
"""
def__new__(cls,inp_tuple:Union[Tuple[int,bytes,int,PathLike],
Tuple[int,bytes,int,PathLike,bytes,bytes,int,int,int,int,int]]
)->'BaseIndexEntry':
"""Override __new__ to allow construction from a tuple for backwards compatibility """
returnsuper().__new__(cls,*inp_tuple)
def__str__(self)->str:
return"%o %s %i\t%s"% (self.mode,self.hexsha,self.stage,self.path)
def__repr__(self)->str:
return"(%o, %s, %i, %s)"% (self.mode,self.hexsha,self.stage,self.path)
@property
defhexsha(self)->str:
"""hex version of our sha"""
returnb2a_hex(self.binsha).decode('ascii')
@property
defstage(self)->int:
"""Stage of the entry, either:
* 0 = default stage
* 1 = stage before a merge or common ancestor entry in case of a 3 way merge
* 2 = stage of entries from the 'left' side of the merge
* 3 = stage of entries from the right side of the merge
:note: For more information, see http://www.kernel.org/pub/software/scm/git/docs/git-read-tree.html
"""
return (self.flags&CE_STAGEMASK)>>CE_STAGESHIFT
@classmethod
deffrom_blob(cls,blob:Blob,stage:int=0)->'BaseIndexEntry':
""":return: Fully equipped BaseIndexEntry at the given stage"""
returncls((blob.mode,blob.binsha,stage<<CE_STAGESHIFT,blob.path))
defto_blob(self,repo:'Repo')->Blob:
""":return: Blob using the information of this index entry"""
returnBlob(repo,self.binsha,self.mode,self.path)
classIndexEntry(BaseIndexEntry):
"""Allows convenient access to IndexEntry data without completely unpacking it.
Attributes usully accessed often are cached in the tuple whereas others are
unpacked on demand.
See the properties for a mapping between names and tuple indices. """
@property
defctime(self)->Tuple[int,int]:
"""
:return:
Tuple(int_time_seconds_since_epoch, int_nano_seconds) of the
file's creation time"""
returncast(Tuple[int,int],unpack(">LL",self.ctime_bytes))
@property
defmtime(self)->Tuple[int,int]:
"""See ctime property, but returns modification time """
returncast(Tuple[int,int],unpack(">LL",self.mtime_bytes))
@classmethod
deffrom_base(cls,base:'BaseIndexEntry')->'IndexEntry':
"""
:return:
Minimal entry as created from the given BaseIndexEntry instance.
Missing values will be set to null-like values
:param base: Instance of type BaseIndexEntry"""
time=pack(">LL",0,0)
returnIndexEntry((base.mode,base.binsha,base.flags,base.path,time,time,0,0,0,0,0))
@classmethod
deffrom_blob(cls,blob:Blob,stage:int=0)->'IndexEntry':
""":return: Minimal entry resembling the given blob object"""
time=pack(">LL",0,0)
returnIndexEntry((blob.mode,blob.binsha,stage<<CE_STAGESHIFT,blob.path,
time,time,0,0,0,0,blob.size))