diff --git a/src/modify_repos/cmd.py b/src/modify_repos/cmd.py index 7710c4f..c196a81 100644 --- a/src/modify_repos/cmd.py +++ b/src/modify_repos/cmd.py @@ -1,11 +1,9 @@ from __future__ import annotations import collections.abc as c -import os import shlex import subprocess import typing as t -from contextlib import contextmanager from pathlib import Path import click @@ -23,30 +21,12 @@ def run_cmd(*args: CmdArg, **kwargs: t.Any) -> subprocess.CompletedProcess[str]: **kwargs, ) - if output := result.stdout.strip(): - click.echo(output) + if result.returncode: + click.echo(result.stdout) + click.secho(f"exited with code {result.returncode}", fg="red") return result def echo_cmd(args: c.Iterable[CmdArg]) -> None: click.echo(f"$ {shlex.join(str(v) for v in args)}") - - -@contextmanager -def pushd(new: Path) -> c.Iterator[None]: - current = Path.cwd() - - if current != new: - echo_cmd(["pushd", os.fspath(new)]) - os.chdir(new) - - yield - - if current != new: - echo_cmd(["popd"]) - os.chdir(current) - - -def git_add(*args: CmdArg) -> None: - run_cmd("git", "add", *args) diff --git a/src/modify_repos/models.py b/src/modify_repos/models.py index b46d87d..a66d193 100644 --- a/src/modify_repos/models.py +++ b/src/modify_repos/models.py @@ -1,13 +1,15 @@ from __future__ import annotations import dataclasses +from contextlib import chdir from functools import cached_property from inspect import isclass from pathlib import Path from pkgutil import resolve_name +import click + from modify_repos.cmd import echo_cmd -from modify_repos.cmd import pushd from modify_repos.cmd import run_cmd @@ -25,7 +27,7 @@ class Repo: self.dir.parent.mkdir(parents=True, exist_ok=True) if self.dir.exists(): - with pushd(self.dir): + with chdir(self.dir): run_cmd("git", "switch", "-f", script.target) run_cmd("git", "pull", "--prune") else: @@ -51,7 +53,7 @@ class Repo: ) script.modify(self) - if run_cmd("git", "status", "--porcelain"): + if run_cmd("git", "status", "--porcelain").stdout: run_cmd("git", "add", "--all") run_cmd("git", "commit", "--message", f"{script.title}\n\n{script.body}") @@ -134,13 +136,25 @@ class Script: ignore.write_text("*\n") for repo in self.list_repos(): + click.secho(repo.full_name, fg="green") + if self.select_for_clone(repo): repo.clone(self) - if self.select_for_modify(repo): - with pushd(repo.dir): + with chdir(repo.dir): + if self.select_for_modify(repo): repo.modify(self) - repo.push(self) + + if self.push: + repo.push(self) + else: + click.secho("skipping push", fg="yellow") + else: + click.secho("skipping modify", fg="yellow") + else: + click.secho("skipping clone", fg="yellow") + + click.echo() def select_for_clone(self, repo: Repo) -> bool: return True