{"thread":{"id":"66804","subject":"[PATCH] builtin/reflog: expire --all and named refs in one loop","startedAt":"2026-11-08T01:22:13Z","lastAt":"2026-11-11T12:42:39Z","messageCount":9,"participants":["Laveesh Bansal","Patrick Steinhardt","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"557825","messageId":"20261108012213.89468-1-laveeshb@laveeshbansal.com","threadId":"66804","inReplyTo":null,"subject":"[PATCH] builtin/reflog: expire --all and named refs in one loop","fromName":"Laveesh Bansal","fromEmail":"laveeshb@laveeshbansal.com","sentAt":"2026-11-08T01:22:13Z","receivedAt":"2026-11-08T01:22:13Z","isPatch":true,"sender":{"key":"laveeshb@laveeshbansal.com","avatar":null},"body":"\"git reflog expire\" sets up the call to refs_reflog_expire() twice,\nonce for --all and once for refs named on the command line.\nd1536ac3e2 (builtin/reflog: say \"would prune\" for named refs with\n--dry-run, 2026-10-23) fixed the named-ref copy, which was missing\ndry_run. Junio suggested a single loop in the review of that fix [1].\n\nCollect the refs from both sources into one list and expire them in a\nsingle loop. The names given on the command line are still looked up\nin that loop, so the output and the exit status stay the same.\n\n[1] https://lore.kernel.org/git/xmqqqzhdy7pi.fsf@gitster.g/\n\nAssisted-by: Claude Sonnet 5.5\nSigned-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>\n---\n\nNotes:\n    The lookup stays in the loop. Doing it while collecting would report an\n    unknown name before any expiry output, and make \"expire --expire=now\n    HEAD @{-1}\" succeed.\n    \n    I ran the full test suite, and the reflog tests with reftable, sha256\n    and SANITIZE=leak. Output, exit status and the resulting reflogs match\n    the base for the cases I tried, including @{-1} and unknown names.\n\n builtin/reflog.c | 50 +++++++++++++++++++++++-------------------------\n 1 file changed, 24 insertions(+), 26 deletions(-)\n\ndiff --git a/builtin/reflog.c b/builtin/reflog.c\nindex 49e5453dd9..42d37a3724 100644\n--- a/builtin/reflog.c\n+++ b/builtin/reflog.c\n@@ -187,6 +187,10 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,\n \tunsigned int flags = 0;\n \tint verbose = 0;\n \treflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;\n+\tstruct worktree_reflogs collected = {\n+\t\t.reflogs = STRING_LIST_INIT_DUP,\n+\t};\n+\tstruct string_list_item *item;\n \tconst struct option options[] = {\n \t\tOPT_BIT('n', \"dry-run\", &flags, N_(\"do not actually prune any entries\"),\n \t\t\tEXPIRE_REFLOGS_DRY_RUN),\n@@ -244,10 +248,6 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,\n \t}\n \n \tif (do_all) {\n-\t\tstruct worktree_reflogs collected = {\n-\t\t\t.reflogs = STRING_LIST_INIT_DUP,\n-\t\t};\n-\t\tstruct string_list_item *item;\n \t\tstruct worktree **worktrees, **p;\n \n \t\tworktrees = get_worktrees(the_repository);\n@@ -259,44 +259,42 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,\n \t\t\t\t\t     collect_reflog, &collected);\n \t\t}\n \t\tfree_worktrees(worktrees);\n-\n-\t\tfor_each_string_list_item(item, &collected.reflogs) {\n-\t\t\tstruct expire_reflog_policy_cb cb = {\n-\t\t\t\t.opts = opts,\n-\t\t\t\t.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),\n-\t\t\t};\n-\n-\t\t\treflog_expire_options_set_refname(&cb.opts,  item->string);\n-\t\t\tstatus |= refs_reflog_expire(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t     item->string, flags,\n-\t\t\t\t\t\t     reflog_expiry_prepare,\n-\t\t\t\t\t\t     should_prune_fn,\n-\t\t\t\t\t\t     reflog_expiry_cleanup,\n-\t\t\t\t\t\t     &cb);\n-\t\t}\n-\t\tstring_list_clear(&collected.reflogs, 0);\n \t}\n \n+\t/* util marks the names that still have to be looked up */\n \tfor (i = 0; i < argc; i++) {\n-\t\tchar *ref;\n+\t\titem = string_list_append(&collected.reflogs, argv[i]);\n+\t\titem->util = (void *)1;\n+\t}\n+\n+\tfor_each_string_list_item(item, &collected.reflogs) {\n \t\tstruct expire_reflog_policy_cb cb = {\n \t\t\t.opts = opts,\n \t\t\t.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),\n \t\t};\n-\n-\t\tif (!repo_dwim_log(the_repository, argv[i], strlen(argv[i]), NULL, &ref)) {\n-\t\t\tstatus |= error(_(\"reflog could not be found: '%s'\"), argv[i]);\n+\t\tconst char *refname = item->string;\n+\t\tchar *ref = NULL;\n+\n+\t\tif (item->util &&\n+\t\t    !repo_dwim_log(the_repository, item->string,\n+\t\t\t\t   strlen(item->string), NULL, &ref)) {\n+\t\t\tstatus |= error(_(\"reflog could not be found: '%s'\"),\n+\t\t\t\t\titem->string);\n \t\t\tcontinue;\n \t\t}\n-\t\treflog_expire_options_set_refname(&cb.opts, ref);\n+\t\tif (ref)\n+\t\t\trefname = ref;\n+\n+\t\treflog_expire_options_set_refname(&cb.opts, refname);\n \t\tstatus |= refs_reflog_expire(get_main_ref_store(the_repository),\n-\t\t\t\t\t     ref, flags,\n+\t\t\t\t\t     refname, flags,\n \t\t\t\t\t     reflog_expiry_prepare,\n \t\t\t\t\t     should_prune_fn,\n \t\t\t\t\t     reflog_expiry_cleanup,\n \t\t\t\t\t     &cb);\n \t\tfree(ref);\n \t}\n+\tstring_list_clear(&collected.reflogs, 0);\n \n \treflog_clear_expire_config(&opts);\n \n-- \n2.54.0\n\n\n"},{"id":"557905","messageId":"avGU-Wjkd821PLc0@pks.im","threadId":"66804","inReplyTo":"20261108012213.89468-1-laveeshb@laveeshbansal.com","subject":"Re: [PATCH] builtin/reflog: expire --all and named refs in one loop","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-11-09T09:40:09Z","receivedAt":"2026-11-09T09:40:09Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Sat, Nov 07, 2026 at 05:22:13PM -0800, Laveesh Bansal wrote:\n> \"git reflog expire\" sets up the call to refs_reflog_expire() twice,\n> once for --all and once for refs named on the command line.\n> d1536ac3e2 (builtin/reflog: say \"would prune\" for named refs with\n> --dry-run, 2026-10-23) fixed the named-ref copy, which was missing\n> dry_run. Junio suggested a single loop in the review of that fix [1].\n\nWe don't typically refer to suggestions in our commit messages like\nthis. You should instead explain what the underlying issue is in your\nown words and why that issue should be fixed so that readers can read\nthis message without having to hunt for context in other mailing list\nthreads.\n\nThat being said, it can be useful to refer to this mail after the \"---\"\nline or additional context.\n\n> Collect the refs from both sources into one list and expire them in a\n> single loop. The names given on the command line are still looked up\n> in that loop, so the output and the exit status stay the same.\n\nYou say what you do, but you don't say why you do it or what it buys us.\n\n> diff --git a/builtin/reflog.c b/builtin/reflog.c\n> index 49e5453dd9..42d37a3724 100644\n> --- a/builtin/reflog.c\n> +++ b/builtin/reflog.c\n> @@ -259,44 +259,42 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,\n>  \t\t\t\t\t     collect_reflog, &collected);\n>  \t\t}\n>  \t\tfree_worktrees(worktrees);\n> -\n> -\t\tfor_each_string_list_item(item, &collected.reflogs) {\n> -\t\t\tstruct expire_reflog_policy_cb cb = {\n> -\t\t\t\t.opts = opts,\n> -\t\t\t\t.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),\n> -\t\t\t};\n> -\n> -\t\t\treflog_expire_options_set_refname(&cb.opts,  item->string);\n> -\t\t\tstatus |= refs_reflog_expire(get_main_ref_store(the_repository),\n> -\t\t\t\t\t\t     item->string, flags,\n> -\t\t\t\t\t\t     reflog_expiry_prepare,\n> -\t\t\t\t\t\t     should_prune_fn,\n> -\t\t\t\t\t\t     reflog_expiry_cleanup,\n> -\t\t\t\t\t\t     &cb);\n> -\t\t}\n> -\t\tstring_list_clear(&collected.reflogs, 0);\n>  \t}\n>  \n> +\t/* util marks the names that still have to be looked up */\n>  \tfor (i = 0; i < argc; i++) {\n> -\t\tchar *ref;\n> +\t\titem = string_list_append(&collected.reflogs, argv[i]);\n> +\t\titem->util = (void *)1;\n> +\t}\n\nOkay, so instead of carrying the logic around `refs_reflog_expire()`\ntwice you instead now first gather all reflog names that we want to\nexpire, and then we do that in a single step. That seems like a useful\nsimplification to me.\n\nOne thing I wonder though: now that we first gather reflog names, can't\nwe also easily deduplicate them? For example if the same reflog name was\ngiven multiple times on the command line, or in case the reflog is\nincluded both via a command line argument and via \"--all\".\n\nSo I wonder whether we should both sort and deduplicate entries here.\n\n> +\tfor_each_string_list_item(item, &collected.reflogs) {\n>  \t\tstruct expire_reflog_policy_cb cb = {\n>  \t\t\t.opts = opts,\n>  \t\t\t.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),\n>  \t\t};\n> -\n> -\t\tif (!repo_dwim_log(the_repository, argv[i], strlen(argv[i]), NULL, &ref)) {\n> -\t\t\tstatus |= error(_(\"reflog could not be found: '%s'\"), argv[i]);\n> +\t\tconst char *refname = item->string;\n> +\t\tchar *ref = NULL;\n> +\n> +\t\tif (item->util &&\n> +\t\t    !repo_dwim_log(the_repository, item->string,\n> +\t\t\t\t   strlen(item->string), NULL, &ref)) {\n> +\t\t\tstatus |= error(_(\"reflog could not be found: '%s'\"),\n> +\t\t\t\t\titem->string);\n>  \t\t\tcontinue;\n>  \t\t}\n> -\t\treflog_expire_options_set_refname(&cb.opts, ref);\n> +\t\tif (ref)\n> +\t\t\trefname = ref;\n\nOkay, we only apply per-reflog options in the case where the reflog name\nwas explicitly specified by the user, becaues only in that case do we\ncall `repo_dwim_log()`. This retains the status quo, but now that one\nsees this single loop it makes you wonder whether that's a sensible\nthing to do.\n\nPatrick\n\n"},{"id":"557960","messageId":"20261109161313.54630-1-laveeshb@laveeshbansal.com","threadId":"66804","inReplyTo":"avGU-Wjkd821PLc0@pks.im","subject":"Re: [PATCH] builtin/reflog: expire --all and named refs in one loop","fromName":"Laveesh Bansal","fromEmail":"laveeshb@laveeshbansal.com","sentAt":"2026-11-09T16:13:13Z","receivedAt":"2026-11-09T16:13:13Z","isPatch":true,"sender":{"key":"laveeshb@laveeshbansal.com","avatar":null},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> One thing I wonder though: now that we first gather reflog names, can't\n> we also easily deduplicate them? For example if the same reflog name was\n> given multiple times on the command line, or in case the reflog is\n> included both via a command line argument and via \"--all\".\n\nI want this patch to change no behavior, and deduplicating would change\nthe output: with \"--verbose\", a name given twice lists its entries\ntwice today, and sorting would reorder them.  It could be done in the\nloop, by skipping a name already seen, but that is a separate change.\n\n> Okay, we only apply per-reflog options in the case where the reflog name\n> was explicitly specified by the user, becaues only in that case do we\n> call `repo_dwim_log()`.\n\nreflog_expire_options_set_refname() is called for every entry in the\nloop, as it was in both loops before, so the per-reflog options apply\nto the --all entries too.  Only repo_dwim_log() is conditional: the\nnames from --all come straight from the ref store and need no lookup.\n\nI'll reword the commit message as you suggest and send a v2 once others\nhave had a chance to comment.\n\n"},{"id":"557985","messageId":"xmqqwlqlydo2.fsf@gitster.g","threadId":"66804","inReplyTo":"avGU-Wjkd821PLc0@pks.im","subject":"Re: [PATCH] builtin/reflog: expire --all and named refs in one loop","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-09T20:04:29Z","receivedAt":"2026-11-09T20:04:29Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n2 Okay, so instead of carrying the logic around `refs_reflog_expire()`\n> twice you instead now first gather all reflog names that we want to\n> expire, and then we do that in a single step. That seems like a useful\n> simplification to me.\n>\n> One thing I wonder though: now that we first gather reflog names, can't\n> we also easily deduplicate them? For example if the same reflog name was\n> given multiple times on the command line, or in case the reflog is\n> included both via a command line argument and via \"--all\".\n>\n> So I wonder whether we should both sort and deduplicate entries here.\n\nVery good point.  I cannot think of a reason why we should not\nconsider it a bug that the current two-loop model processes and\nreports about the same thing twice, and the collect-then-process\nmodel allows deduplication almost for free to address that bug.\n\nThanks.\n\n"},{"id":"558027","messageId":"avLDE1gGsjjy2ryd@pks.im","threadId":"66804","inReplyTo":"20261109161313.54630-1-laveeshb@laveeshbansal.com","subject":"Re: [PATCH] builtin/reflog: expire --all and named refs in one loop","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-11-10T07:09:07Z","receivedAt":"2026-11-10T07:09:07Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Nov 09, 2026 at 08:13:13AM -0800, Laveesh Bansal wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> \n> > One thing I wonder though: now that we first gather reflog names, can't\n> > we also easily deduplicate them? For example if the same reflog name was\n> > given multiple times on the command line, or in case the reflog is\n> > included both via a command line argument and via \"--all\".\n> \n> I want this patch to change no behavior, and deduplicating would change\n> the output: with \"--verbose\", a name given twice lists its entries\n> twice today, and sorting would reorder them.  It could be done in the\n> loop, by skipping a name already seen, but that is a separate change.\n\nYou could add it as a second commit to this patch series. Like that, the\nrefactoring and the change in behaviour would still be separate from one\nanother. And that second commit would be a good point in case vouching\nfor why the refactoring is a good idea.\n\n> > Okay, we only apply per-reflog options in the case where the reflog name\n> > was explicitly specified by the user, becaues only in that case do we\n> > call `repo_dwim_log()`.\n> \n> reflog_expire_options_set_refname() is called for every entry in the\n> loop, as it was in both loops before, so the per-reflog options apply\n> to the --all entries too.  Only repo_dwim_log() is conditional: the\n> names from --all come straight from the ref store and need no lookup.\n> \n> I'll reword the commit message as you suggest and send a v2 once others\n> have had a chance to comment.\n\nThanks!\n\nPatrick\n\n"},{"id":"558093","messageId":"cover.1794324064.git.laveeshb@laveeshbansal.com","threadId":"66804","inReplyTo":"20261108012213.89468-1-laveeshb@laveeshbansal.com","subject":"[PATCH v2 0/2] builtin/reflog: expire each reflog only once","fromName":"Laveesh Bansal","fromEmail":"laveeshb@laveeshbansal.com","sentAt":"2026-11-10T15:26:39Z","receivedAt":"2026-11-10T15:26:39Z","isPatch":true,"sender":{"key":"laveeshb@laveeshbansal.com","avatar":null},"body":"This series makes \"git reflog expire\" expire each reflog only once.  1/2\nputs the --all and the named-ref paths in one loop without changing\nbehavior.  2/2 skips a reflog that was already expired in this run, so\na name given twice, or one that --all also covers, is no longer expired\nand reported twice.\n\nv1 was the refactor alone.  I had kept it free of behavior changes and\nplanned to handle repeated reflogs in a later patch.  After the\ndiscussion on v1 [1][2], I am bringing that change into v2 as a second\npatch, which keeps the refactor separate.\n\nChanges since v1: 1/2 only has a new commit message, and 2/2 is new.\n\nI ran the full test suite, and the reflog tests with reftable, sha256\nand SANITIZE=leak.  Against the base, the output differs only where a\nreflog is reached twice.\n\nAssisted-by: Claude Sonnet 5.5\n\n[1] https://lore.kernel.org/git/avLDE1gGsjjy2ryd@pks.im/\n[2] https://lore.kernel.org/git/xmqqwlqlydo2.fsf@gitster.g/\n\n\nLaveesh Bansal (2):\n  builtin/reflog: expire --all and named refs in one loop\n  builtin/reflog: expire each reflog only once\n\n builtin/reflog.c  | 56 ++++++++++++++++++++++++++---------------------\n t/t1410-reflog.sh | 14 ++++++++++++\n 2 files changed, 45 insertions(+), 25 deletions(-)\n\n-- \n2.54.0\n\n\n"},{"id":"558094","messageId":"5878cbe0ebc18b9c61cce37587fcd3a211e8d354.1794324064.git.laveeshb@laveeshbansal.com","threadId":"66804","inReplyTo":"cover.1794324064.git.laveeshb@laveeshbansal.com","subject":"[PATCH v2 1/2] builtin/reflog: expire --all and named refs in one loop","fromName":"Laveesh Bansal","fromEmail":"laveeshb@laveeshbansal.com","sentAt":"2026-11-10T15:26:40Z","receivedAt":"2026-11-10T15:26:40Z","isPatch":true,"sender":{"key":"laveeshb@laveeshbansal.com","avatar":null},"body":"\"git reflog expire\" calls refs_reflog_expire() from two places, one\nfor --all and one for the refs named on the command line, each with\nits own copy of the callback setup. The copies can drift apart:\nd1536ac3e2 (builtin/reflog: say \"would prune\" for named refs with\n--dry-run, 2026-10-23) fixed a copy that was missing dry_run.\n\nCollect the reflogs from both sources into one list and expire them\nin a single loop, so the call is set up in one place. The names given\non the command line are still looked up in that loop, so the output\nand the exit status stay the same.\n\nSigned-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>\n---\n builtin/reflog.c | 50 +++++++++++++++++++++++-------------------------\n 1 file changed, 24 insertions(+), 26 deletions(-)\n\ndiff --git a/builtin/reflog.c b/builtin/reflog.c\nindex 49e5453dd9..42d37a3724 100644\n--- a/builtin/reflog.c\n+++ b/builtin/reflog.c\n@@ -187,6 +187,10 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,\n \tunsigned int flags = 0;\n \tint verbose = 0;\n \treflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;\n+\tstruct worktree_reflogs collected = {\n+\t\t.reflogs = STRING_LIST_INIT_DUP,\n+\t};\n+\tstruct string_list_item *item;\n \tconst struct option options[] = {\n \t\tOPT_BIT('n', \"dry-run\", &flags, N_(\"do not actually prune any entries\"),\n \t\t\tEXPIRE_REFLOGS_DRY_RUN),\n@@ -244,10 +248,6 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,\n \t}\n \n \tif (do_all) {\n-\t\tstruct worktree_reflogs collected = {\n-\t\t\t.reflogs = STRING_LIST_INIT_DUP,\n-\t\t};\n-\t\tstruct string_list_item *item;\n \t\tstruct worktree **worktrees, **p;\n \n \t\tworktrees = get_worktrees(the_repository);\n@@ -259,44 +259,42 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,\n \t\t\t\t\t     collect_reflog, &collected);\n \t\t}\n \t\tfree_worktrees(worktrees);\n-\n-\t\tfor_each_string_list_item(item, &collected.reflogs) {\n-\t\t\tstruct expire_reflog_policy_cb cb = {\n-\t\t\t\t.opts = opts,\n-\t\t\t\t.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),\n-\t\t\t};\n-\n-\t\t\treflog_expire_options_set_refname(&cb.opts,  item->string);\n-\t\t\tstatus |= refs_reflog_expire(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t     item->string, flags,\n-\t\t\t\t\t\t     reflog_expiry_prepare,\n-\t\t\t\t\t\t     should_prune_fn,\n-\t\t\t\t\t\t     reflog_expiry_cleanup,\n-\t\t\t\t\t\t     &cb);\n-\t\t}\n-\t\tstring_list_clear(&collected.reflogs, 0);\n \t}\n \n+\t/* util marks the names that still have to be looked up */\n \tfor (i = 0; i < argc; i++) {\n-\t\tchar *ref;\n+\t\titem = string_list_append(&collected.reflogs, argv[i]);\n+\t\titem->util = (void *)1;\n+\t}\n+\n+\tfor_each_string_list_item(item, &collected.reflogs) {\n \t\tstruct expire_reflog_policy_cb cb = {\n \t\t\t.opts = opts,\n \t\t\t.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),\n \t\t};\n-\n-\t\tif (!repo_dwim_log(the_repository, argv[i], strlen(argv[i]), NULL, &ref)) {\n-\t\t\tstatus |= error(_(\"reflog could not be found: '%s'\"), argv[i]);\n+\t\tconst char *refname = item->string;\n+\t\tchar *ref = NULL;\n+\n+\t\tif (item->util &&\n+\t\t    !repo_dwim_log(the_repository, item->string,\n+\t\t\t\t   strlen(item->string), NULL, &ref)) {\n+\t\t\tstatus |= error(_(\"reflog could not be found: '%s'\"),\n+\t\t\t\t\titem->string);\n \t\t\tcontinue;\n \t\t}\n-\t\treflog_expire_options_set_refname(&cb.opts, ref);\n+\t\tif (ref)\n+\t\t\trefname = ref;\n+\n+\t\treflog_expire_options_set_refname(&cb.opts, refname);\n \t\tstatus |= refs_reflog_expire(get_main_ref_store(the_repository),\n-\t\t\t\t\t     ref, flags,\n+\t\t\t\t\t     refname, flags,\n \t\t\t\t\t     reflog_expiry_prepare,\n \t\t\t\t\t     should_prune_fn,\n \t\t\t\t\t     reflog_expiry_cleanup,\n \t\t\t\t\t     &cb);\n \t\tfree(ref);\n \t}\n+\tstring_list_clear(&collected.reflogs, 0);\n \n \treflog_clear_expire_config(&opts);\n \n-- \n2.54.0\n\n\n"},{"id":"558095","messageId":"0e5d78f552deaf3bd0ba2ec70666cebc2957c9e4.1794324064.git.laveeshb@laveeshbansal.com","threadId":"66804","inReplyTo":"cover.1794324064.git.laveeshb@laveeshbansal.com","subject":"[PATCH v2 2/2] builtin/reflog: expire each reflog only once","fromName":"Laveesh Bansal","fromEmail":"laveeshb@laveeshbansal.com","sentAt":"2026-11-10T15:26:41Z","receivedAt":"2026-11-10T15:26:41Z","isPatch":true,"sender":{"key":"laveeshb@laveeshbansal.com","avatar":null},"body":"\"git reflog expire\" processes a reflog once for every time it is named,\nand reports it every time:\n\n    $ git reflog expire --dry-run --verbose --expire=now main main\n    would prune commit (initial): one\n    would prune commit (initial): one\n\nThe same happens for two names of the same ref, such as \"main\" and\n\"refs/heads/main\", and for a name that --all already covers.\n\nSkip a reflog that this run has already processed.\n\nSigned-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>\n---\n\nNotes:\n    With --updateref, the first pass can move the ref, so a second pass\n    over the same reflog could prune entries that the new tip no longer\n    reaches.  It now ends as if the name had been given once.\n\n builtin/reflog.c  |  8 ++++++++\n t/t1410-reflog.sh | 14 ++++++++++++++\n 2 files changed, 22 insertions(+)\n\ndiff --git a/builtin/reflog.c b/builtin/reflog.c\nindex 42d37a3724..4221136298 100644\n--- a/builtin/reflog.c\n+++ b/builtin/reflog.c\n@@ -190,6 +190,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,\n \tstruct worktree_reflogs collected = {\n \t\t.reflogs = STRING_LIST_INIT_DUP,\n \t};\n+\tstruct string_list seen = STRING_LIST_INIT_DUP;\n \tstruct string_list_item *item;\n \tconst struct option options[] = {\n \t\tOPT_BIT('n', \"dry-run\", &flags, N_(\"do not actually prune any entries\"),\n@@ -285,6 +286,12 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,\n \t\tif (ref)\n \t\t\trefname = ref;\n \n+\t\tif (string_list_has_string(&seen, refname)) {\n+\t\t\tfree(ref);\n+\t\t\tcontinue;\n+\t\t}\n+\t\tstring_list_insert(&seen, refname);\n+\n \t\treflog_expire_options_set_refname(&cb.opts, refname);\n \t\tstatus |= refs_reflog_expire(get_main_ref_store(the_repository),\n \t\t\t\t\t     refname, flags,\n@@ -295,6 +302,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,\n \t\tfree(ref);\n \t}\n \tstring_list_clear(&collected.reflogs, 0);\n+\tstring_list_clear(&seen, 0);\n \n \treflog_clear_expire_config(&opts);\n \ndiff --git a/t/t1410-reflog.sh b/t/t1410-reflog.sh\nindex 998a7e8667..d22216c36d 100755\n--- a/t/t1410-reflog.sh\n+++ b/t/t1410-reflog.sh\n@@ -274,6 +274,20 @@ test_expect_success 'reflog expire --dry-run --verbose --all' '\n \ttest_cmp before after\n '\n \n+test_expect_success 'reflog expire does not process a reflog twice' '\n+\tgit reflog expire --dry-run --verbose \\\n+\t\t--expire=$(($test_tick - 10000)) \\\n+\t\t--expire-unreachable=$(($test_tick - 10000)) \\\n+\t\t--stale-fix \\\n+\t\t--all >once &&\n+\tgit reflog expire --dry-run --verbose \\\n+\t\t--expire=$(($test_tick - 10000)) \\\n+\t\t--expire-unreachable=$(($test_tick - 10000)) \\\n+\t\t--stale-fix \\\n+\t\t--all main refs/heads/main main >twice &&\n+\ttest_cmp once twice\n+'\n+\n test_expect_success 'reflog expire' '\n \n \tgit reflog expire --verbose \\\n-- \n2.54.0\n\n\n"},{"id":"558199","messageId":"avRiv5WpCZQCbRnZ@pks.im","threadId":"66804","inReplyTo":"0e5d78f552deaf3bd0ba2ec70666cebc2957c9e4.1794324064.git.laveeshb@laveeshbansal.com","subject":"Re: [PATCH v2 2/2] builtin/reflog: expire each reflog only once","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-11-11T12:42:39Z","receivedAt":"2026-11-11T12:42:39Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Tue, Nov 10, 2026 at 07:26:41AM -0800, Laveesh Bansal wrote:\n> \"git reflog expire\" processes a reflog once for every time it is named,\n> and reports it every time:\n> \n>     $ git reflog expire --dry-run --verbose --expire=now main main\n>     would prune commit (initial): one\n>     would prune commit (initial): one\n> \n> The same happens for two names of the same ref, such as \"main\" and\n> \"refs/heads/main\", and for a name that --all already covers.\n> \n> Skip a reflog that this run has already processed.\n> \n> Signed-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>\n> ---\n> \n> Notes:\n>     With --updateref, the first pass can move the ref, so a second pass\n>     over the same reflog could prune entries that the new tip no longer\n>     reaches.  It now ends as if the name had been given once.\n\nIsn't this note here important information to point out in the commit\nmessage? It sounds like a bug to me that we behave differently depending\non how many times a refname is passed.\n\n> diff --git a/builtin/reflog.c b/builtin/reflog.c\n> index 42d37a3724..4221136298 100644\n> --- a/builtin/reflog.c\n> +++ b/builtin/reflog.c\n> @@ -190,6 +190,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,\n>  \tstruct worktree_reflogs collected = {\n>  \t\t.reflogs = STRING_LIST_INIT_DUP,\n>  \t};\n> +\tstruct string_list seen = STRING_LIST_INIT_DUP;\n>  \tstruct string_list_item *item;\n>  \tconst struct option options[] = {\n>  \t\tOPT_BIT('n', \"dry-run\", &flags, N_(\"do not actually prune any entries\"),\n> @@ -285,6 +286,12 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,\n>  \t\tif (ref)\n>  \t\t\trefname = ref;\n>  \n> +\t\tif (string_list_has_string(&seen, refname)) {\n> +\t\t\tfree(ref);\n> +\t\t\tcontinue;\n> +\t\t}\n> +\t\tstring_list_insert(&seen, refname);\n\nHmm. This here retain the current ordering. But is there a reason why we\nhave to maintain it?\n\nThe reason I'm asking is that the behaviour here is O(n * 2 log n), as\nfor every string you add to the list you both search whether it exists\nalready and then you search for the insert position. And we could easily\nspeed this up by using `string_list_append()` and then sorting and\ndeduping once after the loop.\n\n> diff --git a/t/t1410-reflog.sh b/t/t1410-reflog.sh\n> index 998a7e8667..d22216c36d 100755\n> --- a/t/t1410-reflog.sh\n> +++ b/t/t1410-reflog.sh\n> @@ -274,6 +274,20 @@ test_expect_success 'reflog expire --dry-run --verbose --all' '\n>  \ttest_cmp before after\n>  '\n>  \n> +test_expect_success 'reflog expire does not process a reflog twice' '\n> +\tgit reflog expire --dry-run --verbose \\\n> +\t\t--expire=$(($test_tick - 10000)) \\\n> +\t\t--expire-unreachable=$(($test_tick - 10000)) \\\n> +\t\t--stale-fix \\\n> +\t\t--all >once &&\n> +\tgit reflog expire --dry-run --verbose \\\n> +\t\t--expire=$(($test_tick - 10000)) \\\n> +\t\t--expire-unreachable=$(($test_tick - 10000)) \\\n> +\t\t--stale-fix \\\n> +\t\t--all main refs/heads/main main >twice &&\n> +\ttest_cmp once twice\n> +'\n\nArguably, if the above is a bug, we should also have a test for it here.\n\nPatrick\n\n"}]}