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
This commit is contained in:
Junio C Hamano 2023-04-04 14:28:27 -07:00
commit 7e13d654c2
2 changed files with 46 additions and 35 deletions

View File

@ -110,7 +110,7 @@ static struct decoration idnums;
static uint32_t last_idnum;
struct anonymized_entry {
struct hashmap_entry hash;
const char *anon;
char *anon;
const char orig[FLEX_ARRAY];
};
@ -139,43 +139,56 @@ static int anonymized_entry_cmp(const void *cmp_data UNUSED,
return strcmp(a->orig, b->orig);
}
static struct anonymized_entry *add_anonymized_entry(struct hashmap *map,
unsigned hash,
const char *orig, size_t len,
char *anon)
{
struct anonymized_entry *ret, *old;
if (!map->cmpfn)
hashmap_init(map, anonymized_entry_cmp, NULL, 0);
FLEX_ALLOC_MEM(ret, orig, orig, len);
hashmap_entry_init(&ret->hash, hash);
ret->anon = anon;
old = hashmap_put_entry(map, ret, hash);
if (old) {
free(old->anon);
free(old);
}
return ret;
}
/*
* Basically keep a cache of X->Y so that we can repeatedly replace
* the same anonymized string with another. The actual generation
* is farmed out to the generate function.
*/
static const char *anonymize_str(struct hashmap *map,
char *(*generate)(void *),
const char *orig, size_t len,
void *data)
char *(*generate)(void),
const char *orig, size_t len)
{
struct anonymized_entry_key key;
struct anonymized_entry *ret;
if (!map->cmpfn)
hashmap_init(map, anonymized_entry_cmp, NULL, 0);
hashmap_entry_init(&key.hash, memhash(orig, len));
key.orig = orig;
key.orig_len = len;
/* First check if it's a token the user configured manually... */
if (anonymized_seeds.cmpfn)
ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key);
else
ret = NULL;
ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key);
/* ...otherwise check if we've already seen it in this context... */
if (!ret)
ret = hashmap_get_entry(map, &key, hash, &key);
/* ...and finally generate a new mapping if necessary */
if (!ret) {
FLEX_ALLOC_MEM(ret, orig, orig, len);
hashmap_entry_init(&ret->hash, key.hash.hash);
ret->anon = generate(data);
hashmap_put(map, &ret->hash);
}
if (!ret)
ret = add_anonymized_entry(map, key.hash.hash,
orig, len, generate());
return ret->anon;
}
@ -188,12 +201,12 @@ static const char *anonymize_str(struct hashmap *map,
*/
static void anonymize_path(struct strbuf *out, const char *path,
struct hashmap *map,
char *(*generate)(void *))
char *(*generate)(void))
{
while (*path) {
const char *end_of_component = strchrnul(path, '/');
size_t len = end_of_component - path;
const char *c = anonymize_str(map, generate, path, len, NULL);
const char *c = anonymize_str(map, generate, path, len);
strbuf_addstr(out, c);
path = end_of_component;
if (*path)
@ -368,7 +381,7 @@ static void print_path_1(const char *path)
printf("%s", path);
}
static char *anonymize_path_component(void *data)
static char *anonymize_path_component(void)
{
static int counter;
struct strbuf out = STRBUF_INIT;
@ -390,7 +403,7 @@ static void print_path(const char *path)
}
}
static char *generate_fake_oid(void *data)
static char *generate_fake_oid(void)
{
static uint32_t counter = 1; /* avoid null oid */
const unsigned hashsz = the_hash_algo->rawsz;
@ -406,7 +419,7 @@ static const char *anonymize_oid(const char *oid_hex)
{
static struct hashmap objs;
size_t len = strlen(oid_hex);
return anonymize_str(&objs, generate_fake_oid, oid_hex, len, NULL);
return anonymize_str(&objs, generate_fake_oid, oid_hex, len);
}
static void show_filemodify(struct diff_queue_struct *q,
@ -503,7 +516,7 @@ static const char *find_encoding(const char *begin, const char *end)
return bol;
}
static char *anonymize_ref_component(void *data)
static char *anonymize_ref_component(void)
{
static int counter;
struct strbuf out = STRBUF_INIT;
@ -543,13 +556,13 @@ static const char *anonymize_refname(const char *refname)
* We do not even bother to cache commit messages, as they are unlikely
* to be repeated verbatim, and it is not that interesting when they are.
*/
static char *anonymize_commit_message(const char *old)
static char *anonymize_commit_message(void)
{
static int counter;
return xstrfmt("subject %d\n\nbody\n", counter++);
}
static char *anonymize_ident(void *data)
static char *anonymize_ident(void)
{
static int counter;
struct strbuf out = STRBUF_INIT;
@ -592,7 +605,7 @@ static void anonymize_ident_line(const char **beg, const char **end)
len = split.mail_end - split.name_begin;
ident = anonymize_str(&idents, anonymize_ident,
split.name_begin, len, NULL);
split.name_begin, len);
strbuf_addstr(out, ident);
strbuf_addch(out, ' ');
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,
mark_next_object(&commit->object);
if (anonymize) {
reencoded = anonymize_commit_message(message);
reencoded = anonymize_commit_message();
} else if (encoding) {
switch(reencode_mode) {
case REENCODE_YES:
@ -733,7 +746,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
show_progress();
}
static char *anonymize_tag(void *data)
static char *anonymize_tag(void)
{
static int counter;
struct strbuf out = STRBUF_INIT;
@ -795,7 +808,7 @@ static void handle_tag(const char *name, struct tag *tag)
if (message) {
static struct hashmap tags;
message = anonymize_str(&tags, anonymize_tag,
message, message_size, NULL);
message, message_size);
message_size = strlen(message);
}
}
@ -1126,11 +1139,6 @@ static void handle_deletes(void)
}
}
static char *anonymize_seed(void *data)
{
return xstrdup(data);
}
static int parse_opt_anonymize_map(const struct option *opt,
const char *arg, int unset)
{
@ -1152,7 +1160,8 @@ static int parse_opt_anonymize_map(const struct option *opt,
if (!keylen || !*value)
return error(_("--anonymize-map token cannot be empty"));
anonymize_str(map, anonymize_seed, arg, keylen, (void *)value);
add_anonymized_entry(map, memhash(arg, keylen), arg, keylen,
xstrdup(value));
return 0;
}

View File

@ -25,6 +25,7 @@ test_expect_success 'setup simple repo' '
test_expect_success 'export anonymized stream' '
git fast-export --anonymize --all \
--anonymize-map=retain-me \
--anonymize-map=xyzzy:should-not-appear \
--anonymize-map=xyzzy:custom-name \
--anonymize-map=other \
>stream
@ -41,6 +42,7 @@ test_expect_success 'stream omits path names' '
test_expect_success 'stream contains user-specified names' '
grep retain-me stream &&
! grep should-not-appear stream &&
grep custom-name stream
'