原文(日本語に翻訳)
WorktreeCreate フックで type: "http" のサポートを追加 — レスポンスJSONの hookSpecificOutput.worktreePath で作成されたワークツリーパスを返すことができます。
原文(英語)
Added WorktreeCreate hook support for type: "http" — return the created worktree path via hookSpecificOutput.worktreePath in the response JSON
概要
WorktreeCreate フックが type: "http" をサポートするようになりました。外部HTTPサーバーがワークツリーの作成先パスを動的に決定し、hookSpecificOutput.worktreePath フィールドでClaudeに返すことができます。これにより、ワークツリーの管理をより柔軟に制御できます。
基本的な使い方
settings.json でHTTPタイプのフックを設定します。
json
{
"hooks": {
"WorktreeCreate": [
{
"matcher": "",
"hooks": [
{
"type": "http",
"url": "http://localhost:8080/worktree/create"
}
]
}
]
}
}HTTPサーバーは以下の形式でレスポンスを返します。
json
{
"hookSpecificOutput": {
"worktreePath": "/path/to/created/worktree"
}
}実践例
Pythonでシンプルなワークツリー管理サーバー
python
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
import subprocess
import os
import tempfile
class WorktreeHandler(BaseHTTPRequestHandler):
def do_POST(self):
if self.path == "/worktree/create":
content_length = int(self.headers['Content-Length'])
body = json.loads(self.rfile.read(content_length))
# カスタムディレクトリにワークツリーを作成
base_dir = os.path.expanduser("~/worktrees")
os.makedirs(base_dir, exist_ok=True)
# タイムスタンプベースのユニークなパスを生成
import datetime
ts = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
worktree_path = os.path.join(base_dir, f"task_{ts}")
# gitワークツリーを作成
branch_name = f"task-{ts}"
subprocess.run(
["git", "worktree", "add", "-b", branch_name, worktree_path],
check=True
)
response = {
"hookSpecificOutput": {
"worktreePath": worktree_path
}
}
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(response).encode())
if __name__ == "__main__":
server = HTTPServer(("localhost", 8080), WorktreeHandler)
print("ワークツリー管理サーバーを起動中... ポート: 8080")
server.serve_forever()共有ストレージへのワークツリー配置
python
# 複数のCI/CDエージェントで共有ストレージを利用する例
SHARED_STORAGE = "/mnt/shared/worktrees"
def create_worktree(request_data):
agent_id = request_data.get("agentId", "default")
worktree_path = f"{SHARED_STORAGE}/{agent_id}/workspace"
# 共有ストレージ上にワークツリーを作成
os.makedirs(worktree_path, exist_ok=True)
subprocess.run(["git", "worktree", "add", worktree_path], check=True)
return {
"hookSpecificOutput": {
"worktreePath": worktree_path
}
}Docker/コンテナ環境での動的パス管理
json
{
"hooks": {
"WorktreeCreate": [
{
"matcher": "",
"hooks": [
{
"type": "http",
"url": "http://worktree-manager:9090/api/v1/worktrees",
"headers": {
"Authorization": "Bearer ${WORKTREE_MANAGER_TOKEN}",
"Content-Type": "application/json"
}
}
]
}
]
}
}注意点
- HTTPサーバーは
worktreePathフィールドを含むhookSpecificOutputオブジェクトをレスポンスに含める必要があります - サーバーが返すパスは実際に存在するgitワークツリーのパスである必要があります
- HTTPサーバーが応答しない場合やエラーを返した場合、フォールバック動作についてはドキュメントを確認してください
- ローカルの
type: "command"フックとは異なり、HTTPフックは外部サービスに依存するため、サービスの可用性を考慮してください