code_state - Tinker Documentation

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:

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)
    ```