Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2bc2ac0

Browse filesBrowse files
committed
Use BaseIndexEntry named access in index/fun.py
1 parent 4b6430b commit 2bc2ac0
Copy full SHA for 2bc2ac0

2 files changed

+38-60Lines changed: 38 additions & 60 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎git/index/fun.py‎

Copy file name to clipboardExpand all lines: git/index/fun.py
+9-8Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
if TYPE_CHECKING:
5959
from .base import IndexFile
60+
from git.db import GitCmdObjectDB
6061
from git.objects.tree import TreeCacheTup
6162
# from git.objects.fun import EntryTupOrNone
6263

@@ -149,15 +150,15 @@ def write_cache(entries: Sequence[Union[BaseIndexEntry, 'IndexEntry']], stream:
149150
# body
150151
for entry in entries:
151152
beginoffset = tell()
152-
write(entry[4]) # ctime
153-
write(entry[5]) # mtime
154-
path_str: str = entry[3]
153+
write(entry.ctime_bytes) # ctime
154+
write(entry.mtime_bytes) # mtime
155+
path_str = str(entry.path)
155156
path: bytes = force_bytes(path_str, encoding=defenc)
156157
plen = len(path) & CE_NAMEMASK # path length
157-
assert plen == len(path), "Path %s too long to fit into index" % entry[3]
158-
flags = plen | (entry[2] & CE_NAMEMASK_INV) # clear possible previous values
159-
write(pack(">LLLLLL20sH", entry[6], entry[7], entry[0],
160-
entry[8], entry[9], entry[10], entry[1], flags))
158+
assert plen == len(path), "Path %s too long to fit into index" % entry.path
159+
flags = plen | (entry.flags & CE_NAMEMASK_INV) # clear possible previous values
160+
write(pack(">LLLLLL20sH", entry.dev, entry.inode, entry.mode,
161+
entry.uid, entry.gid, entry.size, entry.binsha, flags))
161162
write(path)
162163
real_size = ((tell() - beginoffset + 8) & ~7)
163164
write(b"\0" * ((beginoffset + real_size) - tell()))
@@ -311,7 +312,7 @@ def _tree_entry_to_baseindexentry(tree_entry: 'TreeCacheTup', stage: int) -> Bas
311312
return BaseIndexEntry((tree_entry[1], tree_entry[0], stage << CE_STAGESHIFT, tree_entry[2]))
312313

313314

314-
def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntry]:
315+
def aggressive_tree_merge(odb: 'GitCmdObjectDB', tree_shas: Sequence[bytes]) -> List[BaseIndexEntry]:
315316
"""
316317
:return: list of BaseIndexEntries representing the aggressive merge of the given
317318
trees. All valid entries are on stage 0, whereas the conflicting ones are left
Collapse file

‎git/index/typ.py‎

Copy file name to clipboardExpand all lines: git/index/typ.py
+29-52Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
# typing ----------------------------------------------------------------------
1313

14-
from typing import (List, Sequence, TYPE_CHECKING, Tuple, cast)
14+
from typing import (NamedTuple, Sequence, TYPE_CHECKING, Tuple, Union, cast)
1515

1616
from git.types import PathLike
1717

@@ -59,7 +59,23 @@ def __call__(self, stage_blob: Blob) -> bool:
5959
return False
6060

6161

62-
class BaseIndexEntry(tuple):
62+
class BaseIndexEntryHelper(NamedTuple):
63+
"""Typed namedtuple to provide named attribute access for BaseIndexEntry.
64+
Needed to allow overriding __new__ in child class to preserve backwards compat."""
65+
mode: int
66+
binsha: bytes
67+
flags: int
68+
path: PathLike
69+
ctime_bytes: bytes = pack(">LL", 0, 0)
70+
mtime_bytes: bytes = pack(">LL", 0, 0)
71+
dev: int = 0
72+
inode: int = 0
73+
uid: int = 0
74+
gid: int = 0
75+
size: int = 0
76+
77+
78+
class BaseIndexEntry(BaseIndexEntryHelper):
6379

6480
"""Small Brother of an index entry which can be created to describe changes
6581
done to the index in which case plenty of additional information is not required.
@@ -68,26 +84,22 @@ class BaseIndexEntry(tuple):
6884
expecting a BaseIndexEntry can also handle full IndexEntries even if they
6985
use numeric indices for performance reasons. """
7086

87+
def __new__(cls, inp_tuple: Union[Tuple[int, bytes, int, PathLike],
88+
Tuple[int, bytes, int, PathLike, bytes, bytes, int, int, int, int, int]]
89+
) -> 'BaseIndexEntry':
90+
"""Override __new__ to allow construction from a tuple for backwards compatibility """
91+
return super().__new__(cls, *inp_tuple)
92+
7193
def __str__(self) -> str:
7294
return "%o %s %i\t%s" % (self.mode, self.hexsha, self.stage, self.path)
7395

7496
def __repr__(self) -> str:
7597
return "(%o, %s, %i, %s)" % (self.mode, self.hexsha, self.stage, self.path)
7698

77-
@property
78-
def mode(self) -> int:
79-
""" File Mode, compatible to stat module constants """
80-
return self[0]
81-
82-
@property
83-
def binsha(self) -> bytes:
84-
"""binary sha of the blob """
85-
return self[1]
86-
8799
@property
88100
def hexsha(self) -> str:
89101
"""hex version of our sha"""
90-
return b2a_hex(self[1]).decode('ascii')
102+
return b2a_hex(self.binsha).decode('ascii')
91103

92104
@property
93105
def stage(self) -> int:
@@ -100,17 +112,7 @@ def stage(self) -> int:
100112
101113
:note: For more information, see http://www.kernel.org/pub/software/scm/git/docs/git-read-tree.html
102114
"""
103-
return (self[2] & CE_STAGEMASK) >> CE_STAGESHIFT
104-
105-
@property
106-
def path(self) -> str:
107-
""":return: our path relative to the repository working tree root"""
108-
return self[3]
109-
110-
@property
111-
def flags(self) -> List[str]:
112-
""":return: flags stored with this entry"""
113-
return self[2]
115+
return (self.flags & CE_STAGEMASK) >> CE_STAGESHIFT
114116

115117
@classmethod
116118
def from_blob(cls, blob: Blob, stage: int = 0) -> 'BaseIndexEntry':
@@ -136,40 +138,15 @@ def ctime(self) -> Tuple[int, int]:
136138
:return:
137139
Tuple(int_time_seconds_since_epoch, int_nano_seconds) of the
138140
file's creation time"""
139-
return cast(Tuple[int, int], unpack(">LL", self[4]))
141+
return cast(Tuple[int, int], unpack(">LL", self.ctime_bytes))
140142

141143
@property
142144
def mtime(self) -> Tuple[int, int]:
143145
"""See ctime property, but returns modification time """
144-
return cast(Tuple[int, int], unpack(">LL", self[5]))
145-
146-
@property
147-
def dev(self) -> int:
148-
""" Device ID """
149-
return self[6]
150-
151-
@property
152-
def inode(self) -> int:
153-
""" Inode ID """
154-
return self[7]
155-
156-
@property
157-
def uid(self) -> int:
158-
""" User ID """
159-
return self[8]
160-
161-
@property
162-
def gid(self) -> int:
163-
""" Group ID """
164-
return self[9]
165-
166-
@property
167-
def size(self) -> int:
168-
""":return: Uncompressed size of the blob """
169-
return self[10]
146+
return cast(Tuple[int, int], unpack(">LL", self.mtime_bytes))
170147

171148
@classmethod
172-
def from_base(cls, base):
149+
def from_base(cls, base: 'BaseIndexEntry') -> 'IndexEntry':
173150
"""
174151
:return:
175152
Minimal entry as created from the given BaseIndexEntry instance.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.