Keep configs in ~/.config/build-release/ and tool state in ~/.local/state/build-release/, under XDG_* where set
CI / tests (pull_request) Successful in 6m34s

This commit is contained in:
2026-12-03 18:56:28 +01:00
parent 20c83f861f
commit 1ca2c8efe0
142 changed files with 1095 additions and 933 deletions
+13 -12
View File
@@ -10,7 +10,7 @@ kept in a Jira epic's description: edit those two in Jira.
open prints the path of a file holding the record's text, and nothing else. Read and edit that
file. Where the record is kept as a file, the path is that file. Where it is kept elsewhere, the
path is a copy under ~/.local/state/project-record/<key>/<worktree>/, a folder named for the
path is a copy under ~/.local/state/build-release/project-record/<key>/<worktree>/, a folder named for the
worktree that ran open, so two worktrees of one project don't share a copy. The copy is written
afresh with the record as it stands, so open it again before each edit. open also removes the
folders of worktrees git no longer lists. A record that doesn't exist yet has no file at the path:
@@ -27,6 +27,7 @@ until open runs again and writes the copy afresh.
Exit 2 on bad arguments, and 1, saying why, on any other failure.
"""
import hashlib
import os
import pathlib
import shutil
import sys
@@ -36,12 +37,12 @@ from record_store.stores import FileStore, goals_store, local_file, RECORDS, Rec
from git_cli.git_repo import git, Git
from project_config.main import located
from project_config.settings import ProjectConfig
from loop_state.paths import STATE_FOLDER
from loop_state.paths import state_folder
# Not git's verbs, such as checkout and commit: those read as acting on the repo.
COMMANDS = ("open", "save")
# Where the copy of a record kept outside files goes, under the user's home, in a folder per project and worktree.
COPIES = f"{STATE_FOLDER}/project-record"
COPIES = "project-record"
# The goals file opens like a record, though it is a setting of its own.
GOALS = "goals-and-audience"
@@ -54,8 +55,8 @@ def main(argv: list[str]) -> int:
print(f"usage: project-record {'|'.join(COMMANDS)} <record>, where <record> is one of {', '.join((*RECORDS, GOALS))}", file=sys.stderr)
return 2
home = pathlib.Path.home()
config = located(None, home)
done = config if isinstance(config, Failure) else performed(config, argv[0], argv[1], home)
config = located(None, home, os.environ)
done = config if isinstance(config, Failure) else performed(config, argv[0], argv[1], state_folder(os.environ, home))
if isinstance(done, Failure):
print(f"project-record: {done.reason}", file=sys.stderr)
return 1
@@ -63,16 +64,16 @@ def main(argv: list[str]) -> int:
return 0
def performed(config: ProjectConfig, command: str, name: str, home: pathlib.Path, run: Git = git) -> str | Failure:
def performed(config: ProjectConfig, command: str, name: str, state: pathlib.Path, run: Git = git) -> str | Failure:
"""What command, one of COMMANDS, prints for the record called name. An open also prunes the copies of removed worktrees."""
if command == "open":
pruned(config, home, run)
return act(command, *opened(config, name, home))
pruned(config, state, run)
return act(command, *opened(config, name, state))
def opened(config: ProjectConfig, name: str, home: pathlib.Path) -> tuple[RecordStore, pathlib.Path]:
def opened(config: ProjectConfig, name: str, state: pathlib.Path) -> tuple[RecordStore, pathlib.Path]:
"""The store of the record called name, and the copy it gets where it isn't kept as a file."""
copy = home / COPIES / config.key / worktree_folder(config.top) / f"{name}.md"
copy = state / COPIES / config.key / worktree_folder(config.top) / f"{name}.md"
return (goals_store(config) if name == GOALS else store(config, name)), copy
@@ -81,10 +82,10 @@ def worktree_folder(top: pathlib.Path) -> str:
return f"{top.name}-{hashlib.sha256(str(top).encode()).hexdigest()[:8]}"
def pruned(config: ProjectConfig, home: pathlib.Path, run: Git = git) -> None:
def pruned(config: ProjectConfig, state: pathlib.Path, run: Git = git) -> None:
"""Remove the copies of the worktrees git no longer lists, so the folders don't pile up one per chunk. Best effort: a folder that stays only takes disk, and where git can't list the worktrees nothing is removed."""
listed = run("worktree", "list", "--porcelain")
folders = home / COPIES / config.key
folders = state / COPIES / config.key
if isinstance(listed, Failure) or not folders.is_dir():
return
worktrees = [config.top, *(pathlib.Path(line.removeprefix("worktree ")) for line in listed.splitlines() if line.startswith("worktree "))]
+21 -19
View File
@@ -17,38 +17,39 @@ from project_config.settings import Page, ProjectConfig
TOOL = pathlib.Path(__file__).resolve().parents[1] / "project-record"
COPIES = pathlib.Path("/home/me/.local/state/project-record/acme")
STATE = pathlib.Path("/home/me/.local/state/build-release")
COPIES = STATE / "project-record/acme"
def worktree_config(top: str, **kept: Page) -> ProjectConfig:
return ProjectConfig("acme", pathlib.Path("/home/me/.claude-projects/acme"), pathlib.Path(top), {"goals-and-audience": pathlib.Path(top, "README.md")}, pathlib.Path("/home/me/acme.env"), pages=kept)
return ProjectConfig("acme", pathlib.Path("/home/me/.config/build-release/acme"), pathlib.Path(top), {"goals-and-audience": pathlib.Path(top, "README.md")}, pathlib.Path("/home/me/acme.env"), pages=kept)
class Opened(unittest.TestCase):
def test_a_record_on_a_confluence_page_gets_a_copy_in_a_folder_of_its_worktree(self):
page = Page("https://acme.atlassian.net", "2293761", "confluence:https://acme.atlassian.net/wiki/spaces/ENG/pages/2293761")
record, copy = main.opened(worktree_config("/code/acme", decisions=page), "decisions", pathlib.Path("/home/me"))
record, copy = main.opened(worktree_config("/code/acme", decisions=page), "decisions", STATE)
self.assertEqual((copy.parent.parent, copy.name), (COPIES, "decisions.md"))
self.assertRegex(copy.parent.name, r"^acme-[0-9a-f]{8}$")
self.assertIsInstance(record, ConfluencePage)
def test_the_goals_file_opens_through_the_goals_store(self):
record, copy = main.opened(worktree_config("/code/acme"), "goals-and-audience", pathlib.Path("/home/me"))
record, copy = main.opened(worktree_config("/code/acme"), "goals-and-audience", STATE)
self.assertEqual((record.label, copy.parent.parent, copy.name), ("/code/acme/README.md", COPIES, "goals-and-audience.md"))
def test_two_worktrees_of_one_project_get_copies_that_differ(self):
home = pathlib.Path("/home/me")
home = STATE
main_checkout = main.opened(worktree_config("/code/acme"), "goals-and-audience", home)[1]
other = main.opened(worktree_config("/code/acme-item-5"), "goals-and-audience", home)[1]
self.assertNotEqual(main_checkout, other)
def test_one_worktree_always_gets_the_same_copy(self):
home = pathlib.Path("/home/me")
home = STATE
first = main.opened(worktree_config("/code/acme"), "goals-and-audience", home)[1]
self.assertEqual(first, main.opened(worktree_config("/code/acme"), "goals-and-audience", home)[1])
def test_two_worktrees_named_alike_in_different_folders_get_copies_that_differ(self):
home = pathlib.Path("/home/me")
home = STATE
first = main.opened(worktree_config("/code/acme-item-5"), "goals-and-audience", home)[1]
second = main.opened(worktree_config("/other/acme-item-5"), "goals-and-audience", home)[1]
self.assertNotEqual(first, second)
@@ -58,9 +59,10 @@ class Pruned(unittest.TestCase):
def setUp(self) -> None:
self.home = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory()))
self.config = worktree_config("/code/acme-item-5")
self.folders = self.home / ".local/state/project-record/acme"
self.state = self.home / ".local/state/build-release"
self.folders = self.state / "project-record/acme"
for top in ("/code/acme", "/code/acme-item-5", "/code/acme-item-6"):
copy = main.opened(worktree_config(top), "goals-and-audience", self.home)[1]
copy = main.opened(worktree_config(top), "goals-and-audience", self.state)[1]
copy.parent.mkdir(parents=True)
copy.write_text("edit")
@@ -71,37 +73,37 @@ class Pruned(unittest.TestCase):
return lambda *args, **kwargs: "" if args != ("worktree", "list", "--porcelain") else "".join(f"worktree {top}\nHEAD abc\nbranch refs/heads/x\n\n" for top in tops)
def test_removes_the_copies_of_worktrees_git_no_longer_lists_and_keeps_the_others(self):
main.pruned(self.config, self.home, self.listed("/code/acme", "/code/acme-item-5"))
main.pruned(self.config, self.state, self.listed("/code/acme", "/code/acme-item-5"))
self.assertEqual(self.kept(), {main.worktree_folder(pathlib.Path(top)) for top in ("/code/acme", "/code/acme-item-5")})
def test_always_keeps_the_copy_of_the_worktree_that_asked(self):
main.pruned(self.config, self.home, self.listed("/code/acme"))
main.pruned(self.config, self.state, self.listed("/code/acme"))
self.assertIn(main.worktree_folder(pathlib.Path("/code/acme-item-5")), self.kept())
def test_removes_nothing_where_git_cannot_list_the_worktrees(self):
main.pruned(self.config, self.home, lambda *args, **kwargs: Failure("not a repository"))
main.pruned(self.config, self.state, lambda *args, **kwargs: Failure("not a repository"))
self.assertEqual(len(self.kept()), 3)
def test_leaves_files_beside_the_folders_alone(self):
(self.folders / "decisions.md").write_text("from before the folders")
main.pruned(self.config, self.home, self.listed("/code/acme"))
main.pruned(self.config, self.state, self.listed("/code/acme"))
self.assertTrue((self.folders / "decisions.md").exists())
def test_a_copy_that_cannot_be_removed_does_not_fail_the_open(self):
self.folders.chmod(0o500)
self.addCleanup(self.folders.chmod, 0o700)
main.pruned(self.config, self.home, self.listed("/code/acme"))
main.pruned(self.config, self.state, self.listed("/code/acme"))
self.assertEqual(len(self.kept()), 3)
def test_an_open_prunes_and_a_save_does_not(self):
run = self.listed("/code/acme", "/code/acme-item-5")
self.assertEqual(main.performed(self.config, "save", "goals-and-audience", self.home, run), "/code/acme-item-5/README.md is the record itself, so nothing is left to store.")
self.assertEqual(main.performed(self.config, "save", "goals-and-audience", self.state, run), "/code/acme-item-5/README.md is the record itself, so nothing is left to store.")
self.assertEqual(len(self.kept()), 3)
self.assertEqual(main.performed(self.config, "open", "goals-and-audience", self.home, run), "/code/acme-item-5/README.md")
self.assertEqual(main.performed(self.config, "open", "goals-and-audience", self.state, run), "/code/acme-item-5/README.md")
self.assertEqual(self.kept(), {main.worktree_folder(pathlib.Path(top)) for top in ("/code/acme", "/code/acme-item-5")})
def test_does_nothing_where_the_project_has_no_copies(self):
main.pruned(worktree_config("/code/acme"), self.home / "elsewhere", self.listed("/code/acme"))
main.pruned(worktree_config("/code/acme"), self.state / "elsewhere", self.listed("/code/acme"))
class Act(unittest.TestCase):
@@ -130,7 +132,7 @@ class Act(unittest.TestCase):
class BlackBox(unittest.TestCase):
def setUp(self) -> None:
self.top = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())).resolve()
self.home = configured(self, self.top).parents[1]
self.home = configured(self, self.top).parents[2]
def run_tool(self, *args: str) -> subprocess.CompletedProcess[str]:
return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, cwd=self.top, env=os.environ | {"HOME": str(self.home)}, text=True)
@@ -153,7 +155,7 @@ class BlackBox(unittest.TestCase):
self.assertIn("usage: project-record open|save <record>, where <record> is one of decisions, score-anchors, comprehension-history, reading-cost, changelog, goals-and-audience", ran.stderr)
def test_exits_1_saying_why_where_no_config_names_the_checkout(self):
(self.home / ".claude-projects" / self.top.name / "config.toml").unlink()
(self.home / ".config/build-release" / self.top.name / "config.toml").unlink()
ran = self.run_tool("open", "decisions")
self.assertEqual((ran.returncode, ran.stdout), (1, ""))
self.assertIn("no config names the checkout", ran.stderr)