git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "brian m. carlson" <sandals@crustytoothpaste.net>
Cc: git@vger.kernel.org, "Jeff King" <peff@peff.net>,
	"Derrick Stolee" <stolee@gmail.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: Re: [PATCH v2 03/13] hex: introduce functions to print arbitrary hashes
Date: Tue, 16 Oct 2018 10:54:23 +0900	[thread overview]
Message-ID: <xmqqo9bu4pww.fsf@gitster-ct.c.googlers.com> (raw)
In-Reply-To: <20181015021900.1030041-4-sandals@crustytoothpaste.net> (brian m. carlson's message of "Mon, 15 Oct 2018 02:18:50 +0000")

"brian m. carlson" <sandals@crustytoothpaste.net> writes:

> 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.

> +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).



  reply	other threads:[~2018-10-16  1:54 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-15  2:18 [PATCH v2 00/13] Base SHA-256 implementation brian m. carlson
2018-10-15  2:18 ` [PATCH v2 01/13] sha1-file: rename algorithm to "sha1" brian m. carlson
2018-10-16 15:17   ` Duy Nguyen
2018-10-17 22:53     ` brian m. carlson
2018-10-15  2:18 ` [PATCH v2 02/13] sha1-file: provide functions to look up hash algorithms brian m. carlson
2018-10-17 13:32   ` SZEDER Gábor
2018-10-15  2:18 ` [PATCH v2 03/13] hex: introduce functions to print arbitrary hashes brian m. carlson
2018-10-16  1:54   ` Junio C Hamano [this message]
2018-10-17 23:49     ` brian m. carlson
2018-10-15  2:18 ` [PATCH v2 04/13] cache: make hashcmp and hasheq work with larger hashes brian m. carlson
2018-10-16 15:44   ` Duy Nguyen
2018-10-15  2:18 ` [PATCH v2 05/13] t: add basic tests for our SHA-1 implementation brian m. carlson
2018-10-15  2:18 ` [PATCH v2 06/13] t: make the sha1 test-tool helper generic brian m. carlson
2018-10-15  2:18 ` [PATCH v2 07/13] sha1-file: add a constant for hash block size brian m. carlson
2018-10-15  2:18 ` [PATCH v2 08/13] t/helper: add a test helper to compute hash speed brian m. carlson
2018-10-15  2:18 ` [PATCH v2 09/13] commit-graph: convert to using the_hash_algo brian m. carlson
2018-10-15 15:10   ` Derrick Stolee
2018-10-15  2:18 ` [PATCH v2 10/13] Add a base implementation of SHA-256 support brian m. carlson
2018-10-15 14:59   ` Duy Nguyen
2018-10-15 23:30     ` brian m. carlson
2018-10-16 14:59       ` Duy Nguyen
2018-10-17 16:12   ` SZEDER Gábor
2018-10-17 23:04     ` brian m. carlson
2018-10-15  2:18 ` [PATCH v2 11/13] sha256: add an SHA-256 implementation using libgcrypt brian m. carlson
2018-10-15  2:18 ` [PATCH v2 12/13] hash: add an SHA-256 implementation using OpenSSL brian m. carlson
2018-10-16 15:36   ` Duy Nguyen
2018-10-15  2:19 ` [PATCH v2 13/13] commit-graph: specify OID version for SHA-256 brian m. carlson
2018-10-15 15:11   ` Derrick Stolee
2018-10-16  2:00   ` Junio C Hamano
2018-10-16 22:39     ` brian m. carlson
2018-10-16 15:35   ` Duy Nguyen
2018-10-16 16:01     ` Derrick Stolee
2018-10-16 16:09       ` Duy Nguyen
2018-10-16 22:44         ` brian m. carlson
2018-10-17 14:31           ` Duy Nguyen
2018-10-18  0:06             ` brian m. carlson
2018-10-18 13:03               ` Derrick Stolee
2018-10-19 22:21                 ` brian m. carlson
2018-10-17 12:21   ` Derrick Stolee
2018-10-17 22:38     ` brian m. carlson
2018-10-16  2:00 ` [PATCH v2 00/13] Base SHA-256 implementation Junio C Hamano
2018-10-16  4:01 ` Junio C Hamano
2018-10-16 22:45   ` brian m. carlson
2018-10-16 15:39 ` Duy Nguyen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqo9bu4pww.fsf@gitster-ct.c.googlers.com \
    --to=gitster@pobox.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    --cc=sandals@crustytoothpaste.net \
    --cc=stolee@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).