ignore gitignore files content

This commit is contained in:
Kirill Markin 2024-06-08 10:08:09 +02:00
parent 752ca966c4
commit 5c1c7d98e9
4 changed files with 47 additions and 6 deletions

7
.gitignore vendored
View file

@ -160,3 +160,10 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear # and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
# Ignore egg-info directory
repo_to_text.egg-info/
# Ignore generated repo_structure.txt file
repo_structure.txt

View file

@ -1,12 +1,37 @@
import os import os
import subprocess import subprocess
import pathspec
def get_tree_structure(path='.') -> str: def get_tree_structure(path='.', gitignore_spec=None) -> str:
# Run the tree command and get its output
result = subprocess.run(['tree', path], stdout=subprocess.PIPE) result = subprocess.run(['tree', path], stdout=subprocess.PIPE)
return result.stdout.decode('utf-8') tree_output = result.stdout.decode('utf-8')
if not gitignore_spec:
return tree_output
# Filter the tree output to exclude files in .gitignore (excluding .gitignore itself)
filtered_lines = []
for line in tree_output.splitlines():
parts = line.split()
if not any(gitignore_spec.match_file(os.path.join(path, part)) for part in parts if part != '.gitignore'):
filtered_lines.append(line)
return '\n'.join(filtered_lines)
def load_gitignore(path='.'):
gitignore_path = os.path.join(path, '.gitignore')
if os.path.exists(gitignore_path):
with open(gitignore_path, 'r') as f:
return pathspec.PathSpec.from_lines('gitwildmatch', f)
return None
def is_ignored_file(file_path: str) -> bool:
# Check if the file is part of .git or should be ignored according to .gitignore rules
return '.git' in file_path.split(os.sep) or file_path.endswith('.gitignore')
def save_repo_to_text(path='.') -> None: def save_repo_to_text(path='.') -> None:
tree_structure: str = get_tree_structure(path) gitignore_spec = load_gitignore(path)
tree_structure: str = get_tree_structure(path, gitignore_spec)
with open('repo_structure.txt', 'w') as file: with open('repo_structure.txt', 'w') as file:
file.write(tree_structure + '\n') file.write(tree_structure + '\n')
@ -14,6 +39,11 @@ def save_repo_to_text(path='.') -> None:
for filename in files: for filename in files:
file_path: str = os.path.join(root, filename) file_path: str = os.path.join(root, filename)
relative_path: str = os.path.relpath(file_path, path) relative_path: str = os.path.relpath(file_path, path)
# Check if the file should be ignored
if is_ignored_file(file_path) or (gitignore_spec and gitignore_spec.match_file(file_path)):
continue
file.write(f'\n{relative_path}\n') file.write(f'\n{relative_path}\n')
file.write('```\n') file.write('```\n')
try: try:

View file

@ -1 +1,2 @@
setuptools==70.0.0 setuptools==70.0.0
pathspec==0.12.1

View file

@ -1,10 +1,13 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
with open('requirements.txt') as f:
required = f.read().splitlines()
setup( setup(
name='repo-to-text', name='repo-to-text',
version='0.1', version='0.1',
packages=find_packages(), packages=find_packages(),
install_requires=[], install_requires=required,
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'repo-to-text=repo_to_text.main:main', 'repo-to-text=repo_to_text.main:main',