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.

Post History

42%
+1 −2
Q&A Python script using gitignore_parser doesn't respect .gitignore rules [closed]

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 exclud...

0 answers  ·  posted 11mo ago by ShadowsRanger‭  ·  closed 11mo ago by Karl Knechtel‭

Question python gitignore
#4: Question closed by user avatar Karl Knechtel‭ · 2025-10-26T20:34:52Z (11 months ago)
#3: Post edited by user avatar ShadowsRanger‭ · 2025-10-24T20:47:59Z (11 months ago)
  • I'm using a Python script to merge code files:
  • ```bash
  • python codemerge.py --filters "*.py" --output merged_code.txt .
  • ```
  • The script uses [`gitignore_parser`](https://pypi.org/project/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`?
  • ```python
  • #!/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()
  • ```
  • I'm using a Python script to merge code files:
  • ```bash
  • python codemerge.py --filters "*.py" --output merged_code.txt .
  • ```
  • The script uses [`gitignore_parser`](https://pypi.org/project/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`?
  • ```python
  • #!/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()
  • ```
#2: Post edited by user avatar ShadowsRanger‭ · 2025-10-24T16:57:13Z (11 months ago)
  • I'm using a Python script to merge code files:
  • ```bash
  • python codemerge.py --filters "*.py" --output merged_code.txt .
  • ```
  • The script uses [`gitignore_parser`](https://pypi.org/project/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`?
  • I'm using a Python script to merge code files:
  • ```bash
  • python codemerge.py --filters "*.py" --output merged_code.txt .
  • ```
  • The script uses [`gitignore_parser`](https://pypi.org/project/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`?
  • ```python
  • #!/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()
  • ```
#1: Initial revision by user avatar ShadowsRanger‭ · 2025-10-24T16:56:45Z (11 months ago)
Python script using `gitignore_parser` doesn't respect `.gitignore` rules
I'm using a Python script to merge code files:

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

The script uses [`gitignore_parser`](https://pypi.org/project/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`?