object-name: extract function to parse object ID prefixes
Extract the logic that parses an object ID prefix into a new function. This function will be used by a second callsite in a subsequent commit. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Mar 20, 2026 at 08:07 UTC
28c9254e3b3e1304b5a6c36146cf6fec29f3f5d3
1 file changed
+38
-22
object-name.c
+38
-22
index ff0de06ff9..fd1b010ab3 100644
--- a/object-name.c
+++ b/object-name.c
@@ -270,41 +270,57 @@ int set_disambiguate_hint_config(const char *var, const char *value)
return error("unknown hint type for '%s': %s", var, value);
}
+static int parse_oid_prefix(const char *name, int len,
+ const struct git_hash_algo *algo,
+ char *hex_out,
+ struct object_id *oid_out)
+{
+ for (int i = 0; i < len; i++) {
+ unsigned char c = name[i];
+ unsigned char val;
+ if (c >= '0' && c <= '9') {
+ val = c - '0';
+ } else if (c >= 'a' && c <= 'f') {
+ val = c - 'a' + 10;
+ } else if (c >= 'A' && c <='F') {
+ val = c - 'A' + 10;
+ c -= 'A' - 'a';
+ } else {
+ return -1;
+ }
+
+ if (hex_out)
+ hex_out[i] = c;
+ if (oid_out) {
+ if (!(i & 1))
+ val <<= 4;
+ oid_out->hash[i >> 1] |= val;
+ }
+ }
+
+ if (hex_out)
+ hex_out[len] = '\0';
+ if (oid_out)
+ oid_out->algo = algo ? hash_algo_by_ptr(algo) : GIT_HASH_UNKNOWN;
+
+ return 0;
+}
+
static int init_object_disambiguation(struct repository *r,
const char *name, int len,
const struct git_hash_algo *algo,
struct disambiguate_state *ds)
{
- int i;
-
if (len < MINIMUM_ABBREV || len > GIT_MAX_HEXSZ)
return -1;
memset(ds, 0, sizeof(*ds));
- for (i = 0; i < len ;i++) {
- unsigned char c = name[i];
- unsigned char val;
- if (c >= '0' && c <= '9')
- val = c - '0';
- else if (c >= 'a' && c <= 'f')
- val = c - 'a' + 10;
- else if (c >= 'A' && c <='F') {
- val = c - 'A' + 10;
- c -= 'A' - 'a';
- }
- else
- return -1;
- ds->hex_pfx[i] = c;
- if (!(i & 1))
- val <<= 4;
- ds->bin_pfx.hash[i >> 1] |= val;
- }
+ if (parse_oid_prefix(name, len, algo, ds->hex_pfx, &ds->bin_pfx) < 0)
+ return -1;
ds->len = len;
- ds->hex_pfx[len] = '\0';
ds->repo = r;
- ds->bin_pfx.algo = algo ? hash_algo_by_ptr(algo) : GIT_HASH_UNKNOWN;
odb_prepare_alternates(r->objects);
return 0;
}