58 lines
2.4 KiB
Python
58 lines
2.4 KiB
Python
"""Print the paths a project's config sets.
|
|
|
|
usage: project-config [<setting>]
|
|
|
|
Run inside a checkout, or a worktree of one. It finds the config that names the checkout,
|
|
~/.claude-projects/<key>/config.toml, and prints each setting and its absolute path, as in
|
|
`plan: /home/me/code/acme/todo.md`. Given a setting, it prints only that setting's path.
|
|
|
|
The config is TOML. `checkout` names the project's main checkout, and every other setting is a path:
|
|
"checkout:<path>" inside the checkout, "folder:<path>" inside the config's own folder, or one that
|
|
starts with / or ~/. In a worktree, a "checkout:" path is the worktree's own file.
|
|
|
|
checkout = "~/code/acme-billing"
|
|
plan = "folder:todo.md"
|
|
working-rules = "folder:AGENTS.md"
|
|
decisions = "folder:docs/decisions.md"
|
|
score-anchors = "folder:docs/score-anchors.md"
|
|
comprehension-history = "folder:docs/comprehension-history.md"
|
|
reading-cost = "folder:docs/reading-cost.md"
|
|
changelog = "checkout:CHANGELOG.md"
|
|
goals-and-audience = "folder:goals.md"
|
|
principles = "folder:principles"
|
|
|
|
Exit 1, naming each fault on stderr, where no config names the checkout, or its config leaves out a
|
|
setting, names an unknown one or misspells a path. No setting has a default.
|
|
"""
|
|
import pathlib
|
|
import sys
|
|
|
|
from failure import Failure
|
|
from git_cli.git_repo import git
|
|
from project_config.settings import load, ProjectConfig, SETTINGS
|
|
|
|
|
|
def main(argv: list[str]) -> int:
|
|
if argv[:1] in (["-h"], ["--help"]):
|
|
print(__doc__.strip())
|
|
return 0
|
|
if len(argv) > 1 or (argv and argv[0] not in SETTINGS):
|
|
print(f"usage: project-config [<setting>], where <setting> is one of {', '.join(SETTINGS)}", file=sys.stderr)
|
|
return 1
|
|
config = located()
|
|
if isinstance(config, Failure):
|
|
print(config.reason, file=sys.stderr)
|
|
return 1
|
|
print(config.paths[argv[0]] if argv else "\n".join(f"{name}: {config.paths[name]}" for name in SETTINGS))
|
|
return 0
|
|
|
|
|
|
def located() -> ProjectConfig | Failure:
|
|
found = git("rev-parse", "--path-format=absolute", "--show-toplevel", "--git-common-dir")
|
|
if isinstance(found, Failure):
|
|
return found
|
|
if len(lines := found.splitlines()) != 2:
|
|
return Failure(f"git rev-parse printed {found!r}, not a checkout's top folder and git directory on two lines")
|
|
# The common dir is the main checkout's .git, whichever worktree runs this.
|
|
return load(pathlib.Path(lines[0]), pathlib.Path(lines[1]).parent, pathlib.Path.home())
|