コンテンツにスキップ

AsyncGit

class AsyncGit()

サンドボックス内でGit操作を提供します。

:

# リポジトリをクローン
await sandbox.git.clone(
url="https://github.com/user/repo.git",
path="workspace/repo"
)
# リポジトリのステータスを確認
status = await sandbox.git.status("workspace/repo")
print(f"Modified files: {status.modified}")
# 変更をステージしてコミット
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__(sandbox_id: str, toolbox_api: ToolboxApi,
get_root_dir: Callable[[], Awaitable[str]])

新しいGitハンドラーインスタンスを初期化します。

引数:

  • sandbox_id str - サンドボックスID。
  • toolbox_api ToolboxApi - サンドボックス操作用のAPIクライアント。
  • get_root_dir Callable[[], str] - サンドボックスのデフォルトのルートディレクトリを取得する関数。

AsyncGit.add

@intercept_errors(message_prefix="Failed to add files: ")
async def add(path: str, files: List[str]) -> None

指定したファイルを次回のコミットに向けてステージします。コマンドラインで 「git add」を実行するのと同様です。

引数:

  • path str - Gitリポジトリのルートへのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。
  • files List[str] - リポジトリルートからの相対パスで、ステージするファイルまたはディレクトリのリスト。

:

# 単一ファイルをステージ
await sandbox.git.add("workspace/repo", ["file.txt"])
# 複数ファイルをステージ
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: ")
async def branches(path: str) -> ListBranchResponse

リポジトリ内のブランチを一覧表示します。

引数:

  • path str - Gitリポジトリのルートへのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。

返り値:

  • ListBranchResponse - リポジトリ内のブランチ一覧。

:

response = await sandbox.git.branches("workspace/repo")
print(f"Branches: {response.branches}")

AsyncGit.clone

@intercept_errors(message_prefix="Failed to clone repository: ")
async def clone(url: str,
path: str,
branch: Optional[str] = None,
commit_id: Optional[str] = None,
username: Optional[str] = None,
password: Optional[str] = None) -> None

指定したパスにGitリポジトリをクローンします。特定のブランチまたはコミットの クローンに対応し、認証情報が指定された場合はリモートリポジトリへの 認証も可能です。

引数:

  • url str - クローン元のリポジトリURL。
  • path str - リポジトリをクローンするパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。
  • branch Optional[str] - クローンするブランチ。未指定の場合は デフォルトブランチをクローンします。
  • commit_id Optional[str] - クローンするコミット。指定された場合、 リポジトリはこのコミットのdetached HEAD状態になります。
  • username Optional[str] - 認証用のGitユーザー名。
  • password Optional[str] - 認証用のGitパスワードまたはトークン。

:

# デフォルトブランチをクローン
await sandbox.git.clone(
url="https://github.com/user/repo.git",
path="workspace/repo"
)
# 認証付きで特定のブランチをクローン
await sandbox.git.clone(
url="https://github.com/user/private-repo.git",
path="workspace/private",
branch="develop",
username="user",
password="token"
)
# 特定のコミットをクローン
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: ")
async def commit(path: str,
message: str,
author: str,
email: str,
allow_empty: bool = False) -> GitCommitResponse

ステージ済みの変更から新しいコミットを作成します。コミットする前に必ず add() メソッドで変更をステージしてください。

引数:

  • path str - Git リポジトリのルートへのパス。相対パスはユーザーのルートディレクトリを基準に解決されます。
  • message str - 変更内容を説明するコミットメッセージ。
  • author str - コミット作者の名前。
  • email str - コミット作者のメールアドレス。
  • allow_empty bool, optional - 変更がステージされていない場合でも空のコミットの作成を許可します。デフォルトは False。

:

# 変更をステージしてコミットする
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: ")
async def push(path: str,
username: Optional[str] = None,
password: Optional[str] = None) -> None

現在のブランチのすべてのローカルコミットをリモートリポジトリにプッシュします。リモートリポジトリが認証を必要とする場合は、ユーザー名とパスワード/トークンを指定してください。

引数:

  • path str - Git リポジトリのルートへのパス。相対パスはユーザーのルートディレクトリを基準に解決されます。
  • username Optional[str] - 認証用の Git ユーザー名。
  • password Optional[str] - 認証用の Git パスワードまたはトークン。

:

# 認証なしでプッシュ(公開リポジトリや SSH の場合)
await sandbox.git.push("workspace/repo")
# 認証ありでプッシュ
await sandbox.git.push(
path="workspace/repo",
username="user",
password="github_token"
)

AsyncGit.pull

@intercept_errors(message_prefix="Failed to pull changes: ")
async def pull(path: str,
username: Optional[str] = None,
password: Optional[str] = None) -> None

リモートリポジトリから変更をプルします。リモートリポジトリが認証を必要とする場合は、ユーザー名とパスワード/トークンを指定してください。

引数:

  • path str - Git リポジトリのルートへのパス。相対パスはユーザーのルートディレクトリを基準に解決されます。
  • username Optional[str] - 認証用の Git ユーザー名。
  • password Optional[str] - 認証用の Git パスワードまたはトークン。

:

# 認証なしでプル
await sandbox.git.pull("workspace/repo")
# 認証ありでプル
await sandbox.git.pull(
path="workspace/repo",
username="user",
password="github_token"
)

AsyncGit.status

@intercept_errors(message_prefix="Failed to get status: ")
async def status(path: str) -> GitStatus

現在の Git リポジトリのステータスを取得します。

引数:

  • path str - Git リポジトリのルートへのパス。相対パスはユーザーのルートディレクトリを基準に解決されます。

戻り値:

  • GitStatus - 以下を含むリポジトリのステータス情報:
    • current_branch: 現在のブランチ名
    • file_status: ファイルのステータス一覧
    • ahead: リモートに未プッシュのローカルコミット数
    • behind: ローカルに未プルのリモートコミット数
    • branch_published: ブランチがリモートリポジトリに公開済みかどうか

:

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: ")
async def checkout_branch(path: str, branch: str) -> None

リポジトリでブランチをチェックアウトします。

引数:

  • path str - Git リポジトリのルートへのパス。相対パスはユーザーのルートディレクトリを基準に解決されます。
  • branch str - チェックアウトするブランチ名

:

# ブランチをチェックアウト
await sandbox.git.checkout_branch("workspace/repo", "feature-branch")

AsyncGit.create_branch

@intercept_errors(message_prefix="Failed to create branch: ")
async def create_branch(path: str, name: str) -> None

リポジトリでブランチを作成します。

引数:

  • path str - Git リポジトリのルートへのパス。相対パスはユーザーのルートディレクトリを基準に解決されます。
  • name str - 作成する新しいブランチ名

:

# 新しいブランチを作成
await sandbox.git.create_branch("workspace/repo", "new-feature")

AsyncGit.delete_branch

@intercept_errors(message_prefix="Failed to delete branch: ")
async def delete_branch(path: str, name: str) -> None

リポジトリ内のブランチを削除します。

引数:

  • path str - Gitリポジトリのルートへのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。
  • name str - 削除するブランチ名

:

# ブランチを削除
await sandbox.git.delete_branch("workspace/repo", "old-feature")

GitCommitResponse

class GitCommitResponse()

Git の commit 操作に対するレスポンス。

属性:

  • sha str - コミットの SHA