@matheusbsbs / matheus-bagliano / commits / d5bf91fde4
Fork 0

fast-export: add a "data" callback parameter to anonymize_str()

The anonymize_str() function takes a generator callback, but there's no way to pass extra context to it. Let's add the usual "void *data" parameter to the generator interface and pass it along. This is mildly annoying for existing callers, all of which pass NULL, but is necessary to avoid extra globals in some cases we'll add in a subsequent patch. While we're touching each of these callbacks, we can further observe that none of them use the existing orig/len parameters at all. This makes sense, since the point is for their output to have no discernable basis in the original (my original version had some notion that we might use a one-way function to obfuscate the names, but it was never implemented). So let's drop those extra parameters. If a caller really wants to do something with them, it can pass a struct through the new data parameter. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 23, 2020 at 11:25 UTC d5bf91fde4430532eb725425c3ef9827048af6b5
1 file changed +14 -13
builtin/fast-export.c
+14 -13
@@ -155,8 +155,9 @@ static int anonymized_entry_cmp(const void *unused_cmp_data,
155 * is farmed out to the generate function.
156 */
157 static const char *anonymize_str(struct hashmap *map,
158 - char *(*generate)(const char *, size_t),
159 - const char *orig, size_t len)
158 + char *(*generate)(void *),
159 + const char *orig, size_t len,
160 + void *data)
161 {
162 struct anonymized_entry_key key;
163 struct anonymized_entry *ret;
@@ -172,7 +173,7 @@ static const char *anonymize_str(struct hashmap *map,
173 if (!ret) {
174 FLEX_ALLOC_MEM(ret, orig, orig, len);
175 hashmap_entry_init(&ret->hash, key.hash.hash);
175 - ret->anon = generate(orig, len);
176 + ret->anon = generate(data);
177 hashmap_put(map, &ret->hash);
178 }
179
@@ -187,12 +188,12 @@ static const char *anonymize_str(struct hashmap *map,
188 */
189 static void anonymize_path(struct strbuf *out, const char *path,
190 struct hashmap *map,
190 - char *(*generate)(const char *, size_t))
191 + char *(*generate)(void *))
192 {
193 while (*path) {
194 const char *end_of_component = strchrnul(path, '/');
195 size_t len = end_of_component - path;
195 - const char *c = anonymize_str(map, generate, path, len);
196 + const char *c = anonymize_str(map, generate, path, len, NULL);
197 strbuf_addstr(out, c);
198 path = end_of_component;
199 if (*path)
@@ -367,7 +368,7 @@ static void print_path_1(const char *path)
368 printf("%s", path);
369 }
370
370 -static char *anonymize_path_component(const char *path, size_t len)
371 +static char *anonymize_path_component(void *data)
372 {
373 static int counter;
374 struct strbuf out = STRBUF_INIT;
@@ -389,7 +390,7 @@ static void print_path(const char *path)
390 }
391 }
392
392 -static char *generate_fake_oid(const char *old, size_t len)
393 +static char *generate_fake_oid(void *data)
394 {
395 static uint32_t counter = 1; /* avoid null oid */
396 const unsigned hashsz = the_hash_algo->rawsz;
@@ -405,7 +406,7 @@ static const char *anonymize_oid(const char *oid_hex)
406 {
407 static struct hashmap objs;
408 size_t len = strlen(oid_hex);
408 - return anonymize_str(&objs, generate_fake_oid, oid_hex, len);
409 + return anonymize_str(&objs, generate_fake_oid, oid_hex, len, NULL);
410 }
411
412 static void show_filemodify(struct diff_queue_struct *q,
@@ -502,7 +503,7 @@ static const char *find_encoding(const char *begin, const char *end)
503 return bol;
504 }
505
505 -static char *anonymize_ref_component(const char *old, size_t len)
506 +static char *anonymize_ref_component(void *data)
507 {
508 static int counter;
509 struct strbuf out = STRBUF_INIT;
@@ -555,7 +556,7 @@ static char *anonymize_commit_message(const char *old)
556 return xstrfmt("subject %d\n\nbody\n", counter++);
557 }
558
558 -static char *anonymize_ident(const char *old, size_t len)
559 +static char *anonymize_ident(void *data)
560 {
561 static int counter;
562 struct strbuf out = STRBUF_INIT;
@@ -598,7 +599,7 @@ static void anonymize_ident_line(const char **beg, const char **end)
599
600 len = split.mail_end - split.name_begin;
601 ident = anonymize_str(&idents, anonymize_ident,
601 - split.name_begin, len);
602 + split.name_begin, len, NULL);
603 strbuf_addstr(out, ident);
604 strbuf_addch(out, ' ');
605 strbuf_add(out, split.date_begin, split.tz_end - split.date_begin);
@@ -739,7 +740,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
740 show_progress();
741 }
742
742 -static char *anonymize_tag(const char *old, size_t len)
743 +static char *anonymize_tag(void *data)
744 {
745 static int counter;
746 struct strbuf out = STRBUF_INIT;
@@ -815,7 +816,7 @@ static void handle_tag(const char *name, struct tag *tag)
816 if (message) {
817 static struct hashmap tags;
818 message = anonymize_str(&tags, anonymize_tag,
818 - message, message_size);
819 + message, message_size, NULL);
820 }
821 }
822