gitbutlerapp/gitbutler · error

cannot snapshot a null target commit id

Error message

cannot snapshot a null target commit id

What it means

When recording an oplog snapshot, SnapshotProjectMeta is built from project metadata. The project's target commit id is mandatory for a snapshot; if target_commit_id_or_err() returns a null ObjectId (all zeros), the snapshot would be meaningless, so try_from bails. This protects the undo/timeline from recording snapshots pointing at nothing.

Solutions

  1. Ensure the project's target branch exists and target_commit_id is set before triggering snapshot-recording operations.
  2. Re-assign the workspace/target (re-initialize the project) to repair the null target commit id.
  3. Skip oplog snapshotting (or use *_only* variants) for operations run before target initialization.
  4. Inspect the project metadata storage to see why the target id is null and fix the source of the bad value.

Example fix

// before
let snap = snapshot_project_meta(&ctx)?; // bails if target is null
// after
let meta = project_meta(&ctx)?;
if meta.target_commit_id_or_err()?.is_null() {
    return Ok(None); // no snapshot possible before target init
}
let snap = Some(SnapshotProjectMeta::try_from(&meta)?);
Defensive patterns

Strategy: validation

Validate before calling

let target = project_meta(&ctx)?.target_commit_id_or_err()?;
if target.is_null() { /* skip snapshot or initialize target first */ }

Type guard

fn has_valid_target(meta: &ProjectMeta) -> bool {
    meta.target_commit_id_or_err().map(|id| !id.is_null()).unwrap_or(false)
}

Try / catch

match SnapshotProjectMeta::try_from(&meta) {
    Err(e) if e.to_string().contains("null target commit id") => return Ok(None), // skip snapshot
    other => other?,
}

Prevention

When it happens

Trigger: Creating any oplog snapshot (SnapshotDetails via the snapshot wrapper pattern) for a project whose ProjectMeta has a null target_commit_id - e.g. before the target branch was ever resolved/created, or metadata persisted in a broken state.

Common situations: A freshly initialized project whose default target hasn't been pushed/created yet; corrupted or hand-edited project metadata; a migration/version mismatch leaving target_commit_id unset; tests constructing ProjectMeta manually without a target.

Related errors


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

Appendix: source

Thrown at crates/gitbutler-oplog/src/oplog.rs:77

    virtual_branches: VirtualBranches,
}

#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct SnapshotTarget {
    branch_name: String,
    remote_name: String,
    sha: String,
    push_remote_name: Option<String>,
}

impl TryFrom<&ProjectMeta> for SnapshotProjectMeta {
    type Error = anyhow::Error;

    fn try_from(meta: &ProjectMeta) -> Result<Self> {
        let target_commit_id = meta.target_commit_id_or_err()?;
        if target_commit_id.is_null() {
            bail!("cannot snapshot a null target commit id");
        }
        Ok(Self {
            target_ref: meta.target_ref.as_ref().map(ToString::to_string),
            target_commit_id: target_commit_id.to_string(),
            push_remote: meta.push_remote.clone(),
        })
    }
}

impl TryFrom<SnapshotProjectMeta> for ProjectMeta {
    type Error = anyhow::Error;

    fn try_from(meta: SnapshotProjectMeta) -> Result<Self> {
        let target_ref = meta
            .target_ref
            .map(|name| {
                let name: gix::refs::FullName = name
                    .try_into()

View on GitHub (pinned to 58e5313667)