Skip to content

fix(newTree.fsx): crash if remote branch disappears#326

Open
nodefect wants to merge 3 commits into
tarsgate:masterfrom
nodefect:fix-newTree-stale-branches
Open

fix(newTree.fsx): crash if remote branch disappears#326
nodefect wants to merge 3 commits into
tarsgate:masterfrom
nodefect:fix-newTree-stale-branches

Conversation

@nodefect

Copy link
Copy Markdown
Contributor

Problem
newTree.fsx crashes with fatal: couldn't find remote ref refs/heads/someBranch when a previously-tracked remote branch is deleted (e.g. after a PR is merged).

Cause
The script used git remote set-branches --add \u003cremote\u003e \u003cbranch\u003e once per branch. Since --add is cumulative, deleted branches stayed in the refspec forever and git fetch --all later tried (and failed) to fetch them.

Fix

  1. Introduce GetExistingWorktreeBranches — inspects git worktree list --porcelain so we know which branches existing local worktrees depend on.
  2. Build a single list of candidate branches: worktree branches + head branch + target branch.
  3. For each remote, filter that list to branches that still exist (git ls-remote).
  4. Run one git remote set-branches \u003cremote\u003e \u003cbranch\u003e… (without --add) per remote. This resets the refspec to exactly the branches that still exist, clearing any stale entries.

Closes #325

@github-actions

Copy link
Copy Markdown

⚠️ CI Build Failed!

Hey @nodefect, one or more CI jobs failed. Please check the workflow logs.

@knocte knocte force-pushed the fix-newTree-stale-branches branch from 5d9bb78 to 4f676b6 Compare July 2, 2026 18:43
Reset remote branch tracking to avoid stale refs: replacing
per-branch 'git remote set-branches --add' with a
single per-remote 'git remote set-branches' (without --add)
to reset the tracked branches to only those that still exist.

This fixes the crash when a previously-tracked remote branch is
deleted (e.g. after a PR is merged), because 'git fetch --all'
no longer tries to fetch stale refs.

To avoid breaking existing worktrees, we now also inspect the
branches currently used by local worktrees (via 'git worktree
list --porcelain') and include them in the set of branches to
track.

Fixes tarsgate#325
@knocte knocte force-pushed the fix-newTree-stale-branches branch from 4f676b6 to dfb7d16 Compare July 2, 2026 18:48
@knocte

knocte commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

/pi I just tested this with a PR URL as an argument, and it failed with this error:

knocte@Andrews-MacBook-Pro Work % ./conventions/fix-newTree-stale-branches/scripts/newTree.fsx https://github.com/tarsgate/awto-pi-lot/pull/46
PR #46 source repo: git@github.com:webwarrior-ws/awto-pi-lot.git
PR #46 source branch: nano-gpt
git -C /Users/aidev/Work/awto-pi-lot clone --single-branch --bare git@github.com:webwarrior-ws/awto-pi-lot.git .bare
Cloning into bare repository '.bare'...
git -C /Users/aidev/Work/awto-pi-lot remote rename origin webwarrior-wsFork
git -C /Users/aidev/Work/awto-pi-lot remote set-branches webwarrior-wsFork master nano-gpt
Error when running 'git -C /Users/aidev/Work/awto-pi-lot remote set-branches webwarrior-wsFork master nano-gpt'
Fsdk.ProcessModule+ProcessFailed: ProcessFailed
  "Error when running 'git -C /Users/aidev/Work/awto-pi-lot remote set-branches webwarrior-wsFork master nano-gpt'"
   at Fsdk.ProcessModule.ProcessResult.Unwrap(String errMsg)
   at FSI_0006.action@693(String remoteName) in /Users/aidev/Work/conventions/fix-newTree-stale-branches/scripts/newTree.fsx:line 719
   at FSI_0006.clo@693-9.Invoke(String remoteName)
   at Microsoft.FSharp.Collections.SeqModule.Iterate[T](FSharpFunc`2 action, IEnumerable`1 source) in /Users/runner/work/1/s/src/fsharp/src/FSharp.Core/seq.fs:line 630
   at <StartupCode$FSI_0006>.$FSI_0006.main@() in /Users/aidev/Work/conventions/fix-newTree-stale-branches/scripts/newTree.fsx:line 692
   at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
Stopped due to error

Please fix it, and remember you can push directly to this branch that PR326 targets (thanks to the TOKEN that you have access to).

When a remote has no fetch refspec yet (e.g. after a single-branch
bare clone that was renamed),  without --add
fails. Use --add in that case, and keep the original set-branches
behaviour when refspecs already exist so stale branches are still
cleared.
@nodefect

nodefect commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

The fix has been pushed directly to the PR branch fix-newTree-stale-branches (commit c91b5bc).

Root cause:
git remote set-branches <remote> <branches>… (without --add) fails when a remote has no existing fetch refspecs. This is exactly the situation after a single-branch bare clone whose remote is later renamed.

Fix:
Before deciding which command to run, the script now checks whether remote.<name>.fetch already exists via git config --get-all:

  • If there are already refspecs → use set-branches (no --add) to replace them, which still clears stale branches.
  • If there are no refspecs yet → use set-branches --add to create the initial tracking list safely.

This preserves the stale-branch cleanup for existing remotes while also working for a fresh clone.


View action run | Model: ppq/autoclaw | Time: 7m 59s | Tokens: 2.4M | Cost: $0.14 | Pi SDK v0.78.1 | Action v2.19.2

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

⚠️ CI Build Failed!

Hey @nodefect, one or more CI jobs failed. Please check the workflow logs.

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.

bug(newTree.fsx): crash if remote branch disappears

2 participants