anvil

Python Bindings for Anvil (pyanvil)

pyanvil provides high-level, type-safe Python bindings for embedding the Anvil version control engine directly into Python applications, scripts, and automation workflows. It communicates with the native libanvil dynamic shared library (libanvil.dylib on macOS, libanvil.so on Linux) through ctypes.


1. Installation and Library Linking

To build libanvil from source:

make lib

Ensure the resulting dynamic library resides in lib/ or along your system dynamic library search path (LD_LIBRARY_PATH or DYLD_LIBRARY_PATH).

To make pyanvil importable in your Python environment:

export PYTHONPATH="/path/to/anvil/bindings/python:$PYTHONPATH"

2. Quickstart Example

from pathlib import Path
from tempfile import TemporaryDirectory

from pyanvil import Repository, hash_bytes

# Compute Git SHA-1 content hash of raw payload bytes
digest = hash_bytes(b"hello world\n")
print(f"Computed SHA-1: {digest}")
# Output: 3b18e512dba79e4c8300dd08aeb37f8e728b8dad

# Use a disposable repository and release its native handle before cleanup.
with TemporaryDirectory(prefix="anvil-example-") as directory:
    with Repository.init(directory) as repo:
        Path(directory, "main.py").write_text(
            "print('Hello from Anvil')\n", encoding="utf-8"
        )

        # Stage one path per call, then commit using the native identity.
        repo.add("main.py")
        commit_id = repo.commit("feat: initial python commit")
        print(f"Created commit: {commit_id}")

        status = repo.status()
        print(f"Clean: {status.is_clean}")
        print(f"Staged count: {status.staged_count}")

        repo.branch("feature/speedup")
        repo.checkout("feature/speedup")
        print(f"HEAD: {repo.rev_parse('HEAD')}")

3. API Reference

Module-Level Functions

Repository Class

Lifecycle Management

Working Tree and Staging

Branches and Checkouts


4. Error Handling

pyanvil translates native C ABI return codes into rich Python exceptions: