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
@@ -270,41 +270,57 @@ int set_disambiguate_hint_config(const char *var, const char *value)
270
return error("unknown hint type for '%s': %s", var, value);
271
}
272
273
+static int parse_oid_prefix(const char *name, int len,
274
+ const struct git_hash_algo *algo,
275
+ char *hex_out,
276
+ struct object_id *oid_out)
277
+{
278
+ for (int i = 0; i < len; i++) {
279
+ unsigned char c = name[i];
280
+ unsigned char val;
281
+ if (c >= '0' && c <= '9') {
282
+ val = c - '0';
283
+ } else if (c >= 'a' && c <= 'f') {
284
+ val = c - 'a' + 10;
285
+ } else if (c >= 'A' && c <='F') {
286
+ val = c - 'A' + 10;
287
+ c -= 'A' - 'a';
288
+ } else {
289
+ return -1;
290
+ }
291
+
292
+ if (hex_out)
293
+ hex_out[i] = c;
294
+ if (oid_out) {
295
+ if (!(i & 1))
296
+ val <<= 4;
297
+ oid_out->hash[i >> 1] |= val;
298
+ }
299
+ }
300
+
301
+ if (hex_out)
302
+ hex_out[len] = '\0';
303
+ if (oid_out)
304
+ oid_out->algo = algo ? hash_algo_by_ptr(algo) : GIT_HASH_UNKNOWN;
305
+
306
+ return 0;
307
+}
308
+
309
static int init_object_disambiguation(struct repository *r,
310
const char *name, int len,
311
const struct git_hash_algo *algo,
312
struct disambiguate_state *ds)
313
{
278
- int i;
279
-
314
if (len < MINIMUM_ABBREV || len > GIT_MAX_HEXSZ)
315
return -1;
316
317
memset(ds, 0, sizeof(*ds));
318
285
- for (i = 0; i < len ;i++) {
286
- unsigned char c = name[i];
287
- unsigned char val;
288
- if (c >= '0' && c <= '9')
289
- val = c - '0';
290
- else if (c >= 'a' && c <= 'f')
291
- val = c - 'a' + 10;
292
- else if (c >= 'A' && c <='F') {
293
- val = c - 'A' + 10;
294
- c -= 'A' - 'a';
295
- }
296
- else
297
- return -1;
298
- ds->hex_pfx[i] = c;
299
- if (!(i & 1))
300
- val <<= 4;
301
- ds->bin_pfx.hash[i >> 1] |= val;
302
- }
319
+ if (parse_oid_prefix(name, len, algo, ds->hex_pfx, &ds->bin_pfx) < 0)
320
+ return -1;
321
322
ds->len = len;
305
- ds->hex_pfx[len] = '\0';
323
ds->repo = r;
307
- ds->bin_pfx.algo = algo ? hash_algo_by_ptr(algo) : GIT_HASH_UNKNOWN;
324
odb_prepare_alternates(r->objects);
325
return 0;
326
}