Whole file
mherrmann/gitignore_parser
The author described this change as “Fix: do not resolve symlinks”. It counts as a record because the check below fails on the code as it stood at 37ecfd558 and passes on 721f804b0, with nothing else changed between the two runs.
Projectmherrmann/gitignore_parser
Fix saved2023-10-05
Sharing licenceMIT · LICENSE
Change size+14 −4
What the code was meant to do, written into the code itself as a save note
Fix: do not resolve symlinks
The change
| 2 | 2 | import os | |
| 3 | 3 | import re | |
| 4 | 4 | ||
| 5 | - | from os.path import dirname | |
| 5 | + | from os.path import abspath, dirname | |
| 6 | 6 | from pathlib import Path | |
| 7 | 7 | from typing import Reversible, Union | |
| 8 | 8 | ||
| ⋯ | |||
| 102 | 102 | negation=negation, | |
| 103 | 103 | directory_only=directory_only, | |
| 104 | 104 | anchored=anchored, | |
| 105 | - | base_path=Path(base_path) if base_path else None, | |
| 105 | + | base_path=_normalize_path(base_path) if base_path else None, | |
| 106 | 106 | source=source | |
| 107 | 107 | ) | |
| 108 | 108 | ||
| ⋯ | |||
| 125 | 125 | def match(self, abs_path: Union[str, Path]): | |
| 126 | 126 | matched = False | |
| 127 | 127 | if self.base_path: | |
| 128 | - | rel_path = str(Path(abs_path).resolve().relative_to(self.base_path)) | |
| 128 | + | rel_path = str(_normalize_path(abs_path).relative_to(self.base_path)) | |
| 129 | 129 | else: | |
| 130 | - | rel_path = str(Path(abs_path)) | |
| 130 | + | rel_path = str(_normalize_path(abs_path)) | |
| 131 | 131 | # Path() strips the trailing slash, so we need to preserve it | |
| 132 | 132 | # in case of directory-only negation | |
| 133 | 133 | if self.negation and type(abs_path) == str and abs_path[-1] == '/': | |
| ⋯ | |||
| 208 | 208 | else: | |
| 209 | 209 | res.append('($|\\/)') | |
| 210 | 210 | return ''.join(res) | |
| 211 | + | ||
| 212 | + | ||
| 213 | + | def _normalize_path(path: Union[str, Path]) -> Path: | |
| 214 | + | """Normalize a path without resolving symlinks. | |
| 215 | + | ||
| 216 | + | This is equivalent to `Path.resolve()` except that it does not resolve symlinks. | |
| 217 | + | Note that this simplifies paths by removing double slashes, `..`, `.` etc. like | |
| 218 | + | `Path.resolve()` does. | |
| 219 | + | """ | |
| 220 | + | return Path(abspath(path)) | |
| 211 | 221 | ||
The check that tells the two apart
fail→pass·tests.py::Test::test_symlink_to_another_directory
Check file tests.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before it37ecfd558c4baf573f9df1efc61d5c3414b38764
Broken version dated2023-10-04
Modulegitignore_parser
Units changedIgnoreRule, rule_from_pattern
Fingerprint151d27d7ec60dc4b
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.
Other bugs found in mherrmann/gitignore_parser
- 2024-01-19Fix ValueError for some symlinks
- 2023-10-04rule_from_pattern
- 2023-10-04fnmatch_pathname_to_regex
- 2023-10-03rule_from_pattern
- 2023-10-03fnmatch_pathname_to_regex
- 2023-06-30fnmatch_pathname_to_regex