Skip to content

Commit

Permalink
Support npm-style #ref suffix; fixes issue 16
Browse files Browse the repository at this point in the history
  • Loading branch information
dankegel committed May 24, 2018
1 parent ff5c252 commit 89e93af
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
18 changes: 12 additions & 6 deletions giturlparse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def get_parsed(self):
'port',
'name',
'owner',
'ref',
])

def parse(self):
Expand All @@ -68,34 +69,39 @@ def parse(self):
'port': None,
'name': None,
'owner': None,
'ref': None,
}
regexes = [
(r'^(?P<protocol>https?|git|ssh|rsync)\://'
'(?:(?P<user>.+)@)*'
'(?P<resource>[a-z0-9_.-]*)'
'[:/]*'
'(?P<port>[\d]+){0,1}'
'(?P<pathname>\/(?P<owner>.+)/(?P<name>.+).git)'),
'(?P<pathname>\/(?P<owner>.+)/(?P<name>.+).git)'
'(#(?P<ref>.+)){0,1}$'),
(r'(git\+)?'
'((?P<protocol>\w+)://)'
'((?P<user>\w+)@)?'
'((?P<resource>[\w\.\-]+))'
'(:(?P<port>\d+))?'
'(?P<pathname>(\/(?P<owner>\w+)/)?'
'(\/?(?P<name>[\w\-]+)(\.git)?)?)'),
'(\/?(?P<name>[\w\-]+)(\.git)?)?)'
'(#(?P<ref>.+)){0,1}$'),
(r'^(?:(?P<user>.+)@)*'
'(?P<resource>[a-z0-9_.-]*)[:/]*'
'(?P<port>[\d]+){0,1}'
'[:](?P<pathname>\/?(?P<owner>.+)/(?P<name>.+).git)'),
'[:](?P<pathname>\/?(?P<owner>.+)/(?P<name>.+).git)'
'(#(?P<ref>.+)){0,1}$'),
(r'((?P<user>\w+)@)?'
'((?P<resource>[\w\.\-]+))'
'[\:\/]{1,2}'
'(?P<pathname>((?P<owner>\w+)/)?'
'((?P<name>[\w\-]+)(\.git)?)?)'),
'((?P<name>[\w\-]+)(\.git)?)?)'
'(#(?P<ref>.+)){0,1}$'),
]
for regex in regexes:
if re.search(regex, self._url):
m = re.search(regex, self._url)
m = re.search(regex, self._url)
if m:
d.update(m.groupdict())
break
else:
Expand Down
48 changes: 48 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'ssh://[email protected]:29418/owner/repo.git': {
'pathname': '/owner/repo.git',
Expand All @@ -48,6 +49,7 @@ def git_urls():
'port': '29418',
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'ssh://example.com/owner/repo.git': {
'pathname': '/owner/repo.git',
Expand All @@ -59,6 +61,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'ssh://example.com:29418/owner/repo.git': {
'pathname': '/owner/repo.git',
Expand All @@ -70,6 +73,7 @@ def git_urls():
'port': '29418',
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'[email protected]:/owner/repo.git': {
'pathname': '/owner/repo.git',
Expand All @@ -81,6 +85,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'example.com:/owner/repo.git': {
'pathname': '/owner/repo.git',
Expand All @@ -92,6 +97,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'[email protected]:owner/repo.git': {
'pathname': 'owner/repo.git',
Expand All @@ -103,6 +109,34 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
# gitlab and github allow omitting the .git
'[email protected]:owner/repo': {
'pathname': 'owner/repo',
'protocols': [],
'protocol': 'ssh',
'href': '[email protected]:owner/repo',
'resource': 'example.com',
'user': 'user',
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
# npm and others allow specifying a branch or commit. See
# https://docs.npmjs.com/files/package.json#git-urls-as-dependencies
'[email protected]:owner/repo.git#blort': {
'pathname': 'owner/repo.git',
'protocols': [],
'protocol': 'ssh',
'href': '[email protected]:owner/repo.git#blort',
'resource': 'example.com',
'user': 'user',
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': 'blort',
},
'[email protected]:owner/repo.git': {
'pathname': 'owner/repo.git',
Expand All @@ -114,6 +148,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'example.com:owner/repo.git': {
'pathname': 'owner/repo.git',
Expand All @@ -125,6 +160,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'rsync://example.com/owner/repo.git': {
'pathname': '/owner/repo.git',
Expand All @@ -136,6 +172,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'git://example.com/owner/repo.git': {
'pathname': '/owner/repo.git',
Expand All @@ -147,6 +184,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'http://example.com/owner/repo.git': {
'pathname': '/owner/repo.git',
Expand All @@ -158,6 +196,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'https://example.com/owner/repo.git': {
'pathname': '/owner/repo.git',
Expand All @@ -169,6 +208,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'https://example.com/owner/repo': {
'pathname': '/owner/repo',
Expand All @@ -180,6 +220,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'example.com:repo.git': {
'pathname': 'repo.git',
Expand All @@ -191,6 +232,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': None,
'ref': None,
},
'https://example.com/repo': {
'pathname': '/repo',
Expand All @@ -202,6 +244,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': None,
'ref': None,
},
'https://example.in/repo': {
'pathname': '/repo',
Expand All @@ -213,6 +256,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': None,
'ref': None,
},
'[email protected]:repo.git': {
'pathname': 'repo.git',
Expand All @@ -224,6 +268,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': None,
'ref': None,
},
'git+ssh://example.com/owner/repo.git': {
'pathname': '/owner/repo.git',
Expand All @@ -235,6 +280,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'git+https://example.com/owner/repo.git': {
'pathname': '/owner/repo.git',
Expand All @@ -246,6 +292,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'owner',
'ref': None,
},
'https://[email protected]/user/repo': {
'pathname': '/user/repo',
Expand All @@ -257,6 +304,7 @@ def git_urls():
'port': None,
'name': 'repo',
'owner': 'user',
'ref': None,
},
}

Expand Down
1 change: 1 addition & 0 deletions test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def test_parse(git_urls):
assert d['port'] == result.port
assert d['name'] == result.name
assert d['owner'] == result.owner
assert d['ref'] == result.ref


def test_parse_raises_on_invalid_url(invalid_urls):
Expand Down

0 comments on commit 89e93af

Please sign in to comment.