Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support npm-style #ref suffix; fixes issue 16 #17

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 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 @@ -62,40 +63,45 @@ def parse(self):
'pathname': None,
'protocols': self._get_protocols(),
'protocol': 'ssh',
'href': self._url,
'href': self._url.split("#")[0],
'resource': None,
'user': None,
'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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would this ever be used? https://docs.npmjs.com/files/package.json#git-urls-as-dependencies? Thats an NPM url.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's just an example of one project that uses the convention. yarn uses it, too.

I've been using it in my scripts to configure buildbot, and I find it useful.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, it's not that much code to do without this feature.

Copy link
Collaborator

@retr0h retr0h May 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but npm isnt a git url neither are yarn urls. I have not seen a git url having this format so I would have a hard time accepting a PR like this bc it's outside the scope of the project.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a useful convention even outside of npm, but it's easy to implement as a tiny wrapper around git-url-parse, so closing.

'[email protected]:owner/repo.git#blort': {
'pathname': 'owner/repo.git',
'protocols': [],
'protocol': 'ssh',
'href': '[email protected]:owner/repo.git',
'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