Using the standard lib urlparse
will fail to parse many valid git URLs.
>>> from urllib.parse import urlparse
>>> urlparse('[email protected]:Org/Private-repo.git')
ParseResult(scheme='', netloc='', path='[email protected]:Org/Private-repo.git', params='', query='', fragment='')
https://pypi.python.org/pypi/git-url-parse is a fairly good parser of git URLs with a similar interface to urlparse
.
>>> import giturlparse
>>> url = giturlparse.parse('ssh://[email protected]:3333/org/repo.git')
>>> url
Parsed(pathname='/org/repo.git', protocols=['ssh'], protocol='ssh', href='ssh://[email protected]:3333/org/repo.git', resource='gitlab.com', user='git', port='3333', name='repo', owner='org')
>>> url.resource
'gitlab.com'
https://pypi.org/project/giturlparse/ is another one, which is more recently updated, and uses a similar API.
Note both of those PyPI packages install to directory giturlparse
, so they conflict with each other, but they due to having a similar API they are almost interchangable.