fast-export: store anonymized oids as hex strings

When fast-export stores anonymized oids, it does so as binary strings.
And while the anonymous mapping storage is binary-clean (at least as of
the previous commit), this will become awkward when we start exposing
more of it to the user. In particular, if we allow a method for
retaining token "foo", then users may want to specify a hex oid as such
a token.

Let's just switch to storing the hex strings. The difference in memory
usage is negligible (especially considering how infrequently we'd
generally store an oid compared to, say, path components).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2020-06-23 11:24:51 -04:00 committed by Junio C Hamano
parent b897bf5f37
commit 750bb32589

View File

@ -387,16 +387,19 @@ static void *generate_fake_oid(const void *old, size_t *len)
{ {
static uint32_t counter = 1; /* avoid null oid */ static uint32_t counter = 1; /* avoid null oid */
const unsigned hashsz = the_hash_algo->rawsz; const unsigned hashsz = the_hash_algo->rawsz;
unsigned char *out = xcalloc(hashsz, 1); struct object_id oid;
put_be32(out + hashsz - 4, counter++); char *hex = xmallocz(GIT_MAX_HEXSZ);
return out;
oidclr(&oid);
put_be32(oid.hash + hashsz - 4, counter++);
return oid_to_hex_r(hex, &oid);
} }
static const struct object_id *anonymize_oid(const struct object_id *oid) static const char *anonymize_oid(const char *oid_hex)
{ {
static struct hashmap objs; static struct hashmap objs;
size_t len = the_hash_algo->rawsz; size_t len = strlen(oid_hex);
return anonymize_mem(&objs, generate_fake_oid, oid, &len); return anonymize_mem(&objs, generate_fake_oid, oid_hex, &len);
} }
static void show_filemodify(struct diff_queue_struct *q, static void show_filemodify(struct diff_queue_struct *q,
@ -455,9 +458,9 @@ static void show_filemodify(struct diff_queue_struct *q,
*/ */
if (no_data || S_ISGITLINK(spec->mode)) if (no_data || S_ISGITLINK(spec->mode))
printf("M %06o %s ", spec->mode, printf("M %06o %s ", spec->mode,
oid_to_hex(anonymize ? anonymize ?
anonymize_oid(&spec->oid) : anonymize_oid(oid_to_hex(&spec->oid)) :
&spec->oid)); oid_to_hex(&spec->oid));
else { else {
struct object *object = lookup_object(the_repository, struct object *object = lookup_object(the_repository,
&spec->oid); &spec->oid);
@ -712,9 +715,10 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
if (mark) if (mark)
printf(":%d\n", mark); printf(":%d\n", mark);
else else
printf("%s\n", oid_to_hex(anonymize ? printf("%s\n",
anonymize_oid(&obj->oid) : anonymize ?
&obj->oid)); anonymize_oid(oid_to_hex(&obj->oid)) :
oid_to_hex(&obj->oid));
i++; i++;
} }