Skip to content

Commit 1645d1d

Browse files
committed
Refactor URL handling to provide toggleable functions
1 parent 3f35019 commit 1645d1d

File tree

1 file changed

+68
-7
lines changed

1 file changed

+68
-7
lines changed

src/vcspull/url.py

+68-7
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,71 @@
44

55
from libvcs.url.git import DEFAULT_RULES
66

7-
# Find the core-git-scp rule and modify it to be explicit
8-
for rule in DEFAULT_RULES:
9-
if rule.label == "core-git-scp":
10-
# Make the rule explicit so it can be detected with is_explicit=True
11-
rule.is_explicit = True
12-
# Increase the weight to ensure it takes precedence
13-
rule.weight = 100
7+
8+
def enable_ssh_style_url_detection() -> None:
9+
"""Enable detection of SSH-style URLs as explicit Git URLs.
10+
11+
This makes the core-git-scp rule explicit, which allows URLs like
12+
'user@hostname:path/to/repo.git' to be detected with is_explicit=True.
13+
14+
Examples
15+
--------
16+
>>> from vcspull.url import enable_ssh_style_url_detection
17+
>>> from libvcs.url.git import GitURL
18+
>>> # Without the patch
19+
>>> GitURL.is_valid('user@hostname:path/to/repo.git', is_explicit=True)
20+
False
21+
>>> # With the patch
22+
>>> enable_ssh_style_url_detection()
23+
>>> GitURL.is_valid('user@hostname:path/to/repo.git', is_explicit=True)
24+
True
25+
"""
26+
for rule in DEFAULT_RULES:
27+
if rule.label == "core-git-scp":
28+
# Make the rule explicit so it can be detected with is_explicit=True
29+
rule.is_explicit = True
30+
# Increase the weight to ensure it takes precedence
31+
rule.weight = 100
32+
33+
34+
def disable_ssh_style_url_detection() -> None:
35+
"""Disable detection of SSH-style URLs as explicit Git URLs.
36+
37+
This reverts the core-git-scp rule to its original state, where URLs like
38+
'user@hostname:path/to/repo.git' are not detected with is_explicit=True.
39+
40+
Examples
41+
--------
42+
>>> from vcspull.url import enable_ssh_style_url_detection, disable_ssh_style_url_detection
43+
>>> from libvcs.url.git import GitURL
44+
>>> # Enable the patch
45+
>>> enable_ssh_style_url_detection()
46+
>>> GitURL.is_valid('user@hostname:path/to/repo.git', is_explicit=True)
47+
True
48+
>>> # Disable the patch
49+
>>> disable_ssh_style_url_detection()
50+
>>> GitURL.is_valid('user@hostname:path/to/repo.git', is_explicit=True)
51+
False
52+
"""
53+
for rule in DEFAULT_RULES:
54+
if rule.label == "core-git-scp":
55+
# Revert to original state
56+
rule.is_explicit = False
57+
rule.weight = 0
58+
59+
60+
def is_ssh_style_url_detection_enabled() -> bool:
61+
"""Check if SSH-style URL detection is enabled.
62+
63+
Returns
64+
-------
65+
bool: True if SSH-style URL detection is enabled, False otherwise.
66+
"""
67+
for rule in DEFAULT_RULES:
68+
if rule.label == "core-git-scp":
69+
return rule.is_explicit
70+
return False
71+
72+
73+
# Enable SSH-style URL detection by default
74+
enable_ssh_style_url_detection()

0 commit comments

Comments
 (0)