diff --git a/kittens/diff/collect.py b/kittens/diff/collect.py old mode 100644 new mode 100755 index 104f8ee11..bda96cc1d --- a/kittens/diff/collect.py +++ b/kittens/diff/collect.py @@ -4,8 +4,10 @@ import os import re from contextlib import suppress +from fnmatch import fnmatch from functools import lru_cache from hashlib import md5 +from itertools import product from kitty.guess_mime_type import guess_type from typing import TYPE_CHECKING, Dict, List, Set, Optional, Iterator, Tuple, Union @@ -37,6 +39,8 @@ class Segment: class Collection: + ignore_paths: Tuple[str, ...] = () + def __init__(self) -> None: self.changes: Dict[str, str] = {} self.renames: Dict[str, str] = {} @@ -105,22 +109,29 @@ 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: + 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 + path = os.path.abspath(os.path.join(dirpath, filename)) + path_name_map[path] = name = os.path.relpath(path, base) + names.add(name) + pmap[name] = path + + def collect_files(collection: Collection, left: str, right: str) -> None: left_names: Set[str] = set() right_names: Set[str] = set() left_path_map: Dict[str, str] = {} right_path_map: Dict[str, str] = {} - def walk(base: str, names: Set[str], pmap: Dict[str, str]) -> None: - for dirpath, dirnames, filenames in os.walk(base): - for filename in filenames: - path = os.path.abspath(os.path.join(dirpath, filename)) - path_name_map[path] = name = os.path.relpath(path, base) - names.add(name) - pmap[name] = path + 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) - walk(right, right_names, right_path_map) 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])} for n in changed_names: diff --git a/kittens/diff/main.py b/kittens/diff/main.py old mode 100644 new mode 100755 index 6d3097960..f0e4676ba --- a/kittens/diff/main.py +++ b/kittens/diff/main.py @@ -655,6 +655,7 @@ def main(args: List[str]) -> None: opts = init_config(cli_opts) set_diff_command(opts.diff_cmd) lines_for_path.replace_tab_by = opts.replace_tab_by + Collection.ignore_paths = opts.ignore_paths left, right = map(get_remote_file, (left, right)) if os.path.isdir(left) != os.path.isdir(right): raise SystemExit('The items to be diffed should both be either directories or files. Comparing a directory to a file is not valid.') diff --git a/kittens/diff/options/definition.py b/kittens/diff/options/definition.py old mode 100644 new mode 100755 index 3b8529fc6..9a7d2a669 --- a/kittens/diff/options/definition.py +++ b/kittens/diff/options/definition.py @@ -47,6 +47,14 @@ opt('replace_tab_by', '\\x20\\x20\\x20\\x20', option_type='python_string', long_text='The string to replace tabs with. Default is to use four spaces.' ) + +opt('ignore_paths', '', + option_type='pattern_list', + long_text='''List of patterns that are matched against directory and file +names and ignored when directories are compared. +''', + ) + egr() # }}} # colors {{{ diff --git a/kittens/diff/options/parse.py b/kittens/diff/options/parse.py index 6ba056c03..e4ffa26b1 100644 --- a/kittens/diff/options/parse.py +++ b/kittens/diff/options/parse.py @@ -1,7 +1,7 @@ # generated by gen-config.py DO NOT edit import typing -from kittens.diff.options.utils import parse_map, syntax_aliases +from kittens.diff.options.utils import parse_map, pattern_list, syntax_aliases from kitty.conf.utils import merge_dicts, positive_int, python_string, to_color, to_color_or_none @@ -86,6 +86,9 @@ class Parser: for k in parse_map(val): ans['map'].append(k) + def ignore_paths(self, val: str, ans: typing.Dict[str, typing.Any]): + ans['ignore_paths'] = pattern_list(val) + def create_result_dict() -> typing.Dict[str, typing.Any]: return { diff --git a/kittens/diff/options/types.py b/kittens/diff/options/types.py index 9a7bccec5..f265c8a95 100644 --- a/kittens/diff/options/types.py +++ b/kittens/diff/options/types.py @@ -20,6 +20,7 @@ option_names = ( # {{{ 'highlight_removed_bg', 'hunk_bg', 'hunk_margin_bg', + 'ignore_paths', 'map', 'margin_bg', 'margin_fg', @@ -49,6 +50,7 @@ class Options: highlight_removed_bg: Color = Color(253, 184, 192) hunk_bg: Color = Color(241, 248, 255) hunk_margin_bg: Color = Color(219, 237, 255) + ignore_paths: typing.Tuple[str, ...] = () margin_bg: Color = Color(250, 251, 252) margin_fg: Color = Color(170, 170, 170) margin_filler_bg: typing.Optional[kitty.fast_data_types.Color] = None diff --git a/kittens/diff/options/utils.py b/kittens/diff/options/utils.py old mode 100644 new mode 100755 index 0e88b3bc4..4777306bf --- a/kittens/diff/options/utils.py +++ b/kittens/diff/options/utils.py @@ -3,7 +3,8 @@ # License: GPLv3 Copyright: 2021, Kovid Goyal -from typing import Any, Dict, Iterable, Tuple, Union +import shlex +from typing import Any, Dict, Iterable, List, Tuple, Union from kitty.conf.utils import ( KeyFuncWrapper, KittensKeyDefinition, parse_kittens_key @@ -58,6 +59,10 @@ def syntax_aliases(raw: str) -> Dict[str, str]: return ans +def pattern_list(raw: str) -> Tuple[str, ...]: + return tuple(shlex.split(raw, comments=True)) + + def parse_map(val: str) -> Iterable[KittensKeyDefinition]: x = parse_kittens_key(val, func_with_args.args_funcs) if x is not None: diff --git a/kitty_tests/diff.py b/kitty_tests/diff.py index 37bee2e00..7578854e2 100644 --- a/kitty_tests/diff.py +++ b/kitty_tests/diff.py @@ -43,3 +43,43 @@ class TestDiff(BaseTest): highlights = [h(0, 1, 1), h(1, 3, 2)] self.ae(['S1SaE1ES2SbcE2Ed'], split_with_highlights('abcd', 10, highlights)) + + def test_walk(self): + from pathlib import Path + import tempfile + from kittens.diff.collect import walk + + with tempfile.TemporaryDirectory() as tmpdir: + # /tmp/test/ + # ├── a + # │ └── b + # │ └── c + # ├── d + # ├── #d# + # ├── e + # ├── e~ + # └── f + # │ └── g + # └── h space + Path(tmpdir, "a/b").mkdir(parents=True) + Path(tmpdir, "a/b/c").touch() + Path(tmpdir, "b").touch() + Path(tmpdir, "d").touch() + Path(tmpdir, "#d#").touch() + Path(tmpdir, "e").touch() + Path(tmpdir, "e~").touch() + Path(tmpdir, "f").mkdir() + Path(tmpdir, "f/g").touch() + Path(tmpdir, "h space").touch() + expected_names = {"d", "e", "f/g", "h space"} + expected_pmap = { + "d": f"{tmpdir}/d", + "e": f"{tmpdir}/e", + "f/g": f"{tmpdir}/f/g", + "h space": f"{tmpdir}/h space" + } + names = set() + pmap = {} + walk(tmpdir, names, pmap, ("*~", "#*#", "b")) + self.ae(expected_names, names) + self.ae(expected_pmap, pmap)