Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Python script using `gitignore_parser` doesn't respect `.gitignore` rules

Post

Python script using gitignore_parser doesn't respect .gitignore rules [closed]

+1
−2

Closed as not constructive by Karl Knechtel‭ on Oct 26, 2025 at 20:34

This question cannot be answered in a way that is helpful to anyone. It's not possible to learn something from possible answers, except for the solution for the specific problem of the asker.

This question was closed; new answers can no longer be added. Users with the Vote on Holds ability may vote to reopen this question if it has been improved or closed incorrectly.

I'm using a Python script to merge code files:

python codemerge.py --filters "*.py" --output merged_code.txt .

The script uses gitignore_parser to skip files in .gitignore. My .gitignore excludes venv/. However, the merged output still includes files from venv and other ignored files. No errors are shown.

I wonder if gitignore_parser requires absolute paths, or if I'm missing something else in the integration.

How can I make the script respect .gitignore?

#!/usr/bin/env python3
import argparse
import fnmatch
import sys
from pathlib import Path
from typing import Callable, List, Optional
from gitignore_parser import parse_gitignore


def merge_files(
    paths: List[str],
    includes: Optional[List[str]],
    ignores: Optional[List[str]],
    ignore_matcher: Callable[[Path], bool],
    recursive: bool = True,
) -> str:
    merged_content: List[str] = []

    def process_file(file_path: Path) -> None:
        if should_include(file_path, includes, ignores, ignore_matcher):
            merged_content.append(f"# File: {file_path}\n")
            merged_content.append(file_path.read_text(encoding="utf-8"))
            merged_content.append("\n\n")

    for path_str in paths:
        path = Path(path_str)
        if not path.exists():
            continue
        if path.is_file():
            process_file(path)
        elif path.is_dir() and recursive:
            for file in path.rglob("*"):
                if file.is_file():
                    process_file(file)

    return "".join(merged_content)


def should_include(
    file_path: Path,
    includes: Optional[List[str]],
    ignores: Optional[List[str]],
    ignore_matcher: Callable[[Path], bool],
) -> bool:
    if ignore_matcher(file_path):
        return False

    str_path = str(file_path)
    if ignores:
        for pattern in ignores:
            if fnmatch.fnmatch(str_path, pattern):
                return False

    if not includes:
        return True

    for pattern in includes:
        if fnmatch.fnmatch(str_path, pattern):
            return True

    return False


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Merge code files into one output")
    parser.add_argument("merge_paths", nargs="+", help="Directories or files to merge")
    parser.add_argument("-i", "--ignores", nargs="*", help="Glob patterns to ignore")
    parser.add_argument(
        "-f", "--filters", nargs="*", default=["*"], help="Glob patterns to include"
    )
    parser.add_argument(
        "--format", default="text", choices=["text"], help="Output format"
    )
    parser.add_argument("--output", help="File to save merged content; default stdout")
    parser.add_argument(
        "--no-recursive",
        action="store_true",
        help="Disable recursive directory traversal",
    )
    return parser.parse_args()


def main() -> None:
    args = parse_args()
    gitignore_path = Path(".gitignore")
    ignore_matcher: Callable[[Path], bool] = (
        parse_gitignore(gitignore_path) if gitignore_path.exists() else lambda x: False
    )

    merged: str = merge_files(
        paths=args.merge_paths,
        includes=args.filters,
        ignores=args.ignores,
        ignore_matcher=ignore_matcher,
        recursive=not args.no_recursive,
    )

    if args.output:
        Path(args.output).write_text(merged, encoding="utf-8")
    else:
        sys.stdout.write(merged)


if __name__ == "__main__":
    main()

History

2 comment threads

Please isolate a problem first (2 comments)
Without having checked the exact behaviour of the library, I notice that you check gitignore patterns... (1 comment)
Please isolate a problem first
Karl Knechtel‭ wrote 11 months ago

While I appreciate that the site needs more questions generally, this doesn't relieve the general burden, for questions that are fundamentally about fixing existing code, to debug the code and focus on a concrete issue. As it stands, we have a complete program, and no certainty as to whether the problem is caused by the ignore_matcher not working as you expect, the algorithm making use of it, or even e.g. the argument parsing.

Please try to create a minimal reproducible example that is actually minimal; for example by unconditionally calling parse_gitignore with a hard-coded gitignore_path (and show contents of the file); try to verify the result (do you in fact get a Callable[[Path], bool]? If it's a class instance, does it offer any useful diagnostics like __str__?); try using it directly (when called, does the bool result match your expectation?) etc. Then you can focus the question properly.

Karl Knechtel‭ wrote 11 months ago

In particular: where the code says

    if ignore_matcher(file_path):
        return False

Please start by checking what values of file_path are passed throughout a sample program run (does that meet your expectation?) and what results you get (does that meet your expectation?).