Skip to content

Atomic Transactions

Transactions collect multiple file changes and commit them as a single atomic operation. No partial commits — either all changes land or none do.

from githosted import Client
client = Client()
repo = client.repo("my-project")
with repo.transaction("Refactor auth module") as tx:
tx.write("src/auth.py", new_auth_code)
tx.write("tests/test_auth.py", new_test_code)
tx.delete("src/old_auth.py")

repo.transaction(message, *, ref, expected_head)

Section titled “repo.transaction(message, *, ref, expected_head)”

Start a transaction. Returns a context manager that stages changes and commits on exit.

with repo.transaction("Add feature") as tx:
tx.write("src/feature.py", code)
tx.write("tests/test_feature.py", tests)
with repo.transaction(
"Fix bug on feature branch",
ref="feature/fix",
expected_head=file.head_sha,
) as tx:
tx.write("src/main.py", fixed_code)

Stage a file write. Content can be str or bytes.

Stage a file deletion.

import json
file = repo.read("package.json")
pkg = json.loads(file.content)
pkg["version"] = "2.0.0"
with repo.transaction(
"Bump version to 2.0.0",
expected_head=file.head_sha,
) as tx:
tx.write("package.json", json.dumps(pkg, indent=2))
tx.write("CHANGELOG.md", changelog_entry)

Agents can use transactions to create atomic “save points” of their work:

changes = generate_code(prompt, existing_files)
with repo.transaction(f"Agent: {prompt[:60]}") as tx:
for path, content in changes.items():
tx.write(path, content)

If expected_head is set and the branch has moved, the transaction throws a StaleHeadError. See Error Handling.