1414# limitations under the License.
1515import asyncio
1616import dataclasses
17+ import fnmatch
1718import importlib .resources as pkg_resources
1819import json
1920import os
2425from datetime import datetime , timezone
2526from enum import Enum
2627from pathlib import Path
27- from typing import Any , Dict , Tuple
28+ from typing import Any , Dict , Optional , Set , Tuple
2829
2930import yaml
3031from rich .console import Console
@@ -315,31 +316,43 @@ def snake_to_camelcase(name: str) -> str:
315316 return "" .join (n .capitalize () for n in name .split ("_" ))
316317
317318
318- def get_railsignore_path () -> Path :
319+ def get_railsignore_path (path : Optional [ str ] = None ) -> Optional [ Path ] :
319320 """Helper to get railsignore path.
320321
322+ Args:
323+ path (Optional[str]): The starting path to search for the .railsignore file.
324+
321325 Returns:
322- Path: The.railsignore file path.
323- """
324- current_path = Path (__file__ ).resolve ()
326+ Path: The .railsignore file path, if found.
325327
326- # Navigate to the root directory by going up 4 levels
327- root_dir = current_path .parents [1 ]
328+ Raises:
329+ FileNotFoundError: If the .railsignore file is not found.
330+ """
331+ current_path = Path (path ) if path else Path .cwd ()
328332
329- file_path = root_dir / ".railsignore"
333+ while True :
334+ railsignore_file = current_path / ".railsignore"
335+ if railsignore_file .exists () and railsignore_file .is_file ():
336+ return railsignore_file
337+ if current_path == current_path .parent :
338+ break
339+ current_path = current_path .parent
330340
331- return file_path
341+ return None
332342
333343
334- def get_railsignore_patterns () -> set [str ]:
344+ def get_railsignore_patterns (railsignore_path ) -> Set [str ]:
335345 """
336346 Helper to retrieve all specified patterns in railsignore.
337347 Returns:
338348 Set[str]: The set of filenames or glob patterns in railsignore
339349 """
340350 ignored_patterns = set ()
341351
342- railsignore_path = get_railsignore_path ()
352+ if railsignore_path is None :
353+ return ignored_patterns
354+
355+ # railsignore_path = get_railsignore_path()
343356
344357 # File doesn't exist or is empty
345358 if not railsignore_path .exists () or not os .path .getsize (railsignore_path ):
@@ -362,3 +375,15 @@ def get_railsignore_patterns() -> set[str]:
362375 except FileNotFoundError :
363376 print (f"No { railsignore_path } found in the current directory." )
364377 return ignored_patterns
378+
379+
380+ def is_ignored_by_railsignore (filename , ignore_patterns ) -> bool :
381+ ignore = False
382+
383+ # Load candidate patterns from railsignore
384+
385+ for pattern in ignore_patterns :
386+ if fnmatch .fnmatch (filename , pattern ):
387+ ignore = True
388+
389+ return ignore
0 commit comments