GithubHelp home page GithubHelp logo

Comments (2)

VojtechStep avatar VojtechStep commented on July 17, 2024 1

Alternatively, we can ask Git itself to give us the list of files it sees, that's what pre-commit does internally - see here.

from agda-unimath.

fredrik-bakke avatar fredrik-bakke commented on July 17, 2024

I asked GPT4 for a solution:

The easiest way to achieve this is by using the pathspec Python library, which allows you to match paths using .gitignore style patterns.

First, you'll need to install the library. You can do this using pip:

pip install pathspec

Then, you can use the library in your script. You will need to read the contents of your .gitignore file and use them to compile a PathSpec object. Then you can use the match_file method on this object to test whether a given path should be ignored.

Here's how you can modify your generate_index function to ignore directories specified in your .gitignore:

from pathspec import PathSpec
from pathspec.patterns import GitWildMatchPattern

def generate_index(root, header):
    status = 0
    entry_lists = []

    # Read .gitignore file
    with open('.gitignore', 'r') as f:
        gitignore = f.read().splitlines()

    # Compile .gitignore file into a PathSpec
    pathspec = PathSpec.from_lines(GitWildMatchPattern, gitignore)

    for namespace in sorted(utils.get_subdirectories_recursive(root)):
        if namespace == 'temp':
            continue

        # Test if path matches .gitignore
        if pathspec.match_file(namespace):
            continue

        entry_list, s = generate_namespace_entry_list(namespace)
        entry_lists.append(entry_list)
        status |= s

    index = f'{header}\n\n' + '\n\n'.join(entry_lists) + '\n'
    return index, status

In this script, pathspec.match_file(namespace) returns True if the namespace matches any pattern in your .gitignore. If it does, then continue skips to the next iteration of the loop, effectively ignoring that directory.

Remember to take care of the relative paths. If your .gitignore file is not in the same directory as the script, you'll need to adjust the path when opening the file. Similarly, you'll need to make sure the paths you're testing with match_file are relative to the location of your .gitignore.

from agda-unimath.

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.