gitbutlerapp/gitbutler · error

Starting index state can only be fetched while in edit mode

Error message

Starting index state can only be fetched while in edit mode

What it means

starting_index_state returns the diff between the index and the initial commit captured when edit mode was entered. It only makes sense while the repository is in OperatingMode::Edit; in any other mode there is no edit-session metadata to compare against, so the function bails with this message.

Solutions

  1. Check in_edit_mode(ctx, perm) (or match operating_mode) before calling starting_index_state.
  2. Enter edit mode first via the mode-transition API, then fetch the starting index state.
  3. If a race with mode exit is expected, treat the bail as an empty/no-session result in the caller.
  4. Refresh project/mode state (reload workspace) if you believe edit mode is active but the metadata is missing.

Example fix

// before
let state = starting_index_state(&ctx, &perm)?;
// after
if !in_edit_mode(&ctx, &perm)? {
    return Ok(Vec::new()); // not in edit mode: no starting index state
}
let state = starting_index_state(&ctx, &perm)?;
Defensive patterns

Strategy: type-guard

Validate before calling

let in_edit = matches!(operating_mode(&ctx, &perm)?, OperatingMode::Edit(_));

Type guard

fn is_edit_mode(mode: &OperatingMode) -> bool {
    matches!(mode, OperatingMode::Edit(_))
}

Try / catch

match starting_index_state(&ctx, &perm) {
    Err(e) if e.to_string().contains("only be fetched while in edit mode") => Ok(Vec::new()),
    other => other,
}

Prevention

When it happens

Trigger: Calling starting_index_state when operating_mode(ctx, perm) returns anything other than OperatingMode::Edit(_), e.g. the workspace is in OpenWorkspace mode, no workspace is active, or edit mode was exited/reset before the call.

Common situations: A UI command races with the user leaving edit mode; calling the internal function from another module without first checking in_edit_mode; project freshly opened where edit-mode metadata was never written; oplog/undo restored a snapshot that left a non-edit mode active.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at crates/gitbutler-edit-mode/src/lib.rs:440

}

#[derive(Serialize, Debug, Clone)]
#[cfg_attr(feature = "export-schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct ConflictEntryPresence {
    pub ours: bool,
    pub theirs: bool,
    pub ancestor: bool,
}
#[cfg(feature = "export-schema")]
but_schemars::register_sdk_type!(ConflictEntryPresence);

pub(crate) fn starting_index_state(
    ctx: &Context,
    perm: &RepoShared,
) -> Result<Vec<(TreeChange, Option<ConflictEntryPresence>)>> {
    let OperatingMode::Edit(metadata) = operating_mode(ctx, perm)? else {
        bail!("Starting index state can only be fetched while in edit mode")
    };

    let repo = &*ctx.repo.get()?;
    let commit = repo.find_commit(metadata.commit_oid)?;
    let commit_parent_tree = if commit.is_conflicted() {
        but_core::Commit::try_from(commit.clone())?
            .tree_id_or_kind(but_core::commit::TreeKind::Base)?
            .detach()
    } else {
        commit
            .parent_ids()
            .next()
            .context("edited commit had no parent")?
            .object()?
            .try_into_commit()?
            .tree_id()?
            .detach()
    };

View on GitHub (pinned to 58e5313667)