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.

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)