🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.git
11 messages
11 total messages Started by Laveesh Bansal Sun, 08 Nov 2026 01:22
[PATCH] builtin/reflog: expire --all and named refs in one loop
#2633
Author: Laveesh Bansal
Date: Sun, 08 Nov 2026 01:22
122 lines
13447 bytes
"git reflog expire" sets up the call to refs_reflog_expire() twice,
once for --all and once for refs named on the command line.
d1536ac3e2 (builtin/reflog: say "would prune" for named refs with
--dry-run, 2026-10-23) fixed the named-ref copy, which was missing
dry_run. Junio suggested a single loop in the review of that fix [1].

Collect the refs from both sources into one list and expire them in a
single loop. The names given on the command line are still looked up
in that loop, so the output and the exit status stay the same.

[1] https://lore.kernel.org/git/xmqqqzhdy7pi.fsf@gitster.g/

Assisted-by: Claude Sonnet 5.5
Signed-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>
---

Notes:
    The lookup stays in the loop. Doing it while collecting would report an
    unknown name before any expiry output, and make "expire --expire=now
    HEAD @{-1}" succeed.
    
    I ran the full test suite, and the reflog tests with reftable, sha256
    and SANITIZE=leak. Output, exit status and the resulting reflogs match
    the base for the cases I tried, including @{-1} and unknown names.

 builtin/reflog.c | 50 +++++++++++++++++++++++-------------------------
 1 file changed, 24 insertions(+), 26 deletions(-)

diff --git a/builtin/reflog.c b/builtin/reflog.c
index 49e5453dd9..42d37a3724 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -187,6 +187,10 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 	unsigned int flags = 0;
 	int verbose = 0;
 	reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
+	struct worktree_reflogs collected = {
+		.reflogs = STRING_LIST_INIT_DUP,
+	};
+	struct string_list_item *item;
 	const struct option options[] = {
 		OPT_BIT('n', "dry-run", &flags, N_("do not actually prune any entries"),
 			EXPIRE_REFLOGS_DRY_RUN),
@@ -244,10 +248,6 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 	}
 
 	if (do_all) {
-		struct worktree_reflogs collected = {
-			.reflogs = STRING_LIST_INIT_DUP,
-		};
-		struct string_list_item *item;
 		struct worktree **worktrees, **p;
 
 		worktrees = get_worktrees(the_repository);
@@ -259,44 +259,42 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 					     collect_reflog, &collected);
 		}
 		free_worktrees(worktrees);
-
-		for_each_string_list_item(item, &collected.reflogs) {
-			struct expire_reflog_policy_cb cb = {
-				.opts = opts,
-				.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
-			};
-
-			reflog_expire_options_set_refname(&cb.opts,  item->string);
-			status |= refs_reflog_expire(get_main_ref_store(the_repository),
-						     item->string, flags,
-						     reflog_expiry_prepare,
-						     should_prune_fn,
-						     reflog_expiry_cleanup,
-						     &cb);
-		}
-		string_list_clear(&collected.reflogs, 0);
 	}
 
+	/* util marks the names that still have to be looked up */
 	for (i = 0; i < argc; i++) {
-		char *ref;
+		item = string_list_append(&collected.reflogs, argv[i]);
+		item->util = (void *)1;
+	}
+
+	for_each_string_list_item(item, &collected.reflogs) {
 		struct expire_reflog_policy_cb cb = {
 			.opts = opts,
 			.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
 		};
-
-		if (!repo_dwim_log(the_repository, argv[i], strlen(argv[i]), NULL, &ref)) {
-			status |= error(_("reflog could not be found: '%s'"), argv[i]);
+		const char *refname = item->string;
+		char *ref = NULL;
+
+		if (item->util &&
+		    !repo_dwim_log(the_repository, item->string,
+				   strlen(item->string), NULL, &ref)) {
+			status |= error(_("reflog could not be found: '%s'"),
+					item->string);
 			continue;
 		}
-		reflog_expire_options_set_refname(&cb.opts, ref);
+		if (ref)
+			refname = ref;
+
+		reflog_expire_options_set_refname(&cb.opts, refname);
 		status |= refs_reflog_expire(get_main_ref_store(the_repository),
-					     ref, flags,
+					     refname, flags,
 					     reflog_expiry_prepare,
 					     should_prune_fn,
 					     reflog_expiry_cleanup,
 					     &cb);
 		free(ref);
 	}
+	string_list_clear(&collected.reflogs, 0);
 
 	reflog_clear_expire_config(&opts);
 
-- 
2.54.0
Re: [PATCH] builtin/reflog: expire --all and named refs in one loop
#2745
Author: Junio C Hamano
Date: Mon, 09 Nov 2026 20:04
21 lines
13466 bytes
Patrick Steinhardt <ps@pks.im> writes:

2 Okay, so instead of carrying the logic around `refs_reflog_expire()`
> twice you instead now first gather all reflog names that we want to
> expire, and then we do that in a single step. That seems like a useful
> simplification to me.
>
> One thing I wonder though: now that we first gather reflog names, can't
> we also easily deduplicate them? For example if the same reflog name was
> given multiple times on the command line, or in case the reflog is
> included both via a command line argument and via "--all".
>
> So I wonder whether we should both sort and deduplicate entries here.

Very good point.  I cannot think of a reason why we should not
consider it a bug that the current two-loop model processes and
reports about the same thing twice, and the collect-then-process
model allows deduplication almost for free to address that bug.

Thanks.
[PATCH v2 0/2] builtin/reflog: expire each reflog only once
#2844
Author: Laveesh Bansal
Date: Tue, 10 Nov 2026 15:26
34 lines
10701 bytes
This series makes "git reflog expire" expire each reflog only once.  1/2
puts the --all and the named-ref paths in one loop without changing
behavior.  2/2 skips a reflog that was already expired in this run, so
a name given twice, or one that --all also covers, is no longer expired
and reported twice.

v1 was the refactor alone.  I had kept it free of behavior changes and
planned to handle repeated reflogs in a later patch.  After the
discussion on v1 [1][2], I am bringing that change into v2 as a second
patch, which keeps the refactor separate.

Changes since v1: 1/2 only has a new commit message, and 2/2 is new.

I ran the full test suite, and the reflog tests with reftable, sha256
and SANITIZE=leak.  Against the base, the output differs only where a
reflog is reached twice.

Assisted-by: Claude Sonnet 5.5

[1] https://lore.kernel.org/git/avLDE1gGsjjy2ryd@pks.im/
[2] https://lore.kernel.org/git/xmqqwlqlydo2.fsf@gitster.g/


Laveesh Bansal (2):
  builtin/reflog: expire --all and named refs in one loop
  builtin/reflog: expire each reflog only once

 builtin/reflog.c  | 56 ++++++++++++++++++++++++++---------------------
 t/t1410-reflog.sh | 14 ++++++++++++
 2 files changed, 45 insertions(+), 25 deletions(-)

-- 
2.54.0
[PATCH v2 1/2] builtin/reflog: expire --all and named refs in one loop
#2843
Author: Laveesh Bansal
Date: Tue, 10 Nov 2026 15:26
110 lines
13279 bytes
"git reflog expire" calls refs_reflog_expire() from two places, one
for --all and one for the refs named on the command line, each with
its own copy of the callback setup. The copies can drift apart:
d1536ac3e2 (builtin/reflog: say "would prune" for named refs with
--dry-run, 2026-10-23) fixed a copy that was missing dry_run.

Collect the reflogs from both sources into one list and expire them
in a single loop, so the call is set up in one place. The names given
on the command line are still looked up in that loop, so the output
and the exit status stay the same.

Signed-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>
---
 builtin/reflog.c | 50 +++++++++++++++++++++++-------------------------
 1 file changed, 24 insertions(+), 26 deletions(-)

diff --git a/builtin/reflog.c b/builtin/reflog.c
index 49e5453dd9..42d37a3724 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -187,6 +187,10 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 	unsigned int flags = 0;
 	int verbose = 0;
 	reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
+	struct worktree_reflogs collected = {
+		.reflogs = STRING_LIST_INIT_DUP,
+	};
+	struct string_list_item *item;
 	const struct option options[] = {
 		OPT_BIT('n', "dry-run", &flags, N_("do not actually prune any entries"),
 			EXPIRE_REFLOGS_DRY_RUN),
@@ -244,10 +248,6 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 	}
 
 	if (do_all) {
-		struct worktree_reflogs collected = {
-			.reflogs = STRING_LIST_INIT_DUP,
-		};
-		struct string_list_item *item;
 		struct worktree **worktrees, **p;
 
 		worktrees = get_worktrees(the_repository);
@@ -259,44 +259,42 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 					     collect_reflog, &collected);
 		}
 		free_worktrees(worktrees);
-
-		for_each_string_list_item(item, &collected.reflogs) {
-			struct expire_reflog_policy_cb cb = {
-				.opts = opts,
-				.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
-			};
-
-			reflog_expire_options_set_refname(&cb.opts,  item->string);
-			status |= refs_reflog_expire(get_main_ref_store(the_repository),
-						     item->string, flags,
-						     reflog_expiry_prepare,
-						     should_prune_fn,
-						     reflog_expiry_cleanup,
-						     &cb);
-		}
-		string_list_clear(&collected.reflogs, 0);
 	}
 
+	/* util marks the names that still have to be looked up */
 	for (i = 0; i < argc; i++) {
-		char *ref;
+		item = string_list_append(&collected.reflogs, argv[i]);
+		item->util = (void *)1;
+	}
+
+	for_each_string_list_item(item, &collected.reflogs) {
 		struct expire_reflog_policy_cb cb = {
 			.opts = opts,
 			.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
 		};
-
-		if (!repo_dwim_log(the_repository, argv[i], strlen(argv[i]), NULL, &ref)) {
-			status |= error(_("reflog could not be found: '%s'"), argv[i]);
+		const char *refname = item->string;
+		char *ref = NULL;
+
+		if (item->util &&
+		    !repo_dwim_log(the_repository, item->string,
+				   strlen(item->string), NULL, &ref)) {
+			status |= error(_("reflog could not be found: '%s'"),
+					item->string);
 			continue;
 		}
-		reflog_expire_options_set_refname(&cb.opts, ref);
+		if (ref)
+			refname = ref;
+
+		reflog_expire_options_set_refname(&cb.opts, refname);
 		status |= refs_reflog_expire(get_main_ref_store(the_repository),
-					     ref, flags,
+					     refname, flags,
 					     reflog_expiry_prepare,
 					     should_prune_fn,
 					     reflog_expiry_cleanup,
 					     &cb);
 		free(ref);
 	}
+	string_list_clear(&collected.reflogs, 0);
 
 	reflog_clear_expire_config(&opts);
 
-- 
2.54.0
[PATCH v2 2/2] builtin/reflog: expire each reflog only once
#2841
Author: Laveesh Bansal
Date: Tue, 10 Nov 2026 15:26
85 lines
12287 bytes
"git reflog expire" processes a reflog once for every time it is named,
and reports it every time:

    $ git reflog expire --dry-run --verbose --expire=now main main
    would prune commit (initial): one
    would prune commit (initial): one

The same happens for two names of the same ref, such as "main" and
"refs/heads/main", and for a name that --all already covers.

Skip a reflog that this run has already processed.

Signed-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>
---

Notes:
    With --updateref, the first pass can move the ref, so a second pass
    over the same reflog could prune entries that the new tip no longer
    reaches.  It now ends as if the name had been given once.

 builtin/reflog.c  |  8 ++++++++
 t/t1410-reflog.sh | 14 ++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/builtin/reflog.c b/builtin/reflog.c
index 42d37a3724..4221136298 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -190,6 +190,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 	struct worktree_reflogs collected = {
 		.reflogs = STRING_LIST_INIT_DUP,
 	};
+	struct string_list seen = STRING_LIST_INIT_DUP;
 	struct string_list_item *item;
 	const struct option options[] = {
 		OPT_BIT('n', "dry-run", &flags, N_("do not actually prune any entries"),
@@ -285,6 +286,12 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 		if (ref)
 			refname = ref;
 
+		if (string_list_has_string(&seen, refname)) {
+			free(ref);
+			continue;
+		}
+		string_list_insert(&seen, refname);
+
 		reflog_expire_options_set_refname(&cb.opts, refname);
 		status |= refs_reflog_expire(get_main_ref_store(the_repository),
 					     refname, flags,
@@ -295,6 +302,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 		free(ref);
 	}
 	string_list_clear(&collected.reflogs, 0);
+	string_list_clear(&seen, 0);
 
 	reflog_clear_expire_config(&opts);
 
diff --git a/t/t1410-reflog.sh b/t/t1410-reflog.sh
index 998a7e8667..d22216c36d 100755
--- a/t/t1410-reflog.sh
+++ b/t/t1410-reflog.sh
@@ -274,6 +274,20 @@ test_expect_success 'reflog expire --dry-run --verbose --all' '
 	test_cmp before after
 '
 
+test_expect_success 'reflog expire does not process a reflog twice' '
+	git reflog expire --dry-run --verbose \
+		--expire=$(($test_tick - 10000)) \
+		--expire-unreachable=$(($test_tick - 10000)) \
+		--stale-fix \
+		--all >once &&
+	git reflog expire --dry-run --verbose \
+		--expire=$(($test_tick - 10000)) \
+		--expire-unreachable=$(($test_tick - 10000)) \
+		--stale-fix \
+		--all main refs/heads/main main >twice &&
+	test_cmp once twice
+'
+
 test_expect_success 'reflog expire' '
 
 	git reflog expire --verbose \
-- 
2.54.0
[PATCH v3 0/2] builtin/reflog: expire each reflog only once
#3545
Author: Laveesh Bansal
Date: Sat, 21 Nov 2026 19:53
27 lines
10303 bytes
This series makes "git reflog expire" expire each reflog only once.  1/2
puts the --all and the named-ref paths in one loop without changing
behavior.  2/2 skips a reflog that was already expired in this run.

Changes since v2, after Patrick's review [1] (1/2 is unchanged):

 - 2/2: describe the --updateref case in the commit message, and add a
   test for it.

 - 2/2: skip reflogs already seen with a strset instead of a sorted
   string_list.  The order in which reflogs are expired stays as it was.

Assisted-by: Claude Sonnet 5.5

[1] https://lore.kernel.org/git/avRiv5WpCZQCbRnZ@pks.im/

Laveesh Bansal (2):
  builtin/reflog: expire --all and named refs in one loop
  builtin/reflog: expire each reflog only once

 builtin/reflog.c  | 56 ++++++++++++++++++++++++++---------------------
 t/t1410-reflog.sh | 37 +++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+), 25 deletions(-)

-- 
2.54.0
[PATCH v3 1/2] builtin/reflog: expire --all and named refs in one loop
#3546
Author: Laveesh Bansal
Date: Sat, 21 Nov 2026 19:53
110 lines
13198 bytes
"git reflog expire" calls refs_reflog_expire() from two places, one
for --all and one for the refs named on the command line, each with
its own copy of the callback setup. The copies can drift apart:
d1536ac3e2 (builtin/reflog: say "would prune" for named refs with
--dry-run, 2026-10-23) fixed a copy that was missing dry_run.

Collect the reflogs from both sources into one list and expire them
in a single loop, so the call is set up in one place. The names given
on the command line are still looked up in that loop, so the output
and the exit status stay the same.

Signed-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>
---
 builtin/reflog.c | 50 +++++++++++++++++++++++-------------------------
 1 file changed, 24 insertions(+), 26 deletions(-)

diff --git a/builtin/reflog.c b/builtin/reflog.c
index 49e5453dd9..42d37a3724 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -187,6 +187,10 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 	unsigned int flags = 0;
 	int verbose = 0;
 	reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
+	struct worktree_reflogs collected = {
+		.reflogs = STRING_LIST_INIT_DUP,
+	};
+	struct string_list_item *item;
 	const struct option options[] = {
 		OPT_BIT('n', "dry-run", &flags, N_("do not actually prune any entries"),
 			EXPIRE_REFLOGS_DRY_RUN),
@@ -244,10 +248,6 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 	}
 
 	if (do_all) {
-		struct worktree_reflogs collected = {
-			.reflogs = STRING_LIST_INIT_DUP,
-		};
-		struct string_list_item *item;
 		struct worktree **worktrees, **p;
 
 		worktrees = get_worktrees(the_repository);
@@ -259,44 +259,42 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 					     collect_reflog, &collected);
 		}
 		free_worktrees(worktrees);
-
-		for_each_string_list_item(item, &collected.reflogs) {
-			struct expire_reflog_policy_cb cb = {
-				.opts = opts,
-				.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
-			};
-
-			reflog_expire_options_set_refname(&cb.opts,  item->string);
-			status |= refs_reflog_expire(get_main_ref_store(the_repository),
-						     item->string, flags,
-						     reflog_expiry_prepare,
-						     should_prune_fn,
-						     reflog_expiry_cleanup,
-						     &cb);
-		}
-		string_list_clear(&collected.reflogs, 0);
 	}
 
+	/* util marks the names that still have to be looked up */
 	for (i = 0; i < argc; i++) {
-		char *ref;
+		item = string_list_append(&collected.reflogs, argv[i]);
+		item->util = (void *)1;
+	}
+
+	for_each_string_list_item(item, &collected.reflogs) {
 		struct expire_reflog_policy_cb cb = {
 			.opts = opts,
 			.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
 		};
-
-		if (!repo_dwim_log(the_repository, argv[i], strlen(argv[i]), NULL, &ref)) {
-			status |= error(_("reflog could not be found: '%s'"), argv[i]);
+		const char *refname = item->string;
+		char *ref = NULL;
+
+		if (item->util &&
+		    !repo_dwim_log(the_repository, item->string,
+				   strlen(item->string), NULL, &ref)) {
+			status |= error(_("reflog could not be found: '%s'"),
+					item->string);
 			continue;
 		}
-		reflog_expire_options_set_refname(&cb.opts, ref);
+		if (ref)
+			refname = ref;
+
+		reflog_expire_options_set_refname(&cb.opts, refname);
 		status |= refs_reflog_expire(get_main_ref_store(the_repository),
-					     ref, flags,
+					     refname, flags,
 					     reflog_expiry_prepare,
 					     should_prune_fn,
 					     reflog_expiry_cleanup,
 					     &cb);
 		free(ref);
 	}
+	string_list_clear(&collected.reflogs, 0);
 
 	reflog_clear_expire_config(&opts);
 
-- 
2.54.0
[PATCH v3 2/2] builtin/reflog: expire each reflog only once
#3547
Author: Laveesh Bansal
Date: Sat, 21 Nov 2026 19:53
126 lines
13722 bytes
"git reflog expire" processes a reflog once for every time it is named,
and reports it every time:

    $ git reflog expire --dry-run --verbose --expire=now main main
    would prune commit (initial): one
    would prune commit (initial): one

The same happens for two names of the same ref, such as "main" and
"refs/heads/main", and for a name that --all already covers.

With --updateref, naming a ref twice can also lose entries.  The first
pass can move the ref to the last entry it kept, and the second pass
then prunes entries that mention commits the new tip does not reach.

Skip a reflog that this run has already processed.  Add tests for the
repeated output and for --updateref.

Signed-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>
---

Notes:
    I kept the order in which reflogs are expired.  With --updateref,
    expiring a branch can move it, and "HEAD" is checked against the tip of
    every ref, so expiring "HEAD" before or after that branch can prune
    different entries.  Sorting the names would change that order, so I
    used a strset instead.
    
    This does not catch names that reach a reflog through a worktree
    prefix, such as "HEAD" and "main-worktree/HEAD", or names that do not
    exist.  Those are still processed or reported twice.

 builtin/reflog.c  |  8 ++++++++
 t/t1410-reflog.sh | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/builtin/reflog.c b/builtin/reflog.c
index 42d37a3724..51a3d29505 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -12,6 +12,7 @@
 #include "reflog.h"
 #include "refs.h"
 #include "parse-options.h"
+#include "strmap.h"
 
 #define BUILTIN_REFLOG_SHOW_USAGE \
 	N_("git reflog [show] [<log-options>] [<ref>]")
@@ -190,6 +191,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 	struct worktree_reflogs collected = {
 		.reflogs = STRING_LIST_INIT_DUP,
 	};
+	struct strset seen = STRSET_INIT;
 	struct string_list_item *item;
 	const struct option options[] = {
 		OPT_BIT('n', "dry-run", &flags, N_("do not actually prune any entries"),
@@ -285,6 +287,11 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 		if (ref)
 			refname = ref;
 
+		if (!strset_add(&seen, refname)) {
+			free(ref);
+			continue;
+		}
+
 		reflog_expire_options_set_refname(&cb.opts, refname);
 		status |= refs_reflog_expire(get_main_ref_store(the_repository),
 					     refname, flags,
@@ -295,6 +302,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 		free(ref);
 	}
 	string_list_clear(&collected.reflogs, 0);
+	strset_clear(&seen);
 
 	reflog_clear_expire_config(&opts);
 
diff --git a/t/t1410-reflog.sh b/t/t1410-reflog.sh
index 998a7e8667..92aeff9bfe 100755
--- a/t/t1410-reflog.sh
+++ b/t/t1410-reflog.sh
@@ -274,6 +274,43 @@ test_expect_success 'reflog expire --dry-run --verbose --all' '
 	test_cmp before after
 '
 
+test_expect_success 'reflog expire does not process a reflog twice' '
+	git reflog expire --dry-run --verbose \
+		--expire=$(($test_tick - 10000)) \
+		--expire-unreachable=$(($test_tick - 10000)) \
+		--stale-fix \
+		--all >once &&
+	git reflog expire --dry-run --verbose \
+		--expire=$(($test_tick - 10000)) \
+		--expire-unreachable=$(($test_tick - 10000)) \
+		--stale-fix \
+		--all main refs/heads/main main >twice &&
+	test_cmp once twice
+'
+
+test_expect_success 'reflog expire --updateref with a ref named twice' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		git checkout -b topic &&
+		test_commit one &&
+		test_commit two &&
+		test_commit three &&
+		git reset --hard two &&
+		git reset --hard three &&
+		# Without the newest entry the reflog ends at two while
+		# topic is at three, so --updateref has a ref to move.
+		git reflog delete topic@{0} &&
+		git reflog topic >expect &&
+		git reflog expire --updateref --expire=never \
+			--expire-unreachable=now topic topic &&
+		git reflog topic >actual &&
+		test_cmp expect actual &&
+		test_cmp_rev two topic
+	)
+'
+
 test_expect_success 'reflog expire' '
 
 	git reflog expire --verbose \
-- 
2.54.0
[PATCH v4 0/2] builtin/reflog: expire each reflog only once
#4200
Author: Laveesh Bansal
Date: Tue, 01 Dec 2026 15:43
24 lines
10175 bytes
This series makes "git reflog expire" expire each reflog only once.  1/2
puts the --all and the named-ref paths in one loop without changing
behavior.  2/2 skips a reflog that was already expired in this run.

Changes since v3 after review [1]:

 - 2/2: I added a NEEDSWORK comment that says the check does not catch
   names that reach a reflog through a worktree prefix.

[1] https://lore.kernel.org/all/aw64_jhQf4a4JJQX@pks.im/

Assisted-by: Claude Sonnet 5.5

Laveesh Bansal (2):
  builtin/reflog: expire --all and named refs in one loop
  builtin/reflog: expire each reflog only once

 builtin/reflog.c  | 63 ++++++++++++++++++++++++++++-------------------
 t/t1410-reflog.sh | 37 ++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+), 26 deletions(-)

-- 
2.54.0
[PATCH v4 1/2] builtin/reflog: expire --all and named refs in one loop
#4201
Author: Laveesh Bansal
Date: Tue, 01 Dec 2026 15:43
110 lines
13234 bytes
"git reflog expire" calls refs_reflog_expire() from two places, one
for --all and one for the refs named on the command line, each with
its own copy of the callback setup. The copies can drift apart:
d1536ac3e2 (builtin/reflog: say "would prune" for named refs with
--dry-run, 2026-10-23) fixed a copy that was missing dry_run.

Collect the reflogs from both sources into one list and expire them
in a single loop, so the call is set up in one place. The names given
on the command line are still looked up in that loop, so the output
and the exit status stay the same.

Signed-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>
---
 builtin/reflog.c | 50 +++++++++++++++++++++++-------------------------
 1 file changed, 24 insertions(+), 26 deletions(-)

diff --git a/builtin/reflog.c b/builtin/reflog.c
index 49e5453dd9..42d37a3724 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -187,6 +187,10 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 	unsigned int flags = 0;
 	int verbose = 0;
 	reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
+	struct worktree_reflogs collected = {
+		.reflogs = STRING_LIST_INIT_DUP,
+	};
+	struct string_list_item *item;
 	const struct option options[] = {
 		OPT_BIT('n', "dry-run", &flags, N_("do not actually prune any entries"),
 			EXPIRE_REFLOGS_DRY_RUN),
@@ -244,10 +248,6 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 	}
 
 	if (do_all) {
-		struct worktree_reflogs collected = {
-			.reflogs = STRING_LIST_INIT_DUP,
-		};
-		struct string_list_item *item;
 		struct worktree **worktrees, **p;
 
 		worktrees = get_worktrees(the_repository);
@@ -259,44 +259,42 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 					     collect_reflog, &collected);
 		}
 		free_worktrees(worktrees);
-
-		for_each_string_list_item(item, &collected.reflogs) {
-			struct expire_reflog_policy_cb cb = {
-				.opts = opts,
-				.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
-			};
-
-			reflog_expire_options_set_refname(&cb.opts,  item->string);
-			status |= refs_reflog_expire(get_main_ref_store(the_repository),
-						     item->string, flags,
-						     reflog_expiry_prepare,
-						     should_prune_fn,
-						     reflog_expiry_cleanup,
-						     &cb);
-		}
-		string_list_clear(&collected.reflogs, 0);
 	}
 
+	/* util marks the names that still have to be looked up */
 	for (i = 0; i < argc; i++) {
-		char *ref;
+		item = string_list_append(&collected.reflogs, argv[i]);
+		item->util = (void *)1;
+	}
+
+	for_each_string_list_item(item, &collected.reflogs) {
 		struct expire_reflog_policy_cb cb = {
 			.opts = opts,
 			.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
 		};
-
-		if (!repo_dwim_log(the_repository, argv[i], strlen(argv[i]), NULL, &ref)) {
-			status |= error(_("reflog could not be found: '%s'"), argv[i]);
+		const char *refname = item->string;
+		char *ref = NULL;
+
+		if (item->util &&
+		    !repo_dwim_log(the_repository, item->string,
+				   strlen(item->string), NULL, &ref)) {
+			status |= error(_("reflog could not be found: '%s'"),
+					item->string);
 			continue;
 		}
-		reflog_expire_options_set_refname(&cb.opts, ref);
+		if (ref)
+			refname = ref;
+
+		reflog_expire_options_set_refname(&cb.opts, refname);
 		status |= refs_reflog_expire(get_main_ref_store(the_repository),
-					     ref, flags,
+					     refname, flags,
 					     reflog_expiry_prepare,
 					     should_prune_fn,
 					     reflog_expiry_cleanup,
 					     &cb);
 		free(ref);
 	}
+	string_list_clear(&collected.reflogs, 0);
 
 	reflog_clear_expire_config(&opts);
 
-- 
2.54.0
[PATCH v4 2/2] builtin/reflog: expire each reflog only once
#4202
Author: Laveesh Bansal
Date: Tue, 01 Dec 2026 15:43
131 lines
13916 bytes
"git reflog expire" processes a reflog once for every time it is named,
and reports it every time:

    $ git reflog expire --dry-run --verbose --expire=now main main
    would prune commit (initial): one
    would prune commit (initial): one

The same happens for two names of the same ref, such as "main" and
"refs/heads/main", and for a name that --all already covers.

With --updateref, naming a ref twice can also lose entries.  The first
pass can move the ref to the last entry it kept, and the second pass
then prunes entries that mention commits the new tip does not reach.

Skip a reflog that this run has already processed.  Add tests for the
repeated output and for --updateref.

Signed-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>
---

Notes:
    I kept the order in which reflogs are expired.  With --updateref,
    expiring a branch can move it, and "HEAD" is checked against the tip of
    every ref, so expiring "HEAD" before or after that branch can prune
    different entries.  Sorting the names would change that order, so I
    used a strset instead.
    
    This does not catch names that reach a reflog through a worktree
    prefix, such as "HEAD" and "main-worktree/HEAD", or names that do not
    exist.  Those are still processed or reported twice.

 builtin/reflog.c  | 13 +++++++++++++
 t/t1410-reflog.sh | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/builtin/reflog.c b/builtin/reflog.c
index 42d37a3724..e58931b28c 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -12,6 +12,7 @@
 #include "reflog.h"
 #include "refs.h"
 #include "parse-options.h"
+#include "strmap.h"
 
 #define BUILTIN_REFLOG_SHOW_USAGE \
 	N_("git reflog [show] [<log-options>] [<ref>]")
@@ -190,6 +191,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 	struct worktree_reflogs collected = {
 		.reflogs = STRING_LIST_INIT_DUP,
 	};
+	struct strset seen = STRSET_INIT;
 	struct string_list_item *item;
 	const struct option options[] = {
 		OPT_BIT('n', "dry-run", &flags, N_("do not actually prune any entries"),
@@ -285,6 +287,16 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 		if (ref)
 			refname = ref;
 
+		/*
+		 * NEEDSWORK: this does not catch names that reach the same
+		 * reflog through a worktree prefix, such as "HEAD" and
+		 * "main-worktree/HEAD".
+		 */
+		if (!strset_add(&seen, refname)) {
+			free(ref);
+			continue;
+		}
+
 		reflog_expire_options_set_refname(&cb.opts, refname);
 		status |= refs_reflog_expire(get_main_ref_store(the_repository),
 					     refname, flags,
@@ -295,6 +307,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
 		free(ref);
 	}
 	string_list_clear(&collected.reflogs, 0);
+	strset_clear(&seen);
 
 	reflog_clear_expire_config(&opts);
 
diff --git a/t/t1410-reflog.sh b/t/t1410-reflog.sh
index 998a7e8667..92aeff9bfe 100755
--- a/t/t1410-reflog.sh
+++ b/t/t1410-reflog.sh
@@ -274,6 +274,43 @@ test_expect_success 'reflog expire --dry-run --verbose --all' '
 	test_cmp before after
 '
 
+test_expect_success 'reflog expire does not process a reflog twice' '
+	git reflog expire --dry-run --verbose \
+		--expire=$(($test_tick - 10000)) \
+		--expire-unreachable=$(($test_tick - 10000)) \
+		--stale-fix \
+		--all >once &&
+	git reflog expire --dry-run --verbose \
+		--expire=$(($test_tick - 10000)) \
+		--expire-unreachable=$(($test_tick - 10000)) \
+		--stale-fix \
+		--all main refs/heads/main main >twice &&
+	test_cmp once twice
+'
+
+test_expect_success 'reflog expire --updateref with a ref named twice' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		git checkout -b topic &&
+		test_commit one &&
+		test_commit two &&
+		test_commit three &&
+		git reset --hard two &&
+		git reset --hard three &&
+		# Without the newest entry the reflog ends at two while
+		# topic is at three, so --updateref has a ref to move.
+		git reflog delete topic@{0} &&
+		git reflog topic >expect &&
+		git reflog expire --updateref --expire=never \
+			--expire-unreachable=now topic topic &&
+		git reflog topic >actual &&
+		test_cmp expect actual &&
+		test_cmp_rev two topic
+	)
+'
+
 test_expect_success 'reflog expire' '
 
 	git reflog expire --verbose \
-- 
2.54.0
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads