@matheusbsbs / matheus-bagliano / commits / dadacf10dc
Fork 0

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 <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Feb 22, 2020 at 20:17 UTC dadacf10dc9e11046e2c8c49347174e71cef3fa3
2 files changed +33 -7
cache.h
+7
@@ -1481,6 +1481,9 @@ int set_disambiguate_hint_config(const char *var, const char *value);
1481 int get_sha1_hex(const char *hex, unsigned char *sha1);
1482 int get_oid_hex(const char *hex, struct object_id *sha1);
1483
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 +
1487 /*
1488 * Read `len` pairs of hexadecimal digits from `hex` and write the
1489 * 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 */
1519 */
1520 int parse_oid_hex(const char *hex, struct object_id *oid, const char **end);
1521
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 +
1526 /*
1527 * This reads short-hand syntax that not only evaluates to a commit
1528 * object name, but also can act as if the end user spelled the name
hex.c
+26 -7
@@ -47,32 +47,51 @@ int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
47 return 0;
48 }
49
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)
52 {
53 int i;
53 - for (i = 0; i < the_hash_algo->rawsz; i++) {
54 + for (i = 0; i < algop->rawsz; i++) {
55 int val = hex2chr(hex);
56 if (val < 0)
57 return -1;
57 - *sha1++ = val;
58 + *hash++ = val;
59 hex += 2;
60 }
61 return 0;
62 }
63
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 +
75 int get_oid_hex(const char *hex, struct object_id *oid)
76 {
65 - return get_sha1_hex(hex, oid->hash);
77 + return get_oid_hex_algop(hex, oid, the_hash_algo);
78 }
79
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)
83 {
70 - int ret = get_oid_hex(hex, oid);
84 + int ret = get_hash_hex_algop(hex, oid->hash, algop);
85 if (!ret)
72 - *end = hex + the_hash_algo->hexsz;
86 + *end = hex + algop->hexsz;
87 return ret;
88 }
89
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 +
95 char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash,
96 const struct git_hash_algo *algop)
97 {