| 725 | |
| 726 | @functools.lru_cache |
| 727 | def find_gitignores(dir: str) -> list[tuple[str, GitIgnoreSpec]]: |
| 728 | parent_dir = os.path.dirname(dir) |
| 729 | if parent_dir == dir or os.path.exists(os.path.join(dir, ".git")): |
| 730 | parent_gitignores = [] |
| 731 | git_info_exclude = os.path.join(dir, ".git", "info", "exclude") |
| 732 | if os.path.isfile(git_info_exclude): |
| 733 | with open(git_info_exclude) as f: |
| 734 | exclude_lines = f.readlines() |
| 735 | try: |
| 736 | parent_gitignores = [(dir, GitIgnoreSpec.from_lines("gitignore", exclude_lines))] |
| 737 | except GitIgnorePatternError: |
| 738 | print(f"error: could not parse {git_info_exclude}", file=sys.stderr) |
| 739 | else: |
| 740 | parent_gitignores = find_gitignores(parent_dir) |
| 741 | |
| 742 | gitignore = os.path.join(dir, ".gitignore") |
| 743 | if os.path.isfile(gitignore): |
| 744 | with open(gitignore) as f: |
| 745 | lines = f.readlines() |
| 746 | try: |
| 747 | return parent_gitignores + [(dir, GitIgnoreSpec.from_lines("gitignore", lines))] |
| 748 | except GitIgnorePatternError: |
| 749 | print(f"error: could not parse {gitignore}", file=sys.stderr) |
| 750 | return parent_gitignores |
| 751 | return parent_gitignores |
| 752 | |
| 753 | |
| 754 | def is_init_file(path: str) -> bool: |