GitoxideLabs/gitoxide · error

Tried to use as blob, but was

Error message

Tried to use {} as blob, but was {}

What it means

`gix::object::Object::into_blob()` converts a generic object handle into a `Blob`, panicking if the object is actually a tree, commit, or tag. The panic includes the object's id and kind ('Tried to use {id} as blob, but was {kind}') because the caller requested a kind conversion that does not match the underlying object.

Solutions

  1. Check `object.kind == gix_object::Kind::Blob` before calling `into_blob()`
  2. Use `object.try_into_blob()?` and propagate the error instead of panicking
  3. Match on `object.kind` and dispatch to `into_commit()`/`into_tree()`/`into_tag()` as appropriate
  4. Verify the source of the id actually refers to a blob (e.g. a tree entry with blob mode)

Example fix

// before
let blob = repo.find_object(entry_id)?.into_blob(); // panics if tree/commit
// after
let object = repo.find_object(entry_id)?;
let blob = object.try_into_blob().map_err(|o| anyhow::anyhow!("{} is a {:?}, not a blob", o.id, o.kind))?;
Defensive patterns

Strategy: type-guard

Validate before calling

if object.kind != gix_object::Kind::Blob {
    return Err(anyhow::anyhow!("expected blob, got {:?}", object.kind));
}
let blob = object.into_blob();

Type guard

fn as_blob<'r>(o: gix::Object<'r>) -> Option<gix::objs::Blob<'r>> {
    o.try_into().ok()
}

Prevention

When it happens

Trigger: Calling `repo.find_object(id)?.into_blob()` where `id` points at a commit, tree, or tag; assuming a lookup result is a blob without checking `object.kind`; walking data (e.g. tree entries, rev-list output) and unconditionally calling `into_blob()`.

Common situations: Reading file contents from a tree entry whose id is a submodule (commit) or tree; passing a commit id from a ref into blob-reading code; scripts iterating mixed object kinds.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of GitoxideLabs/gitoxide@e73179060b (2026-09-08). Data as JSON: /api/errors/2ca673b1589332d5. Report an issue: GitHub.

Appendix: source

Thrown at gix/src/object/mod.rs:80

    pub(crate) fn from_data(
        id: impl Into<ObjectId>,
        kind: Kind,
        data: Vec<u8>,
        repo: &'repo crate::Repository,
    ) -> Self {
        Object {
            id: id.into(),
            kind,
            data,
            repo,
        }
    }

    /// Transform this object into a blob, or panic if it is none.
    pub fn into_blob(self) -> Blob<'repo> {
        match self.try_into() {
            Ok(blob) => blob,
            Err(this) => panic!("Tried to use {} as blob, but was {}", this.id, this.kind),
        }
    }

    /// Transform this object into a tree, or panic if it is none.
    pub fn into_tree(self) -> Tree<'repo> {
        match self.try_into() {
            Ok(tree) => tree,
            Err(this) => panic!("Tried to use {} as tree, but was {}", this.id, this.kind),
        }
    }

    /// Transform this object into a commit, or panic if it is none.
    pub fn into_commit(self) -> Commit<'repo> {
        match self.try_into() {
            Ok(commit) => commit,
            Err(this) => panic!("Tried to use {} as commit, but was {}", this.id, this.kind),
        }
    }

View on GitHub (pinned to e73179060b)