-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
334 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1560,63 +1560,87 @@ class GitURL(str): | |
Represents a url to a git repo and can parse info about / modify the | ||
protocol | ||
References: | ||
https://git-scm.com/docs/git-clone#_git_urls | ||
TODO: can use git-well as a helper here. | ||
CommandLine: | ||
xdoctest -m /home/joncrall/code/xcookie/xcookie/main.py GitURL | ||
xdoctest -m xcookie.main GitURL | ||
Example: | ||
>>> from git_well.git_remote_protocol import * # NOQA | ||
>>> url1 = GitURL('https://foo.bar/user/repo.git') | ||
>>> url2 = GitURL('[email protected]:group/repo.git') | ||
>>> print(url1.to_git()) | ||
>>> print(url1.to_https()) | ||
>>> print(url2.to_git()) | ||
>>> print(url2.to_https()) | ||
[email protected]:user/repo.git | ||
https://foo.bar/user/repo.git | ||
[email protected]:group/repo.git | ||
https://foo.bar/group/repo.git | ||
>>> print(ub.urepr(url1.info)) | ||
>>> print(ub.urepr(url2.info)) | ||
{ | ||
'host': 'foo.bar', | ||
'group': 'user', | ||
'repo_name': 'repo.git', | ||
'user': None, | ||
'protocol': 'https', | ||
'url': 'https://foo.bar/user/repo.git', | ||
} | ||
{ | ||
'host': 'foo.bar', | ||
'group': 'group', | ||
'repo_name': 'repo.git', | ||
'user': 'git', | ||
'protocol': 'git', | ||
'url': '[email protected]:group/repo.git', | ||
} | ||
>>> urls = [ | ||
>>> GitURL('https://foo.bar/user/repo.git'), | ||
>>> GitURL('ssh://foo.bar/user/repo.git'), | ||
>>> GitURL('ssh://[email protected]/user/repo.git'), | ||
>>> GitURL('[email protected]:group/repo.git'), | ||
>>> GitURL('host:path/to/my/repo/.git'), | ||
>>> ] | ||
>>> for url in urls: | ||
>>> print('---') | ||
>>> print(f'url = {url}') | ||
>>> print(ub.urepr(url.info)) | ||
>>> print('As git : ' + url.to_git()) | ||
>>> print('As ssh : ' + url.to_ssh()) | ||
>>> print('As https : ' + url.to_https()) | ||
""" | ||
|
||
def __init__(self, data): | ||
# note: inheriting from str so data is handled in __new__ | ||
self._info = None | ||
|
||
def _parse(self): | ||
import parse | ||
parse.Parser('ssh://{user}') | ||
|
||
@property | ||
def info(self): | ||
if self._info is None: | ||
url = self | ||
info = {} | ||
if url.startswith('https://'): | ||
parts = url.split('https://')[1].split('/') | ||
parts = url.split('https://')[1].split('/', 3) | ||
info['host'] = parts[0] | ||
info['group'] = parts[1] | ||
info['repo_name'] = parts[2] | ||
info['user'] = None | ||
info['protocol'] = 'https' | ||
elif url.startswith('git@'): | ||
url.split('git@')[1] | ||
parts = url.split('git@')[1].split(':') | ||
info['host'] = parts[0] | ||
info['group'] = parts[1].split('/')[0] | ||
info['repo_name'] = parts[1].split('/')[1] | ||
info['user'] = 'git' | ||
info['protocol'] = 'git' | ||
elif url.startswith('ssh://'): | ||
parts = url.split('ssh://')[1].split('/', 3) | ||
user = None | ||
if '@' in parts[0]: | ||
user, host = parts[0].split('@') | ||
else: | ||
host = parts[0] | ||
info['host'] = host | ||
info['user'] = user | ||
info['group'] = parts[1] | ||
info['repo_name'] = parts[2] | ||
info['protocol'] = 'ssh' | ||
elif url.endswith('/.git'): | ||
# An ssh protocol to an explicit directory | ||
host, rest = url.split(':', 1) | ||
parts = rest.rsplit('/', 2) | ||
info['host'] = host | ||
info['group'] = parts[0] | ||
# info['group'] = '' | ||
info['repo_name'] = parts[1] + '/.git' | ||
info['protocol'] = 'scp' | ||
elif '//' not in url and '@' not in url: | ||
parts = url.split(':') | ||
info['host'] = parts[0] | ||
info['group'] = parts[1].split('/')[0] | ||
info['repo_name'] = parts[1].split('/')[1] | ||
info['protocol'] = 'ssh' | ||
else: | ||
raise ValueError(url) | ||
info['url'] = url | ||
|
@@ -1628,11 +1652,22 @@ def to_git(self): | |
new_url = 'git@' + info['host'] + ':' + info['group'] + '/' + info['repo_name'] | ||
return self.__class__(new_url) | ||
|
||
def to_ssh(self): | ||
info = self.info | ||
user = info.get('user', None) | ||
if user is None: | ||
user_part = '' | ||
else: | ||
user_part = user + '@' | ||
new_url = 'ssh://' + user_part + info['host'] + '/' + info['group'] + '/' + info['repo_name'] | ||
return self.__class__(new_url) | ||
|
||
def to_https(self): | ||
info = self.info | ||
new_url = 'https://' + info['host'] + '/' + info['group'] + '/' + info['repo_name'] | ||
return self.__class__(new_url) | ||
|
||
|
||
if __name__ == '__main__': | ||
""" | ||
CommandLine: | ||
|
Oops, something went wrong.