Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5d3818e

Browse files
committed
Finish initial typing of index folder
1 parent 2e2fe18 commit 5d3818e

File tree

6 files changed

+123
-81
lines changed

6 files changed

+123
-81
lines changed

git/index/base.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from git.exc import (
1919
GitCommandError,
2020
CheckoutError,
21+
GitError,
2122
InvalidGitRepositoryError
2223
)
2324
from git.objects import (
@@ -66,10 +67,10 @@
6667

6768
# typing -----------------------------------------------------------------------------
6869

69-
from typing import (Any, BinaryIO, Callable, Dict, IO, Iterable, Iterator, List,
70+
from typing import (Any, BinaryIO, Callable, Dict, IO, Iterable, Iterator, List, NoReturn,
7071
Sequence, TYPE_CHECKING, Tuple, Union)
7172

72-
from git.types import PathLike, TBD
73+
from git.types import Commit_ish, PathLike, TBD
7374

7475
if TYPE_CHECKING:
7576
from subprocess import Popen
@@ -372,13 +373,13 @@ def from_tree(cls, repo: 'Repo', *treeish: Treeish, **kwargs: Any) -> 'IndexFile
372373

373374
# UTILITIES
374375
@unbare_repo
375-
def _iter_expand_paths(self, paths: Sequence[PathLike]) -> Iterator[PathLike]:
376+
def _iter_expand_paths(self: 'IndexFile', paths: Sequence[PathLike]) -> Iterator[PathLike]:
376377
"""Expand the directories in list of paths to the corresponding paths accordingly,
377378
378379
Note: git will add items multiple times even if a glob overlapped
379380
with manually specified paths or if paths where specified multiple
380381
times - we respect that and do not prune"""
381-
def raise_exc(e):
382+
def raise_exc(e: Exception) -> NoReturn:
382383
raise e
383384
r = str(self.repo.working_tree_dir)
384385
rs = r + os.sep
@@ -426,7 +427,8 @@ def raise_exc(e):
426427
# END path exception handling
427428
# END for each path
428429

429-
def _write_path_to_stdin(self, proc: 'Popen', filepath: PathLike, item, fmakeexc, fprogress,
430+
def _write_path_to_stdin(self, proc: 'Popen', filepath: PathLike, item: TBD, fmakeexc: Callable[..., GitError],
431+
fprogress: Callable[[PathLike, bool, TBD], None],
430432
read_from_stdout: bool = True) -> Union[None, str]:
431433
"""Write path to proc.stdin and make sure it processes the item, including progress.
432434
@@ -498,7 +500,7 @@ def unmerged_blobs(self) -> Dict[PathLike, List[Tuple[StageType, Blob]]]:
498500
line.sort()
499501
return path_map
500502

501-
@classmethod
503+
@ classmethod
502504
def entry_key(cls, *entry: Union[BaseIndexEntry, PathLike, StageType]) -> Tuple[PathLike, StageType]:
503505
return entry_key(*entry)
504506

@@ -631,8 +633,8 @@ def _store_path(self, filepath: PathLike, fprogress: Callable) -> BaseIndexEntry
631633
return BaseIndexEntry((stat_mode_to_index_mode(st.st_mode),
632634
istream.binsha, 0, to_native_path_linux(filepath)))
633635

634-
@unbare_repo
635-
@git_working_dir
636+
@ unbare_repo
637+
@ git_working_dir
636638
def _entries_for_paths(self, paths: List[str], path_rewriter: Callable, fprogress: Callable,
637639
entries: List[BaseIndexEntry]) -> List[BaseIndexEntry]:
638640
entries_added: List[BaseIndexEntry] = []
@@ -788,8 +790,8 @@ def add(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, 'Submodule']
788790
# create objects if required, otherwise go with the existing shas
789791
null_entries_indices = [i for i, e in enumerate(entries) if e.binsha == Object.NULL_BIN_SHA]
790792
if null_entries_indices:
791-
@git_working_dir
792-
def handle_null_entries(self):
793+
@ git_working_dir
794+
def handle_null_entries(self: 'IndexFile') -> None:
793795
for ei in null_entries_indices:
794796
null_entry = entries[ei]
795797
new_entry = self._store_path(null_entry.path, fprogress)
@@ -969,8 +971,13 @@ def move(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, 'Submodule'
969971

970972
return out
971973

972-
def commit(self, message: str, parent_commits=None, head: bool = True, author: Union[None, 'Actor'] = None,
973-
committer: Union[None, 'Actor'] = None, author_date: Union[str, None] = None,
974+
def commit(self,
975+
message: str,
976+
parent_commits: Union[Commit_ish, None] = None,
977+
head: bool = True,
978+
author: Union[None, 'Actor'] = None,
979+
committer: Union[None, 'Actor'] = None,
980+
author_date: Union[str, None] = None,
974981
commit_date: Union[str, None] = None,
975982
skip_hooks: bool = False) -> Commit:
976983
"""Commit the current default index file, creating a commit object.

git/index/fun.py

Lines changed: 35 additions & 27 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.objects.fun import EntryTup
6061

6162
# ------------------------------------------------------------------------------------
6263

@@ -188,7 +189,7 @@ def entry_key(*entry: Union[BaseIndexEntry, PathLike, int]) -> Tuple[PathLike, i
188189

189190
def is_entry_tuple(entry: Tuple) -> TypeGuard[Tuple[PathLike, int]]:
190191
return isinstance(entry, tuple) and len(entry) == 2
191-
192+
192193
if len(entry) == 1:
193194
entry_first = entry[0]
194195
assert isinstance(entry_first, BaseIndexEntry)
@@ -259,8 +260,8 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0
259260
:param sl: slice indicating the range we should process on the entries list
260261
:return: tuple(binsha, list(tree_entry, ...)) a tuple of a sha and a list of
261262
tree entries being a tuple of hexsha, mode, name"""
262-
tree_items = [] # type: List[Tuple[Union[bytes, str], int, str]]
263-
tree_items_append = tree_items.append
263+
tree_items: List[Tuple[bytes, int, str]] = []
264+
264265
ci = sl.start
265266
end = sl.stop
266267
while ci < end:
@@ -272,7 +273,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0
272273
rbound = entry.path.find('/', si)
273274
if rbound == -1:
274275
# its not a tree
275-
tree_items_append((entry.binsha, entry.mode, entry.path[si:]))
276+
tree_items.append((entry.binsha, entry.mode, entry.path[si:]))
276277
else:
277278
# find common base range
278279
base = entry.path[si:rbound]
@@ -289,7 +290,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0
289290
# enter recursion
290291
# ci - 1 as we want to count our current item as well
291292
sha, _tree_entry_list = write_tree_from_cache(entries, odb, slice(ci - 1, xi), rbound + 1)
292-
tree_items_append((sha, S_IFDIR, base))
293+
tree_items.append((sha, S_IFDIR, base))
293294

294295
# skip ahead
295296
ci = xi
@@ -306,7 +307,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0
306307
return (istream.binsha, tree_items_stringified)
307308

308309

309-
def _tree_entry_to_baseindexentry(tree_entry: Tuple[str, int, str], stage: int) -> BaseIndexEntry:
310+
def _tree_entry_to_baseindexentry(tree_entry: Tuple[bytes, int, str], stage: int) -> BaseIndexEntry:
310311
return BaseIndexEntry((tree_entry[1], tree_entry[0], stage << CE_STAGESHIFT, tree_entry[2]))
311312

312313

@@ -319,23 +320,30 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr
319320
:param tree_shas: 1, 2 or 3 trees as identified by their binary 20 byte shas
320321
If 1 or two, the entries will effectively correspond to the last given tree
321322
If 3 are given, a 3 way merge is performed"""
322-
out = [] # type: List[BaseIndexEntry]
323-
out_append = out.append
323+
out: List[BaseIndexEntry] = []
324324

325325
# one and two way is the same for us, as we don't have to handle an existing
326326
# index, instrea
327327
if len(tree_shas) in (1, 2):
328328
for entry in traverse_tree_recursive(odb, tree_shas[-1], ''):
329-
out_append(_tree_entry_to_baseindexentry(entry, 0))
329+
out.append(_tree_entry_to_baseindexentry(entry, 0))
330330
# END for each entry
331331
return out
332332
# END handle single tree
333333

334334
if len(tree_shas) > 3:
335335
raise ValueError("Cannot handle %i trees at once" % len(tree_shas))
336336

337+
EntryTupOrNone = Union[EntryTup, None]
338+
339+
def is_three_entry_list(inp) -> TypeGuard[List[EntryTupOrNone]]:
340+
return isinstance(inp, list) and len(inp) == 3
341+
337342
# three trees
338-
for base, ours, theirs in traverse_trees_recursive(odb, tree_shas, ''):
343+
for three_entries in traverse_trees_recursive(odb, tree_shas, ''):
344+
345+
assert is_three_entry_list(three_entries)
346+
base, ours, theirs = three_entries
339347
if base is not None:
340348
# base version exists
341349
if ours is not None:
@@ -347,23 +355,23 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr
347355
if(base[0] != ours[0] and base[0] != theirs[0] and ours[0] != theirs[0]) or \
348356
(base[1] != ours[1] and base[1] != theirs[1] and ours[1] != theirs[1]):
349357
# changed by both
350-
out_append(_tree_entry_to_baseindexentry(base, 1))
351-
out_append(_tree_entry_to_baseindexentry(ours, 2))
352-
out_append(_tree_entry_to_baseindexentry(theirs, 3))
358+
out.append(_tree_entry_to_baseindexentry(base, 1))
359+
out.append(_tree_entry_to_baseindexentry(ours, 2))
360+
out.append(_tree_entry_to_baseindexentry(theirs, 3))
353361
elif base[0] != ours[0] or base[1] != ours[1]:
354362
# only we changed it
355-
out_append(_tree_entry_to_baseindexentry(ours, 0))
363+
out.append(_tree_entry_to_baseindexentry(ours, 0))
356364
else:
357365
# either nobody changed it, or they did. In either
358366
# case, use theirs
359-
out_append(_tree_entry_to_baseindexentry(theirs, 0))
367+
out.append(_tree_entry_to_baseindexentry(theirs, 0))
360368
# END handle modification
361369
else:
362370

363371
if ours[0] != base[0] or ours[1] != base[1]:
364372
# they deleted it, we changed it, conflict
365-
out_append(_tree_entry_to_baseindexentry(base, 1))
366-
out_append(_tree_entry_to_baseindexentry(ours, 2))
373+
out.append(_tree_entry_to_baseindexentry(base, 1))
374+
out.append(_tree_entry_to_baseindexentry(ours, 2))
367375
# else:
368376
# we didn't change it, ignore
369377
# pass
@@ -376,8 +384,8 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr
376384
else:
377385
if theirs[0] != base[0] or theirs[1] != base[1]:
378386
# deleted in ours, changed theirs, conflict
379-
out_append(_tree_entry_to_baseindexentry(base, 1))
380-
out_append(_tree_entry_to_baseindexentry(theirs, 3))
387+
out.append(_tree_entry_to_baseindexentry(base, 1))
388+
out.append(_tree_entry_to_baseindexentry(theirs, 3))
381389
# END theirs changed
382390
# else:
383391
# theirs didn't change
@@ -386,20 +394,20 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr
386394
# END handle ours
387395
else:
388396
# all three can't be None
389-
if ours is None:
397+
if ours is None and theirs is not None:
390398
# added in their branch
391-
out_append(_tree_entry_to_baseindexentry(theirs, 0))
392-
elif theirs is None:
399+
out.append(_tree_entry_to_baseindexentry(theirs, 0))
400+
elif theirs is None and ours is not None:
393401
# added in our branch
394-
out_append(_tree_entry_to_baseindexentry(ours, 0))
395-
else:
402+
out.append(_tree_entry_to_baseindexentry(ours, 0))
403+
elif ours is not None and theirs is not None:
396404
# both have it, except for the base, see whether it changed
397405
if ours[0] != theirs[0] or ours[1] != theirs[1]:
398-
out_append(_tree_entry_to_baseindexentry(ours, 2))
399-
out_append(_tree_entry_to_baseindexentry(theirs, 3))
406+
out.append(_tree_entry_to_baseindexentry(ours, 2))
407+
out.append(_tree_entry_to_baseindexentry(theirs, 3))
400408
else:
401409
# it was added the same in both
402-
out_append(_tree_entry_to_baseindexentry(ours, 0))
410+
out.append(_tree_entry_to_baseindexentry(ours, 0))
403411
# END handle two items
404412
# END handle heads
405413
# END handle base exists

git/index/util.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111

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

14-
from typing import (Any, Callable)
14+
from typing import (Any, Callable, TYPE_CHECKING)
1515

1616
from git.types import PathLike, _T
1717

18+
if TYPE_CHECKING:
19+
from git.index import IndexFile
20+
1821
# ---------------------------------------------------------------------------------
1922

2023

@@ -63,7 +66,7 @@ def post_clear_cache(func: Callable[..., _T]) -> Callable[..., _T]:
6366
"""
6467

6568
@wraps(func)
66-
def post_clear_cache_if_not_raised(self, *args: Any, **kwargs: Any) -> _T:
69+
def post_clear_cache_if_not_raised(self: 'IndexFile', *args: Any, **kwargs: Any) -> _T:
6770
rval = func(self, *args, **kwargs)
6871
self._delete_entries_cache()
6972
return rval
@@ -78,7 +81,7 @@ def default_index(func: Callable[..., _T]) -> Callable[..., _T]:
7881
on that index only. """
7982

8083
@wraps(func)
81-
def check_default_index(self, *args: Any, **kwargs: Any) -> _T:
84+
def check_default_index(self: 'IndexFile', *args: Any, **kwargs: Any) -> _T:
8285
if self._file_path != self._index_path():
8386
raise AssertionError(
8487
"Cannot call %r on indices that do not represent the default git index" % func.__name__)
@@ -93,9 +96,9 @@ def git_working_dir(func: Callable[..., _T]) -> Callable[..., _T]:
9396
repository in order to assure relative paths are handled correctly"""
9497

9598
@wraps(func)
96-
def set_git_working_dir(self, *args: Any, **kwargs: Any) -> _T:
99+
def set_git_working_dir(self: 'IndexFile', *args: Any, **kwargs: Any) -> _T:
97100
cur_wd = os.getcwd()
98-
os.chdir(self.repo.working_tree_dir)
101+
os.chdir(str(self.repo.working_tree_dir))
99102
try:
100103
return func(self, *args, **kwargs)
101104
finally:

0 commit comments

Comments
 (0)