-
Notifications
You must be signed in to change notification settings - Fork 675
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
5 changed files
with
68 additions
and
54 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
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 |
---|---|---|
|
@@ -56,39 +56,26 @@ def update(): | |
repos_in_use = set() | ||
for repo_name, repo in config.repos.items(): | ||
repo = parse_repo_url(repo, repo_name) | ||
repos_in_use.add((repo.server, repo.owner, repo.repo)) | ||
if repo.path.exists(): # TODO: use update instead of remove and clone | ||
repos_in_use.add((repo.server, repo.owner, repo.repo, repo.branch)) | ||
if repo.path.exists(): | ||
shutil.rmtree(repo.path, ignore_errors=True) | ||
if not repo.path.exists(): | ||
repo.path.parent.mkdir(parents=True, exist_ok=True) | ||
try: | ||
dulwich.porcelain.clone( | ||
f'https://{repo.server}/{repo.owner}/{repo.repo}.git', | ||
str(repo.path), | ||
checkout=True, | ||
depth=1, | ||
branch=repo.branch, | ||
) | ||
output('') | ||
output(f'Repo `{repo.name}` updated', style='green') | ||
except: | ||
shutil.rmtree(repo.path, ignore_errors=True) | ||
output(f'Failed to clone repo {repo.name}', style='red') | ||
else: | ||
try: | ||
import dulwich.porcelain | ||
|
||
dulwich.porcelain.pull( | ||
str(repo.path), f'https://{repo.server}/{repo.owner}/{repo.repo}.git', refspecs=repo.branch, force=True | ||
) | ||
dulwich.porcelain.clean(str(repo.path), str(repo.path)) | ||
output('') | ||
output(f'Repo `{repo.name}` updated', style='green') | ||
except: | ||
shutil.rmtree(repo.path, ignore_errors=True) | ||
output(f'Failed to update repo {repo.name}', style='red') | ||
for c in REPO_DIR.glob('*/*/*'): | ||
repo_spec = tuple(c.parts[-3:]) | ||
repo.path.parent.mkdir(parents=True, exist_ok=True) | ||
try: | ||
dulwich.porcelain.clone( | ||
repo.url, | ||
str(repo.path), | ||
checkout=True, | ||
depth=1, | ||
branch=repo.branch, | ||
) | ||
output('') | ||
output(f'Repo `{repo.name}` updated', style='green') | ||
except Exception as e: | ||
shutil.rmtree(repo.path, ignore_errors=True) | ||
output(f'Failed to clone repo {repo.name}', style='red') | ||
output(e) | ||
for c in REPO_DIR.glob('*/*/*/*'): | ||
repo_spec = tuple(c.parts[-4:]) | ||
if repo_spec not in repos_in_use: | ||
shutil.rmtree(c, ignore_errors=True) | ||
output(f'Removed unused repo cache {c}') | ||
|
@@ -127,26 +114,50 @@ def ensure_repo_updated(): | |
) | ||
|
||
|
||
GIT_REPO_RE = re.compile(r'git\+https://(?P<server>.+)/(?P<owner>.+)/(?P<repo>.+?)(@(?P<branch>.+))?$') | ||
GIT_HTTP_RE = re.compile(r'(?P<schema>git|ssh|http|https):\/\/(?P<server>[\.\w\d\-]+)\/(?P<owner>[\w\d\-]+)\/(?P<repo>[\w\d\-\_\.]+)(@(?P<branch>.+))?(\/)?$') | ||
GIT_SSH_RE = re.compile(r'git@(?P<server>[\.\w\d-]+):(?P<owner>[\w\d\-]+)\/(?P<repo>[\w\d\-\_\.]+)(@(?P<branch>.+))?(\/)?$') | ||
|
||
|
||
def parse_repo_url(repo_url: str, repo_name: typing.Optional[str] = None) -> RepoInfo: | ||
""" | ||
parse the git repo url to server, owner, repo name, branch | ||
>>> parse_repo_url('git+https://github.com/bentoml/bentovllm@main') | ||
>>> parse_repo_url('https://github.com/bentoml/bentovllm@main') | ||
('github.com', 'bentoml', 'bentovllm', 'main') | ||
>>> parse_repo_url('git+https://github.com/bentoml/bentovllm') | ||
>>> parse_repo_url('https://github.com/bentoml/bentovllm.git@main') | ||
('github.com', 'bentoml', 'bentovllm', 'main') | ||
>>> parse_repo_url('https://github.com/bentoml/bentovllm') | ||
('github.com', 'bentoml', 'bentovllm', 'main') | ||
>>> parse_repo_url('[email protected]:bentoml/openllm-models.git') | ||
('github.com', 'bentoml', 'openllm-models', 'main') | ||
""" | ||
match = GIT_REPO_RE.match(repo_url) | ||
if not match: | ||
raise ValueError(f'Invalid git repo url: {repo_url}') | ||
match = GIT_HTTP_RE.match(repo_url) | ||
if match: | ||
schema = match.group('schema') | ||
else: | ||
match = GIT_SSH_RE.match(repo_url) | ||
if not match: | ||
raise ValueError(f'Invalid git repo url: {repo_url}') | ||
schema = None | ||
|
||
if match.group('branch') is not None: | ||
repo_url = repo_url[:match.start('branch') - 1] | ||
|
||
server = match.group('server') | ||
owner = match.group('owner') | ||
repo = match.group('repo') | ||
if repo.endswith('.git'): | ||
repo = repo[:-4] | ||
branch = match.group('branch') or 'main' | ||
path = REPO_DIR / server / owner / repo | ||
|
||
if schema is not None: | ||
repo_url = f'{schema}://{server}/{owner}/{repo}' | ||
else: | ||
repo_url = f'git@{server}:{owner}/{repo}' | ||
|
||
path = REPO_DIR / server / owner / repo / branch | ||
return RepoInfo( | ||
name=repo if repo_name is None else repo_name, | ||
url=repo_url, | ||
|
@@ -165,6 +176,12 @@ def add(name: str, repo: str): | |
output(f'Invalid repo name: {name}, should only contain letters, numbers and underscores', style='red') | ||
return | ||
|
||
try: | ||
parse_repo_url(repo) | ||
except ValueError as e: | ||
output(f'Invalid repo url: {repo}', style='red') | ||
return | ||
|
||
config = load_config() | ||
if name in config.repos: | ||
override = questionary.confirm(f'Repo {name} already exists({config.repos[name]}), override?').ask() | ||
|