Skip to content

Commit 4e2be00

Browse files
committed
Refactor URL handling to provide toggleable functions
1 parent e0b795d commit 4e2be00

File tree

1 file changed

+65
-7
lines changed

1 file changed

+65
-7
lines changed

src/vcspull/url.py

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

0 commit comments

Comments
 (0)