Skip to content

Commit d3397f1

Browse files
committed
Error Handling for Inaccessible Directories
1 parent 549723f commit d3397f1

2 files changed

Lines changed: 42 additions & 31 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3333

3434
### Fixed
3535

36+
- **Error Handling for Inaccessible Directories**: Fixed a crash (`PermissionError`) that occurred when scanning directories with restricted read permissions. The application will now skip such directories and print a warning, preventing unintended modifications to files that might have been excluded by an unreadable `.gitignore` or other configuration files.
3637
- **Deleting empty lines**: Detected and fixed the removal of empty lines at the end of processed files
3738
- **Language-Specific Indentation**: Fixed the indentation in the generated 'Table of Contents' to respect common style conventions for each language (e.g., 4 spaces for Python, 2 for JavaScript).
3839

agent_docstrings/core.py

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ def parse_gitignore(gitignore_path: Path) -> Set[str]:
6969
line = line.strip()
7070
if line and not line.startswith("#"):
7171
patterns.add(line)
72+
except PermissionError:
73+
raise
7274
except Exception:
7375
pass
7476
return patterns
@@ -118,6 +120,8 @@ def load_blacklist_whitelist(directory: Path) -> Tuple[Set[str], Set[str]]:
118120
for line in f
119121
if line.strip() and not line.startswith("#")
120122
)
123+
except PermissionError:
124+
raise
121125
except Exception:
122126
pass
123127
if whitelist_file.exists():
@@ -128,6 +132,8 @@ def load_blacklist_whitelist(directory: Path) -> Tuple[Set[str], Set[str]]:
128132
for line in f
129133
if line.strip() and not line.startswith("#")
130134
)
135+
except PermissionError:
136+
raise
131137
except Exception:
132138
pass
133139
return blacklist, whitelist
@@ -483,41 +489,45 @@ def discover_and_process_files(paths: List[str], verbose: bool = False, beta: bo
483489
files_to_process = []
484490

485491
for p_str in paths:
486-
path = Path(p_str).resolve()
487-
if not path.exists():
488-
print(f"Warning: '{p_str}' is not a valid path. Skipping.")
489-
continue
490-
491-
if path.is_dir():
492-
# Collect all gitignore patterns from the directory tree
493-
ignore_patterns = set()
494-
current_dir = path
495-
while current_dir != current_dir.parent:
496-
gitignore_path = current_dir / '.gitignore'
497-
if gitignore_path.exists():
498-
ignore_patterns.update(parse_gitignore(gitignore_path))
499-
current_dir = current_dir.parent
500-
501-
# Load blacklist and whitelist from the root directory
502-
blacklist_patterns, whitelist_patterns = load_blacklist_whitelist(path)
503-
504-
for root, dirs, files in os.walk(path):
505-
root_path = Path(root)
492+
try:
493+
path = Path(p_str).resolve()
494+
if not path.exists():
495+
print(f"Warning: '{p_str}' is not a valid path. Skipping.")
496+
continue
506497

507-
# Filter directories to avoid walking into ignored ones
508-
dirs[:] = [d for d in dirs if d not in DEFAULT_IGNORE_DIRS and not is_path_ignored(root_path / d, ignore_patterns, path)]
498+
if path.is_dir():
499+
# Collect all gitignore patterns from the directory tree
500+
ignore_patterns = set()
501+
current_dir = path
502+
while current_dir != current_dir.parent:
503+
gitignore_path = current_dir / '.gitignore'
504+
if gitignore_path.exists():
505+
ignore_patterns.update(parse_gitignore(gitignore_path))
506+
current_dir = current_dir.parent
509507

510-
for file in files:
511-
file_path = root_path / file
508+
# Load blacklist and whitelist from the root directory
509+
blacklist_patterns, whitelist_patterns = load_blacklist_whitelist(path)
510+
511+
for root, dirs, files in os.walk(path):
512+
root_path = Path(root)
512513

513-
# Check if file should be processed
514-
if not should_process_file(file_path, path, ignore_patterns,
515-
blacklist_patterns, whitelist_patterns):
516-
continue
514+
# Filter directories to avoid walking into ignored ones
515+
dirs[:] = [d for d in dirs if d not in DEFAULT_IGNORE_DIRS and not is_path_ignored(root_path / d, ignore_patterns, path)]
517516

518-
files_to_process.append(file_path)
519-
elif path.is_file():
520-
files_to_process.append(path)
517+
for file in files:
518+
file_path = root_path / file
519+
520+
# Check if file should be processed
521+
if not should_process_file(file_path, path, ignore_patterns,
522+
blacklist_patterns, whitelist_patterns):
523+
continue
524+
525+
files_to_process.append(file_path)
526+
elif path.is_file():
527+
files_to_process.append(path)
528+
except PermissionError:
529+
print(f"Warning: Could not read configuration (e.g., .gitignore) in '{p_str}' due to a permission error. Skipping path to ensure no unintended files are modified.")
530+
continue
521531

522532
# Process all collected files
523533
for file_path in sorted(list(set(files_to_process))):

0 commit comments

Comments
 (0)