gitbutlerapp/gitbutler · error

Cannot unapply branch

Error message

Cannot unapply branch '{branch}' from an ad-hoc workspace because the workspace cannot be empty

What it means

`unapply` refuses to remove the workspace's own reference branch from an ad-hoc workspace, because an ad-hoc (unnamed) workspace would become empty and cease to exist. The library requires a workspace to always retain at least its workspace branch.

Solutions

  1. Do not call unapply on the workspace reference branch itself; skip it in your loop
  2. Convert to a named/managed workspace first if you need to restructure it completely
  3. Unapply a different (non-workspace) branch instead

Example fix

// before
for branch in branches { unapply(repo, meta, &ws, branch, ...)?; }
// after
for branch in branches {
    if branch == workspace_ref_name { continue; }
    unapply(repo, meta, &ws, branch, ...)?;
}
Defensive patterns

Strategy: validation

Validate before calling

fn can_unapply(ws: &Workspace, branch: &gix::refs::FullNameRef) -> bool {
    !(matches!(ws.kind, but_graph::workspace::WorkspaceKind::AdHoc)
        && ws.ref_name().map(ToOwned::to_owned).as_ref() == Some(&branch.to_owned()))
}

Prevention

When it happens

Trigger: Calling `unapply` with `branch` equal to the workspace reference name while `ws.kind` is `WorkspaceKind::AdHoc` (and the branch is not a managed ref).

Common situations: Programmatically iterating all segments of an ad-hoc workspace and attempting to unapply each one, accidentally including the workspace branch itself.

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/6346b694659912b9. Report an issue: GitHub.

Appendix: source

Thrown at crates/but-workspace/src/branch/unapply.rs:217

        let branch_commit_id = branch_ref
            .as_mut()
            .map(|reference| reference.peel_to_id().map(|id| id.detach()))
            .transpose()?;

        if ws.has_workspace_commit_in_ancestry(repo) {
            bail!("Refusing to work on workspace whose workspace commit isn't at the top");
        }

        let workspace_ref_name = ws.ref_name().map(ToOwned::to_owned);
        if matches!(ws.kind, but_graph::workspace::WorkspaceKind::AdHoc)
            && branch_ref
                .as_ref()
                .zip(workspace_ref_name.as_ref())
                .is_some_and(|(to_unapply, workspace_ref_name)| {
                    to_unapply.name() == workspace_ref_name.as_ref()
                })
        {
            bail!(
                "Cannot unapply branch '{branch}' from an ad-hoc workspace because the workspace cannot be empty",
                branch = branch.shorten()
            );
        }

        if let Some(workspace_ref_name) = workspace_ref_name.as_ref()
            && workspace_ref_name == branch
        {
            return unapply_workspace_reference(
                ws,
                repo,
                meta,
                workspace_ref_name.as_ref(),
                workspace_disposition,
            );
        }

        let branch_in_ws = ws.find_segment_and_stack_by_refname(branch);

View on GitHub (pinned to 58e5313667)