{"record":{"id":"a908ba4ebe3da9ea","repo":"gitbutlerapp/gitbutler","slug":"refusing-to-apply-a-reference-that-already-is-a-wo","errorCode":null,"errorMessage":"Refusing to apply a reference that already is a workspace: '{}'","messagePattern":"Refusing to apply a reference that already is a workspace: '(.+?)'","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-workspace/src/branch/apply.rs","lineNumber":362,"sourceCode":"        });\n    };\n\n    if let Some(ws_ref_name) = ws.ref_name()\n        && repo.try_find_reference(ws_ref_name)?.is_none()\n    {\n        // The workspace is the probably ad-hoc, and doesn't exist, *assume* unborn.\n        bail!(\n            \"Cannot create reference on unborn branch '{}'\",\n            ws_ref_name.shorten()\n        );\n    }\n\n    if ws.has_workspace_commit_in_ancestry(repo) {\n        bail!(\"Refusing to work on workspace whose workspace commit isn't at the top\");\n    }\n\n    if meta.workspace_opt(branch.as_ref())?.is_some() {\n        bail!(\n            \"Refusing to apply a reference that already is a workspace: '{}'\",\n            branch.shorten()\n        );\n    }\n    // In general, we only have to deal with one branch to apply. But when we are on an adhoc workspace,\n    // we need to assure both branches go into the existing or the new workspace:\n    //  - the current one and the one to apply, if these are different.\n    // The returned workspace ref name will be set to the new merge commit, if created, or it may not change\n    // at all if the workspace can be created by just setting metadata.\n    let (workspace_ref_name_to_update, branches_to_apply) = match &ws.kind {\n        WorkspaceKind::Managed { ref_info }\n        | WorkspaceKind::ManagedMissingWorkspaceCommit { ref_info } => {\n            (ref_info.ref_name.clone(), vec![branch.clone()])\n        }\n        WorkspaceKind::AdHoc => {\n            // We need to switch over to a possibly existing workspace.\n            // We know that the current branch is *not* reachable from the workspace or isn't naturally included,\n            // so it needs to be added as well.","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-workspace/src/branch/apply.rs#L344-L380","documentation":"`Workspace::apply` refuses to apply a branch reference that is itself already a workspace root in the workspace metadata. A reference can only be either a workspace or an applied branch, not both, so applying it as a branch would corrupt the workspace structure. This is a deliberate precondition, not a transient failure.","triggerScenarios":"Calling `but_workspace::branch::apply` (or higher-level `but apply`) with a branch name that `meta.workspace_opt(branch)` reports as an existing workspace, while that workspace commit is not at the top of its ancestry (the prior check passed).","commonSituations":"Users pass the workspace/meta branch name instead of a feature branch; a branch previously used as a workspace is re-applied after the workspace commit was removed from the tip; scripted apply loops iterating over all refs including workspace branches.","solutions":["Use the actual feature branch name, not the workspace branch, in the apply call","If the branch should no longer be a workspace, remove its workspace registration in the metadata before applying","Check `meta.workspace_opt(branch_ref)` before calling apply and skip or handle workspace references explicitly"],"exampleFix":"// before\nws.apply(meta, \"my-workspace-branch\", ...)?;\n// after\nif meta.workspace_opt(&branch)?.is_some() {\n    bail!(\"{ } is a workspace, pick a stack branch instead\");\n}\nws.apply(meta, \"feature-branch\", ...)?;","handlingStrategy":"validation","validationCode":"if meta.workspace_opt(&branch_ref)?.is_some() {\n    return Err(format!(\"{} is a workspace; apply a regular branch instead\", branch_ref.shorten()));\n}","typeGuard":"fn is_workspace_branch(meta: &M, r: &gix::refs::Reference) -> bool {\n    meta.workspace_opt(r).map(|o| o.is_some()).unwrap_or(false)\n}","tryCatchPattern":"match apply(...) {\n    Err(e) if e.to_string().contains(\"already is a workspace\") => /* pick a stack branch instead */,\n    Err(e) => return Err(e.into()),\n    Ok(r) => r,\n}","preventionTips":["Resolve the branch to a stack branch, never the workspace branch itself","Before scripting applies, filter out refs registered as workspaces","Check workspace metadata listing before applying unfamiliar branch names"],"tags":["git","workspace","precondition"],"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"}