From 8e4ebbdfca16dacc2763e852afbdd94708186eb0 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Fri, 14 Mar 2025 09:34:02 +0800 Subject: [PATCH] Add oid_from_str --- git/oid.ha | 25 +++++++++++++++++++++++++ diff --git a/git/oid.ha b/git/oid.ha index 0e1688f70279d812307e1be3994c71d25578a45e..ae81198758a4674798778a5714f88755314ae2fb 100644 --- a/git/oid.ha +++ b/git/oid.ha @@ -1,6 +1,31 @@ use crypto::sha1; use crypto::sha256; +use encoding::hex; +use io; + +// Invalid hash length +export type hashlen = !void; export type oid = (oid_sha1 | oid_sha256); export type oid_sha1 = [sha1::SZ]u8; export type oid_sha256 = [sha256::SZ]u8; + +export fn oid_from_str(s: const str) (oid | io::error | hashlen) = { + 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 hashlen; + }; + + return o; +}; -- 2.48.1