From 3a95abcfb29f9e8b6ca5f4188c2dad65d21d4626 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 14 Sep 2025 02:44:38 +0800 Subject: [PATCH] Just use one SHA-256 oid type --- git/oid.ha | 36 ++++++++++++------------------------ diff --git a/git/oid.ha b/git/oid.ha index 08010430266a3a8e0e5b36f095216df6f1a05ef9..3a6f98eab5e9d1c7c63bde5d3ce71e1bdf01983e 100644 --- a/git/oid.ha +++ b/git/oid.ha @@ -1,35 +1,23 @@ -use crypto::sha1; use crypto::sha256; use encoding::hex; use errors; use io; -export type oid = (oid_sha1 | oid_sha256); -export type oid_sha1 = [sha1::SZ]u8; -export type oid_sha256 = [sha256::SZ]u8; +export type oid = [sha256::SZ]u8; -// oid_from_str converts a hex string to an oid. -export fn oid_from_str(s: const str) (oid | nomem | errors::invalid) = { - const d = match (hex::decodestr(s)) { - case let u: []u8 => yield u; - case nomem => return nomem; - case errors::invalid => return errors::invalid; - case => abort("unreachable"); - }; +// Parses a hex-encoded string representation of an [[oid]]. +export fn parse_oid(s: const str) (oid | nomem | errors::invalid) = { + const d = hex::decodestr(s)?; defer free(d); - let o: oid = switch (len(s)) { - case sha1::SZ => - let p: oid_sha1 = [0...]; - p[..] = d[..]; - yield p; - case sha256::SZ => - let p: oid_sha256 = [0...]; - p[..] = d[..]; - yield p; - case => - return errors::invalid; - }; + let o: oid; + o[..] = d[..]; return o; }; + +// Returns a hex-encoded string representation of the given [[oid]]. +// The returned string is allocated on the heap and must be freed by the caller. +export fn oid_string(id: oid) const str { + return hex::encodestr(id); +} -- 2.48.1