Git
class Git()
サンドボックス内での Git 操作を提供します。
例:
# リポジトリをクローンsandbox.git.clone( url="https://github.com/user/repo.git", path="workspace/repo")
# リポジトリのステータスを確認status = sandbox.git.status("workspace/repo")print(f"Modified files: {status.modified}")
# 変更をステージしてコミットsandbox.git.add("workspace/repo", ["file.txt"])sandbox.git.commit( path="workspace/repo", message="Update file", author="John Doe", email="john@example.com")
Git.init
def __init__(sandbox_id: str, toolbox_api: ToolboxApi, get_root_dir: Callable[[], str])
新しい Git ハンドラーのインスタンスを初期化します。
引数:
sandbox_id
str - サンドボックス ID。toolbox_api
ToolboxApi - サンドボックス操作用の API クライアント。get_root_dir
Callable[[], str] - サンドボックスのデフォルトのルートディレクトリを取得する関数。
Git.add
@intercept_errors(message_prefix="Failed to add files: ")def add(path: str, files: List[str]) -> None
指定したファイルを次回のコミットに向けてステージします。コマンドラインで
git add
を実行するのと同様です。
引数:
path
str - Git リポジトリのルートへのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。files
List[str] - ステージするファイルまたはディレクトリのリスト。リポジトリルートからの相対パス。
例:
# 単一ファイルをステージsandbox.git.add("workspace/repo", ["file.txt"])
# 複数ファイルをステージsandbox.git.add("workspace/repo", [ "src/main.py", "tests/test_main.py", "README.md"])
Git.branches
@intercept_errors(message_prefix="Failed to list branches: ")def branches(path: str) -> ListBranchResponse
リポジトリ内のブランチを一覧表示します。
引数:
path
str - Git リポジトリのルートへのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。
戻り値:
ListBranchResponse
- リポジトリ内のブランチ一覧。
例:
response = sandbox.git.branches("workspace/repo")print(f"Branches: {response.branches}")
Git.clone
@intercept_errors(message_prefix="Failed to clone repository: ")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 パスワードまたはトークン。
例:
# デフォルトブランチをクローンsandbox.git.clone( url="https://github.com/user/repo.git", path="workspace/repo")
# 認証付きで特定のブランチをクローンsandbox.git.clone( url="https://github.com/user/private-repo.git", path="workspace/private", branch="develop", username="user", password="token")
# 特定のコミットをクローンsandbox.git.clone( url="https://github.com/user/repo.git", path="workspace/repo-old", commit_id="abc123")
Git.commit
@intercept_errors(message_prefix="Failed to commit changes: ")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。
例:
# 変更をステージしてコミットするsandbox.git.add("workspace/repo", ["README.md"])sandbox.git.commit( path="workspace/repo", message="Update documentation", author="John Doe", email="john@example.com", allow_empty=True)
Git.push
@intercept_errors(message_prefix="Failed to push changes: ")def push(path: str, username: Optional[str] = None, password: Optional[str] = None) -> None
現在のブランチのすべてのローカルコミットをリモート リポジトリにプッシュします。リモートリポジトリが認証を要求する場合は、 username と password/token を指定してください。
引数:
path
str - Git リポジトリのルートへのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。username
Optional[str] - 認証用の Git ユーザー名。password
Optional[str] - 認証用の Git パスワードまたはトークン。
例:
# 認証なしでプッシュ(公開リポジトリや SSH の場合)sandbox.git.push("workspace/repo")
# 認証ありでプッシュsandbox.git.push( path="workspace/repo", username="user", password="github_token")
Git.pull
@intercept_errors(message_prefix="Failed to pull changes: ")def pull(path: str, username: Optional[str] = None, password: Optional[str] = None) -> None
リモートリポジトリから変更をプルします。リモートリポジトリが認証を要求する場合は、 username と password/token を指定してください。
引数:
path
str - Git リポジトリのルートへのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。username
Optional[str] - 認証用の Git ユーザー名。password
Optional[str] - 認証用の Git パスワードまたはトークン。
例:
# 認証なしでプルsandbox.git.pull("workspace/repo")
# 認証ありでプルsandbox.git.pull( path="workspace/repo", username="user", password="github_token")
Git.status
@intercept_errors(message_prefix="Failed to get status: ")def status(path: str) -> GitStatus
現在の Git リポジトリのステータスを取得します。
引数:
path
str - Git リポジトリのルートへのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。
戻り値:
GitStatus
- リポジトリのステータス情報(以下を含む):- current_branch: 現在のブランチ名
- file_status: ファイルの状態のリスト
- ahead: リモートに未プッシュのローカルコミット数
- behind: ローカルに未プルのリモートコミット数
- branch_published: ブランチがリモートリポジトリに公開済みかどうか
例:
status = sandbox.git.status("workspace/repo")print(f"On branch: {status.current_branch}")print(f"Commits ahead: {status.ahead}")print(f"Commits behind: {status.behind}")
Git.checkout_branch
@intercept_errors(message_prefix="Failed to checkout branch: ")def checkout_branch(path: str, branch: str) -> None
リポジトリでブランチをチェックアウトします。
引数:
path
str - Git リポジトリのルートへのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。branch
str - チェックアウトするブランチ名
例:
# ブランチをチェックアウトsandbox.git.checkout_branch("workspace/repo", "feature-branch")
Git.create_branch
@intercept_errors(message_prefix="Failed to create branch: ")def create_branch(path: str, name: str) -> None
リポジトリでブランチを作成します。
引数:
path
str - Git リポジトリのルートへのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。name
str - 作成する新しいブランチ名
例:
# 新しいブランチを作成sandbox.git.create_branch("workspace/repo", "new-feature")
Git.delete_branch
@intercept_errors(message_prefix="Failed to delete branch: ")def delete_branch(path: str, name: str) -> None
リポジトリ内のブランチを削除します。
引数:
path
str - Git リポジトリのルートへのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。name
str - 削除するブランチ名
例:
# ブランチを削除するsandbox.git.delete_branch("workspace/repo", "old-feature")
GitCommitResponse
class GitCommitResponse()
Git の commit に対するレスポンス。
属性:
sha
str - コミットの SHA