|
| 1 | +from pathlib import Path |
| 2 | +from typing import Optional, Sequence |
| 3 | +import pathspec |
| 4 | +import sys |
| 5 | + |
| 6 | +from .patterns import DEFAULT_EXTENSIONS, EXCLUDED_DIRS, EXCLUDED_PATTERNS |
| 7 | + |
| 8 | + |
| 9 | +def find_gitignore(start_path: Path) -> Optional[Path]: |
| 10 | + """Search for .gitignore file in current and parent directories.""" |
| 11 | + print(f"Searching for gitignore from: {start_path}", file=sys.stderr) |
| 12 | + current = start_path.absolute() |
| 13 | + while current != current.parent: |
| 14 | + gitignore = current / ".gitignore" |
| 15 | + if gitignore.is_file(): |
| 16 | + print(f"Found gitignore at: {gitignore}", file=sys.stderr) |
| 17 | + return gitignore |
| 18 | + current = current.parent |
| 19 | + print("No gitignore found", file=sys.stderr) |
| 20 | + return None |
| 21 | + |
| 22 | + |
| 23 | +def get_gitignore_spec( |
| 24 | + path: Path, extra_patterns: Optional[list[str]] = None, verbose: bool = False |
| 25 | +) -> pathspec.PathSpec: |
| 26 | + """Load .gitignore patterns and combine with our default exclusions.""" |
| 27 | + if verbose: |
| 28 | + print(f"Getting gitignore spec for: {path}", file=sys.stderr) |
| 29 | + |
| 30 | + patterns = list(EXCLUDED_PATTERNS) |
| 31 | + if verbose: |
| 32 | + print(f"Added {len(EXCLUDED_PATTERNS)} default patterns", file=sys.stderr) |
| 33 | + |
| 34 | + # Add directory exclusions |
| 35 | + dir_patterns = [f"{d}/" for d in EXCLUDED_DIRS] |
| 36 | + patterns.extend(dir_patterns) |
| 37 | + if verbose: |
| 38 | + print(f"Added {len(dir_patterns)} directory exclusions", file=sys.stderr) |
| 39 | + |
| 40 | + # Add any extra patterns provided |
| 41 | + if extra_patterns: |
| 42 | + patterns.extend(extra_patterns) |
| 43 | + if verbose: |
| 44 | + print(f"Added {len(extra_patterns)} extra patterns", file=sys.stderr) |
| 45 | + |
| 46 | + # Add patterns from .gitignore if found |
| 47 | + gitignore_path = find_gitignore(path) if verbose else None |
| 48 | + if gitignore_path: |
| 49 | + with open(gitignore_path) as f: |
| 50 | + gitignore_patterns = [ |
| 51 | + line.strip() for line in f if line.strip() and not line.startswith("#") |
| 52 | + ] |
| 53 | + patterns.extend(gitignore_patterns) |
| 54 | + if verbose: |
| 55 | + print( |
| 56 | + f"Added {len(gitignore_patterns)} patterns from gitignore", |
| 57 | + file=sys.stderr, |
| 58 | + ) |
| 59 | + |
| 60 | + if verbose: |
| 61 | + print(f"Total patterns: {len(patterns)}", file=sys.stderr) |
| 62 | + return pathspec.PathSpec.from_lines("gitwildmatch", patterns) |
| 63 | + |
| 64 | + |
| 65 | +def scan_directory( |
| 66 | + path: Path, |
| 67 | + include: Optional[Sequence[str]] = None, |
| 68 | + extra_patterns: Optional[list[str]] = None, |
| 69 | + verbose: bool = False, |
| 70 | +) -> list[Path]: |
| 71 | + """ |
| 72 | + Scan directory for relevant files. |
| 73 | +
|
| 74 | + Args: |
| 75 | + path: Directory to scan |
| 76 | + include: File extensions to include (without dots) |
| 77 | + extra_patterns: Additional gitignore-style patterns to exclude |
| 78 | + verbose: Whether to print debug information |
| 79 | +
|
| 80 | + Returns: |
| 81 | + List of paths to relevant files |
| 82 | + """ |
| 83 | + if verbose: |
| 84 | + print(f"\nScanning directory: {path}", file=sys.stderr) |
| 85 | + |
| 86 | + if not path.is_dir(): |
| 87 | + raise ValueError(f"Path {path} is not a directory") |
| 88 | + |
| 89 | + # Use provided extensions or defaults |
| 90 | + include_set = {f".{ext.lstrip('.')}" for ext in (include or DEFAULT_EXTENSIONS)} |
| 91 | + |
| 92 | + if verbose: |
| 93 | + print(f"Include extensions: {include_set}", file=sys.stderr) |
| 94 | + |
| 95 | + # Get combined gitignore and default exclusions |
| 96 | + spec = get_gitignore_spec(path, extra_patterns, verbose) |
| 97 | + |
| 98 | + result = [] |
| 99 | + processed = 0 |
| 100 | + skipped = 0 |
| 101 | + |
| 102 | + if verbose: |
| 103 | + print("\nStarting file scan...", file=sys.stderr) |
| 104 | + |
| 105 | + for file_path in path.rglob("*"): |
| 106 | + processed += 1 |
| 107 | + if verbose and processed % 100 == 0: |
| 108 | + print( |
| 109 | + f"Processed {processed} files, found {len(result)}, skipped {skipped}...", |
| 110 | + file=sys.stderr, |
| 111 | + ) |
| 112 | + |
| 113 | + # Skip non-files |
| 114 | + if not file_path.is_file(): |
| 115 | + skipped += 1 |
| 116 | + continue |
| 117 | + |
| 118 | + # Get relative path for pattern matching |
| 119 | + try: |
| 120 | + rel_path = file_path.relative_to(path) |
| 121 | + except ValueError: |
| 122 | + skipped += 1 |
| 123 | + continue |
| 124 | + |
| 125 | + # Skip excluded patterns |
| 126 | + if spec.match_file(str(rel_path)): |
| 127 | + skipped += 1 |
| 128 | + continue |
| 129 | + |
| 130 | + # Apply extension filters |
| 131 | + ext = file_path.suffix.lower() |
| 132 | + if ext not in include_set: |
| 133 | + skipped += 1 |
| 134 | + continue |
| 135 | + |
| 136 | + result.append(file_path) |
| 137 | + |
| 138 | + if verbose: |
| 139 | + print( |
| 140 | + f"\nScan complete: processed {processed} files, found {len(result)}, skipped {skipped}", |
| 141 | + file=sys.stderr, |
| 142 | + ) |
| 143 | + return sorted(result) |
0 commit comments