zed-industries/zed · error · anyhow::Error

invalid hex digit at byte {index} for git oid

Error message

invalid hex digit at byte {index} for git oid

What it means

Oid::from_str found a non-hexadecimal character at byte {index} while decoding a git object id. The length check already passed (SHA-1 or SHA-256 sized), but byte-level decoding hit a character outside [0-9a-f], so the string is not a valid object id — usually a typo, truncated-with-garbage sha, or an oid contaminated with formatting characters.

Source

Thrown at crates/git/src/git.rs:299

impl FromStr for Oid {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        let format = match s.len() {
            1..=SHA1_HEX_LENGTH => OidFormat::Sha1,
            SHA256_HEX_LENGTH => OidFormat::Sha256,
            len => {
                anyhow::bail!(
                    "invalid git oid hex length: expected 1..={SHA1_HEX_LENGTH} for SHA-1 or {SHA256_HEX_LENGTH} for SHA-256, got {len}"
                );
            }
        };

        let mut bytes = [0u8; SHA256_BYTE_LENGTH];
        for (index, byte) in s.bytes().enumerate() {
            let digit = decode_hex_digit(byte)
                .ok_or_else(|| anyhow::anyhow!("invalid hex digit at byte {index} for git oid"))?;
            if index % 2 == 0 {
                bytes[index / 2] = digit << 4;
            } else {
                bytes[index / 2] |= digit;
            }
        }

        Ok(Self { bytes, format })
    }
}

fn decode_hex_digit(byte: u8) -> Option<u8> {
    match byte {
        b'0'..=b'9' => Some(byte - b'0'),
        b'a'..=b'f' => Some(byte - b'a' + 10),
        b'A'..=b'F' => Some(byte - b'A' + 10),
        _ => None,
    }

View on GitHub (pinned to f4178619ac)

Solutions

  1. Strip whitespace, quotes, or 'origin/' style prefixes before parsing sha strings
  2. Validate shas against ^[0-9a-fA-F]+$ at the boundary that produced them
  3. Report the byte index to the user — it pinpoints where the garbage begins
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/git/src/git.rs:299 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of zed-industries/zed@f4178619ac (2026-08-20). Data as JSON: /api/errors/cc7f6d4c75f8e36a. Report an issue: GitHub.