From 4f6cdedb1e5ea8c13d0ccbf57d772a387ef5728f Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Thu, 21 May 2026 12:31:09 +0200 Subject: [PATCH] Refresh worktrees before auto-forwarding branches MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AutoForwardBranches relies on the worktree model to skip any branch that's currently checked out in another worktree (so we don't update its ref behind the worktree's back). The post-fetch refresh wasn't including the worktrees scope, so any external change to the worktree list between lazygit's startup and the fetch — a `git worktree add`, a `git checkout` in a linked worktree, a branch rename — left the in-memory model stale and the skip check returned false negatives. Add WORKTREES to the post-fetch refresh scope when auto-forwarding is enabled. We gate on the config so users with auto-forward disabled don't pay for an extra `git worktree list` plus per-worktree rev-parse on every fetch tick. Co-Authored-By: Claude Opus 4.7 (1M context) --- pkg/gui/controllers/helpers/branches_helper.go | 14 ++++++++------ ...orward_branches_worktree_added_after_startup.go | 9 --------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/pkg/gui/controllers/helpers/branches_helper.go b/pkg/gui/controllers/helpers/branches_helper.go index a53bf2181..8af447f79 100644 --- a/pkg/gui/controllers/helpers/branches_helper.go +++ b/pkg/gui/controllers/helpers/branches_helper.go @@ -286,12 +286,14 @@ func (self *BranchesHelper) deleteRemoteBranches(remoteBranches []*models.Remote } func (self *BranchesHelper) PostFetchRefresh(fetchErr error) error { - self.c.Refresh(types.RefreshOptions{ - Scope: []types.RefreshableView{ - types.BRANCHES, types.COMMITS, types.REMOTES, types.TAGS, types.PULL_REQUESTS, - }, - Mode: types.SYNC, - }) + scope := []types.RefreshableView{ + types.BRANCHES, types.COMMITS, types.REMOTES, types.TAGS, types.PULL_REQUESTS, + } + // AutoForwardBranches needs a fresh worktree model to skip branches that are checked out elsewhere. + if self.c.UserConfig().Git.AutoForwardBranches != "none" { + scope = append(scope, types.WORKTREES) + } + self.c.Refresh(types.RefreshOptions{Scope: scope, Mode: types.SYNC}) if fetchErr != nil { return fetchErr } diff --git a/pkg/integration/tests/sync/fetch_and_auto_forward_branches_worktree_added_after_startup.go b/pkg/integration/tests/sync/fetch_and_auto_forward_branches_worktree_added_after_startup.go index 61cb0fd9f..bee14276a 100644 --- a/pkg/integration/tests/sync/fetch_and_auto_forward_branches_worktree_added_after_startup.go +++ b/pkg/integration/tests/sync/fetch_and_auto_forward_branches_worktree_added_after_startup.go @@ -43,12 +43,8 @@ var FetchAndAutoForwardBranchesWorktreeAddedAfterStartup = NewIntegrationTest(Ne t.Views().Branches(). Lines( Contains("feature").IsSelected(), - /* EXPECTED: Contains("master (worktree linked-worktree) ↓1"), Contains("wt-branch").DoesNotContain("worktree"), - ACTUAL: */ - Contains("master ✓"), - Contains("wt-branch (worktree linked-worktree)"), ) t.Views().Worktrees(). @@ -58,11 +54,6 @@ var FetchAndAutoForwardBranchesWorktreeAddedAfterStartup = NewIntegrationTest(Ne t.Views().Files(). Focus(). - /* EXPECTED: IsEmpty() - ACTUAL: */ - Lines( - Equals("D file03.txt"), - ) }, })