gitbutlerapp/gitbutler · error

Cannot unapply anonymous segments yet even if they have a…

Error message

Cannot unapply anonymous segments yet even if they have a stack id

What it means

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.

Solutions

  1. Name the stack/branch first (assign a ref to the first segment) before unapplying
  2. Unapply via a different flow that supports anonymous segments (workspace-level operations)
  3. Check stack segments before calling and skip stacks whose first segment lacks a ref name

Example fix

// before
await unapplyStack(stackId);
// after
if (stack.segments[0]?.refName) {
  await unapplyStack(stackId);
} else {
  await nameStack(stackId, branchName); // then unapply
}
Defensive patterns

Strategy: validation

Validate before calling

const stack = await getStack(projectId, stackId);
if (!stack.segments[0]?.refName) {
  await nameStackBranch(projectId, stackId, 'my-branch');
}
await unapplyStack(projectId, stackId);

Type guard

function hasNamedFirstSegment(stack) {
  return Boolean(stack.segments?.[0]?.refName);
}

Try / catch

try {
  await unapplyStack(projectId, stackId);
} catch (e) {
  if (String(e).includes('anonymous segments')) {
    await assignBranchName(projectId, stackId);
    await unapplyStack(projectId, stackId);
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18). Data as JSON: /api/errors/3bcfcf2f7414a024. Report an issue: GitHub.

Appendix: source

Thrown at crates/but-api/src/legacy/virtual_branches.rs:604

/// workspace: tip-most branch first, then branches closer to the workspace base.
fn stack_branch_names(
    ctx: &mut Context,
    stack_id: StackId,
    perm: &RepoShared,
) -> Result<Vec<gix::refs::FullName>> {
    let (_repo, ws, _) = ctx.workspace_and_db_with_perm(perm)?;
    let Some(stack) = ws.stacks.iter().find(|stack| stack.id == Some(stack_id)) else {
        return Err(
            anyhow!("branch with ID {stack_id} not found").context(but_error::Code::BranchNotFound)
        );
    };

    if stack
        .segments
        .first()
        .is_none_or(|s| s.ref_name().is_none())
    {
        bail!("Cannot unapply anonymous segments yet even if they have a stack id");
    }

    Ok(stack
        .segments
        .iter()
        .filter_map(|segment| {
            segment
                .ref_info
                .as_ref()
                .map(|ref_info| ref_info.ref_name.clone())
        })
        .collect())
}

/// Commit assigned worktree changes so they remain with the branch being unapplied.
///
/// `ctx` supplies repository, workspace, metadata, and diff settings used to
/// create the assignment commit. `branch` is the reference the new "WIP

View on GitHub (pinned to 58e5313667)