gitbutler icon indicating copy to clipboard operation
gitbutler copied to clipboard

but pull fails with "An edge with desired order 0 already exists" when linked worktrees include a merge commit (0.23.3)

Open sensei-woo opened this issue 3 weeks ago • 1 comments

Version

but 0.23.3 (macOS, CLI)

What happens

but pull and but pull --check fail immediately, before anything is rebased:

Error: An edge with desired order 0 already exists

The message comes from the bail! in add_edge (crates/but-rebase/src/graph_rebase/mutate.rs): the child commit already has an outgoing edge with the same desired_order.

Narrowing it down

  • but pull --check --update workspace works. but pull --check --update worktrees fails with the error above. So the trigger is the linked-worktree half of but pull (the default is --update worktrees,workspace), not the applied lanes. It fails the same with 2 lanes applied or with ~30.
  • The repo has ~20 linked worktrees that GitButler lists in the worktrees array of but status --json. I worked on a copy of .git (APFS clone, worktree metadata pointed at empty directories) and moved worktree metadata dirs in and out of .git/worktrees/:
    • each worktree alone: passes
    • one specific pair: fails (any pair that includes worktree T below fails when the other worktree is one of a few others, so this is a graph shape, not one bad worktree)
  • Worktree T is a branch whose history contains a local merge commit (not on the target). That merge's second parent S is the tip of a side branch that the target has also merged (an upstream merge commit exists on the target with S as its second parent). The merge's first parent is an older commit on the target's first-parent line.
    • Replacing T's tip with a single flat commit (same tree) on top of either parent of its merge: passes. So the merge commit matters.
  • The other worktree P is a branch (or detached HEAD) whose base is S or any commit descending from S on the target (the target tip also works).
    • Replacing P with a single commit on S: fails. On the parent of S or on T's first parent: passes.
  • So the failing shape is: a worktree stack containing a merge commit whose second parent is S, plus another worktree whose base descends from S, while the target has moved (the target is ~150 commits ahead of the workspace base, and S's parent is the workspace base).

Repro status

I could not make a standalone minimal repro. Synthetic repos with that shape (side branch with 1-2 commits merged upstream, a worktree that re-merges the already-upstream side tip locally, a second worktree on S or on the target tip, stale origin/main, one applied lane, with and without distinct commit dates) all pass but pull --check. Something else in the real graph is needed (maybe the first-parent distance, more than one merge on the target, or ordering of many worktrees). I can test patches or a debug build against the real history if useful.

Expected

but pull rebases or reports conflicts per worktree, or skips a worktree it cannot place, instead of aborting the whole command. At minimum the error should name the commit and worktree involved.

Workaround

but pull --update workspace (skips linked worktrees).

sensei-woo avatar Nov 11 '26 08:11 sensei-woo

I hit the same error on but 0.23.4 (macOS, CLI) with a related shape: the local merge commit is in an applied lane, not in a linked worktree. I reduced it to a minimal synthetic repro. It failed on all seven fresh builds I ran.

Shape. Three things have to hold at once, while the target has moved past a commit M:

  1. An applied lane contains a local merge commit ("Merge branch 'main' into lane") whose second parent is M, a commit on the target.
  2. The local main branch points at M. but pull moves local main to the target tip, so a pull made while the target was at M leaves it there.
  3. A linked worktree is checked out at a commit that descends from M and is not in the workspace.

Repro script. Run it in an empty directory. It uses only a local bare remote.

#!/bin/bash
set -eu
R=${1:?dir}; cd "$R"
export GIT_AUTHOR_NAME=t [email protected] GIT_COMMITTER_NAME=t [email protected]
c(){ echo "$1" > "$1.txt"; git add "$1.txt"; git commit -q -m "$1"; }
git init -q --bare -b main origin.git
git clone -q origin.git seed; (cd seed; c base; git push -q origin main)
git clone -q origin.git repo
(cd repo; but status >/dev/null 2>&1 || true)          # workspace base = base
(cd seed; c m; git push -q origin main)                 # target moves to M
M=$(git -C seed rev-parse HEAD)
cd repo; git fetch -q
git branch lane origin/main~1
git worktree add -q ../lane-tmp lane
( cd ../lane-tmp; c lane1; git merge -q --no-edit "$M" -m "Merge branch 'main' into lane"; c lane2 )
git worktree remove ../lane-tmp
but apply lane >/dev/null
git branch wt "$M"; git worktree add -q ../wt wt; ( cd ../wt; c wt1 )
git branch -f main "$M"
(cd "$R/seed"; c n; git push -q origin main)            # target moves past M
git fetch -q
but pull --check    # Error: An edge with desired order 0 already exists

Controls. Each one changes a single step on a fresh build, and each one makes but pull --check pass. I ran every case twice.

  • The lane has plain commits instead of the merge.
  • Local main stays at base instead of M.
  • There is no linked worktree.
  • The worktree is checked out detached at M itself, with no commit of its own.

Real repo. The real repo matched the same three conditions. On an APFS clone of the repository, I moved worktree metadata in and out and unapplied lanes:

  • With no linked worktrees, the check passes.
  • Either of two GitButler-managed worktrees on its own makes it fail. One has two commits on top of M; the other has one commit on top of the new target tip.
  • Every detached worktree I tried at a commit after M that is not in the workspace failed: a new child of M, an intermediate target merge, and the target tip. A detached worktree at M, at the parent of M, or at an applied lane's tip passed.
  • Unapplying the lane that holds the merge commit makes it pass. Unapplying the two lanes already merged upstream does not.
  • With local main moved from M to the target tip, it passes. Moving main back to M makes it fail again.

In that repo the lane's merge was redundant: its first parent already descended from M, left that way by an earlier but pull. In the synthetic repro, some redundant-merge variants failed and some passed, so I don't know whether that detail matters.

--update. The synthetic repro fails with both --update workspace and --update worktrees. My repo failed only with --update workspace and passed with --update worktrees, which is the reverse of what the original report describes. The update mode doesn't look like the deciding factor.

Workaround. Unapply the lane that holds the merge commit, run but pull, then apply the lane again. After that, but pull --check reports up to date.

A warning for anyone bisecting this on a clone. If the clone's .git/worktrees/*/gitdir still points at the original worktree directories, but pull on the clone checks files out into those real directories. Point the metadata at scratch directories first.

Relation to the original report. I also built the shape described above, with the merge inside a linked worktree T and a second worktree on S. It passed but pull --check with local main at the target and with it at the old base, two builds each. So this may be a second path to the same add_edge bail rather than the same trigger. Where local main points in the original repo may still be worth checking.

mattspruiell avatar Nov 17 '26 18:11 mattspruiell