Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion zmk/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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


Expand Down
23 changes: 23 additions & 0 deletions zmk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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"