t1060: Detect stale remote branches before creating new local branch#1567
t1060: Detect stale remote branches before creating new local branch#1567marcusquinn merged 2 commits intomainfrom
Conversation
…t1060) When worktree-helper.sh creates a new branch, it now checks refs/remotes/origin/$branch for stale remote tracking refs. Interactive mode offers choices (delete/continue/abort). Headless mode auto-deletes merged stale refs and warns-but-proceeds for unmerged ones. Chose tty detection (-t 0) for headless vs interactive — matches existing codebase pattern.
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the 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. WalkthroughThe PR adds stale remote branch detection and handling to the worktree helper script. New functions check if remote branches lack local counterparts and determine merge status, with integration into the add workflow. Both interactive and headless mode behaviors are supported. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
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. Comment |
🔍 Code Quality Report�[0;35m[MONITOR]�[0m Code Review Monitoring Report �[0;34m[INFO]�[0m Latest Quality Status: �[0;34m[INFO]�[0m Recent monitoring activity: 📈 Current Quality Metrics
Generated on: Tue Feb 17 18:36:17 UTC 2026 Generated by AI DevOps Framework Code Review Monitoring |
Summary of ChangesHello @marcusquinn, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a valuable feature for detecting and handling stale remote branches before creating a new worktree. The implementation is robust, correctly handling interactive and headless modes, and differentiating between merged and unmerged branches with safe defaults. The code is clear and the documentation is updated accordingly. I have one suggestion to refactor some duplicated code to improve maintainability.
Extract _delete_stale_remote_ref() helper to DRY up the 3 repeated instances of stale remote branch deletion logic in handle_stale_remote_branch().
There was a problem hiding this comment.
Actionable comments posted: 4
🧹 Nitpick comments (1)
.agents/scripts/worktree-helper.sh (1)
137-226: Extract repeated delete-and-prune logic into a helper function.The
git push origin --delete+git fetch --prune+ success message block is duplicated three times (lines 161–164, 180–183, 200–204). A small helper like_delete_stale_ref()would centralize this, reduce copy-paste risk, and make it easier to update the deletion strategy (per the concern above).♻️ Example extraction
+# Delete a stale remote tracking ref (and optionally the remote branch) +_delete_stale_ref() { + local branch="$1" + echo -e "${BLUE}Deleting stale remote ref...${NC}" + git update-ref -d "refs/remotes/origin/$branch" 2>/dev/null || true + git fetch --prune origin 2>/dev/null || true + echo -e "${GREEN}Deleted origin/$branch${NC}" +}Then replace each of the three inline blocks with
_delete_stale_ref "$branch".🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.agents/scripts/worktree-helper.sh around lines 137 - 226, handle_stale_remote_branch repeats the "git push origin --delete ...; git fetch --prune ...; echo Deleted" sequence in three places; create a helper function (e.g., _delete_stale_ref) that accepts the branch name and runs the deletion, prune, and success message, then replace the three inline blocks inside handle_stale_remote_branch with calls to _delete_stale_ref "$branch" to centralize behavior and reduce duplication (ensure the helper mirrors suppression of errors with "2>/dev/null || true" and the same colored success message).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In @.agents/scripts/worktree-helper.sh:
- Around line 160-164: Replace the destructive remote-delete step that runs git
push origin --delete "$branch" (seen in the stale-ref cleanup block) with a safe
local-only removal: delete the local remote-tracking ref using git update-ref -d
"refs/remotes/origin/$branch" and then run git fetch --prune origin to clean up;
alternatively, if removing the remote branch is intentional, add a pre-check
using git ls-remote --heads origin "$branch" and log/confirm before calling git
push origin --delete "$branch". Apply the same change to the repeated
occurrences that currently call git push origin --delete (the blocks later in
the script).
- Around line 494-498: The rmdir call is using the wrong directory name
(".agent" singular) so it never removes the intended directory; update the rmdir
invocations that reference "$path_to_remove/.agent" to use
"$path_to_remove/.agents" instead (fix both occurrences where rmdir
"$path_to_remove/.agent" is used in this script) so the cleanup of the .agents
directory will actually succeed while keeping the existing 2>/dev/null || true
behavior and only removing when empty.
- Around line 653-657: cmd_clean currently calls read -r response
unconditionally which will fail under set -euo pipefail in headless/non-tty
environments; update cmd_clean to guard the interactive prompt by checking if
stdin is a TTY (use [[ -t 0 ]]) before calling read -r response and otherwise
set response to a safe default (e.g., "N" or skip prompting) so the script won’t
exit on EOF; locate the prompt block in cmd_clean and apply the same t1060-style
non-interactive guard used in handle_stale_remote_branch.
- Line 125: The merge-check uses grep with a regex which misinterprets branch
names containing regex metacharacters; update the checks that use grep -q
"origin/$branch$" (seen in the git merged check and the cmd_list and cmd_clean
usages) to use fixed-string matching (e.g., grep -Fq or grep -qF with the
literal "origin/$branch") or escape the branch name before grepping so branch
names like feature/v2.0 don’t produce false matches; apply the same change to
all occurrences in the script (the git branch -r --merged check and the cmd_list
and cmd_clean references).
---
Nitpick comments:
In @.agents/scripts/worktree-helper.sh:
- Around line 137-226: handle_stale_remote_branch repeats the "git push origin
--delete ...; git fetch --prune ...; echo Deleted" sequence in three places;
create a helper function (e.g., _delete_stale_ref) that accepts the branch name
and runs the deletion, prune, and success message, then replace the three inline
blocks inside handle_stale_remote_branch with calls to _delete_stale_ref
"$branch" to centralize behavior and reduce duplication (ensure the helper
mirrors suppression of errors with "2>/dev/null || true" and the same colored
success message).
|
Addressed the Gemini review feedback on |
🔍 Code Quality Report�[0;35m[MONITOR]�[0m Code Review Monitoring Report �[0;34m[INFO]�[0m Latest Quality Status: �[0;34m[INFO]�[0m Recent monitoring activity: 📈 Current Quality Metrics
Generated on: Tue Feb 17 18:41:36 UTC 2026 Generated by AI DevOps Framework Code Review Monitoring |
|
Auto-dismissed: bot review does not block autonomous pipeline



Summary
refs/remotes/origin/$branch) before creating new local branches inworktree-helper.shRef #1506
Summary by CodeRabbit
New Features
Documentation