From 5ae0a8f7132d841e061a03562fd0f7da4e7d1327 Mon Sep 17 00:00:00 2001 From: Denys SAVCHENKO Date: Wed, 5 Jun 2024 11:38:06 +0200 Subject: [PATCH] pre-cleanup tools repo --- odabot/cli.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/odabot/cli.py b/odabot/cli.py index 72e460c..686c9de 100755 --- a/odabot/cli.py +++ b/odabot/cli.py @@ -714,21 +714,28 @@ def make_galaxy_tools(obj, dry_run, loop, force, pattern): with open(git_credentials) as fd: token = fd.read().split(':')[-1].split('@')[0] - def git_clone_or_update(local_path, remote, branch=None, origin='origin'): + def git_clone_or_update(local_path, remote, branch=None, origin='origin', pre_cleanup=False): if os.path.isdir(local_path) and os.listdir(): os.chdir(local_path) - if branch is None: - # determine the default branch automatically - outp = sp.run(['git', 'remote', 'show', origin], check=True, capture_output=True) - m = re.search(r'^\s*(\w+ )*HEAD( \w+)*\s?:\s?(?P\w+)', outp.stdout.decode(), re.MULTILINE) - if m is None: - raise RuntimeError(f"Can't determine default branch for remote {remote}.") - branch = m.group('branch') try: + if branch is None: + # determine the default branch automatically + outp = sp.run(['git', 'remote', 'show', origin], check=True, capture_output=True) + m = re.search(r'^\s*(\w+ )*HEAD( \w+)*\s?:\s?(?P\w+)', outp.stdout.decode(), re.MULTILINE) + if m is None: + raise RuntimeError(f"Can't determine default branch for remote {remote}.") + branch = m.group('branch') + res = sp.run(['git', 'remote', 'get-url', '--push', origin], check=True, capture_output=True, text=True) if res.stdout.strip() != remote: raise ValueError + + if pre_cleanup: + sp.run(['git', 'restore', '--staged', '.'], check=True) + sp.run(['git', 'restore', '.'], check=True) + sp.run(['git', 'clean', '-fd'], check=True) + sp.run(['git', 'checkout', branch], check=True) sp.run(['git', 'pull', origin, branch], check=True) sp.run(['git', 'remote', 'update', origin, '--prune']) @@ -788,7 +795,7 @@ def make_pr(source_repo, source_branch, target_repo, target_branch, title='New P oda_bot_runtime["deployed_tools"] = {} deployed_tools = oda_bot_runtime["deployed_tools"] - git_clone_or_update(tools_repo_dir, tools_repo, target_branch) + git_clone_or_update(tools_repo_dir, tools_repo, target_branch, pre_cleanup=True) os.chdir(tools_repo_dir) sp.run(['git', 'config', 'user.name', git_name], check=True) sp.run(['git', 'config', 'user.email', git_email], check=True)