mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-02 10:56:52 +00:00
Adds a special case for index urls similar to how comments are captured. This should avoid breaking ordering when --index-url (-i) and/or --extra-index-url are used. Also adds a relevant test to cover this case. Closes #612.
This commit is contained in:
parent
4dcb74a498
commit
87001a2778
2 changed files with 33 additions and 0 deletions
|
|
@ -17,6 +17,7 @@ class Requirement:
|
|||
def __init__(self) -> None:
|
||||
self.value: bytes | None = None
|
||||
self.comments: list[bytes] = []
|
||||
self.index_urls: list[bytes] = []
|
||||
|
||||
@property
|
||||
def name(self) -> bytes:
|
||||
|
|
@ -64,6 +65,8 @@ def fix_requirements(f: IO[bytes]) -> int:
|
|||
requirements: list[Requirement] = []
|
||||
before = list(f)
|
||||
after: list[bytes] = []
|
||||
index_options: list[bytes] = []
|
||||
extra_index_options: list[bytes] = []
|
||||
|
||||
before_string = b''.join(before)
|
||||
|
||||
|
|
@ -97,6 +100,13 @@ def fix_requirements(f: IO[bytes]) -> int:
|
|||
requirement.comments.append(line)
|
||||
elif line.lstrip().startswith(b'#') or line.strip() == b'':
|
||||
requirement.comments.append(line)
|
||||
elif (
|
||||
line.lstrip().startswith(b'-i ') or
|
||||
line.lstrip().startswith(b'--index-url')
|
||||
):
|
||||
index_options.append(line)
|
||||
elif line.lstrip().startswith(b'--extra-index-url'):
|
||||
extra_index_options.append(line)
|
||||
else:
|
||||
requirement.append_value(line)
|
||||
|
||||
|
|
@ -113,6 +123,8 @@ def fix_requirements(f: IO[bytes]) -> int:
|
|||
if req.value != b'pkg-resources==0.0.0\n'
|
||||
]
|
||||
|
||||
after.extend(index_options)
|
||||
after.extend(extra_index_options)
|
||||
for requirement in sorted(requirements):
|
||||
after.extend(requirement.comments)
|
||||
assert requirement.value, requirement.value
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue