diff --git a/bin/do_epic/repos.py b/bin/do_epic/repos.py index b9666b3d..57cd084c 100644 --- a/bin/do_epic/repos.py +++ b/bin/do_epic/repos.py @@ -4,6 +4,7 @@ A missing checkout is cloned from the host and owner of the epic's other repos ( import dataclasses import pathlib import re +import shutil from do_epic.config import DoEpicConfig from failure import Failure @@ -15,8 +16,10 @@ from project_config.settings import by_name, CONFIG, ProjectConfig, PROJECTS 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, as on a prompt for a password or a host key. +# 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) @@ -59,8 +62,10 @@ def ensure(name: str, checkout: pathlib.Path, others: list[pathlib.Path], git: G url = clone_url(name, others, git) if isinstance(url, Failure): return url - cloned = git("clone", "--", url, str(checkout), timeout=CLONE_SECONDS) + 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 diff --git a/bin/do_epic/test_repos.py b/bin/do_epic/test_repos.py index bcf2ef88..8e03ec38 100644 --- a/bin/do_epic/test_repos.py +++ b/bin/do_epic/test_repos.py @@ -13,21 +13,25 @@ from project_config.test_project_config import write_config class FakeGit: - """git as locate calls it: origins maps a checkout to its origin URL, cloned holds each `clone` it ran, and refused names a URL whose clone fails.""" + """git as locate calls it: origins maps a checkout to its origin URL, cloned holds each `clone` that worked, and refused names a URL whose clone fails after it made the checkout's `.git`, as a stopped git does.""" def __init__(self, origins: dict[str, str | Failure], refused: str | None = None) -> None: self.origins, self.refused = origins, refused self.cloned: list[tuple[str, str]] = [] self.clone_timeouts: list[float | None] = [] + self.clone_options: list[tuple[str, ...]] = [] def __call__(self, *args: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False, errors: str = "strict") -> str | Failure: if args[:3] == ("remote", "get-url", "origin"): return self.origins[cwd or ""] - assert args[0] == "clone" and args[1] == "--", args - if args[2] == self.refused: + clone = args.index("clone") + self.clone_options.append(args[:clone]) + assert args[clone + 1] == "--", args + url, destination = args[clone + 2:clone + 4] + pathlib.Path(destination, ".git").mkdir(parents=True) + if url == self.refused: return Failure("git clone: repository not found") - self.cloned.append((args[2], args[3])) + self.cloned.append((url, destination)) self.clone_timeouts.append(timeout) - pathlib.Path(args[3], ".git").mkdir(parents=True) return "" @@ -145,11 +149,20 @@ class LocateTest(unittest.TestCase): self.origins[str(self.billing)] = "https://bot:s3cret@gitea.larvit.se" self.assertNotIn("s3cret", self.unsure(FakeGit(self.origins), "stock")) - def test_stops_a_clone_that_waits_for_a_prompt(self) -> None: + def test_stops_a_clone_that_waits_and_makes_a_prompt_fail(self) -> None: self.configure() git = FakeGit(self.origins) self.locate(git) self.assertEqual(git.clone_timeouts, [600]) + self.assertEqual(git.clone_options, [("-c", "core.askPass=true", "-c", "core.sshCommand=ssh -o BatchMode=yes")]) + + def test_removes_what_a_failed_clone_began_so_that_the_next_run_clones_again(self) -> None: + self.configure() + failing = FakeGit(self.origins, refused="git@github.com:acme/ledger") + self.unsure(failing) + self.assertFalse((self.home / "code" / "ledger").exists()) + found = self.locate(FakeGit(self.origins)) + self.assertIsInstance(found, Located) def test_asks_where_the_place_to_clone_into_is_a_file(self) -> None: self.configure()