Skip to content

ステータスライン JSON に workspace.git_worktree を追加

原文(日本語に翻訳)

現在のディレクトリがリンクされた git worktree 内にある場合に設定される workspace.git_worktree をステータスライン JSON 入力に追加しました。

原文(英語)

Added workspace.git_worktree to the status line JSON input, set whenever the current directory is inside a linked git worktree

概要

Claude Code v2.1.98 では、ステータスライン JSON 入力に workspace.git_worktree フィールドが追加されました。このフィールドは、現在の作業ディレクトリがリンクされた git worktree(git worktree add で作成されたサブディレクトリ)の内部にある場合に設定されます。ステータスラインをカスタマイズしているユーザーやツールは、このフィールドを使って現在 worktree 内で作業していることを検出し、適切な表示や動作を実装できます。

基本的な使い方

git worktree を使用している場合、Claude Code のステータスライン JSON に workspace.git_worktree フィールドが自動的に追加されます。

bash
# git worktree を作成
git worktree add ../feature-branch feature/new-feature

# worktree ディレクトリに移動
cd ../feature-branch

# このディレクトリで Claude Code を起動すると、
# ステータスライン JSON に workspace.git_worktree が設定される
claude

ステータスライン JSON の例(worktree 内の場合):

json
{
  "workspace": {
    "git_worktree": "/path/to/linked-worktree",
    "root": "/path/to/main-repo"
  }
}

実践例

git worktree を使った並行開発の管理

bash
# メインリポジトリで複数の worktree を作成
cd /projects/my-app

# 機能開発用の worktree
git worktree add ../my-app-feature feature/new-ui

# バグ修正用の worktree
git worktree add ../my-app-hotfix hotfix/critical-bug

# 各 worktree で Claude Code を使用すると、
# workspace.git_worktree でどの worktree かが識別される
cd ../my-app-feature
claude  # workspace.git_worktree = "/path/to/my-app-feature"

cd ../my-app-hotfix
claude  # workspace.git_worktree = "/path/to/my-app-hotfix"

カスタムステータスライン設定での worktree 情報表示

json
// .claude/settings.json
{
  "statusLine": {
    "format": "{% if workspace.git_worktree %}[worktree: {{ workspace.git_worktree | basename }}] {% endif %}{{ workspace.branch }}"
  }
}

このように設定することで、worktree 内で作業中は [worktree: feature-branch] main のようにステータスラインに worktree 名が表示されます。

スクリプトでの worktree 検出

bash
#!/bin/bash
# Claude Code のステータスラインを JSON で取得して worktree を確認するスクリプト例

STATUS=$(claude --status --output-format json 2>/dev/null)

if echo "$STATUS" | jq -e '.workspace.git_worktree' > /dev/null 2>&1; then
  WORKTREE=$(echo "$STATUS" | jq -r '.workspace.git_worktree')
  echo "現在 git worktree 内で作業中: $WORKTREE"
else
  echo "メインリポジトリで作業中"
fi

ステータスラインでの worktree 識別

複数のターミナルで異なる worktree を使って作業している場合、workspace.git_worktree の有無と値でどのコンテキストで作業しているかを明確に識別できます。

bash
# worktree 一覧を確認
git worktree list
# /projects/my-app          abc1234 [main]
# /projects/my-app-feature  def5678 [feature/new-ui]
# /projects/my-app-hotfix   ghi9012 [hotfix/critical-bug]

# 各ディレクトリで Claude Code を使うと、ステータスラインに
# それぞれの worktree パスが workspace.git_worktree として表示される

注意点

  • workspace.git_worktreeリンクされた worktree(git worktree add で作成されたもの)内にいる場合のみ設定されます
  • メインの作業ツリー(プライマリ worktree)では workspace.git_worktree は設定されません
  • このフィールドは Claude Code v2.1.98 以降で利用可能です
  • ステータスライン JSON は --status フラグまたは設定によってカスタマイズ可能です
  • git worktree は git 2.5.0 以降で利用できる機能です
  • workspace.git_worktree の値は worktree の絶対パスです

関連情報