Skip to content

Commit

Permalink
Update util yaml and git url helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Erotemic committed Oct 12, 2024
1 parent af9b984 commit 40f5eb4
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 58 deletions.
7 changes: 5 additions & 2 deletions requirements/runtime.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ black>=22.1.0 ; python_version >= '3.6.2' # Pyt

packaging>=21.3

PyYAML>=6.0
ruamel.yaml >= 0.17.21
# PyYAML>=6.0
# ruamel.yaml >= 0.17.21
ruamel.yaml>=0.17.22
PyYAML>=6.0.1 ; python_version < '4.0' and python_version >= '3.12' # Python 3.12+
PyYAML>=6.0 ; python_version < '3.12' # Python 3.6 - 3.11

argcomplete>=1.0

Expand Down
2 changes: 1 addition & 1 deletion xcookie/builders/github_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def codecov_action(cls, *args, **kwargs):
https://github.com/codecov/codecov-action
"""
return cls.action({
'uses': 'codecov/codecov-action@v4.0.1'
'uses': 'codecov/codecov-action@v4.5.0'
}, *args, **kwargs)

@classmethod
Expand Down
97 changes: 66 additions & 31 deletions xcookie/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
Loading

0 comments on commit 40f5eb4

Please sign in to comment.