Skip to content

Commit 28c9254

Browse files
pks-tgitster
authored andcommitted
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>
1 parent e30bff8 commit 28c9254

1 file changed

Lines changed: 38 additions & 22 deletions

File tree

object-name.c

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -270,41 +270,57 @@ int set_disambiguate_hint_config(const char *var, const char *value)
270270
return error("unknown hint type for '%s': %s", var, value);
271271
}
272272

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+
273309
static int init_object_disambiguation(struct repository *r,
274310
const char *name, int len,
275311
const struct git_hash_algo *algo,
276312
struct disambiguate_state *ds)
277313
{
278-
int i;
279-
280314
if (len < MINIMUM_ABBREV || len > GIT_MAX_HEXSZ)
281315
return -1;
282316

283317
memset(ds, 0, sizeof(*ds));
284318

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;
303321

304322
ds->len = len;
305-
ds->hex_pfx[len] = '\0';
306323
ds->repo = r;
307-
ds->bin_pfx.algo = algo ? hash_algo_by_ptr(algo) : GIT_HASH_UNKNOWN;
308324
odb_prepare_alternates(r->objects);
309325
return 0;
310326
}

0 commit comments

Comments
 (0)