{"record":{"id":"6346b694659912b9","repo":"gitbutlerapp/gitbutler","slug":"cannot-unapply-branch-branch-from-an-ad-hoc-wo","errorCode":null,"errorMessage":"Cannot unapply branch '{branch}' from an ad-hoc workspace because the workspace cannot be empty","messagePattern":"Cannot unapply branch '(.+?)' from an ad-hoc workspace because the workspace cannot be empty","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-workspace/src/branch/unapply.rs","lineNumber":217,"sourceCode":"        let branch_commit_id = branch_ref\n            .as_mut()\n            .map(|reference| reference.peel_to_id().map(|id| id.detach()))\n            .transpose()?;\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        let workspace_ref_name = ws.ref_name().map(ToOwned::to_owned);\n        if matches!(ws.kind, but_graph::workspace::WorkspaceKind::AdHoc)\n            && branch_ref\n                .as_ref()\n                .zip(workspace_ref_name.as_ref())\n                .is_some_and(|(to_unapply, workspace_ref_name)| {\n                    to_unapply.name() == workspace_ref_name.as_ref()\n                })\n        {\n            bail!(\n                \"Cannot unapply branch '{branch}' from an ad-hoc workspace because the workspace cannot be empty\",\n                branch = branch.shorten()\n            );\n        }\n\n        if let Some(workspace_ref_name) = workspace_ref_name.as_ref()\n            && workspace_ref_name == branch\n        {\n            return unapply_workspace_reference(\n                ws,\n                repo,\n                meta,\n                workspace_ref_name.as_ref(),\n                workspace_disposition,\n            );\n        }\n\n        let branch_in_ws = ws.find_segment_and_stack_by_refname(branch);","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-workspace/src/branch/unapply.rs#L199-L235","documentation":"`unapply` refuses to remove the workspace's own reference branch from an ad-hoc workspace, because an ad-hoc (unnamed) workspace would become empty and cease to exist. The library requires a workspace to always retain at least its workspace branch.","triggerScenarios":"Calling `unapply` with `branch` equal to the workspace reference name while `ws.kind` is `WorkspaceKind::AdHoc` (and the branch is not a managed ref).","commonSituations":"Programmatically iterating all segments of an ad-hoc workspace and attempting to unapply each one, accidentally including the workspace branch itself.","solutions":["Do not call unapply on the workspace reference branch itself; skip it in your loop","Convert to a named/managed workspace first if you need to restructure it completely","Unapply a different (non-workspace) branch instead"],"exampleFix":"// before\nfor branch in branches { unapply(repo, meta, &ws, branch, ...)?; }\n// after\nfor branch in branches {\n    if branch == workspace_ref_name { continue; }\n    unapply(repo, meta, &ws, branch, ...)?;\n}","handlingStrategy":"validation","validationCode":"fn can_unapply(ws: &Workspace, branch: &gix::refs::FullNameRef) -> bool {\n    !(matches!(ws.kind, but_graph::workspace::WorkspaceKind::AdHoc)\n        && ws.ref_name().map(ToOwned::to_owned).as_ref() == Some(&branch.to_owned()))\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Skip the workspace reference itself when unapplying all branches of an ad-hoc workspace","Only dissolve ad-hoc workspaces via dedicated teardown APIs"],"tags":["git","workspace","ad-hoc"],"backgroundTag":"unsupported-operation","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"}