How could I easily extract hostname from a git URL like ssh://[email protected]:3333/org/repo.git
u = urlparse(s)
gives me
ParseResult(scheme='ssh', netloc='[email protected]:3333', path='/org/repo.git', params='', query='', fragment='')
which means that netloc is closest to what I want and this leaves a disappointing amount of work to me.
Should I do
u.netloc.split('@')[1].split(':')[0]
or is there a library that handles it better?
The returned ParseResult
has a hostname
attribute:
>>> urlparse('ssh://[email protected]:3333/org/repo.git').hostname
'gitlab.org.net'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With