Cleanup previous PR

This commit is contained in:
Kovid Goyal
2022-06-06 17:19:52 +05:30
parent fc217dafba
commit 79f7954048
7 changed files with 47 additions and 27 deletions

View File

@@ -7,9 +7,11 @@ from contextlib import suppress
from fnmatch import fnmatch
from functools import lru_cache
from hashlib import md5
from itertools import product
from typing import (
TYPE_CHECKING, Dict, Iterator, List, Optional, Sequence, Set, Tuple, Union
)
from kitty.guess_mime_type import guess_type
from typing import TYPE_CHECKING, Dict, List, Set, Optional, Iterator, Tuple, Union
if TYPE_CHECKING:
from .highlight import DiffHighlight # noqa
@@ -39,7 +41,7 @@ class Segment:
class Collection:
ignore_paths: Tuple[str, ...] = ()
ignore_names: Tuple[str, ...] = ()
def __init__(self) -> None:
self.changes: Dict[str, str] = {}
@@ -109,14 +111,19 @@ def resolve_remote_name(path: str, default: str) -> str:
return default
def walk(base: str, names: Set[str], pmap: Dict[str, str], ignore_paths: Tuple[str, ...]) -> None:
def allowed_items(items: Sequence[str], ignore_patterns: Sequence[str]) -> Iterator[str]:
for name in items:
for pat in ignore_patterns:
if fnmatch(name, pat):
break
else:
yield name
def walk(base: str, names: Set[str], pmap: Dict[str, str], ignore_names: Tuple[str, ...]) -> None:
for dirpath, dirnames, filenames in os.walk(base):
ignored = [_dir for _dir, pat in product(dirnames, ignore_paths) if fnmatch(_dir, pat)]
for _dir in ignored:
dirnames.pop(dirnames.index(_dir))
for filename in filenames:
if any(fnmatch(filename, pat) for pat in ignore_paths if pat):
continue
dirnames[:] = allowed_items(dirnames, ignore_names)
for filename in allowed_items(filenames, ignore_names):
path = os.path.abspath(os.path.join(dirpath, filename))
path_name_map[path] = name = os.path.relpath(path, base)
names.add(name)
@@ -129,8 +136,8 @@ def collect_files(collection: Collection, left: str, right: str) -> None:
left_path_map: Dict[str, str] = {}
right_path_map: Dict[str, str] = {}
walk(left, left_names, left_path_map, collection.ignore_paths)
walk(right, right_names, right_path_map, collection.ignore_paths)
walk(left, left_names, left_path_map, collection.ignore_names)
walk(right, right_names, right_path_map, collection.ignore_names)
common_names = left_names & right_names
changed_names = {n for n in common_names if data_for_path(left_path_map[n]) != data_for_path(right_path_map[n])}