Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse a git URL like 'ssh://[email protected]:3333/org/repo.git'?

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?

like image 470
d33tah Avatar asked Apr 21 '16 20:04

d33tah


2 Answers

The returned ParseResult has a hostname attribute:

>>> urlparse('ssh://[email protected]:3333/org/repo.git').hostname
'gitlab.org.net'
like image 170
Mureinik Avatar answered Nov 14 '22 23:11

Mureinik


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.

like image 45
John Vandenberg Avatar answered Nov 15 '22 00:11

John Vandenberg