Skip to content

Commit 002c4fc

Browse files
authored
urls: Decouple regexes, variable consistency (#463)
2 parents 4469c2a + 080ece7 commit 002c4fc

File tree

7 files changed

+72
-29
lines changed

7 files changed

+72
-29
lines changed

CHANGES

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ $ pip install --user --upgrade --pre libvcs
1515

1616
<!-- Maintainers, insert changes / features for the next release here -->
1717

18+
### Breaking changes
19+
20+
#### urls: Variable changes (#463)
21+
22+
- `RE_PIP_REV` moved from `libvcs.url.git` to `libvcs.url.constants`.
23+
- Regex pattern for user (e.g. `git@`) decoupled to `RE_USER`.
24+
- `RE_PATH` and `SCP_REGEX` (now `RE_SCP`) no longer include user regex pattern
25+
- Existing patterns now use `RE_USER` explicitly.
26+
- `REGEX_SCP` renamed to `RE_SCP` for consistency.
27+
1828
### Documentation
1929

2030
- Automatically linkify links that were previously only text.
@@ -26,6 +36,7 @@ $ pip install --user --upgrade --pre libvcs
2636
- poetry: 1.8.1 -> 1.8.2
2737

2838
See also: https://github.com/python-poetry/poetry/blob/1.8.2/CHANGELOG.md
39+
2940
- Code quality: Use f-strings in more places (#460)
3041

3142
via [ruff 0.4.2](https://github.com/astral-sh/ruff/blob/v0.4.2/CHANGELOG.md).

docs/url/constants.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
myst:
3+
html_meta:
4+
"property=og:locale": "en_US"
5+
---
6+
(url-parser-constants)=
7+
8+
# Constants - `libvcs.url.constants`
9+
10+
```{eval-rst}
11+
.. automodule:: libvcs.url.constants
12+
:members:
13+
:undoc-members:
14+
```

docs/url/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,5 @@ svn
269269
hg
270270
base
271271
registry
272+
constants
272273
```

src/libvcs/url/constants.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Constants shared across ``libvcs.url``."""
2+
3+
RE_USER = r"""
4+
((?P<user>[^/:@]+)@)?
5+
"""
6+
"""Optional user, e.g. 'git@'"""
7+
8+
# Credit, pip (license: MIT):
9+
# https://github.com/pypa/pip/blob/22.1.2/src/pip/_internal/vcs/git.py#L39-L52
10+
# We modified it to have groupings
11+
RE_SCP = r"""
12+
# Server, e.g. 'github.com'.
13+
(?P<hostname>([^/:]+))
14+
(?P<separator>:)
15+
# The server-side path. e.g. 'user/project.git'. Must start with an
16+
# alphanumeric character so as not to be confusable with a Windows paths
17+
# like 'C:/foo/bar' or 'C:\foo\bar'.
18+
(?P<path>(\w[^:.]+))
19+
"""
20+
"""Regular expression for scp-style of git URLs."""
21+
22+
#
23+
# Third-party URLs, e.g. npm, pip, etc.
24+
#
25+
RE_PIP_REV = r"""
26+
(@(?P<rev>.*))
27+
"""
28+
"""Pip-style revision for branch or revision."""

src/libvcs/url/git.py

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,9 @@
2323
from libvcs._internal.dataclasses import SkipDefaultFieldsReprMixin
2424

2525
from .base import Rule, RuleMap, URLProtocol
26-
27-
# Credit, pip (license: MIT):
28-
# https://github.com/pypa/pip/blob/22.1.2/src/pip/_internal/vcs/git.py#L39-L52
29-
# We modified it to have groupings
30-
SCP_REGEX = r"""
31-
# Optional user, e.g. 'git@'
32-
((?P<user>\w+)@)?
33-
# Server, e.g. 'github.com'.
34-
(?P<hostname>([^/:]+))
35-
(?P<separator>:)
36-
# The server-side path. e.g. 'user/project.git'. Must start with an
37-
# alphanumeric character so as not to be confusable with a Windows paths
38-
# like 'C:/foo/bar' or 'C:\foo\bar'.
39-
(?P<path>(\w[^:.]+))
40-
"""
26+
from .constants import RE_PIP_REV, RE_SCP, RE_USER
4127

4228
RE_PATH = r"""
43-
((?P<user>\w+)@)?
4429
(?P<hostname>([^/:]+))
4530
(:(?P<port>\d{1,5}))?
4631
(?P<separator>[:,/])?
@@ -69,6 +54,7 @@
6954
rf"""
7055
^{RE_SCHEME}
7156
://
57+
{RE_USER}
7258
{RE_PATH}
7359
{RE_SUFFIX}?
7460
""",
@@ -83,7 +69,8 @@
8369
pattern=re.compile(
8470
rf"""
8571
^(?P<scheme>ssh)?
86-
{SCP_REGEX}
72+
{RE_USER}
73+
{RE_SCP}
8774
{RE_SUFFIX}?
8875
""",
8976
re.VERBOSE,
@@ -121,11 +108,6 @@
121108
)
122109
"""
123110

124-
RE_PIP_REV = r"""
125-
(@(?P<rev>.*))
126-
"""
127-
128-
129111
PIP_DEFAULT_RULES: list[Rule] = [
130112
Rule(
131113
label="pip-url",
@@ -134,6 +116,7 @@
134116
rf"""
135117
{RE_PIP_SCHEME}
136118
://
119+
{RE_USER}
137120
{RE_PATH}
138121
{RE_SUFFIX}?
139122
{RE_PIP_REV}?
@@ -148,7 +131,8 @@
148131
pattern=re.compile(
149132
rf"""
150133
{RE_PIP_SCP_SCHEME}
151-
{SCP_REGEX}?
134+
{RE_USER}
135+
{RE_SCP}?
152136
{RE_SUFFIX}?
153137
{RE_PIP_REV}?
154138
""",

src/libvcs/url/hg.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
from typing import Optional
2323

2424
from libvcs._internal.dataclasses import SkipDefaultFieldsReprMixin
25-
from libvcs.url.git import RE_PIP_REV, RE_SUFFIX, SCP_REGEX
25+
from libvcs.url.git import RE_SUFFIX
2626

2727
from .base import Rule, RuleMap, URLProtocol
28+
from .constants import RE_PIP_REV, RE_SCP, RE_USER
2829

2930
RE_PATH = r"""
30-
((?P<user>\w+)@)?
3131
(?P<hostname>([^/:]+))
3232
(:(?P<port>\d{1,5}))?
3333
(?P<separator>[:,/])?
@@ -53,6 +53,7 @@
5353
rf"""
5454
^{RE_SCHEME}
5555
://
56+
{RE_USER}
5657
{RE_PATH}
5758
{RE_SUFFIX}?
5859
{RE_PIP_REV}?
@@ -66,7 +67,8 @@
6667
pattern=re.compile(
6768
rf"""
6869
^(?P<scheme>ssh)?
69-
{SCP_REGEX}
70+
{RE_USER}
71+
{RE_SCP}
7072
{RE_SUFFIX}?
7173
""",
7274
re.VERBOSE,
@@ -100,6 +102,7 @@
100102
rf"""
101103
^{RE_PIP_SCHEME}
102104
://
105+
{RE_USER}
103106
{RE_PATH}
104107
{RE_SUFFIX}?
105108
{RE_PIP_REV}?

src/libvcs/url/svn.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@
2323
from typing import Optional
2424

2525
from libvcs._internal.dataclasses import SkipDefaultFieldsReprMixin
26-
from libvcs.url.git import RE_PIP_REV, SCP_REGEX
2726

2827
from .base import Rule, RuleMap, URLProtocol
28+
from .constants import RE_PIP_REV, RE_SCP, RE_USER
2929

3030
RE_PATH = r"""
31-
((?P<user>[^/:@]+)@)?
3231
(?P<hostname>([^/:@]+))
3332
(:(?P<port>\d{1,5}))?
3433
(?P<separator>[:,/])?
@@ -56,6 +55,7 @@
5655
rf"""
5756
^{RE_SCHEME}
5857
://
58+
{RE_USER}
5959
{RE_PATH}
6060
{RE_PIP_REV}?
6161
""",
@@ -68,7 +68,8 @@
6868
pattern=re.compile(
6969
rf"""
7070
^(?P<scheme>ssh)?
71-
{SCP_REGEX}
71+
{RE_USER}
72+
{RE_SCP}
7273
{RE_PIP_REV}?
7374
""",
7475
re.VERBOSE,
@@ -101,6 +102,7 @@
101102
rf"""
102103
^{RE_PIP_SCHEME}
103104
://
105+
{RE_USER}
104106
{RE_PATH}
105107
{RE_PIP_REV}?
106108
""",

0 commit comments

Comments
 (0)