GitoxideLabs/gitoxide · error

Tried to use as tag, but was

Error message

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

What it means

This panic comes from `Object::into_tag()` in gix, the panic-based conversion of a generic `Object` handle into a `Tag`. The library throws it because the object is not an annotated tag (its kind was Blob, Tree, or Commit). The fallible counterpart is `try_into_tag()`, so the panic signals a caller assumption that the id denotes a tag object.

Solutions

  1. Check `object.kind == gix::object::Kind::Tag` before calling `into_tag()`.
  2. Use `try_into_tag()` and handle the `Err` case to get the original object back instead of a panic.
  3. For lightweight tags, treat the target object by its own kind rather than assuming a tag object exists.
  4. Verify with `git cat-file -t <id>` that the id is of type `tag`.

Example fix

// before
let tag = repo.find_object(id)?.into_tag();

// after
let object = repo.find_object(id)?;
if object.kind == gix::object::Kind::Tag {
    let tag = object.try_into_tag().expect("kind checked as tag");
} else {
    // lightweight tag: object is the target itself
}
Defensive patterns

Strategy: type-guard

Validate before calling

let object = repo.find_object(tag_id)?;
if object.kind != gix::object::Kind::Tag {
    return Err(anyhow::anyhow!("id {} is {:?}, not an annotated tag", tag_id, object.kind));
}

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling `object.into_tag()` on an id that resolves to a blob, tree, or lightweight-commit reference. Happens when dereferencing refs without checking whether they point to annotated tag objects.

Common situations: Loading the object a `refs/tags/*` ref points at when the tag is lightweight (pointing straight at a commit); iterating repository objects and converting each to a tag; confusing tag names with tag objects.

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

Appendix: source

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

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

    /// Transform this object into a tag, or return it as part of the `Err` if it is no commit.
    pub fn try_into_tag(self) -> Result<Tag<'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)