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
maint
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; static uint32_t last_idnum;
struct anonymized_entry { struct anonymized_entry {
struct hashmap_entry hash; struct hashmap_entry hash;
const char *anon; char *anon;
const char orig[FLEX_ARRAY]; 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); 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 * Basically keep a cache of X->Y so that we can repeatedly replace
* the same anonymized string with another. The actual generation * the same anonymized string with another. The actual generation
* is farmed out to the generate function. * is farmed out to the generate function.
*/ */
static const char *anonymize_str(struct hashmap *map, static const char *anonymize_str(struct hashmap *map,
char *(*generate)(void *), char *(*generate)(void),
const char *orig, size_t len, const char *orig, size_t len)
void *data)
{ {
struct anonymized_entry_key key; struct anonymized_entry_key key;
struct anonymized_entry *ret; struct anonymized_entry *ret;


if (!map->cmpfn)
hashmap_init(map, anonymized_entry_cmp, NULL, 0);

hashmap_entry_init(&key.hash, memhash(orig, len)); hashmap_entry_init(&key.hash, memhash(orig, len));
key.orig = orig; key.orig = orig;
key.orig_len = len; key.orig_len = len;


/* First check if it's a token the user configured manually... */ /* First check if it's a token the user configured manually... */
if (anonymized_seeds.cmpfn)
ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key); ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key);
else
ret = NULL;


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


/* ...and finally generate a new mapping if necessary */ /* ...and finally generate a new mapping if necessary */
if (!ret) { if (!ret)
FLEX_ALLOC_MEM(ret, orig, orig, len); ret = add_anonymized_entry(map, key.hash.hash,
hashmap_entry_init(&ret->hash, key.hash.hash); orig, len, generate());
ret->anon = generate(data);
hashmap_put(map, &ret->hash);
}


return ret->anon; 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, static void anonymize_path(struct strbuf *out, const char *path,
struct hashmap *map, struct hashmap *map,
char *(*generate)(void *)) char *(*generate)(void))
{ {
while (*path) { while (*path) {
const char *end_of_component = strchrnul(path, '/'); const char *end_of_component = strchrnul(path, '/');
size_t len = end_of_component - 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); strbuf_addstr(out, c);
path = end_of_component; path = end_of_component;
if (*path) if (*path)
@ -368,7 +381,7 @@ static void print_path_1(const char *path)
printf("%s", path); printf("%s", path);
} }


static char *anonymize_path_component(void *data) static char *anonymize_path_component(void)
{ {
static int counter; static int counter;
struct strbuf out = STRBUF_INIT; 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 */ static uint32_t counter = 1; /* avoid null oid */
const unsigned hashsz = the_hash_algo->rawsz; const unsigned hashsz = the_hash_algo->rawsz;
@ -406,7 +419,7 @@ static const char *anonymize_oid(const char *oid_hex)
{ {
static struct hashmap objs; static struct hashmap objs;
size_t len = strlen(oid_hex); 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, 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; return bol;
} }


static char *anonymize_ref_component(void *data) static char *anonymize_ref_component(void)
{ {
static int counter; static int counter;
struct strbuf out = STRBUF_INIT; 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 * 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. * 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; static int counter;
return xstrfmt("subject %d\n\nbody\n", counter++); return xstrfmt("subject %d\n\nbody\n", counter++);
} }


static char *anonymize_ident(void *data) static char *anonymize_ident(void)
{ {
static int counter; static int counter;
struct strbuf out = STRBUF_INIT; 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; len = split.mail_end - split.name_begin;
ident = anonymize_str(&idents, anonymize_ident, ident = anonymize_str(&idents, anonymize_ident,
split.name_begin, len, NULL); split.name_begin, len);
strbuf_addstr(out, ident); strbuf_addstr(out, ident);
strbuf_addch(out, ' '); strbuf_addch(out, ' ');
strbuf_add(out, split.date_begin, split.tz_end - split.date_begin); 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); mark_next_object(&commit->object);
if (anonymize) { if (anonymize) {
reencoded = anonymize_commit_message(message); reencoded = anonymize_commit_message();
} else if (encoding) { } else if (encoding) {
switch(reencode_mode) { switch(reencode_mode) {
case REENCODE_YES: case REENCODE_YES:
@ -733,7 +746,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
show_progress(); show_progress();
} }


static char *anonymize_tag(void *data) static char *anonymize_tag(void)
{ {
static int counter; static int counter;
struct strbuf out = STRBUF_INIT; struct strbuf out = STRBUF_INIT;
@ -795,7 +808,7 @@ static void handle_tag(const char *name, struct tag *tag)
if (message) { if (message) {
static struct hashmap tags; static struct hashmap tags;
message = anonymize_str(&tags, anonymize_tag, message = anonymize_str(&tags, anonymize_tag,
message, message_size, NULL); message, message_size);
message_size = strlen(message); 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, static int parse_opt_anonymize_map(const struct option *opt,
const char *arg, int unset) const char *arg, int unset)
{ {
@ -1152,7 +1160,8 @@ static int parse_opt_anonymize_map(const struct option *opt,
if (!keylen || !*value) if (!keylen || !*value)
return error(_("--anonymize-map token cannot be empty")); 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; return 0;
} }

View File

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


test_expect_success 'stream contains user-specified names' ' test_expect_success 'stream contains user-specified names' '
grep retain-me stream && grep retain-me stream &&
! grep should-not-appear stream &&
grep custom-name stream grep custom-name stream
' '