{"record":{"id":"cbed589739afb545","repo":"gitbutlerapp/gitbutler","slug":"cannot-unapply-a-branch-from-an-ad-hoc-detached-wo","errorCode":null,"errorMessage":"Cannot unapply a branch from an ad-hoc detached workspace","messagePattern":"Cannot unapply a branch from an ad-hoc detached workspace","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-workspace/src/branch/unapply.rs","lineNumber":248,"sourceCode":"                workspace_ref_name.as_ref(),\n                workspace_disposition,\n            );\n        }\n\n        let branch_in_ws = ws.find_segment_and_stack_by_refname(branch);\n        if branch_in_ws.is_none() {\n            if branch_ref.is_none() {\n                bail!(\n                    \"Cannot unapply non-existing branch '{branch}'\",\n                    branch = branch.shorten()\n                );\n            }\n            // The branch exists in Git, but does not in the workspace: Nothing to do.\n            return Ok(Outcome::new(Cow::Borrowed(workspace)));\n        }\n        let Some(workspace_ref_name) = workspace_ref_name else {\n            // This is an ad-hoc workspace by merit of being unnamed.\n            bail!(\"Cannot unapply a branch from an ad-hoc detached workspace\");\n        };\n        let mut ws_md = meta.workspace(workspace_ref_name.as_ref())?;\n        if ws.kind.has_managed_ref() || ws.has_metadata() {\n            ws.reconcile_metadata(&mut ws_md)?;\n        }\n        let branch_removed_from_ws_meta = ws_md.unapply_branch(branch);\n        if !branch_removed_from_ws_meta {\n            // The branch wasn't in workspace metadata, yet it was present, so also delete its branch metadata\n            // as it could be used to disambiguate the segment.\n            // TODO: this will actually be observable even if it doens't work, unless it's run in a transaction, which right now it's not!\n            //       Should be able to redo the traversal with an overlay that hides branch metadata, but I'd say it's not important enough.\n            meta.remove(branch)?;\n            let graph = ws\n                .graph\n                .redo_traversal_with_overlay(repo, meta, Overlay::default())?;\n            let workspace = graph.into_workspace()?;\n            if workspace.refname_is_segment(branch) {\n                bail!(","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-workspace/src/branch/unapply.rs#L230-L266","documentation":"Unapply requires a named workspace reference to update workspace metadata and rewrite references. A workspace that is ad-hoc purely because it has no name (detached, unnamed workspace) cannot support the bookkeeping unapply performs, so the operation is rejected.","triggerScenarios":"Calling `unapply` when `ws.ref_name()` is `None` — i.e., operating on a detached-HEAD-style ad-hoc workspace with no workspace reference.","commonSituations":"Operating on a repository where the user checked out a commit directly (detached HEAD) and the workspace was built ad-hoc; automation running before any workspace reference was created.","solutions":["Create/assign a named workspace reference first (apply operation) and retry unapply there","Check out a branch instead of a detached commit before using workspace operations","Skip unapply for unnamed workspaces in tooling that enumerates workspaces"],"exampleFix":"// before\nunapply(repo, meta, &ws, branch, ...)?; // fails when ws.ref_name() is None\n// after\nif ws.ref_name().is_none() {\n    anyhow::bail!(\"operation requires a named workspace\");\n}\nunapply(repo, meta, &ws, branch, ...)?;","handlingStrategy":"validation","validationCode":"if ws.ref_name().is_none() {\n    anyhow::bail!(\"unapply requires a named workspace\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only run workspace mutation APIs on named (managed) workspaces","Create a workspace reference before batch operations when HEAD may be detached"],"tags":["git","workspace","detached-head"],"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"}