|
| 1 | +""" |
| 2 | +Utility functions for working with git |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import re |
| 7 | +from pathlib import Path |
| 8 | +from typing import Tuple |
| 9 | + |
| 10 | +import typer |
| 11 | + |
| 12 | +from epics_containers_cli.globals import Architecture |
| 13 | +from epics_containers_cli.logging import log |
| 14 | +from epics_containers_cli.shell import ( |
| 15 | + EC_REGISTRY_MAPPING, |
| 16 | + check_beamline_repo, |
| 17 | + run_command, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def get_image_name( |
| 22 | + repo: str, arch: Architecture = Architecture.linux, target: str = "developer" |
| 23 | +) -> str: |
| 24 | + registry = repo2registry(repo).lower().removesuffix(".git") |
| 25 | + |
| 26 | + image = f"{registry}-{arch}-{target}" |
| 27 | + log.info("repo = %s image = %s", repo, image) |
| 28 | + return image |
| 29 | + |
| 30 | + |
| 31 | +def get_git_name(folder: Path = Path(".")) -> Tuple[str, Path]: |
| 32 | + """ |
| 33 | + work out the git repo name and top level folder for a local clone |
| 34 | + """ |
| 35 | + os.chdir(folder) |
| 36 | + path = str(run_command("git rev-parse --show-toplevel", interactive=False)) |
| 37 | + git_root = Path(path.strip()) |
| 38 | + |
| 39 | + remotes = str(run_command("git remote -v", interactive=False)) |
| 40 | + log.debug(f"remotes = {remotes}") |
| 41 | + |
| 42 | + matches = re.findall(r"((?:(?:git@)|(?:http[s]+:\/\/)).*) (?:.fetch.)", remotes) |
| 43 | + |
| 44 | + if len(matches) > 0: |
| 45 | + repo_name = str(matches[0]) |
| 46 | + else: |
| 47 | + log.error(f"folder {folder.absolute()} cannot parse repo name {remotes}") |
| 48 | + raise typer.Exit(1) |
| 49 | + |
| 50 | + log.debug(f"repo_name = {repo_name}, git_root = {git_root}") |
| 51 | + return repo_name, git_root |
| 52 | + |
| 53 | + |
| 54 | +# work out what the registry name is for a given repo remote e.g. |
| 55 | +def repo2registry(repo_name: str) -> str: |
| 56 | + """convert a repo name to the related a container registry name""" |
| 57 | + |
| 58 | + log.debug("extracting fields from repo name %s", repo_name) |
| 59 | + |
| 60 | + match_git = re.match(r"git@([^:]*):(.*)\/(.*)(?:.git)", repo_name) |
| 61 | + match_http = re.match(r"https:\/\/([^\/]*)\/([^\/]*)\/([^\/]*)", repo_name) |
| 62 | + for match in [match_git, match_http]: |
| 63 | + if match is not None: |
| 64 | + source_reg, org, repo = match.groups() |
| 65 | + break |
| 66 | + else: |
| 67 | + log.error(f"repo {repo_name} is not a valid git remote") |
| 68 | + raise typer.Exit(1) |
| 69 | + |
| 70 | + log.debug("source_reg = %s org = %s repo = %s", source_reg, org, repo) |
| 71 | + |
| 72 | + if not EC_REGISTRY_MAPPING: |
| 73 | + log.error("environment variable EC_REGISTRY_MAPPING not set") |
| 74 | + raise typer.Exit(1) |
| 75 | + |
| 76 | + for mapping in EC_REGISTRY_MAPPING.split(): |
| 77 | + if mapping.split("=")[0] == source_reg: |
| 78 | + registry = mapping.split("=")[1] |
| 79 | + registry = f"{registry}/{org}/{repo}" |
| 80 | + break |
| 81 | + else: |
| 82 | + log.error(f"repo {repo_name} does not match any registry mapping") |
| 83 | + log.error("please update the environment variable EC_REGISTRY_MAPPING") |
| 84 | + raise typer.Exit(1) |
| 85 | + |
| 86 | + return registry |
| 87 | + |
| 88 | + |
| 89 | +def versions(beamline_repo: str, ioc_name: str, folder: Path): |
| 90 | + """ |
| 91 | + determine the versions of an IOC instance by discovering the tags in the |
| 92 | + beamline repo at which changes to the instance were made since the last |
| 93 | + tag |
| 94 | + """ |
| 95 | + check_beamline_repo(beamline_repo) |
| 96 | + typer.echo(f"Available instance versions for {ioc_name}:") |
| 97 | + |
| 98 | + run_command(f"git clone {beamline_repo} {folder}", interactive=False) |
| 99 | + |
| 100 | + ioc_name = Path(ioc_name).name |
| 101 | + os.chdir(folder) |
| 102 | + result = str(run_command("git tag", interactive=False)) |
| 103 | + log.debug(f"checking these tags for changes in the instance: {result}") |
| 104 | + |
| 105 | + tags = result.split("\n") |
| 106 | + for tag in tags: |
| 107 | + if tag == "": |
| 108 | + continue |
| 109 | + cmd = f"git diff --name-only {tag} {tag}^" |
| 110 | + result = str(run_command(cmd, interactive=False)) |
| 111 | + |
| 112 | + if ioc_name in result: |
| 113 | + typer.echo(f" {tag}") |
0 commit comments