Skip to content

feat: add worktree pull command and worktree-aware CLAUDE.md - #4

Merged
nanasess merged 2 commits into
mainfrom
feat/pull-command-and-worktree-claude-md
Mar 30, 2026
Merged

feat: add worktree pull command and worktree-aware CLAUDE.md#4
nanasess merged 2 commits into
mainfrom
feat/pull-command-and-worktree-claude-md

Conversation

@nanasess

@nanasess nanasess commented Mar 30, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add worktree pull subcommand to bulk-pull all git repositories under the current directory (works from both project root and worktree directories)
  • Add generate_worktree_claude_md() to prepend worktree context (task name, working directory, project root) to CLAUDE.md during worktree creation, so agents retain context after /compact

Test plan

  • worktree pull --help でヘルプが表示されること
  • プロジェクトルートで worktree pull を実行し、配下の全リポジトリが pull されること
  • worktree ディレクトリで worktree pull を実行し、配下の全リポジトリが pull されること
  • worktree create <task> で CLAUDE.md にワークツリーコンテキストが付加されること

🤖 Generated with Claude Code

Summary by CodeRabbit

リリースノート

  • 新機能
    • 新たに pull コマンドを追加しました。プロジェクト内の全リポジトリに対して git pull を一括実行できます。
    • CLAUDE.md をワークツリーコンテキスト情報付きで自動生成するようになりました。これにより、タスク情報やプロジェクト構造が埋め込まれます。

…tion

Add `worktree pull` subcommand to pull all git repositories under the
current directory in bulk. Works from both project root and worktree
directories.

Also add `generate_worktree_claude_md()` to prepend worktree context
(task name, working directory, project root) to CLAUDE.md when creating
worktrees, so agents retain context after /compact.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Mar 30, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@nanasess has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 17 minutes and 18 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 17 minutes and 18 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: faa7e118-c28e-49c4-af30-4fc2d18d900d

📥 Commits

Reviewing files that changed from the base of the PR and between e4e56de and ef3fc04.

📒 Files selected for processing (1)
  • lib/cmd_pull.sh
📝 Walkthrough

Walkthrough

このプルリクエストは、ネストされたGitリポジトリ全体に対して一括でgit pullを実行する新しいpullコマンドを追加し、同時にワークツリーのタスク固有のメタデータを含めたコンテキスト付きCLAUDE.mdファイル生成メカニズムを実装しています。

Changes

Cohort / File(s) Summary
CLAUDE.md生成処理の分岐
lib/cmd_create.sh
非Gitアイテム配置処理で、CLAUDE.mdに限定して従来のシンボリックリンク作成の代わりにgenerate_worktree_claude_md関数を呼び出して生成ファイルを配置する分岐を追加。その他のアイテムは従来通りシンボリックリンク作成。
新規プルコマンド実装
lib/cmd_pull.sh
worktree pullサブコマンドの実装。ヘルプ処理、引数解析、プロジェクトルート/名前取得、リポジトリ列挙、各リポジトリでのgit pull実行、実行結果の判定(「すでに最新」vs「実際の更新」)、ステータス追跡、集計結果サマリー出力を実装。
ワークツリーコンテキスト付き生成関数
lib/common.sh
新規関数generate_worktree_claude_mdを追加。タスク名、ワークツリーディレクトリ、プロジェクトルートなどのメタデータを含む「ワークツリーコンテキスト」ヘッダーを生成し、ソースファイルの内容と結合して生成ファイルを作成。
メインスクリプトへのコマンド統合
worktree
新しいcmd_pull.shライブラリを読み込み、pullコマンドをメインスクリプトのコマンドディスパッチに追加。ヘルプテキストにも反映。

Sequence Diagram

sequenceDiagram
    participant User
    participant Main as worktree (main)
    participant CmdPull as cmd_pull
    participant Git as git repositories
    participant Logger as Logging Output

    User->>Main: worktree pull
    Main->>CmdPull: cmd_pull "$@"
    activate CmdPull
    CmdPull->>CmdPull: Parse arguments & validate
    CmdPull->>CmdPull: get_project_root
    CmdPull->>CmdPull: get_project_name
    CmdPull->>CmdPull: list_git_repos
    CmdPull->>Logger: Print section headers
    loop For each repository
        CmdPull->>Logger: Log current branch
        CmdPull->>Git: git pull
        Git-->>CmdPull: Return status & output
        CmdPull->>CmdPull: Parse output ("Already up to date" or update)
        CmdPull->>Logger: Log success/update message
        CmdPull->>CmdPull: Record status in RESULTS array
    end
    CmdPull->>Logger: print_summary RESULTS
    CmdPull-->>Main: Return status
    deactivate CmdPull
    Main-->>User: Command output
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 新しいpullコマンドで、
リポジトリ群をまとめて更新し、
ワークツリーにはタスク文脈を添えて、
CLAUDEがコードを理解しやすく、
整理整頓の完成! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title clearly summarizes the two main changes: adding a worktree pull command and implementing worktree-aware CLAUDE.md generation.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/pull-command-and-worktree-claude-md

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (3)
lib/cmd_pull.sh (2)

75-80: detached HEAD 状態の考慮

git branch --show-current は detached HEAD 状態では空文字列を返します。現在のコードはこれを適切に処理していますが、detached HEAD であることをユーザーに明示的に通知すると親切です。

💡 提案: detached HEAD の明示
         local current_branch
         current_branch="$(git -C "$repo_path" branch --show-current 2>/dev/null)"
         if [ -n "$current_branch" ]; then
             log_info "  ブランチ: ${current_branch}"
+        else
+            log_warn "  detached HEAD 状態です"
         fi
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/cmd_pull.sh` around lines 75 - 80, When current_branch (from
current_branch="$(git -C "$repo_path" branch --show-current 2>/dev/null)") is
empty, detect detached HEAD and log it: call git -C "$repo_path" rev-parse
--short HEAD (or git -C "$repo_path" symbolic-ref -q --short HEAD) to obtain the
commit id and then use log_info to print a message like "ブランチ: detached HEAD
(commit <short-hash>)" instead of skipping output; update the logic around
current_branch and the log_info call so detached HEAD is explicitly reported
while preserving existing behavior for normal branches.

82-96: git pull のマージ動作を検討してください

git pull はデフォルトでマージコミットを作成する可能性があります。意図しないマージコミットを避けるため、--ff-only オプションの使用を検討してください。

また、detached HEAD 状態やアップストリームが設定されていないブランチでは git pull が失敗する可能性がありますが、現在のエラーハンドリングで対応されています。

♻️ 提案: --ff-only オプションの使用
         # git pull
         local pull_output
-        if pull_output=$(git -C "$repo_path" pull 2>&1); then
+        if pull_output=$(git -C "$repo_path" pull --ff-only 2>&1); then
             if echo "$pull_output" | grep -q "Already up to date"; then

fast-forward できない場合は失敗として扱われ、ユーザーが手動で対処できます。

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/cmd_pull.sh` around lines 82 - 96, Replace the plain git pull invocation
with a fast-forward-only pull to avoid creating unintended merge commits: update
the git command in the block that captures pull_output (the line using
pull_output=$(git -C "$repo_path" pull 2>&1)) to use --ff-only, and keep the
existing error branch (which populates RESULTS["$repo"] and logs via log_error)
so fast-forward failures are treated as errors requiring manual intervention; no
other control flow changes are needed.
lib/common.sh (1)

58-88: ソースファイルの存在チェックがない

$src が存在しない場合、cat "$src" は失敗しますが、$dst ファイルは部分的に作成されてしまいます(heredoc 部分のみ)。呼び出し元で存在チェックを行っているか確認するか、この関数内でバリデーションを追加することを検討してください。

🛡️ 提案: ソースファイルの存在チェックを追加
 generate_worktree_claude_md() {
     local src="$1"
     local dst="$2"
     local task_name="$3"
     local task_dir="$4"
     local project_root="$5"
 
+    if [ ! -f "$src" ]; then
+        log_warn "CLAUDE.md が見つかりません: ${src}"
+        return 1
+    fi
+
     {
         cat <<WORKTREE_CONTEXT
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/common.sh` around lines 58 - 88, The generate_worktree_claude_md function
writes a heredoc then cats $src into $dst without verifying $src exists, which
can leave a partially-created $dst; modify generate_worktree_claude_md to first
validate that the source file (parameter src) exists and is readable, and if not
return a non-zero status and do not create/overwrite dst; to avoid partial
writes, write the combined content to a temporary file (e.g., in the same
directory) and atomically mv it to dst only after successful cat, and ensure any
error paths clean up the temp file and propagate a clear error message/exit code
so callers can handle the failure.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@lib/cmd_pull.sh`:
- Around line 75-80: When current_branch (from current_branch="$(git -C
"$repo_path" branch --show-current 2>/dev/null)") is empty, detect detached HEAD
and log it: call git -C "$repo_path" rev-parse --short HEAD (or git -C
"$repo_path" symbolic-ref -q --short HEAD) to obtain the commit id and then use
log_info to print a message like "ブランチ: detached HEAD (commit <short-hash>)"
instead of skipping output; update the logic around current_branch and the
log_info call so detached HEAD is explicitly reported while preserving existing
behavior for normal branches.
- Around line 82-96: Replace the plain git pull invocation with a
fast-forward-only pull to avoid creating unintended merge commits: update the
git command in the block that captures pull_output (the line using
pull_output=$(git -C "$repo_path" pull 2>&1)) to use --ff-only, and keep the
existing error branch (which populates RESULTS["$repo"] and logs via log_error)
so fast-forward failures are treated as errors requiring manual intervention; no
other control flow changes are needed.

In `@lib/common.sh`:
- Around line 58-88: The generate_worktree_claude_md function writes a heredoc
then cats $src into $dst without verifying $src exists, which can leave a
partially-created $dst; modify generate_worktree_claude_md to first validate
that the source file (parameter src) exists and is readable, and if not return a
non-zero status and do not create/overwrite dst; to avoid partial writes, write
the combined content to a temporary file (e.g., in the same directory) and
atomically mv it to dst only after successful cat, and ensure any error paths
clean up the temp file and propagate a clear error message/exit code so callers
can handle the failure.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8d95941f-cc3b-4cbd-9bc9-dc413fea67b6

📥 Commits

Reviewing files that changed from the base of the PR and between 01fda0d and e4e56de.

📒 Files selected for processing (4)
  • lib/cmd_create.sh
  • lib/cmd_pull.sh
  • lib/common.sh
  • worktree

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a pull subcommand to the worktree tool for bulk updating git repositories and updates the create command to generate a CLAUDE.md file containing worktree-specific context. A suggestion was made to use LC_ALL=C and --ff-only with git pull to ensure consistent output parsing and prevent the script from hanging on interactive merges.

Comment thread lib/cmd_pull.sh Outdated
Address review feedback: --ff-only prevents interactive merge/rebase
from hanging the script, and LC_ALL=C ensures "Already up to date"
string matching is locale-independent.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@nanasess
nanasess merged commit a2c04eb into main Mar 30, 2026
1 check passed
@nanasess
nanasess deleted the feat/pull-command-and-worktree-claude-md branch March 30, 2026 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant