gitbutlerapp/gitbutler · error

worktree state must be read from the main worktree - a…

Error message

worktree state must be read from the main worktree - a linked-worktree context has its own database, letting adoption and archived state diverge

What it means

worktrees_with_state only supports being called from the main worktree. A linked worktree (git worktree add) has its own database handle, so reading worktree state from it would let adoption metadata and archived-worktree state diverge from the main worktree's source of truth. The function deliberately bails to prevent this inconsistency.

Solutions

  1. Run the command from the main worktree of the repository
  2. Open the gix::Repository at the main worktree's path (e.g. via the common/git dir) before calling worktrees_with_state
  3. Detect repo.kind() == LinkedWorkTree in your own code up front and redirect to the main worktree with a clear message

Example fix

// before
let repo = gix::open(linked_worktree_path)?;
let entries = worktrees_with_state(&repo, &mut db)?;
// after
let repo = gix::open(main_worktree_path)?;
if repo.kind() == gix::repository::Kind::LinkedWorkTree {
    anyhow::bail!("open the main worktree instead");
}
let entries = worktrees_with_state(&repo, &mut db)?;
Defensive patterns

Strategy: validation

Validate before calling

use gix::repository::Kind;
if repo.kind() == Kind::LinkedWorkTree {
    anyhow::bail!("call from the main worktree");
}
let entries = worktrees_with_state(&repo, &mut db)?;

Type guard

fn is_main_worktree(repo: &gix::Repository) -> bool {
    repo.kind() != gix::repository::Kind::LinkedWorkTree
}

Try / catch

match worktrees_with_state(&repo, &mut db) {
    Err(e) if e.to_string().contains("linked-worktree") => eprintln!("run from the main worktree"),
    other => other?,
}

Prevention

When it happens

Trigger: Calling but_db::worktrees::worktrees_with_state(repo, db) while repo.kind() == gix::repository::Kind::LinkedWorkTree, i.e. the gix::Repository was opened inside a linked worktree directory instead of the main worktree.

Common situations: A developer runs the CLI from a linked worktree checkout, or embeds but in a tool that resolves the repo from a secondary worktree path; automation that iterates worktrees but opens each linked worktree as the repository context.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18). Data as JSON: /api/errors/65d89944613be169. Report an issue: GitHub.

Appendix: source

Thrown at crates/but-db/src/worktrees.rs:120

/// pruned, or their administrative directory deleted by hand - so a worktree
/// created later under such a name starts out active. A worktree whose checkout
/// is gone but whose administrative directory git still keeps (prunable, possibly
/// locked or on unmounted media) keeps its row until git itself prunes it.
///
/// Worktrees whose checkout is gone from disk (prunable) are never returned.
/// Entries are identity only - whether a worktree has a usable `HEAD` (readable,
/// born, not the workspace ref) is resolved freshly by [`worktree_head()`]
/// wherever a consumer actually needs it.
///
/// Errors when `repo` is itself a linked worktree: such a repository stores its
/// database in the worktree's private git dir, so adoption and archived state
/// would silently diverge from the main worktree's database.
pub fn worktrees_with_state(
    repo: &gix::Repository,
    db: &mut DbHandle,
) -> Result<Vec<WorktreeEntry>> {
    if repo.kind() == gix::repository::Kind::LinkedWorkTree {
        anyhow::bail!(
            "worktree state must be read from the main worktree - \
             a linked-worktree context has its own database, letting adoption \
             and archived state diverge"
        );
    }
    let (all_names, mut worktrees) = enumerate_worktrees(repo)?;

    let archived = reconcile_archived(db, &all_names)?;

    for wt in &mut worktrees {
        wt.archived = archived.contains(&wt.name);
    }
    Ok(worktrees)
}

/// Enumerate the linked worktrees of `repo`, returning the names of ALL of them
/// (for adoption - a worktree that is unusable today must still be adopted today,
/// not when it becomes usable) along with the entries whose checkout still exists

View on GitHub (pinned to 58e5313667)