コンテンツにスキップ

Git操作

Daytona SDKは、Sandbox内のgitモジュールを通じてGitの組み込みサポートを提供します。本ガイドでは、利用可能なすべてのGit操作とベストプラクティスを解説します。

基本操作

Daytona SDKは、Sandbox(Daytonaが管理する隔離された一時的な実行環境)内でGitリポジトリのクローン、ステータス確認、管理を行う機能を提供します。gitモジュールを使ってGitリポジトリを操作できます。

ファイル操作と同様に、クローンの基準ディレクトリは現在のSandboxユーザーのホームです。例えば、workspace/repo/home/[username]/workspace/repo を意味します。先頭を / にすることで作業ディレクトリに絶対パスも指定できます。

リポジトリのクローン

Daytona SDKは、PythonおよびTypeScriptからSandboxにGitリポジトリをクローンできます。パブリック/プライベートリポジトリ、特定ブランチのクローンに対応し、Personal Access Tokenを用いた認証も可能です。

# Basic clone
sandbox.git.clone(
url="https://github.com/user/repo.git",
path="workspace/repo"
)
# Clone with authentication
sandbox.git.clone(
url="https://github.com/user/repo.git",
path="workspace/repo",
username="git",
password="personal_access_token"
)
# Clone specific branch
sandbox.git.clone(
url="https://github.com/user/repo.git",
path="workspace/repo",
branch="develop"
)

リポジトリのステータス

Daytona SDKは、Sandbox内のGitリポジトリのステータス確認にも対応しています。PythonおよびTypeScriptで、現在のブランチ、変更ファイル、メインブランチに対する先行/遅行コミット数を取得できます。

# Get repository status
status = sandbox.git.status("workspace/repo")
print(f"Current branch: {status.current_branch}")
print(f"Commits ahead: {status.ahead}")
print(f"Commits behind: {status.behind}")
for file in status.file_status:
print(f"File: {file.name}")
# List branches
response = sandbox.git.branches("workspace/repo")
for branch in response.branches:
print(f"Branch: {branch}")

ブランチ操作

Daytona SDK は、Git リポジトリのブランチ管理機能を提供します。ブランチの作成・切り替え・削除が可能です。

ブランチの管理

Daytona SDK では、Python と TypeScript から Git リポジトリのブランチを作成・切り替え・削除できます。

# 新しいブランチを作成
sandbox.git.create_branch("workspace/repo", "feature/new-feature")
# ブランチを切り替え
sandbox.git.checkout_branch("workspace/repo", "feature/new-feature")
# ブランチを削除
sandbox.git.delete_branch("workspace/repo", "feature/old-feature")

ステージングとコミット

Daytona SDK では、Git リポジトリの変更をステージしてコミットできます。Python と TypeScript から、特定のファイルのみ、またはすべての変更をステージし、メッセージ付きでコミットできます。

変更の扱い

# 特定のファイルをステージ
sandbox.git.add("workspace/repo", ["file1.txt", "file2.txt"])
# すべての変更をステージ
sandbox.git.add("workspace/repo", ["."])
# 変更をコミット
sandbox.git.commit("workspace/repo", "feat: add new feature", "John Doe", "john@example.com")

リモート操作

Daytona SDK は Git のリモートリポジトリを扱う機能を提供します。変更のプッシュ、変更のプル、リモートの一覧表示が可能です。

リモートの操作

Daytona SDK は、Python と TypeScript から Git リポジトリに対してプッシュ、プル、リモートの一覧表示を行う機能を提供します。

# 変更をプッシュ
sandbox.git.push("workspace/repo")
# 変更をプル
sandbox.git.pull("workspace/repo")