Skip to content

fix(worktree): reuse an existing branch that owns no worktree instead of failing - #321

Merged
getappz merged 3 commits into
masterfrom
task/339
Jul 25, 2026
Merged

getappz merged 3 commits into
masterfrom
task/339

Conversation

@getappz

@getappz getappz commented Jul 24, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • create_worktree (crates/flare-git-core/src/worktree.rs) only handled two states: already inside a worktree on this branch (no-op), or a brand new branch (git worktree add -b). Missing state: the branch exists as a plain ref with no worktree owning it — e.g. a second-session item claim on a task/N branch that survived from a prior session. -b unconditionally failed there ("a branch named 'task/N' already exists"), with raw git stderr as the only remediation.
  • Hit this exact case live on agentflare item feat: add npm distribution packaging (#28) #29 earlier today — had to invent a throwaway item purely to get a non-colliding branch name.
  • Fix: check refs/heads/<branch> first; if absent, fetch that specific branch and check refs/remotes/origin/<branch> (covers a branch pushed from another machine/session with no local ref yet). Either way, check out the existing ref directly instead of trying to recreate it. Brand-new-branch behavior is unchanged.
  • Validated the approach against git-parsec's worktree_add_existing, an established worktree-lifecycle tool that does the same local-then-remote two-step check.

Fixes the core bug in agentflare item #339, under epic #337.

Test plan

  • cargo build --workspace --all-features — clean
  • cargo fmt --check -p flare-git-core — clean
  • cargo test -p flare-git-core --all-features — 106 passed, 0 failed
  • cargo clippy -p flare-git-core --all-features -- -D warnings — clean
  • New test create_worktree_reuses_a_branch_that_exists_with_no_owning_worktree reproduces the item feat: add npm distribution packaging (#28) #29 collision and asserts it now succeeds

Summary by CodeRabbit

  • Bug Fixes
    • Worktree creation now reuses existing local or remote task branches instead of failing when the branch name is already present.
    • New task branches continue to be created from the requested target branch when no existing task branch is available.
    • Preserved per-worktree build isolation and relevant environment warnings.

… of failing

create_worktree only handled two states: already inside a worktree on
this branch (no-op), or a brand new branch (git worktree add -b).
Missing: the branch exists as a plain ref with no worktree owning it --
e.g. a second-session item claim on a task/N branch that survived from
before. -b unconditionally failed there ("a branch named 'task/N'
already exists"), with the raw git stderr as the only remediation.
Hit this exact case on item #29 today.

Now checks refs/heads/<branch> first, falling back to a targeted fetch
+ refs/remotes/origin/<branch> check when it's only been pushed from
elsewhere with no local ref yet (validated against git-parsec's
worktree_add_existing, which does the same two-step). Either way,
checks out the existing ref directly instead of trying to recreate it.
Brand-new-branch behavior is unchanged.

Agentflare-Agent: claude-code_2-1-219_agent
Agentflare-Branch: task/339
Agentflare-Item: 339
@coderabbitai

coderabbitai Bot commented Jul 24, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 5300fe43-9adc-412f-809b-fc69b8f8868a

📥 Commits

Reviewing files that changed from the base of the PR and between 8a48eaf and f560171.

📒 Files selected for processing (1)
  • crates/flare-git-core/src/worktree.rs

📝 Walkthrough

Walkthrough

create_worktree now detects and reuses existing task branches, including remote-tracking branches, while retaining new-branch creation behavior. A unit test verifies reuse of a local task branch without an existing worktree.

Changes

Worktree branch reuse

Layer / File(s) Summary
Branch detection, creation, and validation
crates/flare-git-core/src/worktree.rs
create_worktree checks local and remote task refs before choosing between reusing a branch and creating one from target_branch; the added test verifies reuse of an existing local branch without an owning worktree.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: reusing an existing branch instead of failing.
Description check ✅ Passed Summary and test plan are complete, but the Notes for reviewers section is missing risk/backward-compatibility details.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch task/339

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

Agentflare-Agent: claude-code_2-1-219_agent
Agentflare-Branch: task/339
Agentflare-Item: 339
@getappz
getappz enabled auto-merge (squash) July 25, 2026 03:00
Agentflare-Agent: claude-code_2-1-219_agent
Agentflare-Branch: task/339
Agentflare-Item: 339
@getappz
getappz merged commit d29352e into master Jul 25, 2026
15 checks passed
@getappz
getappz deleted the task/339 branch July 25, 2026 03:14
getappz added a commit that referenced this pull request Jul 29, 2026
…mmand (#367)

* feat(git-shim): CI paths in trust-root baseline + /flare:git slash command

Item #403, from the 2026-07-29 git-shim competitor audit:

- Added .github/workflows/, .gitlab-ci.yml, .circleci/ to
  TRUST_ROOT_PATHS in classify.rs (CI-wipe was named as an attack
  agentflare didn't yet cover). Documented a known limitation:
  resolve_trust_root_touch self-diffs when the pushed branch equals
  the default branch by name, so this doesn't change behavior for the
  ordinary direct-push case yet -- tracked as an addendum on item #321.

- New /flare:git prompt (mcp_prompts.rs), same mechanism as the
  existing /flare:artifact and /flare:handoff prompts, surfacing the
  agentflare git-shim's own admin subcommands: install-hooks,
  install-shim, uninstall-shim, snapshot list/restore/prune, audit
  preview/prune, doctor. Deliberately scoped to just these -- ordinary
  git commands and other CLI/MCP surfaces (pr_check, etc.) already have
  their own entrypoint (the ! shell-escape, or their own MCP tool) and
  don't need duplicating here.

* fix(git-shim): fmt + sync trust-root deny message + reject hidden git-shim subcommands in /flare:git

- cargo fmt fix for the new test in mcp_prompts.rs
- TrustRootTouch::Unknown deny message now lists policy.trust_root_paths
  instead of a hardcoded subset, so it stays accurate as the list grows
- get_git_command rejects trailer-inject/ref-transaction-log/scope-check
  instead of echoing a run instruction for internal-only subcommands
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