| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,6 +18,7 @@ | |||
| 18 | 18 | from git.exc import ( | |
| 19 | 19 | GitCommandError, | |
| 20 | 20 | CheckoutError, | |
| 21 | + GitError, | ||
| 21 | 22 | InvalidGitRepositoryError | |
| 22 | 23 | ) | |
| 23 | 24 | from git.objects import ( | |
@@ -66,10 +67,10 @@ | |||
| 66 | 67 | ||
| 67 | 68 | # typing ----------------------------------------------------------------------------- | |
| 68 | 69 | ||
| 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, | ||
| 70 | 71 | Sequence, TYPE_CHECKING, Tuple, Union) | |
| 71 | 72 | ||
| 72 | - from git.types import PathLike, TBD | ||
| 73 | + from git.types import Commit_ish, PathLike, TBD | ||
| 73 | 74 | ||
| 74 | 75 | if TYPE_CHECKING: | |
| 75 | 76 | from subprocess import Popen | |
@@ -372,13 +373,13 @@ def from_tree(cls, repo: 'Repo', *treeish: Treeish, **kwargs: Any) -> 'IndexFile | |||
| 372 | 373 | ||
| 373 | 374 | # UTILITIES | |
| 374 | 375 | @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]: | ||
| 376 | 377 | """Expand the directories in list of paths to the corresponding paths accordingly, | |
| 377 | 378 | ||
| 378 | 379 | Note: git will add items multiple times even if a glob overlapped | |
| 379 | 380 | with manually specified paths or if paths where specified multiple | |
| 380 | 381 | times - we respect that and do not prune""" | |
| 381 | - def raise_exc(e): | ||
| 382 | + def raise_exc(e: Exception) -> NoReturn: | ||
| 382 | 383 | raise e | |
| 383 | 384 | r = str(self.repo.working_tree_dir) | |
| 384 | 385 | rs = r + os.sep | |
@@ -426,7 +427,8 @@ def raise_exc(e): | |||
| 426 | 427 | # END path exception handling | |
| 427 | 428 | # END for each path | |
| 428 | 429 | ||
| 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], | ||
| 430 | 432 | read_from_stdout: bool = True) -> Union[None, str]: | |
| 431 | 433 | """Write path to proc.stdin and make sure it processes the item, including progress. | |
| 432 | 434 | ||
@@ -498,7 +500,7 @@ def unmerged_blobs(self) -> Dict[PathLike, List[Tuple[StageType, Blob]]]: | |||
| 498 | 500 | line.sort() | |
| 499 | 501 | return path_map | |
| 500 | 502 | ||
| 501 | - @classmethod | ||
| 503 | + @ classmethod | ||
| 502 | 504 | def entry_key(cls, *entry: Union[BaseIndexEntry, PathLike, StageType]) -> Tuple[PathLike, StageType]: | |
| 503 | 505 | return entry_key(*entry) | |
| 504 | 506 | ||
@@ -631,8 +633,8 @@ def _store_path(self, filepath: PathLike, fprogress: Callable) -> BaseIndexEntry | |||
| 631 | 633 | return BaseIndexEntry((stat_mode_to_index_mode(st.st_mode), | |
| 632 | 634 | istream.binsha, 0, to_native_path_linux(filepath))) | |
| 633 | 635 | ||
| 634 | - @unbare_repo | ||
| 635 | - @git_working_dir | ||
| 636 | + @ unbare_repo | ||
| 637 | + @ git_working_dir | ||
| 636 | 638 | def _entries_for_paths(self, paths: List[str], path_rewriter: Callable, fprogress: Callable, | |
| 637 | 639 | entries: List[BaseIndexEntry]) -> List[BaseIndexEntry]: | |
| 638 | 640 | entries_added: List[BaseIndexEntry] = [] | |
@@ -788,8 +790,8 @@ def add(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, 'Submodule'] | |||
| 788 | 790 | # create objects if required, otherwise go with the existing shas | |
| 789 | 791 | null_entries_indices = [i for i, e in enumerate(entries) if e.binsha == Object.NULL_BIN_SHA] | |
| 790 | 792 | 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: | ||
| 793 | 795 | for ei in null_entries_indices: | |
| 794 | 796 | null_entry = entries[ei] | |
| 795 | 797 | new_entry = self._store_path(null_entry.path, fprogress) | |
@@ -969,8 +971,13 @@ def move(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, 'Submodule' | |||
| 969 | 971 | ||
| 970 | 972 | return out | |
| 971 | 973 | ||
| 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, | ||
| 974 | 981 | commit_date: Union[str, None] = None, | |
| 975 | 982 | skip_hooks: bool = False) -> Commit: | |
| 976 | 983 | """Commit the current default index file, creating a commit object. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -57,6 +57,7 @@ | |||
| 57 | 57 | ||
| 58 | 58 | if TYPE_CHECKING: | |
| 59 | 59 | from .base import IndexFile | |
| 60 | + from git.objects.fun import EntryTup | ||
| 60 | 61 | ||
| 61 | 62 | # ------------------------------------------------------------------------------------ | |
| 62 | 63 | ||
@@ -188,7 +189,7 @@ def entry_key(*entry: Union[BaseIndexEntry, PathLike, int]) -> Tuple[PathLike, i | |||
| 188 | 189 | ||
| 189 | 190 | def is_entry_tuple(entry: Tuple) -> TypeGuard[Tuple[PathLike, int]]: | |
| 190 | 191 | return isinstance(entry, tuple) and len(entry) == 2 | |
| 191 | - | ||
| 192 | + | ||
| 192 | 193 | if len(entry) == 1: | |
| 193 | 194 | entry_first = entry[0] | |
| 194 | 195 | assert isinstance(entry_first, BaseIndexEntry) | |
@@ -259,8 +260,8 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 | |||
| 259 | 260 | :param sl: slice indicating the range we should process on the entries list | |
| 260 | 261 | :return: tuple(binsha, list(tree_entry, ...)) a tuple of a sha and a list of | |
| 261 | 262 | 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 | + | ||
| 264 | 265 | ci = sl.start | |
| 265 | 266 | end = sl.stop | |
| 266 | 267 | while ci < end: | |
@@ -272,7 +273,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 | |||
| 272 | 273 | rbound = entry.path.find('/', si) | |
| 273 | 274 | if rbound == -1: | |
| 274 | 275 | # 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:])) | ||
| 276 | 277 | else: | |
| 277 | 278 | # find common base range | |
| 278 | 279 | base = entry.path[si:rbound] | |
@@ -289,7 +290,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 | |||
| 289 | 290 | # enter recursion | |
| 290 | 291 | # ci - 1 as we want to count our current item as well | |
| 291 | 292 | 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)) | ||
| 293 | 294 | ||
| 294 | 295 | # skip ahead | |
| 295 | 296 | ci = xi | |
@@ -306,7 +307,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 | |||
| 306 | 307 | return (istream.binsha, tree_items_stringified) | |
| 307 | 308 | ||
| 308 | 309 | ||
| 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: | ||
| 310 | 311 | return BaseIndexEntry((tree_entry[1], tree_entry[0], stage << CE_STAGESHIFT, tree_entry[2])) | |
| 311 | 312 | ||
| 312 | 313 | ||
@@ -319,23 +320,30 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr | |||
| 319 | 320 | :param tree_shas: 1, 2 or 3 trees as identified by their binary 20 byte shas | |
| 320 | 321 | If 1 or two, the entries will effectively correspond to the last given tree | |
| 321 | 322 | If 3 are given, a 3 way merge is performed""" | |
| 322 | - out = [] # type: List[BaseIndexEntry] | ||
| 323 | - out_append = out.append | ||
| 323 | + out: List[BaseIndexEntry] = [] | ||
| 324 | 324 | ||
| 325 | 325 | # one and two way is the same for us, as we don't have to handle an existing | |
| 326 | 326 | # index, instrea | |
| 327 | 327 | if len(tree_shas) in (1, 2): | |
| 328 | 328 | 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)) | ||
| 330 | 330 | # END for each entry | |
| 331 | 331 | return out | |
| 332 | 332 | # END handle single tree | |
| 333 | 333 | ||
| 334 | 334 | if len(tree_shas) > 3: | |
| 335 | 335 | raise ValueError("Cannot handle %i trees at once" % len(tree_shas)) | |
| 336 | 336 | ||
| 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 | + | ||
| 337 | 342 | # 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 | ||
| 339 | 347 | if base is not None: | |
| 340 | 348 | # base version exists | |
| 341 | 349 | if ours is not None: | |
@@ -347,23 +355,23 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr | |||
| 347 | 355 | if(base[0] != ours[0] and base[0] != theirs[0] and ours[0] != theirs[0]) or \ | |
| 348 | 356 | (base[1] != ours[1] and base[1] != theirs[1] and ours[1] != theirs[1]): | |
| 349 | 357 | # 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)) | ||
| 353 | 361 | elif base[0] != ours[0] or base[1] != ours[1]: | |
| 354 | 362 | # only we changed it | |
| 355 | - out_append(_tree_entry_to_baseindexentry(ours, 0)) | ||
| 363 | + out.append(_tree_entry_to_baseindexentry(ours, 0)) | ||
| 356 | 364 | else: | |
| 357 | 365 | # either nobody changed it, or they did. In either | |
| 358 | 366 | # case, use theirs | |
| 359 | - out_append(_tree_entry_to_baseindexentry(theirs, 0)) | ||
| 367 | + out.append(_tree_entry_to_baseindexentry(theirs, 0)) | ||
| 360 | 368 | # END handle modification | |
| 361 | 369 | else: | |
| 362 | 370 | ||
| 363 | 371 | if ours[0] != base[0] or ours[1] != base[1]: | |
| 364 | 372 | # 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)) | ||
| 367 | 375 | # else: | |
| 368 | 376 | # we didn't change it, ignore | |
| 369 | 377 | # pass | |
@@ -376,8 +384,8 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr | |||
| 376 | 384 | else: | |
| 377 | 385 | if theirs[0] != base[0] or theirs[1] != base[1]: | |
| 378 | 386 | # 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)) | ||
| 381 | 389 | # END theirs changed | |
| 382 | 390 | # else: | |
| 383 | 391 | # theirs didn't change | |
@@ -386,20 +394,20 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr | |||
| 386 | 394 | # END handle ours | |
| 387 | 395 | else: | |
| 388 | 396 | # all three can't be None | |
| 389 | - if ours is None: | ||
| 397 | + if ours is None and theirs is not None: | ||
| 390 | 398 | # 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: | ||
| 393 | 401 | # 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: | ||
| 396 | 404 | # both have it, except for the base, see whether it changed | |
| 397 | 405 | 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)) | ||
| 400 | 408 | else: | |
| 401 | 409 | # 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)) | ||
| 403 | 411 | # END handle two items | |
| 404 | 412 | # END handle heads | |
| 405 | 413 | # END handle base exists | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,10 +11,13 @@ | |||
| 11 | 11 | ||
| 12 | 12 | # typing ---------------------------------------------------------------------- | |
| 13 | 13 | ||
| 14 | - from typing import (Any, Callable) | ||
| 14 | + from typing import (Any, Callable, TYPE_CHECKING) | ||
| 15 | 15 | ||
| 16 | 16 | from git.types import PathLike, _T | |
| 17 | 17 | ||
| 18 | + if TYPE_CHECKING: | ||
| 19 | + from git.index import IndexFile | ||
| 20 | + | ||
| 18 | 21 | # --------------------------------------------------------------------------------- | |
| 19 | 22 | ||
| 20 | 23 | ||
@@ -63,7 +66,7 @@ def post_clear_cache(func: Callable[..., _T]) -> Callable[..., _T]: | |||
| 63 | 66 | """ | |
| 64 | 67 | ||
| 65 | 68 | @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: | ||
| 67 | 70 | rval = func(self, *args, **kwargs) | |
| 68 | 71 | self._delete_entries_cache() | |
| 69 | 72 | return rval | |
@@ -78,7 +81,7 @@ def default_index(func: Callable[..., _T]) -> Callable[..., _T]: | |||
| 78 | 81 | on that index only. """ | |
| 79 | 82 | ||
| 80 | 83 | @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: | ||
| 82 | 85 | if self._file_path != self._index_path(): | |
| 83 | 86 | raise AssertionError( | |
| 84 | 87 | "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]: | |||
| 93 | 96 | repository in order to assure relative paths are handled correctly""" | |
| 94 | 97 | ||
| 95 | 98 | @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: | ||
| 97 | 100 | cur_wd = os.getcwd() | |
| 98 | - os.chdir(self.repo.working_tree_dir) | ||
| 101 | + os.chdir(str(self.repo.working_tree_dir)) | ||
| 99 | 102 | try: | |
| 100 | 103 | return func(self, *args, **kwargs) | |
| 101 | 104 | finally: | |
| Back | FazBrowse Home | New Git URL |
0 commit comments