git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/6] fast-export: factor out anonymized_entry creation

From
Jeff King <peff@peff.net>
Date
Mar 22, 2023, 17:40 UTC
Message-ID
<20230322174051.GA11424@coredump.intra.peff.net>
In-Reply-To
<20230322173636.GA5643@coredump.intra.peff.net>

When anonymizing output, there's only one spot where we generate new entries to add to our hashmap: when anonymize_str() doesn't find an entry, we use the generate() callback to make one and add it. Let's pull that into its own function in preparation for another caller.

Note that we'll add one extra feature. In anonymize_str(), we know that we won't find an existing entry in the hashmap (since it will only try to add after failing to find one). But other callers won't have the same behavior, so we should catch this case and free the now-dangling entry.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/fast-export.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)
diff --git a/builtin/fast-export.c b/builtin/fast-export.c
index ba9ab3a97e5..5a0b63219ad 100644
--- a/builtin/fast-export.c
+++ b/builtin/fast-export.c
@@ -139,6 +139,29 @@ 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
@@ -164,15 +187,9 @@ static const char *anonymize_str(struct hashmap *map,
 		ret = hashmap_get_entry(map, &key, hash, &key);
 
 	/* ...and finally generate a new mapping if necessary */
-	if (!ret) {
-		if (!map->cmpfn)
-			hashmap_init(map, anonymized_entry_cmp, NULL, 0);
-
-		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(data));
 
 	return ret->anon;
 }
-- 
2.40.0.595.g9b96b494d8c
Previous: Jeff KingNext: Jeff King
Message 4 of 9 in “minor cleanups to fast-export --anonymize”
  1. 0/6 minor cleanups to fast-export --anonymizeJeff King, Mar 22, 2023
  2. 1/6 fast-export: drop const when storing anonymized valuesJeff King, Mar 22, 2023
  3. 2/6 fast-export: simplify initialization of anonymized hashmapsJeff King, Mar 22, 2023
  4. 3/6 fast-export: factor out anonymized_entry creationJeff King, Mar 22, 2023
  5. 4/6 fast-export: de-obfuscate --anonymize-map handlingJeff King, Mar 22, 2023
  6. 5/6 fast-export: drop data parameter from anonymous generatorsJeff King, Mar 22, 2023
  7. 6/6 fast-export: drop unused parameter from anonymize_commit_message()Jeff King, Mar 22, 2023
  8. Derrick StoleeMar 24, 2023
  9. Junio C HamanoMar 24, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.