@matheusbsbs / matheus-bagliano / commits / 61e2a70ff2
Fork 0

hex: add functions to parse hex object IDs in any algorithm

There are some places where we need to parse a hex object ID in any algorithm without knowing beforehand which algorithm is in use. An example is when parsing fast-import marks. Add a get_oid_hex_any to parse an object ID and return the algorithm it belongs to, and additionally add parse_oid_hex_any which is the equivalent change for parse_oid_hex. If the object is not parseable, we return GIT_HASH_UNKNOWN. 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 61e2a70ff26f83f761fa36e0cc16996878d8dd59
2 files changed +32
cache.h
+10
@@ -1523,6 +1523,16 @@ int parse_oid_hex(const char *hex, struct object_id *oid, const char **end);
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 +/*
1528 + * These functions work like get_oid_hex and parse_oid_hex, but they will parse
1529 + * a hex value for any algorithm. The algorithm is detected based on the length
1530 + * and the algorithm in use is returned. If this is not a hex object ID in any
1531 + * algorithm, returns GIT_HASH_UNKNOWN.
1532 + */
1533 +int get_oid_hex_any(const char *hex, struct object_id *oid);
1534 +int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end);
1535 +
1536 /*
1537 * This reads short-hand syntax that not only evaluates to a commit
1538 * object name, but also can act as if the end user spelled the name
hex.c
+22
@@ -72,6 +72,20 @@ int get_oid_hex_algop(const char *hex, struct object_id *oid,
72 return get_hash_hex_algop(hex, oid->hash, algop);
73 }
74
75 +/*
76 + * NOTE: This function relies on hash algorithms being in order from shortest
77 + * length to longest length.
78 + */
79 +int get_oid_hex_any(const char *hex, struct object_id *oid)
80 +{
81 + int i;
82 + for (i = GIT_HASH_NALGOS - 1; i > 0; i--) {
83 + if (!get_hash_hex_algop(hex, oid->hash, &hash_algos[i]))
84 + return i;
85 + }
86 + return GIT_HASH_UNKNOWN;
87 +}
88 +
89 int get_oid_hex(const char *hex, struct object_id *oid)
90 {
91 return get_oid_hex_algop(hex, oid, the_hash_algo);
@@ -87,6 +101,14 @@ int parse_oid_hex_algop(const char *hex, struct object_id *oid,
101 return ret;
102 }
103
104 +int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end)
105 +{
106 + int ret = get_oid_hex_any(hex, oid);
107 + if (ret)
108 + *end = hex + hash_algos[ret].hexsz;
109 + return ret;
110 +}
111 +
112 int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
113 {
114 return parse_oid_hex_algop(hex, oid, end, the_hash_algo);