hash: add a function to lookup hash algorithm by length

There are some cases, such as the dumb HTTP transport and bundles, where we can only determine the hash algorithm in use by the length of the object IDs. Provide a function that looks up the algorithm by length. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Feb 19, 2019 at 00:05 UTC 95399788d158e4628e25db20569d44af21a40906
2 files changed +10
hash.h
+2
@@ -131,6 +131,8 @@ extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
131 int hash_algo_by_name(const char *name);
132 /* Identical, except based on the format ID. */
133 int hash_algo_by_id(uint32_t format_id);
134 +/* Identical, except based on the length. */
135 +int hash_algo_by_length(int len);
136 /* Identical, except for a pointer to struct git_hash_algo. */
137 static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
138 {
sha1-file.c
+8
@@ -189,6 +189,14 @@ int hash_algo_by_id(uint32_t format_id)
189 return GIT_HASH_UNKNOWN;
190 }
191
192 +int hash_algo_by_length(int len)
193 +{
194 + int i;
195 + for (i = 1; i < GIT_HASH_NALGOS; i++)
196 + if (len == hash_algos[i].rawsz)
197 + return i;
198 + return GIT_HASH_UNKNOWN;
199 +}
200
201 /*
202 * This is meant to hold a *small* number of objects that you would