AsyncGit
class AsyncGit()
Provides Git operations within a Sandbox.
Example:
# Clone a repository
await sandbox.git.clone(
url="https://github.com/user/repo.git",
path="workspace/repo"
)
# Check repository status
status = await sandbox.git.status("workspace/repo")
print(f"Modified files: {status.modified}")
# Stage and commit changes
await sandbox.git.add("workspace/repo", ["file.txt"])
await sandbox.git.commit(
path="workspace/repo",
message="Update file",
author="John Doe",
email="john@example.com"
)
AsyncGit.__init__
def __init__(api_client: GitApi)
Initializes a new Git handler instance.
Arguments:
api_clientGitApi - API client for Sandbox Git operations.
AsyncGit.add
@intercept_errors(message_prefix="Failed to add files: ")
@with_instrumentation()
async def add(path: str, files: list[str]) -> None
Stages the specified files for the next commit, similar to running ‘git add’ on the command line.
Arguments:
pathstr - Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.fileslist[str] - List of file paths or directories to stage, relative to the repository root.
Example:
# Stage a single file
await sandbox.git.add("workspace/repo", ["file.txt"])
# Stage multiple files
await sandbox.git.add("workspace/repo", [
"src/main.py",
"tests/test_main.py",
"README.md"
])
AsyncGit.branches
@intercept_errors(message_prefix="Failed to list branches: ")
@with_instrumentation()
async def branches(path: str) -> ListBranchResponse
Lists branches in the repository.
Arguments:
pathstr - Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.
Returns:
ListBranchResponse- List of branches in the repository.
Example:
response = await sandbox.git.branches("workspace/repo")
print(f"Branches: {response.branches}")
AsyncGit.clone
@intercept_errors(message_prefix="Failed to clone repository: ")
@with_instrumentation()
async def clone(url: str,
path: str,
branch: str | None = None,
commit_id: str | None = None,
username: str | None = None,
password: str | None = None) -> None
Clones a Git repository into the specified path. It supports cloning specific branches or commits, and can authenticate with the remote repository if credentials are provided.
Arguments:
urlstr - Repository URL to clone from.pathstr - Path where the repository should be cloned. Relative paths are resolved based on the sandbox working directory.branchstr | None - Specific branch to clone. If not specified, clones the default branch.commit_idstr | None - Specific commit to clone. If specified, the repository will be left in a detached HEAD state at this commit.usernamestr | None - Git username for authentication.passwordstr | None - Git password or token for authentication.
Example:
# Clone the default branch
await sandbox.git.clone(
url="https://github.com/user/repo.git",
path="workspace/repo"
)
# Clone a specific branch with authentication
await sandbox.git.clone(
url="https://github.com/user/private-repo.git",
path="workspace/private",
branch="develop",
username="user",
password="token"
)
# Clone a specific commit
await sandbox.git.clone(
url="https://github.com/user/repo.git",
path="workspace/repo-old",
commit_id="abc123"
)
AsyncGit.commit
@intercept_errors(message_prefix="Failed to commit changes: ")
@with_instrumentation()
async def commit(path: str,
message: str,
author: str,
email: str,
allow_empty: bool = False) -> GitCommitResponse
Creates a new commit with the staged changes. Make sure to stage changes using the add() method before committing.
Arguments:
pathstr - Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.messagestr - Commit message describing the changes.authorstr - Name of the commit author.emailstr - Email address of the commit author.allow_emptybool - Allow creating an empty commit when no changes are staged. Defaults to False.
Example:
# Stage and commit changes
await sandbox.git.add("workspace/repo", ["README.md"])
await sandbox.git.commit(
path="workspace/repo",
message="Update documentation",
author="John Doe",
email="john@example.com",
allow_empty=True
)
AsyncGit.push
@intercept_errors(message_prefix="Failed to push changes: ")
@with_instrumentation()
async def push(path: str,
username: str | None = None,
password: str | None = None) -> None
Pushes all local commits on the current branch to the remote repository. If the remote repository requires authentication, provide username and password/token.
Arguments:
pathstr - Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.usernamestr | None - Git username for authentication.passwordstr | None - Git password or token for authentication.
Example:
# Push without authentication (for public repos or SSH)
await sandbox.git.push("workspace/repo")
# Push with authentication
await sandbox.git.push(
path="workspace/repo",
username="user",
password="github_token"
)
AsyncGit.pull
@intercept_errors(message_prefix="Failed to pull changes: ")
@with_instrumentation()
async def pull(path: str,
username: str | None = None,
password: str | None = None) -> None
Pulls changes from the remote repository. If the remote repository requires authentication, provide username and password/token.
Arguments:
pathstr - Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.usernamestr | None - Git username for authentication.passwordstr | None - Git password or token for authentication.
Example:
# Pull without authentication
await sandbox.git.pull("workspace/repo")
# Pull with authentication
await sandbox.git.pull(
path="workspace/repo",
username="user",
password="github_token"
)
AsyncGit.status
@intercept_errors(message_prefix="Failed to get status: ")
@with_instrumentation()
async def status(path: str) -> GitStatus
Gets the current Git repository status.
Arguments:
pathstr - Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.
Returns:
GitStatus- Repository status information including:- current_branch: Current branch name
- file_status: List of file statuses
- ahead: Number of local commits not pushed to remote
- behind: Number of remote commits not pulled locally
- branch_published: Whether the branch has been published to the remote repository
Example:
status = await sandbox.git.status("workspace/repo")
print(f"On branch: {status.current_branch}")
print(f"Commits ahead: {status.ahead}")
print(f"Commits behind: {status.behind}")
AsyncGit.checkout_branch
@intercept_errors(message_prefix="Failed to checkout branch: ")
@with_instrumentation()
async def checkout_branch(path: str, branch: str) -> None
Checkout branch in the repository.
Arguments:
pathstr - Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.branchstr - Name of the branch to checkout
Example:
# Checkout a branch
await sandbox.git.checkout_branch("workspace/repo", "feature-branch")
AsyncGit.create_branch
@intercept_errors(message_prefix="Failed to create branch: ")
@with_instrumentation()
async def create_branch(path: str, name: str) -> None
Create branch in the repository.
Arguments:
pathstr - Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.namestr - Name of the new branch to create
Example:
# Create a new branch
await sandbox.git.create_branch("workspace/repo", "new-feature")
AsyncGit.delete_branch
@intercept_errors(message_prefix="Failed to delete branch: ")
@with_instrumentation()
async def delete_branch(path: str, name: str) -> None
Delete branch in the repository.
Arguments:
pathstr - Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.namestr - Name of the branch to delete
Example:
# Delete a branch
await sandbox.git.delete_branch("workspace/repo", "old-feature")
GitCommitResponse
class GitCommitResponse()
Response from the git commit.
Attributes:
shastr - The SHA of the commit