From 7921839f089e0185d62df5beb822d07cf1ba2fb6 Mon Sep 17 00:00:00 2001 From: Kirill Markin Date: Sun, 9 Jun 2024 09:38:27 +0200 Subject: [PATCH] ignore-content setting --- .repo-to-text-settings.yaml | 12 ++++++++++++ repo_to_text/main.py | 31 +++++++++++++++++++++++++------ requirements.txt | 1 + 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 .repo-to-text-settings.yaml diff --git a/.repo-to-text-settings.yaml b/.repo-to-text-settings.yaml new file mode 100644 index 0000000..0b1d0e1 --- /dev/null +++ b/.repo-to-text-settings.yaml @@ -0,0 +1,12 @@ +# Details: https://github.com/kirill-markin/repo-to-text + +# Ignore files and directories for "Contents of ..." section +# Syntax: gitignore +ignore-content: + - ".repo-to-text-settings.yaml" + - "README.md" + - "LICENSE" + - "examples/" + - "tests/" + - "MANIFEST.in" + - "setup.py" diff --git a/repo_to_text/main.py b/repo_to_text/main.py index a275276..82295ef 100644 --- a/repo_to_text/main.py +++ b/repo_to_text/main.py @@ -3,6 +3,7 @@ import subprocess import pathspec import logging import argparse +import yaml from datetime import datetime import pyperclip @@ -33,14 +34,32 @@ def get_tree_structure(path='.', gitignore_spec=None) -> str: logging.debug('Tree structure filtering complete') return '\n'.join(filtered_lines) -def load_gitignore(path='.'): +def load_ignore_specs(path='.'): + gitignore_spec = None + content_ignore_spec = None + gitignore_path = os.path.join(path, '.gitignore') if os.path.exists(gitignore_path): logging.debug(f'Loading .gitignore from path: {gitignore_path}') with open(gitignore_path, 'r') as f: - return pathspec.PathSpec.from_lines('gitwildmatch', f) - logging.debug('.gitignore not found') - return None + gitignore_spec = pathspec.PathSpec.from_lines('gitwildmatch', f) + + repo_settings_path = os.path.join(path, '.repo-to-text-settings.yaml') + if os.path.exists(repo_settings_path): + logging.debug(f'Loading .repo-to-text-settings.yaml from path: {repo_settings_path}') + with open(repo_settings_path, 'r') as f: + ignore_data = yaml.safe_load(f) + if 'ignore-content' in ignore_data: + content_ignore_spec = pathspec.PathSpec.from_lines('gitwildmatch', ignore_data['ignore-content']) + + return gitignore_spec, content_ignore_spec + +def should_ignore_file(file_path, relative_path, gitignore_spec, content_ignore_spec): + return ( + is_ignored_path(file_path) or + (gitignore_spec and gitignore_spec.match_file(relative_path)) or + (content_ignore_spec and content_ignore_spec.match_file(relative_path)) + ) def is_ignored_path(file_path: str) -> bool: ignored_dirs = ['.git'] @@ -83,7 +102,7 @@ def remove_empty_dirs(tree_output: str, path='.') -> str: def save_repo_to_text(path='.', output_dir=None) -> str: logging.debug(f'Starting to save repo structure to text for path: {path}') - gitignore_spec = load_gitignore(path) + gitignore_spec, content_ignore_spec = load_ignore_specs(path) tree_structure = get_tree_structure(path, gitignore_spec) tree_structure = remove_empty_dirs(tree_structure, path) @@ -115,7 +134,7 @@ def save_repo_to_text(path='.', output_dir=None) -> str: file_path = os.path.join(root, filename) relative_path = os.path.relpath(file_path, path) - if is_ignored_path(file_path) or (gitignore_spec and gitignore_spec.match_file(relative_path)): + if should_ignore_file(file_path, relative_path, gitignore_spec, content_ignore_spec): continue relative_path = relative_path.replace('./', '', 1) diff --git a/requirements.txt b/requirements.txt index fcd830c..1d9e36e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ pathspec==0.12.1 pytest==8.2.2 argparse==1.4.0 pyperclip==1.8.2 +PyYAML==6.0.1