Skip to content

Commit 7e13d65

Browse files
committed
Merge branch 'jk/fast-export-cleanup'
Code clean-up. * jk/fast-export-cleanup: fast-export: drop unused parameter from anonymize_commit_message() fast-export: drop data parameter from anonymous generators fast-export: de-obfuscate --anonymize-map handling fast-export: factor out anonymized_entry creation fast-export: simplify initialization of anonymized hashmaps fast-export: drop const when storing anonymized values
2 parents f315a8b + d051f17 commit 7e13d65

File tree

2 files changed

+46
-35
lines changed

2 files changed

+46
-35
lines changed

builtin/fast-export.c

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static struct decoration idnums;
110110
static uint32_t last_idnum;
111111
struct anonymized_entry {
112112
struct hashmap_entry hash;
113-
const char *anon;
113+
char *anon;
114114
const char orig[FLEX_ARRAY];
115115
};
116116

@@ -139,43 +139,56 @@ static int anonymized_entry_cmp(const void *cmp_data UNUSED,
139139
return strcmp(a->orig, b->orig);
140140
}
141141

142+
static struct anonymized_entry *add_anonymized_entry(struct hashmap *map,
143+
unsigned hash,
144+
const char *orig, size_t len,
145+
char *anon)
146+
{
147+
struct anonymized_entry *ret, *old;
148+
149+
if (!map->cmpfn)
150+
hashmap_init(map, anonymized_entry_cmp, NULL, 0);
151+
152+
FLEX_ALLOC_MEM(ret, orig, orig, len);
153+
hashmap_entry_init(&ret->hash, hash);
154+
ret->anon = anon;
155+
old = hashmap_put_entry(map, ret, hash);
156+
157+
if (old) {
158+
free(old->anon);
159+
free(old);
160+
}
161+
162+
return ret;
163+
}
164+
142165
/*
143166
* Basically keep a cache of X->Y so that we can repeatedly replace
144167
* the same anonymized string with another. The actual generation
145168
* is farmed out to the generate function.
146169
*/
147170
static const char *anonymize_str(struct hashmap *map,
148-
char *(*generate)(void *),
149-
const char *orig, size_t len,
150-
void *data)
171+
char *(*generate)(void),
172+
const char *orig, size_t len)
151173
{
152174
struct anonymized_entry_key key;
153175
struct anonymized_entry *ret;
154176

155-
if (!map->cmpfn)
156-
hashmap_init(map, anonymized_entry_cmp, NULL, 0);
157-
158177
hashmap_entry_init(&key.hash, memhash(orig, len));
159178
key.orig = orig;
160179
key.orig_len = len;
161180

162181
/* First check if it's a token the user configured manually... */
163-
if (anonymized_seeds.cmpfn)
164-
ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key);
165-
else
166-
ret = NULL;
182+
ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key);
167183

168184
/* ...otherwise check if we've already seen it in this context... */
169185
if (!ret)
170186
ret = hashmap_get_entry(map, &key, hash, &key);
171187

172188
/* ...and finally generate a new mapping if necessary */
173-
if (!ret) {
174-
FLEX_ALLOC_MEM(ret, orig, orig, len);
175-
hashmap_entry_init(&ret->hash, key.hash.hash);
176-
ret->anon = generate(data);
177-
hashmap_put(map, &ret->hash);
178-
}
189+
if (!ret)
190+
ret = add_anonymized_entry(map, key.hash.hash,
191+
orig, len, generate());
179192

180193
return ret->anon;
181194
}
@@ -188,12 +201,12 @@ static const char *anonymize_str(struct hashmap *map,
188201
*/
189202
static void anonymize_path(struct strbuf *out, const char *path,
190203
struct hashmap *map,
191-
char *(*generate)(void *))
204+
char *(*generate)(void))
192205
{
193206
while (*path) {
194207
const char *end_of_component = strchrnul(path, '/');
195208
size_t len = end_of_component - path;
196-
const char *c = anonymize_str(map, generate, path, len, NULL);
209+
const char *c = anonymize_str(map, generate, path, len);
197210
strbuf_addstr(out, c);
198211
path = end_of_component;
199212
if (*path)
@@ -368,7 +381,7 @@ static void print_path_1(const char *path)
368381
printf("%s", path);
369382
}
370383

371-
static char *anonymize_path_component(void *data)
384+
static char *anonymize_path_component(void)
372385
{
373386
static int counter;
374387
struct strbuf out = STRBUF_INIT;
@@ -390,7 +403,7 @@ static void print_path(const char *path)
390403
}
391404
}
392405

393-
static char *generate_fake_oid(void *data)
406+
static char *generate_fake_oid(void)
394407
{
395408
static uint32_t counter = 1; /* avoid null oid */
396409
const unsigned hashsz = the_hash_algo->rawsz;
@@ -406,7 +419,7 @@ static const char *anonymize_oid(const char *oid_hex)
406419
{
407420
static struct hashmap objs;
408421
size_t len = strlen(oid_hex);
409-
return anonymize_str(&objs, generate_fake_oid, oid_hex, len, NULL);
422+
return anonymize_str(&objs, generate_fake_oid, oid_hex, len);
410423
}
411424

412425
static void show_filemodify(struct diff_queue_struct *q,
@@ -503,7 +516,7 @@ static const char *find_encoding(const char *begin, const char *end)
503516
return bol;
504517
}
505518

506-
static char *anonymize_ref_component(void *data)
519+
static char *anonymize_ref_component(void)
507520
{
508521
static int counter;
509522
struct strbuf out = STRBUF_INIT;
@@ -543,13 +556,13 @@ static const char *anonymize_refname(const char *refname)
543556
* We do not even bother to cache commit messages, as they are unlikely
544557
* to be repeated verbatim, and it is not that interesting when they are.
545558
*/
546-
static char *anonymize_commit_message(const char *old)
559+
static char *anonymize_commit_message(void)
547560
{
548561
static int counter;
549562
return xstrfmt("subject %d\n\nbody\n", counter++);
550563
}
551564

552-
static char *anonymize_ident(void *data)
565+
static char *anonymize_ident(void)
553566
{
554567
static int counter;
555568
struct strbuf out = STRBUF_INIT;
@@ -592,7 +605,7 @@ static void anonymize_ident_line(const char **beg, const char **end)
592605

593606
len = split.mail_end - split.name_begin;
594607
ident = anonymize_str(&idents, anonymize_ident,
595-
split.name_begin, len, NULL);
608+
split.name_begin, len);
596609
strbuf_addstr(out, ident);
597610
strbuf_addch(out, ' ');
598611
strbuf_add(out, split.date_begin, split.tz_end - split.date_begin);
@@ -670,7 +683,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
670683

671684
mark_next_object(&commit->object);
672685
if (anonymize) {
673-
reencoded = anonymize_commit_message(message);
686+
reencoded = anonymize_commit_message();
674687
} else if (encoding) {
675688
switch(reencode_mode) {
676689
case REENCODE_YES:
@@ -733,7 +746,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
733746
show_progress();
734747
}
735748

736-
static char *anonymize_tag(void *data)
749+
static char *anonymize_tag(void)
737750
{
738751
static int counter;
739752
struct strbuf out = STRBUF_INIT;
@@ -795,7 +808,7 @@ static void handle_tag(const char *name, struct tag *tag)
795808
if (message) {
796809
static struct hashmap tags;
797810
message = anonymize_str(&tags, anonymize_tag,
798-
message, message_size, NULL);
811+
message, message_size);
799812
message_size = strlen(message);
800813
}
801814
}
@@ -1126,11 +1139,6 @@ static void handle_deletes(void)
11261139
}
11271140
}
11281141

1129-
static char *anonymize_seed(void *data)
1130-
{
1131-
return xstrdup(data);
1132-
}
1133-
11341142
static int parse_opt_anonymize_map(const struct option *opt,
11351143
const char *arg, int unset)
11361144
{
@@ -1152,7 +1160,8 @@ static int parse_opt_anonymize_map(const struct option *opt,
11521160
if (!keylen || !*value)
11531161
return error(_("--anonymize-map token cannot be empty"));
11541162

1155-
anonymize_str(map, anonymize_seed, arg, keylen, (void *)value);
1163+
add_anonymized_entry(map, memhash(arg, keylen), arg, keylen,
1164+
xstrdup(value));
11561165

11571166
return 0;
11581167
}

t/t9351-fast-export-anonymize.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ test_expect_success 'setup simple repo' '
2525
test_expect_success 'export anonymized stream' '
2626
git fast-export --anonymize --all \
2727
--anonymize-map=retain-me \
28+
--anonymize-map=xyzzy:should-not-appear \
2829
--anonymize-map=xyzzy:custom-name \
2930
--anonymize-map=other \
3031
>stream
@@ -41,6 +42,7 @@ test_expect_success 'stream omits path names' '
4142

4243
test_expect_success 'stream contains user-specified names' '
4344
grep retain-me stream &&
45+
! grep should-not-appear stream &&
4446
grep custom-name stream
4547
'
4648

0 commit comments

Comments
 (0)