Re: [RFC PATCH 02/22] hex: add functions to parse hex object IDs in any algorithm
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Jan 15, 2020, 21:40 UTC
- Message-ID
- <xmqqv9pciejd.fsf@gitster-ct.c.googlers.com>
- In-Reply-To
- <20200113124729.3684846-3-sandals@crustytoothpaste.net>
"brian m. carlson" <sandals@crustytoothpaste.net> writes:
Show 13 quoted lines
> +/*
> + * NOTE: This function relies on hash algorithms being in order from shortest
> + * length to longest length.
> + */
> +int get_oid_hex_any(const char *hex, struct object_id *oid)
> +{
> + int i;
> + for (i = GIT_HASH_NALGOS - 1; i > 0; i--) {
> + if (!get_hash_hex_algop(hex, oid->hash, &hash_algos[i]))
> + return i;
> + }
> + return GIT_HASH_UNKNOWN;
> +}Two rather obvious questions are
- what if we have more than one algos that produce hashes of the same length?
- it feels that GIT_HASH_UNKNOWN being 0 wastes the first/zeroth element in the hash_algos[] array.
In the future, I would imagine that we would want to be able to say "here I have a dozen hexdigits that is an abbreviated SHA2 hash", and we would use some syntax (e.g. "sha2:123456123456") for that. Would this function be at the layer that would be extended later to support such a syntax, or would we have a layer higher than this to do so?
Show 18 quoted lines
> int get_oid_hex(const char *hex, struct object_id *oid)
> {
> return get_oid_hex_algop(hex, oid, the_hash_algo);
> @@ -87,6 +101,14 @@ int parse_oid_hex_algop(const char *hex, struct object_id *oid,
> return ret;
> }
>
> +int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end)
> +{
> + int ret = get_oid_hex_any(hex, oid);
> + if (ret)
> + *end = hex + hash_algos[ret].hexsz;
> + return ret;
> +}
> +
> int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
> {
> return parse_oid_hex_algop(hex, oid, end, the_hash_algo);