{"record":{"id":"0cd3300ce01a7c5a","repo":"gitbutlerapp/gitbutler","slug":"missing-reference-segment-to-insert-above-which","errorCode":null,"errorMessage":"Missing reference {segment_to_insert_above} which should be known to workspace metadata to serve as insertion position for {rn}","messagePattern":"Missing reference (.+?) which should be known to workspace metadata to serve as insertion position for (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-workspace/src/branch/apply.rs","lineNumber":682,"sourceCode":"                .find_map(|segment| {\n                    segment\n                        .commits\n                        .iter()\n                        .flat_map(|c| c.ref_iter())\n                        .find_map(|ambiguous_rn| {\n                            (ambiguous_rn == rn.as_ref())\n                                .then_some(segment.ref_name())\n                                .flatten()\n                        })\n                })\n            {\n                match ws_mut.insert_new_segment_above_anchor_if_not_present(\n                    rn.as_ref(),\n                    segment_to_insert_above,\n                ) {\n                    None => {\n                        // For now bail, until we know it's worth fixing this case automatically.\n                        bail!(\n                            \"Missing reference {segment_to_insert_above} which should be known to workspace metadata to serve as insertion position for {rn}\"\n                        );\n                    }\n                    Some(false) => {\n                        // The branch already existed, probably as stack, but it didn't come through. Remove it and use the anchor.\n                        ws_mut.remove_segment(rn.as_ref());\n                        if ws_mut.insert_new_segment_above_anchor_if_not_present(\n                            rn.as_ref(),\n                            segment_to_insert_above,\n                        ) != Some(true)\n                        {\n                            bail!(\n                                \"Failed to assure that {rn} is in the workspace as dependent branch after removing it\"\n                            );\n                        }\n                    }\n                    Some(true) => {}\n                }","sourceCodeStart":664,"sourceCodeEnd":700,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-workspace/src/branch/apply.rs#L664-L700","documentation":"During apply, when inserting an incoming branch as a new stack segment, the code anchors it above an existing reference (`segment_to_insert_above`). If the workspace-metadata insertion helper returns `None` — meaning that anchor reference is not present in the metadata — apply bails, because it cannot place the branch correctly. This is treated as a bug/unsupported state for now.","triggerScenarios":"Calling apply on an adhoc workspace where the computed anchor reference (insertion position) for a branch is missing from workspace metadata — e.g. metadata is stale or the anchor branch was dropped during the merge re-computation.","commonSituations":"Metadata out of sync with the repository after manual `git` operations or a concurrent modification; applying into an adhoc workspace whose reference layout changed between computation and insertion.","solutions":["Refresh/rebuild workspace metadata so the anchor reference is present, then retry apply","Apply the branch with the workspace metadata in a consistent state (close other sessions writing to it)","Report as a bug — the comment in source says this is bailed until automatic handling is worth implementing"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// ensure the anchor exists in workspace metadata\nif !ws.segments().any(|s| s.ref_name() == anchor) {\n    return Err(\"anchor missing from workspace metadata; refresh workspace first\");\n}","typeGuard":null,"tryCatchPattern":"match apply(...) {\n    Err(e) if e.to_string().contains(\"Missing reference\") => { refresh_metadata(); retry_once(); }\n    Err(e) => return Err(e.into()),\n    Ok(r) => r,\n}","preventionTips":["Keep workspace metadata in sync; avoid manual git mutations behind the tool's back","Retry apply after a metadata refresh when adhoc workspaces are involved","Treat this as a bug report trigger if it reproduces consistently"],"tags":["git","workspace","invariant"],"backgroundTag":"internal-invariant-violation","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"}