10
10
from libvcs import GitSync , HgSync , SvnSync
11
11
from libvcs ._internal .run import ProgressCallbackProtocol
12
12
from libvcs ._internal .types import StrPath , VCSLiteral
13
- from libvcs .exc import InvalidVCS
13
+ from libvcs .exc import InvalidVCS , LibVCSException
14
+ from libvcs .url import registry as url_tools
15
+
16
+ if t .TYPE_CHECKING :
17
+ from typing_extensions import TypeGuard
14
18
15
19
16
20
@t .overload
@@ -20,7 +24,7 @@ def create_project(
20
24
dir : StrPath ,
21
25
vcs : t .Literal ["git" ],
22
26
progress_callback : t .Optional [ProgressCallbackProtocol ] = None ,
23
- ** kwargs : dict [t .Any , t .Any ]
27
+ ** kwargs : dict [t .Any , t .Any ],
24
28
) -> GitSync :
25
29
...
26
30
@@ -32,7 +36,7 @@ def create_project(
32
36
dir : StrPath ,
33
37
vcs : t .Literal ["svn" ],
34
38
progress_callback : t .Optional [ProgressCallbackProtocol ] = None ,
35
- ** kwargs : dict [t .Any , t .Any ]
39
+ ** kwargs : dict [t .Any , t .Any ],
36
40
) -> SvnSync :
37
41
...
38
42
@@ -44,7 +48,7 @@ def create_project(
44
48
dir : StrPath ,
45
49
vcs : t .Literal ["hg" ],
46
50
progress_callback : t .Optional [ProgressCallbackProtocol ] = ...,
47
- ** kwargs : dict [t .Any , t .Any ]
51
+ ** kwargs : dict [t .Any , t .Any ],
48
52
) -> HgSync :
49
53
...
50
54
@@ -53,9 +57,9 @@ def create_project(
53
57
* ,
54
58
url : str ,
55
59
dir : StrPath ,
56
- vcs : VCSLiteral ,
60
+ vcs : t . Optional [ VCSLiteral ] = None ,
57
61
progress_callback : t .Optional [ProgressCallbackProtocol ] = None ,
58
- ** kwargs : dict [t .Any , t .Any ]
62
+ ** kwargs : dict [t .Any , t .Any ],
59
63
) -> Union [GitSync , HgSync , SvnSync ]:
60
64
r"""Return an object representation of a VCS repository.
61
65
@@ -71,6 +75,24 @@ def create_project(
71
75
>>> isinstance(r, GitSync)
72
76
True
73
77
"""
78
+ if vcs is None :
79
+ vcs_matches = url_tools .registry .match (url = url , is_explicit = True )
80
+
81
+ if len (vcs_matches ) == 0 :
82
+ raise LibVCSException (f"No vcs found for { url } " )
83
+ if len (vcs_matches ) > 1 :
84
+ raise LibVCSException (f"No exact matches for { url } " )
85
+
86
+ assert vcs_matches [0 ].vcs is not None
87
+
88
+ def is_vcs (val : t .Any ) -> "TypeGuard[VCSLiteral]" :
89
+ return isinstance (val , str ) and val in ["git" , "hg" , "svn" ]
90
+
91
+ if is_vcs (vcs_matches [0 ].vcs ):
92
+ vcs = vcs_matches [0 ].vcs
93
+ else :
94
+ raise InvalidVCS (f"{ url } does not have supported vcs: { vcs } " )
95
+
74
96
if vcs == "git" :
75
97
return GitSync (url = url , dir = dir , progress_callback = progress_callback , ** kwargs )
76
98
elif vcs == "hg" :
0 commit comments