{"record":{"id":"3bcfcf2f7414a024","repo":"gitbutlerapp/gitbutler","slug":"cannot-unapply-anonymous-segments-yet-even-if-they","errorCode":null,"errorMessage":"Cannot unapply anonymous segments yet even if they have a stack id","messagePattern":"Cannot unapply anonymous segments yet even if they have a stack id","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-api/src/legacy/virtual_branches.rs","lineNumber":604,"sourceCode":"/// workspace: tip-most branch first, then branches closer to the workspace base.\nfn stack_branch_names(\n    ctx: &mut Context,\n    stack_id: StackId,\n    perm: &RepoShared,\n) -> Result<Vec<gix::refs::FullName>> {\n    let (_repo, ws, _) = ctx.workspace_and_db_with_perm(perm)?;\n    let Some(stack) = ws.stacks.iter().find(|stack| stack.id == Some(stack_id)) else {\n        return Err(\n            anyhow!(\"branch with ID {stack_id} not found\").context(but_error::Code::BranchNotFound)\n        );\n    };\n\n    if stack\n        .segments\n        .first()\n        .is_none_or(|s| s.ref_name().is_none())\n    {\n        bail!(\"Cannot unapply anonymous segments yet even if they have a stack id\");\n    }\n\n    Ok(stack\n        .segments\n        .iter()\n        .filter_map(|segment| {\n            segment\n                .ref_info\n                .as_ref()\n                .map(|ref_info| ref_info.ref_name.clone())\n        })\n        .collect())\n}\n\n/// Commit assigned worktree changes so they remain with the branch being unapplied.\n///\n/// `ctx` supplies repository, workspace, metadata, and diff settings used to\n/// create the assignment commit. `branch` is the reference the new \"WIP","sourceCodeStart":586,"sourceCodeEnd":622,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-api/src/legacy/virtual_branches.rs#L586-L622","documentation":"unapply_stack_v3 requires the stack's first segment to have a ref name (i.e. be an applied, named branch). If the first segment is anonymous (no ref_name), the code cannot derive branch names to unapply and bails, noting anonymous segments are not yet supported even when they carry a stack id.","triggerScenarios":"Calling unapply_stack_v3_with_perm on a workspace stack whose first segment is anonymous (no branch ref), e.g. an unnamed/uncommitted-changes segment at the head of a lane.","commonSituations":"Unapplying a freshly created stack that was never assigned a branch name; workspace containing default/anonymous segments created by internal operations; invoking unapply on the workspace's implicit first lane.","solutions":["Name the stack/branch first (assign a ref to the first segment) before unapplying","Unapply via a different flow that supports anonymous segments (workspace-level operations)","Check stack segments before calling and skip stacks whose first segment lacks a ref name"],"exampleFix":"// before\nawait unapplyStack(stackId);\n// after\nif (stack.segments[0]?.refName) {\n  await unapplyStack(stackId);\n} else {\n  await nameStack(stackId, branchName); // then unapply\n}","handlingStrategy":"validation","validationCode":"const stack = await getStack(projectId, stackId);\nif (!stack.segments[0]?.refName) {\n  await nameStackBranch(projectId, stackId, 'my-branch');\n}\nawait unapplyStack(projectId, stackId);","typeGuard":"function hasNamedFirstSegment(stack) {\n  return Boolean(stack.segments?.[0]?.refName);\n}","tryCatchPattern":"try {\n  await unapplyStack(projectId, stackId);\n} catch (e) {\n  if (String(e).includes('anonymous segments')) {\n    await assignBranchName(projectId, stackId);\n    await unapplyStack(projectId, stackId);\n  } else throw e;\n}","preventionTips":["Give every stack a named branch before workspace operations","Skip stacks whose first segment is anonymous when batch-unapplying","Check segment refName presence before calling unapply APIs"],"tags":["rust","workspace","virtual-branches"],"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"}