{"record":{"id":"65d89944613be169","repo":"gitbutlerapp/gitbutler","slug":"worktree-state-must-be-read-from-the-main-worktree","errorCode":null,"errorMessage":"worktree state must be read from the main worktree - a linked-worktree context has its own database, letting adoption and archived state diverge","messagePattern":"worktree state must be read from the main worktree - a linked-worktree context has its own database, letting adoption and archived state diverge","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-db/src/worktrees.rs","lineNumber":120,"sourceCode":"/// pruned, or their administrative directory deleted by hand - so a worktree\n/// created later under such a name starts out active. A worktree whose checkout\n/// is gone but whose administrative directory git still keeps (prunable, possibly\n/// locked or on unmounted media) keeps its row until git itself prunes it.\n///\n/// Worktrees whose checkout is gone from disk (prunable) are never returned.\n/// Entries are identity only - whether a worktree has a usable `HEAD` (readable,\n/// born, not the workspace ref) is resolved freshly by [`worktree_head()`]\n/// wherever a consumer actually needs it.\n///\n/// Errors when `repo` is itself a linked worktree: such a repository stores its\n/// database in the worktree's private git dir, so adoption and archived state\n/// would silently diverge from the main worktree's database.\npub fn worktrees_with_state(\n    repo: &gix::Repository,\n    db: &mut DbHandle,\n) -> Result<Vec<WorktreeEntry>> {\n    if repo.kind() == gix::repository::Kind::LinkedWorkTree {\n        anyhow::bail!(\n            \"worktree state must be read from the main worktree - \\\n             a linked-worktree context has its own database, letting adoption \\\n             and archived state diverge\"\n        );\n    }\n    let (all_names, mut worktrees) = enumerate_worktrees(repo)?;\n\n    let archived = reconcile_archived(db, &all_names)?;\n\n    for wt in &mut worktrees {\n        wt.archived = archived.contains(&wt.name);\n    }\n    Ok(worktrees)\n}\n\n/// Enumerate the linked worktrees of `repo`, returning the names of ALL of them\n/// (for adoption - a worktree that is unusable today must still be adopted today,\n/// not when it becomes usable) along with the entries whose checkout still exists","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-db/src/worktrees.rs#L102-L138","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run the command from the main worktree of the repository","Open the gix::Repository at the main worktree's path (e.g. via the common/git dir) before calling worktrees_with_state","Detect repo.kind() == LinkedWorkTree in your own code up front and redirect to the main worktree with a clear message"],"exampleFix":"// before\nlet repo = gix::open(linked_worktree_path)?;\nlet entries = worktrees_with_state(&repo, &mut db)?;\n// after\nlet repo = gix::open(main_worktree_path)?;\nif repo.kind() == gix::repository::Kind::LinkedWorkTree {\n    anyhow::bail!(\"open the main worktree instead\");\n}\nlet entries = worktrees_with_state(&repo, &mut db)?;","handlingStrategy":"validation","validationCode":"use gix::repository::Kind;\nif repo.kind() == Kind::LinkedWorkTree {\n    anyhow::bail!(\"call from the main worktree\");\n}\nlet entries = worktrees_with_state(&repo, &mut db)?;","typeGuard":"fn is_main_worktree(repo: &gix::Repository) -> bool {\n    repo.kind() != gix::repository::Kind::LinkedWorkTree\n}","tryCatchPattern":"match worktrees_with_state(&repo, &mut db) {\n    Err(e) if e.to_string().contains(\"linked-worktree\") => eprintln!(\"run from the main worktree\"),\n    other => other?,\n}","preventionTips":["Always open the repository at the main worktree path for workspace/state queries","Check repo.kind() early in CLI entry points and fail fast with a clear message","Avoid embedding per-linked-worktree databases that can diverge"],"tags":["git","worktree","database","state"],"backgroundTag":"invalid-state-transition","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}