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

[PATCH v3 3/9] reflog: change one->many worktree->refnames to use a string_list

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Dec 22, 2021, 04:06 UTC
Message-ID
<patch-v3-3.9-ba7679e6fc0-20211222T040557Z-avarab@gmail.com>
In-Reply-To
<cover-v3-0.9-00000000000-20211222T040557Z-avarab@gmail.com>

Change the FLEX_ARRAY pattern added in bda3a31cc79 (reflog-expire: Avoid creating new files in a directory inside readdir(3) loop, 2008-01-25) the string-list API instead.

This does not change any behavior, allows us to delete much of this code as it's replaced by things we get from the string-list API for free, as a result we need just one struct to keep track of this data, instead of two.

The "DUP" -> "string_list_append_nodup(..., strbuf_detach(...))" pattern here is the same as that used in a recent memory leak fix in b202e51b154 (grep: fix a "path_list" memory leak, 2021-10-22).

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/reflog.c | 47 ++++++++++++++++++-----------------------------
 1 file changed, 18 insertions(+), 29 deletions(-)
diff --git a/builtin/reflog.c b/builtin/reflog.c
index 6989492bf5c..27851c6efb7 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -48,16 +48,9 @@ struct expire_reflog_policy_cb {
 	struct commit_list *tips;
 };
 
-struct collected_reflog {
-	struct object_id oid;
-	char reflog[FLEX_ARRAY];
-};
-
-struct collect_reflog_cb {
-	struct collected_reflog **e;
-	int alloc;
-	int nr;
-	struct worktree *wt;
+struct worktree_reflogs {
+	struct worktree *worktree;
+	struct string_list reflogs;
 };
 
 /* Remember to update object flag allocation in object.h */
@@ -403,24 +396,20 @@ static void reflog_expiry_cleanup(void *cb_data)
 
 static int collect_reflog(const char *ref, const struct object_id *oid, int unused, void *cb_data)
 {
-	struct collected_reflog *e;
-	struct collect_reflog_cb *cb = cb_data;
+	struct worktree_reflogs *cb = cb_data;
+	struct worktree *worktree = cb->worktree;
 	struct strbuf newref = STRBUF_INIT;
 
 	/*
 	 * Avoid collecting the same shared ref multiple times because
 	 * they are available via all worktrees.
 	 */
-	if (!cb->wt->is_current && ref_type(ref) == REF_TYPE_NORMAL)
+	if (!worktree->is_current && ref_type(ref) == REF_TYPE_NORMAL)
 		return 0;
 
-	strbuf_worktree_ref(cb->wt, &newref, ref);
-	FLEX_ALLOC_STR(e, reflog, newref.buf);
-	strbuf_release(&newref);
+	strbuf_worktree_ref(worktree, &newref, ref);
+	string_list_append_nodup(&cb->reflogs, strbuf_detach(&newref, NULL));
 
-	oidcpy(&e->oid, oid);
-	ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
-	cb->e[cb->nr++] = e;
 	return 0;
 }
 
@@ -609,33 +598,33 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
 	}
 
 	if (do_all) {
-		struct collect_reflog_cb collected;
+		struct worktree_reflogs collected = {
+			.reflogs = STRING_LIST_INIT_DUP,
+		};
+		struct string_list_item *item;
 		struct worktree **worktrees, **p;
-		int i;
 
-		memset(&collected, 0, sizeof(collected));
 		worktrees = get_worktrees();
 		for (p = worktrees; *p; p++) {
 			if (!all_worktrees && !(*p)->is_current)
 				continue;
-			collected.wt = *p;
+			collected.worktree = *p;
 			refs_for_each_reflog(get_worktree_ref_store(*p),
 					     collect_reflog, &collected);
 		}
 		free_worktrees(worktrees);
-		for (i = 0; i < collected.nr; i++) {
-			struct collected_reflog *e = collected.e[i];
+
+		for_each_string_list_item(item, &collected.reflogs) {
 			struct expire_reflog_policy_cb cb = { .cmd = cmd };
 
-			set_reflog_expiry_param(&cb.cmd, explicit_expiry, e->reflog);
-			status |= reflog_expire(e->reflog, flags,
+			set_reflog_expiry_param(&cb.cmd, explicit_expiry, item->string);
+			status |= reflog_expire(item->string, flags,
 						reflog_expiry_prepare,
 						should_expire_reflog_ent,
 						reflog_expiry_cleanup,
 						&cb);
-			free(e);
 		}
-		free(collected.e);
+		string_list_clear(&collected.reflogs, 0);
 	}
 
 	for (; i < argc; i++) {
-- 
2.34.1.1146.gb52885e7c44
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 32 of 37 in “reflog + gc: refactor, progress output, reftable-readyness”
  1. 00/12 reflog + gc: refactor, progress output, reftable-readynessÆvar Arnfjörð Bjarmason, Nov 30, 2021
  2. 01/12 reflog delete: narrow scope of "cmd" passed to count_reflog_ent()Ævar Arnfjörð Bjarmason, Nov 30, 2021
  3. 02/12 reflog expire: narrow scope of "cb" in cmd_reflog_expire()Ævar Arnfjörð Bjarmason, Nov 30, 2021
  4. 03/12 reflog: change one->many worktree->refnames to use a string_listÆvar Arnfjörð Bjarmason, Nov 30, 2021
  5. 04/12 reflog expire: don't do negative comparison on enum valuesÆvar Arnfjörð Bjarmason, Nov 30, 2021
  6. 05/12 reflog expire: refactor & use "tip_commit" only for UE_NORMALÆvar Arnfjörð Bjarmason, Nov 30, 2021
  7. 06/12 reflog expire: don't use lookup_commit_reference_gently()Ævar Arnfjörð Bjarmason, Nov 30, 2021
  8. 07/12 reflog: reduce scope of "struct rev_info"Ævar Arnfjörð Bjarmason, Nov 30, 2021
  9. 09/12 reflog + refs-backend: move "verbose" out of the backendÆvar Arnfjörð Bjarmason, Nov 30, 2021
  10. 11/12 gc + reflog: emit progress output from "reflog expire --all"Ævar Arnfjörð Bjarmason, Nov 30, 2021
  11. 10/12 reflog expire: add progress output on --stale-fixÆvar Arnfjörð Bjarmason, Nov 30, 2021
  12. 08/12 refs files-backend: assume cb->newlog if !EXPIRE_REFLOGS_DRY_RUNÆvar Arnfjörð Bjarmason, Nov 30, 2021
  13. 12/12 gc + reflog: don't stall before initial "git gc" progress outputÆvar Arnfjörð Bjarmason, Nov 30, 2021
  14. 0/9 reftable prep: have builtin/reflog.c handle "--verbose"Ævar Arnfjörð Bjarmason, Dec 16, 2021
  15. 1/9 reflog delete: narrow scope of "cmd" passed to count_reflog_ent()Ævar Arnfjörð Bjarmason, Dec 16, 2021
  16. 2/9 reflog expire: narrow scope of "cb" in cmd_reflog_expire()Ævar Arnfjörð Bjarmason, Dec 16, 2021
  17. 3/9 reflog: change one->many worktree->refnames to use a string_listÆvar Arnfjörð Bjarmason, Dec 16, 2021
  18. 4/9 reflog expire: don't do negative comparison on enum valuesÆvar Arnfjörð Bjarmason, Dec 16, 2021
  19. 5/9 reflog expire: refactor & use "tip_commit" only for UE_NORMALÆvar Arnfjörð Bjarmason, Dec 16, 2021
  20. 7/9 reflog: reduce scope of "struct rev_info"Ævar Arnfjörð Bjarmason, Dec 16, 2021
  21. 6/9 reflog expire: don't use lookup_commit_reference_gently()Ævar Arnfjörð Bjarmason, Dec 16, 2021
  22. 8/9 refs files-backend: assume cb->newlog if !EXPIRE_REFLOGS_DRY_RUNÆvar Arnfjörð Bjarmason, Dec 16, 2021
  23. 9/9 reflog + refs-backend: move "verbose" out of the backendÆvar Arnfjörð Bjarmason, Dec 16, 2021
  24. Junio C HamanoDec 16, 2021
  25. Ævar Arnfjörð BjarmasonDec 16, 2021
  26. Han-Wen NienhuysDec 21, 2021
  27. Ævar Arnfjörð BjarmasonDec 21, 2021
  28. 0/9 reftable prep: have builtin/reflog.c handle "--verbose"Ævar Arnfjörð Bjarmason, Dec 22, 2021
  29. 1/9 reflog delete: narrow scope of "cmd" passed to count_reflog_ent()Ævar Arnfjörð Bjarmason, Dec 22, 2021
  30. 2/9 reflog expire: narrow scope of "cb" in cmd_reflog_expire()Ævar Arnfjörð Bjarmason, Dec 22, 2021
  31. 4/9 reflog expire: use "switch" over enum valuesÆvar Arnfjörð Bjarmason, Dec 22, 2021
  32. 3/9 reflog: change one->many worktree->refnames to use a string_listÆvar Arnfjörð Bjarmason, Dec 22, 2021
  33. 7/9 reflog: reduce scope of "struct rev_info"Ævar Arnfjörð Bjarmason, Dec 22, 2021
  34. 6/9 reflog expire: don't use lookup_commit_reference_gently()Ævar Arnfjörð Bjarmason, Dec 22, 2021
  35. 5/9 reflog expire: refactor & use "tip_commit" only for UE_NORMALÆvar Arnfjörð Bjarmason, Dec 22, 2021
  36. 8/9 refs files-backend: assume cb->newlog if !EXPIRE_REFLOGS_DRY_RUNÆvar Arnfjörð Bjarmason, Dec 22, 2021
  37. 9/9 reflog + refs-backend: move "verbose" out of the backendÆvar Arnfjörð Bjarmason, Dec 22, 2021

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.