Branch management
The Python SDK provides multiple methods to manage the branches in an Infrahub instance.
Get a single branch
- Async
- Sync
from infrahub_sdk import InfrahubClient
client = InfrahubClient()
branch = await client.branch.get(branch_name="main")
from infrahub_sdk import InfrahubClientSync
client = InfrahubClientSync()
branch = client.branch.get(branch_name="main")
Get all the branches
- Async
- Sync
from asyncio import run as aiorun
from rich import print as rprint
from infrahub_sdk import InfrahubClient
async def main():
client = InfrahubClient(address="http://localhost:8000")
branches = await client.branch.all()
rprint(branches)
if __name__ == "__main__":
aiorun(main())
from rich import print as rprint
from infrahub_sdk import InfrahubClientSync
def main():
client = InfrahubClientSync(address="http://localhost:8000")
branches = client.branch.all()
rprint(branches)
if __name__ == "__main__":
main()
Create a branch
- Async
- Sync
from asyncio import run as aiorun
from infrahub_sdk import InfrahubClient
async def main():
client = InfrahubClient(address="http://localhost:8000")
await client.branch.create(branch_name="new-branch", description="description", sync_with_git=False)
print("New branch created")
if __name__ == "__main__":
aiorun(main())
from infrahub_sdk import InfrahubClientSync
def main():
client = InfrahubClientSync(address="http://localhost:8000")
client.branch.create(branch_name="new-branch2", description="description", sync_with_git=False)
print("New branch created")
if __name__ == "__main__":
main()
Rebase a branch
- Async
- Sync
from asyncio import run as aiorun
from infrahub_sdk import InfrahubClient
async def main():
client = InfrahubClient(address="http://localhost:8000")
await client.branch.rebase(branch_name="new-branch")
if __name__ == "__main__":
aiorun(main())
from infrahub_sdk import InfrahubClientSync
def main():
client = InfrahubClientSync(address="http://localhost:8000")
client.branch.rebase(branch_name="new-branch")
if __name__ == "__main__":
main()
Merge a branch
- Async
- Sync
from asyncio import run as aiorun
from infrahub_sdk import InfrahubClient
async def main():
client = InfrahubClient(address="http://localhost:8000")
await client.branch.merge(branch_name="new-branch")
if __name__ == "__main__":
aiorun(main())
from infrahub_sdk import InfrahubClientSync
def main():
client = InfrahubClientSync(address="http://localhost:8000")
client.branch.merge(branch_name="new-branch")
if __name__ == "__main__":
main()
Delete a branch
- Async
- Sync
from infrahub_sdk import InfrahubClient
client = InfrahubClient()
await client.branch.delete(branch_name="new-branch")
from infrahub_sdk import InfrahubClientSync
client = InfrahubClientSync()
client.branch.delete(branch_name="new-branch")
Getting the diff for a branch
Use get_diff_tree to retrieve the full diff of a branch compared to its base branch, including summary counts and the list of changed nodes. It returns None if no diff exists for the branch. Set include_properties=True to also retrieve the value-level details of each change (previous and new value per property).
- Async
- Sync
from infrahub_sdk import InfrahubClient
client = InfrahubClient()
diff = await client.get_diff_tree(branch="new-branch", include_properties=True)
from infrahub_sdk import InfrahubClientSync
client = InfrahubClientSync()
diff = client.get_diff_tree(branch="new-branch", include_properties=True)
If you only need the list of changed nodes, get_diff_summary returns them without the diff metadata.
- Async
- Sync
from infrahub_sdk import InfrahubClient
client = InfrahubClient()
node_diffs = await client.get_diff_summary(branch="new-branch")
from infrahub_sdk import InfrahubClientSync
client = InfrahubClientSync()
node_diffs = client.get_diff_summary(branch="new-branch")