Skip to content

Commit 2f3656d

Browse files
committed
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 <repo path>" if the new repo is not the default repo.
1 parent 96ab901 commit 2f3656d

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

zmk/commands/init.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from rich.table import Table
1818

1919
from ..build import BuildMatrix
20-
from ..config import Config, get_config
20+
from ..config import Config, get_config, set_context_repo
2121
from ..exceptions import FatalError
2222
from ..prompt import UrlPrompt
2323
from ..repo import Repo, find_containing_repo, is_repo
@@ -66,6 +66,9 @@ def init(
6666

6767
repo = Repo(Path() / name)
6868

69+
# Make sure everything we do after this point applies to this new repo.
70+
set_context_repo(ctx, repo)
71+
6972
if revision:
7073
try:
7174
repo.ensure_west_ready()
@@ -98,6 +101,14 @@ def init(
98101

99102
_add_first_keyboard(ctx, console, repo)
100103

104+
if repo.path != cfg.home_path:
105+
console.print()
106+
console.print("[yellow]The new repo is not your default repo.")
107+
console.print('Run the following command before any other "zmk" commands:')
108+
console.print()
109+
console.print(f' [green]cd "{repo.path}"')
110+
console.print()
111+
101112
# TODO: add some help for how to commit and push changes
102113

103114

zmk/config.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class Config:
3333
path: Path
3434
force_home: bool
3535

36+
override_repo_path: Path | None = None
37+
"Set this to override the path used for get_repo() without changing home_path."
38+
3639
def __init__(self, path: Path | None, force_home=False):
3740
self.path = path or _default_config_path()
3841
self.force_home = force_home
@@ -109,7 +112,13 @@ def get_repo(self) -> Repo:
109112
110113
Exits the program if neither the current directory nor the home path
111114
point to a valid directory.
115+
116+
If override_repo_path is set, this takes priority over all other methods
117+
of finding the repo.
112118
"""
119+
if self.override_repo_path:
120+
return Repo(self.override_repo_path)
121+
113122
if not self.force_home:
114123
if home := find_containing_repo():
115124
return Repo(home)
@@ -133,5 +142,19 @@ def get_config(ctx: typer.Context) -> Config:
133142
return cfg
134143

135144

145+
def set_context_repo(ctx: typer.Context, repo: Repo) -> None:
146+
"""
147+
Set the override_repo_path on the Config object for the given context to
148+
point to a given repo. All subsequent calls to get_config(ctx).get_repo()
149+
will return a Repo instance with the same path as the given one.
150+
151+
This should be used when a command wants to apply changes to a specific repo
152+
that isn't necessarily what get_config(ctx).get_repo() would normally use
153+
(e.g. when creating a new repo with "zmk init"), and when that command
154+
calls another command that gets its repo from the context.
155+
"""
156+
get_config(ctx).override_repo_path = repo.path
157+
158+
136159
def _default_config_path():
137160
return Path(typer.get_app_dir("zmk", roaming=False)) / "zmk.ini"

0 commit comments

Comments
 (0)