From 2db44ab778f67b7f7260b4f18bd03f6fd714295d Mon Sep 17 00:00:00 2001 From: Joel Spadin Date: Sun, 11 Jan 2026 10:58:06 -0600 Subject: [PATCH] fix(init): Improve behavior when new repo is not default Fixed #54 by adding a new system where a command can override the repo returned by Config.get_repo(). This ensures that when "zmk init" passes through to "zmk keyboard add", it works on the correct repo regardless of whether the user set the new repo as their default. Also, when initializing a repo but not setting it as the default, any "zmk ..." commands run after that point would still run on the default repo. To try to avoid any confusion caused by that, "zmk init" will now print a message at the end instructing the user to "cd " if the new repo is not the default repo. --- zmk/commands/init.py | 13 ++++++++++++- zmk/config.py | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/zmk/commands/init.py b/zmk/commands/init.py index 7e04c17..c56d77c 100644 --- a/zmk/commands/init.py +++ b/zmk/commands/init.py @@ -17,7 +17,7 @@ from rich.table import Table from ..build import BuildMatrix -from ..config import Config, get_config +from ..config import Config, get_config, set_context_repo from ..exceptions import FatalError from ..prompt import UrlPrompt from ..repo import Repo, find_containing_repo, is_repo @@ -66,6 +66,9 @@ def init( repo = Repo(Path() / name) + # Make sure everything we do after this point applies to this new repo. + set_context_repo(ctx, repo) + if revision: try: repo.ensure_west_ready() @@ -98,6 +101,14 @@ def init( _add_first_keyboard(ctx, console, repo) + if repo.path != cfg.home_path: + console.print() + console.print("[yellow]The new repo is not your default repo.") + console.print('Run the following command before any other "zmk" commands:') + console.print() + console.print(f' [green]cd "{repo.path}"') + console.print() + # TODO: add some help for how to commit and push changes diff --git a/zmk/config.py b/zmk/config.py index e17c9a9..7cd61b3 100644 --- a/zmk/config.py +++ b/zmk/config.py @@ -33,6 +33,9 @@ class Config: path: Path force_home: bool + override_repo_path: Path | None = None + "Set this to override the path used for get_repo() without changing home_path." + def __init__(self, path: Path | None, force_home=False): self.path = path or _default_config_path() self.force_home = force_home @@ -109,7 +112,13 @@ def get_repo(self) -> Repo: Exits the program if neither the current directory nor the home path point to a valid directory. + + If override_repo_path is set, this takes priority over all other methods + of finding the repo. """ + if self.override_repo_path: + return Repo(self.override_repo_path) + if not self.force_home: if home := find_containing_repo(): return Repo(home) @@ -133,5 +142,19 @@ def get_config(ctx: typer.Context) -> Config: return cfg +def set_context_repo(ctx: typer.Context, repo: Repo) -> None: + """ + Set the override_repo_path on the Config object for the given context to + point to a given repo. All subsequent calls to get_config(ctx).get_repo() + will return a Repo instance with the same path as the given one. + + This should be used when a command wants to apply changes to a specific repo + that isn't necessarily what get_config(ctx).get_repo() would normally use + (e.g. when creating a new repo with "zmk init"), and when that command + calls another command that gets its repo from the context. + """ + get_config(ctx).override_repo_path = repo.path + + def _default_config_path(): return Path(typer.get_app_dir("zmk", roaming=False)) / "zmk.ini"