Skip to content

tinker_cookbook.utils.code_state

tinker_cookbook.utils.code_state(modules)

Capture the current code state for one or more Python modules as a diff string.

For each module this function locates its filesystem path, discovers the enclosing Git repository, records the current commit hash (HEAD), and includes combined staged+unstaged changes (git diff HEAD) for the entire repository. The output is suitable for storage alongside experiment metadata to reproduce the exact code state later.

Parameters:

  • modules (Sequence[str | ModuleType]) – Module import names (e.g. "tinker_cookbook.rl") or already-imported module objects.

Returns: str – Concatenated sections, one per repository, each beginning with a header of the form ### repo: <repo_root> @ <commit_hash> followed by the repo-wide diff. Modules not in a Git repository get a short note instead.

state = code_state(["tinker_cookbook", "tinker"])
with open("code.diff", "w") as f:
f.write(state)