Coder Social home page Coder Social logo

Comments (5)

cpburnz avatar cpburnz commented on July 18, 2024 1

The most convenient way to find files under a directory, and exclude some based upon gitignore patterns is to treat the files as sets, and perform operations from there:

import pathspec
import pathspec.util

ignore_spec = pathspec.GitIgnoreSpec.from_lines([
    "*.txt",
    # ...
])

all_files = set(pathspec.util.iter_tree_files("/some/directory"))
ignore_files = set(ignore_spec.match_files(all_files))
keep_files = all_files - ignore_files

I think your 3rd suggestion of adding a negate parameter to PathSpec.match_tree_files() would be the safest way to make this easier. Gitignore has peculiar edge cases that I don't think would behave properly if every match was simply negated. Implementing the logic at the tree level would allow using the file iterator intelligently without having to consume the whole thing in 2 or 3 sets.

from python-pathspec.

oprypin avatar oprypin commented on July 18, 2024

Some ideas on how it could be achieved:

  1. Add a method to PathSpec to negate the whole spec and return a spec with everything flipped.
    A bit similar to

    def __add__(self: Self, other: "PathSpec") -> Self:

  2. Add a method to PathSpec to negate the whole spec and return a wrapper object that has a negated match_file method. (note: until this functionality is in the library, making such a wrapper myself seems quite feasible in the meantime)

  3. Add a "negate" parameter to match_tree_files
    https://github.com/cpburnz/python-pathspec/blob/933dd7da982551300a584c98570993402a56bc27/pathspec/pathspec.py#L267-L268C19
    pass it on to match_files and negate the condition accordingly

    if self._match_file(use_patterns, norm_file):

from python-pathspec.

oprypin avatar oprypin commented on July 18, 2024
  1. Add a "negate" parameter to from_lines which flips the include of each parsed line

from python-pathspec.

oprypin avatar oprypin commented on July 18, 2024

Yes, a negate parameter seems to be the right approach, and indeed I was running into all kinds of edge cases otherwise.

The current approach with scanning the directory tree twice does not seem optimal to me :(

Meanwhile I also realized that there is a decently convenient way in the current state as well, without pulling too much of internals with it:

def match_files_negated(exclude: pathspec.PathSpec, root: str) -> Iterable[str]:
    for path in pathspec.util.iter_tree_files(root):
        if not exclude.match_file(path):
            yield path

from python-pathspec.

cpburnz avatar cpburnz commented on July 18, 2024

The negate parameter for PathSpec.match_files() and related methods has been implemented and is in the new v0.11.2 release.

from python-pathspec.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.