97 lines
5.2 KiB
Python
97 lines
5.2 KiB
Python
"""Where an epic's repo is on this machine: its checkout and its project config, found by the repo's name.
|
|
|
|
A missing checkout is cloned from the host and owner of the epic's other repos (docs/decisions.md → `do-epic: drives one Jira epic …`). Where that is unclear, the answer is an Unsure."""
|
|
import dataclasses
|
|
import pathlib
|
|
import re
|
|
import shutil
|
|
|
|
from project_config.settings import by_name, CONFIG, ProjectConfig, PROJECTS
|
|
from do_epic.config import DoEpicConfig
|
|
from failure import Failure
|
|
from git_cli.git_repo import Git
|
|
from forge.commands import url_beside
|
|
|
|
# One folder name, which can't be a path or a git option.
|
|
REPO_NAME = r"\w[\w.-]*"
|
|
# The addresses a clone may use: https, ssh, or scp-style `user@host:path`. Not ext:: or file:, which run a program or read this machine's files, and an origin can spell either.
|
|
CLONE_ADDRESS = rf"https://[\w.-]+(:\d+)?/\S+|ssh://[\w.@-]+(:\d+)?/\S+|\w[\w.-]*@[\w.-]+:[\w./~-]+"
|
|
# A clone that waits longer is stopped.
|
|
CLONE_SECONDS = 600
|
|
# git's options that make a password or a host key fail at once instead of prompting: an unattended run never waits on a prompt (docs/decisions.md → principle 6).
|
|
NO_PROMPTS = ("-c", "core.askPass=true", "-c", "core.sshCommand=ssh -o BatchMode=yes")
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Located:
|
|
"""checkout is the repo's git checkout, and config the project config that names it."""
|
|
checkout: pathlib.Path
|
|
config: ProjectConfig
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Unsure(Failure):
|
|
"""reason is a question for the human."""
|
|
|
|
|
|
def locate(name: str, others: list[pathlib.Path], config: DoEpicConfig, home: pathlib.Path, git: Git) -> Located | Failure:
|
|
"""The checkout and project config of the repo called name; others are the checkouts of the epic's other repos.
|
|
|
|
A project config names the repo's checkout, and it is cloned where missing. Where no config names the repo, it is cloned into the clone folder, if it isn't there, and the human is asked to write the config, since the repo's plan, goals and records are theirs to place.
|
|
A name that is no repo's name, or a config that can't be read, is a plain Failure. Any other fault is an Unsure."""
|
|
if not re.fullmatch(REPO_NAME, name):
|
|
return Failure(f"{name!r} is not a repo's name: one folder name that starts with a letter, digit or underscore")
|
|
found = by_name(name, home)
|
|
if isinstance(found, Failure):
|
|
return found
|
|
checkout = found.top if found else config.clone_folder / name
|
|
cloned = ensure(name, checkout, others, git)
|
|
if isinstance(cloned, Failure):
|
|
return cloned
|
|
if found is None:
|
|
return Unsure(f"{checkout} {'was cloned' if cloned else 'holds the repo'}, but no project config names it. Write {home / PROJECTS}/<key>/{CONFIG} with `checkout = \"{checkout}\"`, as `project-config --help` shows, and run do-epic again.")
|
|
return Located(checkout, found)
|
|
|
|
|
|
def ensure(name: str, checkout: pathlib.Path, others: list[pathlib.Path], git: Git) -> bool | Failure:
|
|
"""Whether checkout was cloned just now: False where it is a git checkout already. Else it is cloned there from the host and owner of the others, or an Unsure says why not."""
|
|
if (checkout / ".git").exists():
|
|
return False
|
|
if checkout.exists() and (not checkout.is_dir() or any(checkout.iterdir())):
|
|
return Unsure(f"{checkout} exists and is not a git checkout, so {name} cannot be cloned there. Where should it be cloned?")
|
|
url = clone_url(name, others, git)
|
|
if isinstance(url, Failure):
|
|
return url
|
|
cloned = git(*NO_PROMPTS, "clone", "--", url, str(checkout), timeout=CLONE_SECONDS)
|
|
if isinstance(cloned, Failure):
|
|
# What the clone began, and nothing else: checkout was absent or empty. A kept half-clone would pass for the repo on the next run.
|
|
shutil.rmtree(checkout, ignore_errors=True)
|
|
return Unsure(f"cannot clone {url} to {checkout}: {cloned.reason}. Where does {name} live?")
|
|
return True
|
|
|
|
|
|
def without_credentials(url: str) -> str:
|
|
"""url without a `user:token@` after its scheme, so that a clone and a question for the team never carry one. The clone then uses git's own credentials."""
|
|
return re.sub(r"^(https?://)[^/]*@", r"\1", url)
|
|
|
|
|
|
def clone_url(name: str, others: list[pathlib.Path], git: Git) -> str | Failure:
|
|
"""The address of the repo called name, under the host and owner all the others share. An Unsure where they share none or an address can't be built."""
|
|
if not others:
|
|
return Unsure(f"{name} is missing and no other repo of the epic shows what host to clone it from. Where does it live?")
|
|
urls = set()
|
|
for other in others:
|
|
origin = git("remote", "get-url", "origin", cwd=str(other))
|
|
if isinstance(origin, Failure):
|
|
return Unsure(f"cannot read the origin of {other}: {origin.reason}. Where does {name} live?")
|
|
beside = url_beside(without_credentials(origin.strip()), name)
|
|
if beside is None:
|
|
return Unsure(f"{other} has an origin, {without_credentials(origin.strip())}, with no host and owner to put another repo under. Where does {name} live?")
|
|
urls.add(beside)
|
|
if len(urls) > 1:
|
|
return Unsure(f"The epic's other repos sit under more than one host or owner, so {name} could be at {' or '.join(sorted(urls))}. Which holds it?")
|
|
url = next(iter(urls))
|
|
if not re.fullmatch(CLONE_ADDRESS, url):
|
|
return Unsure(f"{url} is not an https, ssh or scp-style address. Where does {name} live?")
|
|
return url
|