mirror of
https://github.com/kevinveenbirkenbach/directory-content-scanner.git
synced 2025-04-22 08:02:24 +02:00
Optimized gitignore function
This commit is contained in:
parent
ab62b4d1b9
commit
69477fa29e
@ -5,44 +5,74 @@ from code_processor import CodeProcessor
|
|||||||
class DirectoryHandler:
|
class DirectoryHandler:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load_gitignore_patterns(root_path):
|
def load_gitignore_patterns(root_path):
|
||||||
"""Collect .gitignore patterns from root_path and all subdirectories."""
|
"""
|
||||||
gitignore_patterns = []
|
Recursively scans for .gitignore files in the given root_path.
|
||||||
for dirpath, dirnames, filenames in os.walk(root_path):
|
Returns a list of tuples (base_dir, patterns) where:
|
||||||
|
- base_dir: the directory in which the .gitignore was found.
|
||||||
|
- patterns: a list of pattern strings from that .gitignore.
|
||||||
|
"""
|
||||||
|
gitignore_data = []
|
||||||
|
for dirpath, _, filenames in os.walk(root_path):
|
||||||
if '.gitignore' in filenames:
|
if '.gitignore' in filenames:
|
||||||
gitignore_path = os.path.join(dirpath, '.gitignore')
|
gitignore_path = os.path.join(dirpath, '.gitignore')
|
||||||
try:
|
try:
|
||||||
with open(gitignore_path, 'r') as f:
|
with open(gitignore_path, 'r') as f:
|
||||||
for line in f:
|
lines = f.readlines()
|
||||||
line = line.strip()
|
# Filter out empty lines and comments.
|
||||||
if line and not line.startswith('#'):
|
patterns = [line.strip() for line in lines if line.strip() and not line.strip().startswith('#')]
|
||||||
# Erzeuge einen absoluten Pattern-Pfad basierend auf dem Speicherort der .gitignore
|
# Save the base directory and its patterns
|
||||||
gitignore_patterns.append(os.path.join(dirpath, line))
|
gitignore_data.append((dirpath, patterns))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error reading {gitignore_path}: {e}")
|
print(f"Error reading {gitignore_path}: {e}")
|
||||||
return gitignore_patterns
|
return gitignore_data
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def is_gitignored(file_path, gitignore_patterns):
|
def is_gitignored(file_path, gitignore_data):
|
||||||
"""Check if file_path matches any .gitignore pattern."""
|
"""
|
||||||
for pattern in gitignore_patterns:
|
Checks if file_path should be ignored according to the .gitignore entries.
|
||||||
if fnmatch.fnmatch(file_path, pattern):
|
For each tuple (base_dir, patterns), if file_path is under base_dir,
|
||||||
|
computes the relative path and matches it against the patterns.
|
||||||
|
"""
|
||||||
|
for base_dir, patterns in gitignore_data:
|
||||||
|
try:
|
||||||
|
rel_path = os.path.relpath(file_path, base_dir)
|
||||||
|
except ValueError:
|
||||||
|
# file_path and base_dir are on different drives
|
||||||
|
continue
|
||||||
|
# If the file is not under the current .gitignore base_dir, skip it.
|
||||||
|
if rel_path.startswith('..'):
|
||||||
|
continue
|
||||||
|
# Check all patterns
|
||||||
|
for pattern in patterns:
|
||||||
|
if pattern.endswith('/'):
|
||||||
|
# Directory pattern: check if any folder in the relative path matches.
|
||||||
|
parts = rel_path.split(os.sep)
|
||||||
|
for part in parts[:-1]:
|
||||||
|
if fnmatch.fnmatch(part + '/', pattern):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
# Check if the relative path matches the pattern.
|
||||||
|
if fnmatch.fnmatch(rel_path, pattern):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def filter_directories(dirs, ignore_file_strings, ignore_hidden):
|
def filter_directories(dirs, ignore_file_strings, ignore_hidden):
|
||||||
"""Filter out directories based on ignore criteria."""
|
"""
|
||||||
|
Filter out directories based on ignore_file_strings and hidden status.
|
||||||
|
"""
|
||||||
if ignore_hidden:
|
if ignore_hidden:
|
||||||
dirs[:] = [d for d in dirs if not d.startswith('.')]
|
dirs[:] = [d for d in dirs if not d.startswith('.')]
|
||||||
dirs[:] = [d for d in dirs if not any(ig in d for ig in ignore_file_strings)]
|
dirs[:] = [d for d in dirs if not any(ig in d for ig in ignore_file_strings)]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def path_or_content_contains(file_path, path_contains, content_contains):
|
def path_or_content_contains(file_path, path_contains, content_contains):
|
||||||
# Check if the file path contains specific strings (whitelist)
|
"""
|
||||||
|
Check if the file path contains specific strings or if the file content does.
|
||||||
|
"""
|
||||||
if path_contains and any(whitelist_str in file_path for whitelist_str in path_contains):
|
if path_contains and any(whitelist_str in file_path for whitelist_str in path_contains):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# Check file content for specific strings (if specified)
|
|
||||||
if content_contains:
|
if content_contains:
|
||||||
try:
|
try:
|
||||||
with open(file_path, 'r') as f:
|
with open(file_path, 'r') as f:
|
||||||
@ -56,12 +86,12 @@ class DirectoryHandler:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def should_print_file(file_path, file_types, ignore_file_strings, ignore_hidden, path_contains, content_contains):
|
def should_print_file(file_path, file_types, ignore_file_strings, ignore_hidden, path_contains, content_contains):
|
||||||
"""
|
"""
|
||||||
Determine if a file should be printed based on various criteria.
|
Determines if a file should be printed based on various criteria.
|
||||||
"""
|
"""
|
||||||
if ignore_hidden and os.path.basename(file_path).startswith('.'):
|
if ignore_hidden and os.path.basename(file_path).startswith('.'):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if file_types and not any(file_path.endswith(file_type) for file_type in file_types):
|
if file_types and not any(file_path.endswith(ft) for ft in file_types):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if any(ignore_str in file_path for ignore_str in ignore_file_strings):
|
if any(ignore_str in file_path for ignore_str in ignore_file_strings):
|
||||||
@ -73,7 +103,9 @@ class DirectoryHandler:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def print_file_content(file_path, no_comments, compress):
|
def print_file_content(file_path, no_comments, compress):
|
||||||
"""Print the content of a file."""
|
"""
|
||||||
|
Prints the content of a file, optionally removing comments or compressing the output.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
with open(file_path, 'r') as f:
|
with open(file_path, 'r') as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
@ -94,16 +126,18 @@ class DirectoryHandler:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def handle_directory(directory, **kwargs):
|
def handle_directory(directory, **kwargs):
|
||||||
"""Handle scanning and printing for directories."""
|
"""
|
||||||
gitignore_patterns = []
|
Scans the directory and processes each file while respecting .gitignore rules.
|
||||||
|
"""
|
||||||
|
gitignore_data = []
|
||||||
if not kwargs.get('no_gitignore'):
|
if not kwargs.get('no_gitignore'):
|
||||||
gitignore_patterns = DirectoryHandler.load_gitignore_patterns(directory)
|
gitignore_data = DirectoryHandler.load_gitignore_patterns(directory)
|
||||||
|
|
||||||
for root, dirs, files in os.walk(directory):
|
for root, dirs, files in os.walk(directory):
|
||||||
DirectoryHandler.filter_directories(dirs, kwargs['ignore_file_strings'], kwargs['ignore_hidden'])
|
DirectoryHandler.filter_directories(dirs, kwargs['ignore_file_strings'], kwargs['ignore_hidden'])
|
||||||
for file in files:
|
for file in files:
|
||||||
file_path = os.path.join(root, file)
|
file_path = os.path.join(root, file)
|
||||||
if gitignore_patterns and DirectoryHandler.is_gitignored(file_path, gitignore_patterns):
|
if gitignore_data and DirectoryHandler.is_gitignored(file_path, gitignore_data):
|
||||||
if kwargs.get('verbose'):
|
if kwargs.get('verbose'):
|
||||||
print(f"Skipped (gitignored): {file_path}")
|
print(f"Skipped (gitignored): {file_path}")
|
||||||
continue
|
continue
|
||||||
@ -122,5 +156,7 @@ class DirectoryHandler:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def handle_file(file_path, **kwargs):
|
def handle_file(file_path, **kwargs):
|
||||||
"""Handle scanning and printing for individual files."""
|
"""
|
||||||
|
Processes an individual file.
|
||||||
|
"""
|
||||||
DirectoryHandler.print_file_content(file_path, kwargs['no_comments'], kwargs['compress'])
|
DirectoryHandler.print_file_content(file_path, kwargs['no_comments'], kwargs['compress'])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user