git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 03/13] hex: introduce functions to print arbitrary hashes

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 16, 2018, 01:54 UTC
Message-ID
<xmqqo9bu4pww.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<20181015021900.1030041-4-sandals@crustytoothpaste.net>
"brian m. carlson" <sandals@crustytoothpaste.net> writes:
Show 31 quoted lines
> diff --git a/cache.h b/cache.h
> index d508f3d4f8..a13d14ce0a 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -1361,9 +1361,9 @@ extern int get_oid_hex(const char *hex, struct object_id *sha1);
>  extern int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);
>  
>  /*
> - * Convert a binary sha1 to its hex equivalent. The `_r` variant is reentrant,
> + * Convert a binary hash to its hex equivalent. The `_r` variant is reentrant,
>   * and writes the NUL-terminated output to the buffer `out`, which must be at
> - * least `GIT_SHA1_HEXSZ + 1` bytes, and returns a pointer to out for
> + * least `GIT_MAX_HEXSZ + 1` bytes, and returns a pointer to out for
>   * convenience.
>   *
>   * The non-`_r` variant returns a static buffer, but uses a ring of 4
> @@ -1371,10 +1371,13 @@ extern int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);
>   *
>   *   printf("%s -> %s", sha1_to_hex(one), sha1_to_hex(two));
>   */
> -extern char *sha1_to_hex_r(char *out, const unsigned char *sha1);
> -extern char *oid_to_hex_r(char *out, const struct object_id *oid);
> -extern char *sha1_to_hex(const unsigned char *sha1);	/* static buffer result! */
> -extern char *oid_to_hex(const struct object_id *oid);	/* same static buffer as sha1_to_hex */
> +char *hash_to_hex_algo_r(char *buffer, const unsigned char *hash, int algo);
> +char *sha1_to_hex_r(char *out, const unsigned char *sha1);
> +char *oid_to_hex_r(char *out, const struct object_id *oid);
> +char *hash_to_hex_algo(const unsigned char *hash, int algo);	/* static buffer result! */
> +char *sha1_to_hex(const unsigned char *sha1);			/* same static buffer */
> +char *hash_to_hex(const unsigned char *hash);			/* same static buffer */
> +char *oid_to_hex(const struct object_id *oid);			/* same static buffer */

Even though in hex.c I see mixture of *_algo and *_algop helper functions, I see only "algo" variants above. Is it our official stance to use primarily the integer index into the algo array when specifying the hash, and when a caller into 'multi-hash' API happens to have other things, it should use functions in 2/13 to convert it to the canonical "int algo" beforehand?

I am not saying it is bad or good to choose the index into the algo array as the primary way to specify the algorithm. I think it is a good idea to pick one and stick to it, and I wanted to make sure that the choice we made here is clearly communicated to any future developer who read this code.

Show 15 quoted lines
> +char *sha1_to_hex(const unsigned char *sha1)
> +{
> +	return hash_to_hex_algo(sha1, GIT_HASH_SHA1);
> +}
> +
> +char *hash_to_hex(const unsigned char *hash)
> +{
> +	return hash_to_hex_algo(hash, hash_algo_by_ptr(the_hash_algo));
>  }
>  
>  char *oid_to_hex(const struct object_id *oid)
>  {
> -	return sha1_to_hex(oid->hash);
> +	return hash_to_hex_algo(oid->hash, hash_algo_by_ptr(the_hash_algo));
>  }

Having said the above, seeing the use of hash_algo_by_ptr() here makes me suspect if it makes more sense to use the algop as the primary way to specify which algorithm the caller wants to use. IOW, making the set of helpers in 02/13 to allow quering by name, format-id, or the integer index and have them all return a pointer to "const struct git_hash_algo". Two immediate downsides I can see is that it exposes the actual structure to the callers (but is it really a problem? Outside callers learn hash sizes etc. by accessing its fields anyway without treating the algo struct as opaque.), and passing an 8-byte pointer may be more costly than passing a small integer index that ranges between 0 and 1 at most (assuming that we'd only use SHA-1 and "the current NewHash" in the code).

Previous: brian m. carlsonNext: brian m. carlson
Message 12 of 44 in “Base SHA-256 implementation”
  1. 00/13 Base SHA-256 implementationbrian m. carlson, Oct 15, 2018
  2. 01/13 sha1-file: rename algorithm to "sha1"brian m. carlson, Oct 15, 2018
  3. Duy NguyenOct 16, 2018
  4. brian m. carlsonOct 17, 2018
  5. 02/13 sha1-file: provide functions to look up hash algorithmsbrian m. carlson, Oct 15, 2018
  6. SZEDER GáborOct 17, 2018
  7. 04/13 cache: make hashcmp and hasheq work with larger hashesbrian m. carlson, Oct 15, 2018
  8. Duy NguyenOct 16, 2018
  9. 06/13 t: make the sha1 test-tool helper genericbrian m. carlson, Oct 15, 2018
  10. 07/13 sha1-file: add a constant for hash block sizebrian m. carlson, Oct 15, 2018
  11. 03/13 hex: introduce functions to print arbitrary hashesbrian m. carlson, Oct 15, 2018
  12. Junio C HamanoOct 16, 2018
  13. brian m. carlsonOct 17, 2018
  14. 05/13 t: add basic tests for our SHA-1 implementationbrian m. carlson, Oct 15, 2018
  15. 08/13 t/helper: add a test helper to compute hash speedbrian m. carlson, Oct 15, 2018
  16. 09/13 commit-graph: convert to using the_hash_algobrian m. carlson, Oct 15, 2018
  17. Derrick StoleeOct 15, 2018
  18. 11/13 sha256: add an SHA-256 implementation using libgcryptbrian m. carlson, Oct 15, 2018
  19. 12/13 hash: add an SHA-256 implementation using OpenSSLbrian m. carlson, Oct 15, 2018
  20. Duy NguyenOct 16, 2018
  21. 13/13 commit-graph: specify OID version for SHA-256brian m. carlson, Oct 15, 2018
  22. Derrick StoleeOct 15, 2018
  23. Junio C HamanoOct 16, 2018
  24. brian m. carlsonOct 16, 2018
  25. Duy NguyenOct 16, 2018
  26. Derrick StoleeOct 16, 2018
  27. Duy NguyenOct 16, 2018
  28. brian m. carlsonOct 16, 2018
  29. Duy NguyenOct 17, 2018
  30. brian m. carlsonOct 18, 2018
  31. Derrick StoleeOct 18, 2018
  32. brian m. carlsonOct 19, 2018
  33. Derrick StoleeOct 17, 2018
  34. brian m. carlsonOct 17, 2018
  35. 10/13 Add a base implementation of SHA-256 supportbrian m. carlson, Oct 15, 2018
  36. Duy NguyenOct 15, 2018
  37. brian m. carlsonOct 15, 2018
  38. Duy NguyenOct 16, 2018
  39. SZEDER GáborOct 17, 2018
  40. brian m. carlsonOct 17, 2018
  41. Junio C HamanoOct 16, 2018
  42. Junio C HamanoOct 16, 2018
  43. brian m. carlsonOct 16, 2018
  44. Duy NguyenOct 16, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.