gitbutler icon indicating copy to clipboard operation
gitbutler copied to clipboard

Stale parent in workspace merge commit wedges CLI ("Could not find branch CLI id '' in IdMap") and shows "Unnamed segment"

Open bmd1905 opened this issue 5 months ago • 7 comments

Version

App 0.20.4 / but CLI 0.20.3

Operating System

macOS (Apple Silicon, Darwin 25.5.0)

Distribution Method

dmg (Mac OS - Apple Silicon)

Describe the issue

A stale parent in the GitButler workspace (octopus) merge commit puts the whole project into an unrecoverable state from both the GUI and the CLI.

Symptoms

  • GUI shows a lane titled "Unnamed segment" holding the commits of a branch that was supposed to be unapplied.
  • but status, but status -fv, but apply <branch>, and but unapply <branch> all fail (or silently no-op) with:
    Error: Could not find branch CLI id '' in IdMap
    
  • GUI Update workspace (integrate upstream) fails:
    API error: (workspace_integrate_upstream)
    Failed to merge bases while cherry picking commit <workspace-commit>.
    Encountered a conflict while merging the commit's merge bases: ...
    
    CLI but pull reports: Error during integration: merge conflict when computing workspace tree.

Root cause (after digging in)

The gitbutler/workspace merge commit had 6 parents. Five matched the recorded stack heads (the target base + 4 applied stacks). The 6th parent was an old head of a branch that had been unapplied/removed — its SHA matched no current stack head in vb_stack_heads.

workspace commit parents:
  46ebe1c4  local-tmp-branch          (head matches)
  d59a2ed6  target base               (matches)
  2c1c4590  <stale reflection head>   (NO stack claims this) <-- orphan
  be451d74  jira-skills-kan           (head matches)
  68ba8dd7  static-skills-reminder    (head matches)
  708b9ba9  auto-mode                 (head matches)

The unapplied branch's recorded head was c07c757 while the workspace parent was the older 2c1c4590 (same commit message, different SHA — the branch had since been amended/rebased, and it was also already merged upstream via yet another SHA). Because that parent maps to no stack, the lane is rendered with an empty CLI id, and the IdMap lookup throws, which aborts status and every mutation that first enumerates the workspace — so there is no in-product way to apply, unapply, or integrate out of the state.

This looks like it was created when a branch that was already merged upstream (via different SHAs) was unapplied/removed without the workspace merge commit being rebuilt to drop its parent.

How I recovered (for reference — all in-product paths were blocked)

  1. Manually set the offending stack in_workspace = false in both virtual_branches.toml and .git/gitbutler/but.sqlite (vb_stacks).
  2. With the orphan stack no longer in the applied set, but pull --check reported the 4 real branches as [ok] and excluded the orphan; but pull then advanced the base and rebased the 4 branches cleanly, dropping the stale parent.

How to reproduce

Approximate sequence (the exact trigger seems to be unapply/remove of an already-merged branch):

  1. Have several applied virtual branches in the workspace.
  2. One branch gets merged upstream, but the merge on the remote uses different commit SHAs than the local branch (e.g. squash/rebase-on-merge or post-review amends).
  3. Amend/rebase that local branch so its head advances, then unapply or remove it.
  4. The workspace merge commit retains the branch's previous head as a parent, which now matches no stack head.
  5. Open the GUI (see "Unnamed segment") and/or run but status (see the IdMap error). Try Update workspace -> integration fails.

Expected behavior

  • Unapplying/removing a branch should rebuild the workspace merge commit so it never retains a parent that no stack head claims.
  • A workspace parent that maps to no stack should be handled gracefully (e.g. dropped or surfaced as a recoverable warning) rather than throwing Could not find branch CLI id '' in IdMap and wedging status + every mutation.
  • There should be an in-product recovery path (e.g. but workspace rebuild/but clean that re-derives the merge commit from the recorded stack heads) instead of requiring hand-edits to virtual_branches.toml / but.sqlite.

Relevant log output

$ but status
╭┄zz [unassigned changes] (no changes)
┊
┊╭┄lo [local-tmp-branch]
┊●   46ebe1c4f8 local
├╯
┊
Error: Could not find branch CLI id '' in IdMap

$ but unapply henry.bui/feat/<branch>
Failed to unapply branch. Branch 'henry.bui/feat/<branch>' not found in any applied stack

$ but pull
Found 1 upstream commits on origin/develop
Updating 4 active branches...
Error during integration: merge conflict when computing workspace tree
Error: merge conflict when computing workspace tree

bmd1905 avatar Jun 28 '26 10:06 bmd1905

Thanks for reporting.

I reproduced the issue locally with a much simpler setup. The crux seems to be anonymous segments which currently can't be recovered from. The simplest fix seems to be to create a branch at the stack tip so it's named again.

How to reproduce

gitbutler on  gitbutler/workspace [$]
❯ but
╭┄zz [uncommitted] (no changes)
┊
┊╭┄fo [foo] (no commits)
├╯
┊
┊● 100d0810ec (upstream) ⏫ 788 commits (checked 23 seconds ago)
├╯ 819584286a (common base) 2026-04-28 ci(lite): add publish configuration

Hint: run `but help` for all commands

gitbutler on  gitbutler/workspace [$]
❯ git br -d foo
Deleted branch foo (was 819584286a).

gitbutler on  gitbutler/workspace [$]
❯ but
╭┄zz [uncommitted] (no changes)
┊
Error: Could not find branch CLI id '' in IdMap

And then the recovery by naming the tip:

❯ git branch foo @~1

gitbutler on  gitbutler/workspace [$]
❯ but
╭┄zz [uncommitted] (no changes)
┊
┊╭┄fo [foo] (no commits)
├╯
┊
┊● 100d0810ec (upstream) ⏫ 788 commits (checked 4 minutes ago)
├╯ 819584286a (common base) 2026-04-28 ci(lite): add publish configuration

Hint: run `but help` for all commands

This is a bit of a bigger issue, even after fixing the Could not find branch CLI id '' in IdMap problem, as there would need to be a standard way of referring to stacks/stack-tips. Right now we are using reference names for the most parts, but there would also have to be an alternative to handle cases like these.

CC @slarse for CLI correctness, and @Caleb-T-Owens for the big picture parts.

Byron avatar Jun 29 '26 00:06 Byron

After the attempted fix in #14673, maybe it makes sense to reel in @jonathantanmy2 to see if there if an IdMap specific fix is possible, or any other fix that doesn't require any caller to be aware of this. Maybe this isn't an issue anymore either once Graphy is used everywhere.

Byron avatar Jul 09 '26 06:07 Byron

I think there's a very long-hanging fruit solution here where we just assign CLI IDs to anonymous segments. We currently explicitly don't do it. I'll give it a shot and see if it fixes the issue.

slarse avatar Jul 09 '26 21:07 slarse

It turns out fixing the crashing was as simple as assigning short IDs to anonymous segments (https://github.com/gitbutlerapp/gitbutler/pull/14700). You still can't do much with them as most (possibly all) commands try to resolve segments to branch refs, which won't work for an anonymous ID, but at least status doesn't throw a fit anymore.

I think a decent enough resolution for the time being is to add support for anonymous segments in but reword so you can name the segment, and print a hint (probably in status) that doing so is required to keep working with it. Once that's done, I think the core problem of this issue is addressed.

slarse avatar Jul 12 '26 11:07 slarse

I just encountered it today.

Image

mohdlatif avatar Jul 27 '26 22:07 mohdlatif

Another reproduction on but 0.22.3 (macOS), with a different trigger and a simpler recovery than the hand-edit in the original report.

Trigger. A [DISCARD] operation (per but oplog) removed a whole applied stack. Its row was deleted from vb_stacks and vb_stack_heads, but the gitbutler/workspace merge commit kept that stack's parent slot, now pointing at the stack's base commit (the previous target tip). No remaining stack claims that parent. I don't know the exact command that ran the discard; it was another process working in the same checkout.

Symptom. but status still worked and showed no "Unnamed segment" or IdMap error. After one new commit landed on the target branch, but pull --check failed:

Error: Failed to merge bases while cherry picking commit <workspace-commit>.
Encountered a conflict while merging the commit's new bases: <rebased-1>, <rebased-2>, <rebased-3>, <orphan-base>, <rebased-4>, <rebased-5>.
Any ids mentioned may be in-memory and inaccessible through the git CLI.

Every new base except one was an in-memory commit rebased onto the new target. The exception was the orphan parent, left at the old target tip. The error repeated on retry with new in-memory IDs each time.

Not a content conflict. None of the applied branches touches the files the upstream commit changed. git merge-tree --write-tree merges every pair of branch heads cleanly. Simulating the full update (each branch rebased onto the new target, then merged together one at a time) is also clean, whether the merge base is the one git picks, the old target or the new target. So the failure appears to come from the orphan parent alone, not from the branches.

Recovery. but unapply <any other applied branch> rebuilt the workspace commit from the remaining stacks and dropped the orphan parent. Afterwards the workspace commit's parents were exactly the applied stack heads, and but pull --check reported every branch [ok]. No edits to virtual_branches.toml or but.sqlite were needed. That workaround wouldn't have applied here anyway, because the stack row was already gone.

Same pattern seen earlier in the session. An earlier discard of a different stack left an orphan parent at the same base commit. The next operation that applied a branch rebuilt the workspace commit and dropped it. That suggests whole-stack discard rewrites the workspace commit's parents in place, and only apply/unapply rebuilds it from stack metadata.

How to spot it: compare git log -1 --format=%p gitbutler/workspace with the branch heads but status shows. A parent that matches no head, typically the old target tip, is the orphan.

mattspruiell avatar Oct 16 '26 05:10 mattspruiell

Another reproduction on but 0.22.3 (macOS), with a different trigger from the ones above: a regular merge on GitHub followed by but pull. No discard, no squash or rebase merge.

Steps (4 times in one day, with 3–4 branches applied):

  1. A branch's PR is merged on GitHub with a merge commit (gh pr merge <n> --merge --delete-branch), which also deletes the remote branch.
  2. but pull in the workspace. It reports the other branches rebased, plus Unnamed segment - rebased.
  3. The merged branch's tip stays a parent of gitbutler/workspace, though it's already in origin/main. virtual_branches.toml still has that stack at in_workspace = true, its local ref is gone, and but status no longer lists it.

Effect: when the stale tips touch the same lines as a new commit, here two merged PRs that each bumped the same version line in plugin.json, the next but commit -b <other-branch> <id> fails:

Error: Failed to merge bases while cherry picking commit <workspace-commit>.
Encountered a conflict while merging the commit's new bases: <merged-tip-1>, <live-lane>, <merged-tip-2>, <live-lane>.

but pull --check reports up to date, and but unapply <merged-branch> fails with Could not find source, since the name is gone.

What cleared it, three times out of three: creating a new branch (but commit -b <new-branch> …). The workspace commit was rebuilt without the stale parents, and the stack went to in_workspace = false. This matches the "unapply another applied branch" workaround above. Detection as suggested above (git log -1 --format=%p gitbutler/workspace against but status's heads) works for us.

If it helps, a but pull that notices a stack whose tip is reachable from the target and drops it from the workspace would cover this case.

developer-mayuan avatar Oct 24 '26 05:10 developer-mayuan