Commit and Reset
Graph-sitter requires you to explicitly commit changes by calling codebase.commit().
Keeping everything in memory enables fast, large-scale writes. See the How it Works guide to learn more.
You can manage your codebase's state with two core APIs:
- Codebase.commit() - Commit changes to disk
- Codebase.reset() - Reset the
codebaseand filesystem to its initial state
Committing Changes
When you make changes to your codebase through Codegen's APIs, they aren't immediately written to disk. You need to explicitly commit them with codebase.commit():
from graph_sitter import Codebase
codebase = Codebase("./")
# Make some changes
file = codebase.get_file("src/app.py")
file.before("# 🌈 hello, world!")
# Changes aren't on disk yet
codebase.commit() # Now they are!This transaction-like behavior helps ensure your changes are atomic and consistent.
Resetting State
The codebase.reset() method allows you to revert the codebase to its initial state:
# Make some changes
codebase.get_file("src/app.py").remove()
codebase.create_file("src/new_file.py", "x = 1")
# Check the changes
assert codebase.get_file("src/app.py", optional=True) is None
assert codebase.get_file("src/new_file.py") is not None
# Reset everything
codebase.reset()
# Changes are reverted
assert codebase.get_file("src/app.py") is not None
assert codebase.get_file("src/new_file.py", optional=True) is Nonereset() reverts both the in-memory state and any uncommitted filesystem
changes. However, it preserves your codemod implementation in .codegen/.