2
2
3
3
Detect VCS from ` git ` , ` hg ` , and ` svn ` URLs.
4
4
5
+ ** Basic example:**
6
+
5
7
``` python
6
8
>> > from libvcs.url.registry import registry, ParserMatch
7
9
>> > from libvcs.url.git import GitURL
@@ -22,6 +24,87 @@ Detect VCS from `git`, `hg`, and `svn` URLs.
22
24
[ParserMatch(vcs = ' git' , match = GitURL(... ))]
23
25
```
24
26
27
+ ** From the ground up:**
28
+
29
+ ``` python
30
+ >> > import dataclasses
31
+ >> > from libvcs.url.base import Rule, RuleMap
32
+ >> > from libvcs.url.registry import ParserMatch, VCSRegistry
33
+ >> > from libvcs.url.git import GitURL
34
+
35
+ This will match `github:org/ repo` :
36
+
37
+ >> > class GitHubPrefix (Rule ):
38
+ ... label = ' gh-prefix'
39
+ ... description = ' Matches prefixes like github:org/repo'
40
+ ... pattern = r ' ^ github:( ?P<path> . * ) $ '
41
+ ... defaults = {
42
+ ... ' hostname' : ' github.com' ,
43
+ ... ' scheme' : ' https'
44
+ ... }
45
+ ... is_explicit = True # We know it's git, not any other VCS
46
+
47
+ Prefix for KDE infrastructure, `kde:group/ repository` :
48
+
49
+ >> > class KDEPrefix(Rule): # https://community.kde.org/Infrastructure/Git
50
+ ... label = ' kde-prefix'
51
+ ... description =' Matches prefixes like kde:org/repo'
52
+ ... pattern = r ' ^ kde:( ?P<path> \w [^ : ]+ ) $ '
53
+ ... defaults = {
54
+ ... ' hostname' : ' invent.kde.org' ,
55
+ ... ' scheme' : ' https'
56
+ ... }
57
+ ... is_explicit = True
58
+
59
+ >> > @ dataclasses.dataclass(repr = False )
60
+ ... class MyGitURLParser(GitURL):
61
+ ... rule_map: RuleMap = RuleMap(
62
+ ... _rule_map = {
63
+ ... ** GitURL.rule_map._rule_map,
64
+ ... ' github_prefix' : GitHubPrefix,
65
+ ... ' kde_prefix' : KDEPrefix,
66
+ ... }
67
+ ... )
68
+
69
+ >> > my_parsers: " ParserLazyMap" = {
70
+ ... " git" : MyGitURLParser,
71
+ ... " hg" : " libvcs.url.hg.HgURL" ,
72
+ ... " svn" : " libvcs.url.svn.SvnURL" ,
73
+ ... }
74
+
75
+ >> > vcs_matcher = VCSRegistry(parsers = my_parsers)
76
+
77
+ >> > vcs_matcher.match(' git@invent.kde.org:plasma/plasma-sdk.git' )
78
+ [ParserMatch(vcs = ' git' , match = MyGitURLParser(... )),
79
+ ParserMatch(vcs = ' hg' , match = HgURL(... )),
80
+ ParserMatch(vcs = ' svn' , match = SvnURL(... ))]
81
+
82
+ Still works with everything GitURL does:
8000
83
+
84
+ >> > vcs_matcher.match(' git+ssh://git@invent.kde.org:plasma/plasma-sdk.git' , is_explicit = True )
85
+ [ParserMatch(vcs = ' git' , match = MyGitURLParser(... ))]
86
+
87
+ >> > vcs_matcher.match(' github:webpack/webpack' , is_explicit = True )
88
+ [ParserMatch(vcs = ' git' ,
89
+ match = MyGitURLParser(url = github:webpack/ webpack,
90
+ hostname = github.com,
91
+ path = webpack/ webpack,
92
+ rule = gh- prefix))]
93
+
94
+ >> > vcs_matcher.match(' github:webpack/webpack' , is_explicit = True )[0 ].match.to_url()
95
+ ' git@github.com:webpack/webpack'
96
+
97
+ >> > vcs_matcher.match(' kde:frameworks/kirigami' , is_explicit = True )
98
+ [ParserMatch(vcs = ' git' ,
99
+ match = MyGitURLParser(url = kde:frameworks/ kirigami,
100
+ hostname = invent.kde.org,
101
+ path = frameworks/ kirigami,
102
+ rule = kde- prefix))]
103
+
104
+ >> > vcs_matcher.match(' kde:frameworks/kirigami' , is_explicit = True )[0 ].match.to_url()
105
+ ' git@invent.kde.org:frameworks/kirigami'
106
+ ```
107
+
25
108
```{eval - rst}
26
109
.. automodule:: libvcs.url.registry
27
110
:members:
0 commit comments