gitbutlerapp/gitbutler · error

Failed to find corresponding segment for

Error message

Failed to find corresponding segment for {ref_name}

What it means

When building a graph rebase editor, every ref used as a mutable entrypoint (workspace/stack refs and worktree tips) must map to a segment in the workspace commit graph. If `segment_and_commit_by_ref_name` returns None for a ref, the graph is inconsistent with the refs and creation aborts.

Solutions

  1. Refresh the workspace/commit graph snapshot and retry create_with_opts
  2. Filter out tips whose ref_name is absent from the graph before calling
  3. Verify each ref still resolves (`git rev-parse <ref>`) and belongs to the workspace graph

Example fix

// before
let tips: Vec<_> = worktree_tips.iter().filter_map(|t| t.ref_name.clone()).collect();
create_with_opts(workspace, &tips, ...)?;
// after
let tips: Vec<_> = worktree_tips.iter()
    .filter_map(|t| t.ref_name.clone())
    .filter(|r| workspace.graph.segment_and_commit_by_ref_name(r.as_bstr()).is_some())
    .collect();
Defensive patterns

Strategy: validation

Validate before calling

for r in &ref_names {
    anyhow::ensure!(
        workspace.graph.segment_and_commit_by_ref_name(r.as_bstr()).is_some(),
        "ref {} missing from workspace graph", r
    );
}

Try / catch

match create_with_opts(&workspace, &tips, opts) {
    Ok(ed) => ed,
    Err(e) if e.to_string().contains("corresponding segment") => {
        let ws = but_workspace::refresh(&repo)?;
        create_with_opts(&ws, &tips, opts)?
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling `create_with_opts` when one of the supplied worktree tip ref_names (or workspace refs) is not present in `workspace.graph` — e.g. the ref was deleted, points outside the computed graph, or the workspace snapshot is stale.

Common situations: Workspace computed before a branch was deleted or force-pushed; passing ref names from a different repo; graph built with a filter that excludes the ref's commits; racing concurrent git operations.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at crates/but-rebase/src/graph_rebase/creation.rs:80

        // TODO(CTO): Look into traversing "in workspace" segments that are not
        // reachable from the entrypoint TODO(CTO): Look into stopping at the
        // common base
        let worktree_tips = workspace.graph.worktree_tips.clone();
        let entrypoint = workspace.graph.entrypoint()?;

        let mut mutable_entrypoints = vec![entrypoint.segment.id];

        for ref_name in options
            .extra_mutable_refs
            .iter()
            .chain(worktree_tips.iter().filter_map(|tip| tip.ref_name.as_ref()))
        {
            let Some((segment, _)) = workspace
                .graph
                .segment_and_commit_by_ref_name(ref_name.as_ref())
            else {
                bail!("Failed to find corresponding segment for {ref_name}");
            };
            mutable_entrypoints.push(segment.id);
        }

        // Segments reachable from a mutable entrypoint (following parent edges)
        // may be rewritten. Every other segment is still included in the
        // editor, but as immutable.
        let mut mutable_segments = HashSet::new();
        for entrypoint in mutable_entrypoints {
            workspace.graph.visit_all_segments_including_start_until(
                entrypoint,
                Direction::Outgoing,
                |segment| !mutable_segments.insert(segment.id),
            );
        }

        // The editor contains every commit the graph contains, so we iterate
        // over all segments rather than only those reachable from an entrypoint.

View on GitHub (pinned to 58e5313667)