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

Re: [PATCH v2 02/13] sha1-file: provide functions to look up hash algorithms

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Oct 17, 2018, 13:32 UTC
Message-ID
<20181017133220.GQ19800@szeder.dev>
In-Reply-To
<20181015021900.1030041-3-sandals@crustytoothpaste.net>
On Mon, Oct 15, 2018 at 02:18:49AM +0000, brian m. carlson wrote:
Show 31 quoted lines
> There are several ways we might refer to a hash algorithm: by name, such
> as in the config file; by format ID, such as in a pack; or internally,
> by a pointer to the hash_algos array.  Provide functions to look up hash
> algorithms based on these various forms and return the internal constant
> used for them.  If conversion to another form is necessary, this
> internal constant can be used to look up the proper data in the
> hash_algos array.
> 
> Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
> ---
>  hash.h      | 13 +++++++++++++
>  sha1-file.c | 21 +++++++++++++++++++++
>  2 files changed, 34 insertions(+)
> 
> diff --git a/hash.h b/hash.h
> index 7c8238bc2e..90f4344619 100644
> --- a/hash.h
> +++ b/hash.h
> @@ -98,4 +98,17 @@ struct git_hash_algo {
>  };
>  extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
>  
> +/*
> + * Return a GIT_HASH_* constant based on the name.  Returns GIT_HASH_UNKNOWN if
> + * the name doesn't match a known algorithm.
> + */
> +int hash_algo_by_name(const char *name);
> +/* Identical, except based on the format ID. */
> +int hash_algo_by_id(uint32_t format_id);
> +/* Identical, except for a pointer to struct git_hash_algo. */
> +inline int hash_algo_by_ptr(const struct git_hash_algo *p)

This has to be declared as static, otherwise the linker will error out when building without optimization:

    LINK git
libgit.a(commit-graph.o): In function `oid_version':
/home/szeder/src/git/commit-graph.c:48: undefined reference to `hash_algo_by_ptr'
libgit.a(hex.o): In function `hash_to_hex':
/home/szeder/src/git/hex.c:123: undefined reference to `hash_algo_by_ptr'
libgit.a(hex.o): In function `oid_to_hex':
/home/szeder/src/git/hex.c:128: undefined reference to `hash_algo_by_ptr'
collect2: error: ld returned 1 exit status
Makefile:2055: recipe for target 'git' failed
make: *** [git] Error 1
Show 37 quoted lines
> +{
> +	return p - hash_algos;
> +}
> +
>  #endif
> diff --git a/sha1-file.c b/sha1-file.c
> index e29825f259..3a75d515eb 100644
> --- a/sha1-file.c
> +++ b/sha1-file.c
> @@ -122,6 +122,27 @@ const char *empty_blob_oid_hex(void)
>  	return oid_to_hex_r(buf, the_hash_algo->empty_blob);
>  }
>  
> +int hash_algo_by_name(const char *name)
> +{
> +	int i;
> +	if (!name)
> +		return GIT_HASH_UNKNOWN;
> +	for (i = 1; i < GIT_HASH_NALGOS; i++)
> +		if (!strcmp(name, hash_algos[i].name))
> +			return i;
> +	return GIT_HASH_UNKNOWN;
> +}
> +
> +int hash_algo_by_id(uint32_t format_id)
> +{
> +	int i;
> +	for (i = 1; i < GIT_HASH_NALGOS; i++)
> +		if (format_id == hash_algos[i].format_id)
> +			return i;
> +	return GIT_HASH_UNKNOWN;
> +}
> +
> +
>  /*
>   * This is meant to hold a *small* number of objects that you would
>   * want read_sha1_file() to be able to return, but yet you do not want
Previous: brian m. carlsonNext: brian m. carlson
Message 6 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.