Skip to content

Commit

Permalink
Merge pull request #71 from oda-hub/pre-cleanup-galaxy-repo
Browse files Browse the repository at this point in the history
Pre-cleanup tools repo
  • Loading branch information
dsavchenko committed Jun 5, 2024
2 parents e67b5e3 + 5ae0a8f commit edff3c9
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions odabot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<branch>\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<branch>\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'])
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit edff3c9

Please sign in to comment.