"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 <lave…@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