]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fast-export: store anonymized oids as hex strings
authorJeff King <peff@peff.net>
Tue, 23 Jun 2020 15:24:51 +0000 (11:24 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 24 Jun 2020 02:56:26 +0000 (19:56 -0700)
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>
builtin/fast-export.c

index 289395a131392b0bf23cd5b3c457b13b1f4237ab..4a3a4c933ea68456b4654462563ed033958ae00e 100644 (file)
@@ -387,16 +387,19 @@ static void *generate_fake_oid(const void *old, size_t *len)
 {
        static uint32_t counter = 1; /* avoid null oid */
        const unsigned hashsz = the_hash_algo->rawsz;
-       unsigned char *out = xcalloc(hashsz, 1);
-       put_be32(out + hashsz - 4, counter++);
-       return out;
+       struct object_id oid;
+       char *hex = xmallocz(GIT_MAX_HEXSZ);
+
+       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;
-       size_t len = the_hash_algo->rawsz;
-       return anonymize_mem(&objs, generate_fake_oid, oid, &len);
+       size_t len = strlen(oid_hex);
+       return anonymize_mem(&objs, generate_fake_oid, oid_hex, &len);
 }
 
 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))
                                printf("M %06o %s ", spec->mode,
-                                      oid_to_hex(anonymize ?
-                                                 anonymize_oid(&spec->oid) :
-                                                 &spec->oid));
+                                      anonymize ?
+                                      anonymize_oid(oid_to_hex(&spec->oid)) :
+                                      oid_to_hex(&spec->oid));
                        else {
                                struct object *object = lookup_object(the_repository,
                                                                      &spec->oid);
@@ -712,9 +715,10 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
                if (mark)
                        printf(":%d\n", mark);
                else
-                       printf("%s\n", oid_to_hex(anonymize ?
-                                                 anonymize_oid(&obj->oid) :
-                                                 &obj->oid));
+                       printf("%s\n",
+                              anonymize ?
+                              anonymize_oid(oid_to_hex(&obj->oid)) :
+                              oid_to_hex(&obj->oid));
                i++;
        }