{"record":{"id":"858c9f228e4b9feb","repo":"gitbutlerapp/gitbutler","slug":"refusing-to-delete-last-named-segment-as-it-w","errorCode":null,"errorMessage":"Refusing to delete last named segment '{}' as it would leave an anonymous segment","messagePattern":"Refusing to delete last named segment '(.+?)' as it would leave an anonymous segment","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-workspace/src/branch/remove_reference.rs","lineNumber":101,"sourceCode":"    let Some((stack, _segment)) = workspace.find_segment_and_stack_by_refname(ref_name) else {\n        return Ok(None);\n    };\n\n    if avoid_anonymous_stacks\n        && (stack\n            .segments\n            .iter()\n            .map(|s| s.commits.len())\n            .sum::<usize>()\n            > 0\n            && stack\n                .segments\n                .iter()\n                .filter(|s| s.ref_info.is_some())\n                .count()\n                < 2)\n    {\n        bail!(\n            \"Refusing to delete last named segment '{}' as it would leave an anonymous segment\",\n            ref_name.shorten()\n        );\n    }\n\n    let deleted_ref = delete_local_branch(repo, ref_name)?;\n\n    let deleted_meta = if keep_metadata {\n        false\n    } else {\n        meta.remove(ref_name)?\n    };\n\n    // Unlikely, hard to test, but can happen.\n    if !deleted_ref && !deleted_meta {\n        return Ok(None);\n    }\n","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-workspace/src/branch/remove_reference.rs#L83-L119","documentation":"remove_reference() refuses to delete a branch if it is the last named segment in the workspace: deleting it would leave the workspace with only anonymous segments, which breaks the stack model (a workspace must retain at least one named segment). The guard counts segments with ref_info before deleting the local branch.","triggerScenarios":"Calling remove_reference() to delete the only (or last remaining) named branch segment in a workspace stack - the filtered count of segments with ref_info is below 2.","commonSituations":"Users deleting the final branch of a workspace in the UI/CLI; cleanup scripts removing all stacked branches one by one and hitting the last one; abandoning a workspace via branch deletion.","solutions":["Abandon/remove the whole workspace instead of deleting its last branch","Create another named branch in the workspace before deleting the last existing one","Treat this error as a signal to call the workspace-abandon API rather than remove_reference"],"exampleFix":"// before\nremove_reference(repo, meta, last_branch_ref) // errors on last named segment\n// after\nif is_last_named_segment(meta, last_branch_ref) {\n    abandon_workspace(...)\n} else {\n    remove_reference(repo, meta, last_branch_ref)\n}","handlingStrategy":"validation","validationCode":"let named = workspace.segments.iter().filter(|s| s.ref_info.is_some()).count();\nif named < 2 && is_named_segment(ref_name) {\n    // deleting would leave an anonymous workspace; use abandon instead\n}","typeGuard":null,"tryCatchPattern":"match remove_reference(repo, meta, ref_name) {\n    Err(e) if e.to_string().contains(\"last named segment\") => abandon_workspace_instead(),\n    other => other?,\n}","preventionTips":["Count named segments before offering branch deletion in UI","Route 'delete last branch' intents to workspace abandonment","Keep at least one named branch per workspace at all times"],"tags":["git","workspace","deletion-guard"],"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"}