Skip to content

Commit 9539978

Browse files
bk2204gitster
authored andcommitted
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>
1 parent 9c9492e commit 9539978

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

hash.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
131131
int hash_algo_by_name(const char *name);
132132
/* Identical, except based on the format ID. */
133133
int hash_algo_by_id(uint32_t format_id);
134+
/* Identical, except based on the length. */
135+
int hash_algo_by_length(int len);
134136
/* Identical, except for a pointer to struct git_hash_algo. */
135137
static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
136138
{

sha1-file.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ int hash_algo_by_id(uint32_t format_id)
189189
return GIT_HASH_UNKNOWN;
190190
}
191191

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+
}
192200

193201
/*
194202
* This is meant to hold a *small* number of objects that you would

0 commit comments

Comments
 (0)