Skip to content

Commit 61e2a70

Browse files
bk2204gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dadacf1 commit 61e2a70

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

cache.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,16 @@ int parse_oid_hex(const char *hex, struct object_id *oid, const char **end);
15231523
int parse_oid_hex_algop(const char *hex, struct object_id *oid, const char **end,
15241524
const struct git_hash_algo *algo);
15251525

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+
15261536
/*
15271537
* This reads short-hand syntax that not only evaluates to a commit
15281538
* object name, but also can act as if the end user spelled the name

hex.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ int get_oid_hex_algop(const char *hex, struct object_id *oid,
7272
return get_hash_hex_algop(hex, oid->hash, algop);
7373
}
7474

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+
7589
int get_oid_hex(const char *hex, struct object_id *oid)
7690
{
7791
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,
87101
return ret;
88102
}
89103

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+
90112
int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
91113
{
92114
return parse_oid_hex_algop(hex, oid, end, the_hash_algo);

0 commit comments

Comments
 (0)