Skip to content

Commit dadacf1

Browse files
bk2204gitster
authored andcommitted
hex: introduce parsing variants taking hash algorithms
Introduce variants of get_oid_hex and parse_oid_hex that parse an arbitrary hash algorithm, implementing internal functions to avoid duplication. These functions can be used in the transport code to parse refs properly. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 768e30e commit dadacf1

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

cache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,9 @@ int set_disambiguate_hint_config(const char *var, const char *value);
14811481
int get_sha1_hex(const char *hex, unsigned char *sha1);
14821482
int get_oid_hex(const char *hex, struct object_id *sha1);
14831483

1484+
/* Like get_oid_hex, but for an arbitrary hash algorithm. */
1485+
int get_oid_hex_algop(const char *hex, struct object_id *oid, const struct git_hash_algo *algop);
1486+
14841487
/*
14851488
* Read `len` pairs of hexadecimal digits from `hex` and write the
14861489
* values to `binary` as `len` bytes. Return 0 on success, or -1 if
@@ -1516,6 +1519,10 @@ char *oid_to_hex(const struct object_id *oid); /* same static buffer */
15161519
*/
15171520
int parse_oid_hex(const char *hex, struct object_id *oid, const char **end);
15181521

1522+
/* Like parse_oid_hex, but for an arbitrary hash algorithm. */
1523+
int parse_oid_hex_algop(const char *hex, struct object_id *oid, const char **end,
1524+
const struct git_hash_algo *algo);
1525+
15191526
/*
15201527
* This reads short-hand syntax that not only evaluates to a commit
15211528
* object name, but also can act as if the end user spelled the name

hex.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,51 @@ int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
4747
return 0;
4848
}
4949

50-
int get_sha1_hex(const char *hex, unsigned char *sha1)
50+
static int get_hash_hex_algop(const char *hex, unsigned char *hash,
51+
const struct git_hash_algo *algop)
5152
{
5253
int i;
53-
for (i = 0; i < the_hash_algo->rawsz; i++) {
54+
for (i = 0; i < algop->rawsz; i++) {
5455
int val = hex2chr(hex);
5556
if (val < 0)
5657
return -1;
57-
*sha1++ = val;
58+
*hash++ = val;
5859
hex += 2;
5960
}
6061
return 0;
6162
}
6263

64+
int get_sha1_hex(const char *hex, unsigned char *sha1)
65+
{
66+
return get_hash_hex_algop(hex, sha1, the_hash_algo);
67+
}
68+
69+
int get_oid_hex_algop(const char *hex, struct object_id *oid,
70+
const struct git_hash_algo *algop)
71+
{
72+
return get_hash_hex_algop(hex, oid->hash, algop);
73+
}
74+
6375
int get_oid_hex(const char *hex, struct object_id *oid)
6476
{
65-
return get_sha1_hex(hex, oid->hash);
77+
return get_oid_hex_algop(hex, oid, the_hash_algo);
6678
}
6779

68-
int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
80+
int parse_oid_hex_algop(const char *hex, struct object_id *oid,
81+
const char **end,
82+
const struct git_hash_algo *algop)
6983
{
70-
int ret = get_oid_hex(hex, oid);
84+
int ret = get_hash_hex_algop(hex, oid->hash, algop);
7185
if (!ret)
72-
*end = hex + the_hash_algo->hexsz;
86+
*end = hex + algop->hexsz;
7387
return ret;
7488
}
7589

90+
int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
91+
{
92+
return parse_oid_hex_algop(hex, oid, end, the_hash_algo);
93+
}
94+
7695
char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash,
7796
const struct git_hash_algo *algop)
7897
{

0 commit comments

Comments
 (0)