GitoxideLabs/gitoxide · error

Tried to use as commit, but was

Error message

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

What it means

This panic comes from `Object::into_commit()` in gix, the panic-based conversion of a generic `Object` handle into a `Commit`. The library throws it because the object is not a commit (its kind was Blob, Tree, or Tag). It mirrors the fallible `try_into_commit()` and its panic indicates the caller assumed the wrong object type.

Solutions

  1. Guard with `object.kind == gix::object::Kind::Commit` before calling `into_commit()`.
  2. Use `try_into_commit()` instead and handle the `Err`, which gives back the original object.
  3. If the id may point at an annotated tag, peel first: `object.peel_to_kind(gix::object::Kind::Commit)`.
  4. Confirm the id is a commit with `git cat-file -t <id>` before porting the logic.

Example fix

// before
let commit = repo.find_object(id)?.into_commit();

// after
let object = repo.find_object(id)?;
let commit = object.peel_to_kind(gix::object::Kind::Commit)?
    .try_into_commit().expect("peeled to commit");
Defensive patterns

Strategy: type-guard

Validate before calling

let object = repo.find_object(commit_id)?;
if object.kind != gix::object::Kind::Commit {
    return Err(anyhow::anyhow!("id {} is {:?}, not a commit", commit_id, object.kind));
}

Type guard

fn as_commit(object: gix::Object<'_>) -> Option<gix::Commit<'_>> {
    (object.kind == gix::object::Kind::Commit).then(|| object.try_into_commit().expect("kind checked"))
}

Try / catch

let commit = match object.try_into_commit() {
    Ok(commit) => commit,
    Err(obj) => return Err(anyhow::anyhow!("object {} was {:?}, not a commit", obj.id, obj.kind)),
};

Prevention

When it happens

Trigger: Calling `object.into_commit()` on an object loaded from an id that resolves to a blob, tree, or tag. Common with `repo.find_object(tree_id)?.into_commit()` or converting objects obtained from a loose iteration over mixed object kinds.

Common situations: Passing a tree or blob id where a commit was expected; handling a tag object without peeling it to the underlying commit; reading objects from pack/loose scans and assuming every entry is a commit.

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

Appendix: source

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

        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),
        }
    }

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

    /// Transform this object into a commit, or return it as part of the `Err` if it is no commit.
    pub fn try_into_commit(self) -> Result<Commit<'repo>, try_into::Error> {
        self.try_into().map_err(|this: Self| try_into::Error {
            id: this.id,
            actual: this.kind,
            expected: gix_object::Kind::Commit,
        })

View on GitHub (pinned to e73179060b)