Article View: linux.kernel.git
Article #4202[PATCH v4 2/2] builtin/reflog: expire each reflog only once
From: Laveesh Bansal
Date: Tue, 01 Dec 2026 15:43
Date: Tue, 01 Dec 2026 15:43
131 lines
13916 bytes
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
Technical Details
Message-ID:
<28d98e8747088a81de71dc295cdc915d94a3977e.1796138650.git.laveeshb@laveeshbansal.com>Path:
mbox-mo1.pugleaf.net!news-lon.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76794-linux-kernel-git=dewy.ruReferences:
<cover.1795290319.git.laveeshb@laveeshbansal.com> <cover.1796138650.git.laveeshb@laveeshbansal.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lon.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76794-linux-kernel-git=dewy.ru From: laveeshb@laveeshbansal.com (Laveesh Bansal) Newsgroups: linux.kernel.git Subject: [PATCH v4 2/2] builtin/reflog: expire each reflog only once Date: Tue, 1 Dec 2026 07:43:05 -0800 Organization: Usenet Network Message-ID: <28d98e8747088a81de71dc295cdc915d94a3977e.1796138650.git.laveeshb@laveeshbansal.com> References: <cover.1795290319.git.laveeshb@laveeshbansal.com> <cover.1796138650.git.laveeshb@laveeshbansal.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="1751756"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:dWLZ98PZX64cRhUL2EDtOYDLHCA= sha256:RtUBhiDwjmA77EztQxAAtZEi1gi8Bsboz9vA2WJl0Jg= sha1:Xg8dYwa6ekXInG0YE28UCAJXAWI= sha256:dUCLpYHNUl1gEAy1mjLHsEHTH8U0VUZymQMMVRq5amY= List-ID: <git.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id D1262117408; Tue, 1 Dec 2026 18:43:52 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -2.75 X-Spam-Status: No, score=-2.75 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=fail (1024-bit key) reason="fail (message has been altered)" header.d=laveeshbansal.com X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ukVc-Vgg4ouH; Tue, 1 Dec 2026 18:43:49 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=git+bounces-76794-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=laveeshbansal.com X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 939AA1173DB; Tue, 1 Dec 2026 18:43:41 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id ACA2D29D8C; Tue, 1 Dec 2026 15:43:39 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796139819; b=FEqhzlEMGcHwUp5H3xO1l1y/bLZA3P57q/psCESdf36MieNOeaom59WvH5a1spOhiv+J yWLQi6lHG3ox5DDJK6RrG+39RS774rnEZYToZjmNCaxzfokzlz4+WeC+k++Joggk+fUK+ yuYDOQT50dpOzUOGRmbdIqsuYvH1LyNo5UeghZL+KP8AbfYfYdQqia6Dpdg2wcKxw+XpY h24+y888PzKRBfvpkrGA448xO4mxalSUxgOadUXgi4VTWpUDeqi1uGn4C1J5dr45IiDUW jVw8R66g9BUc+hoMzE1UdoEgC6gWOnHcLmSadz3LdalD+wxcAgfzBjnNvjXBZ7ySbFA== X-ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796139819; h=X-Original-To:Received:DMARC-Filter:DKIM-Signature:Received:From:To: Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version: Content-Transfer-Encoding:X-ZohoMailClient; bh=aSRmDWcnvqGgpiHgzXWEaNwrBOGWaao64urTXxYjlfo=; b=RkofOl68qCYgTEmiK4FT5EqAbU4i/qKkmD3kv/3578bDo8vAbQV3/+lHRvEmy15rTeiQ RJ3ny/JiSZP/IdB5Q5hkDH25GcAoWwk467UbJFIHwABwFe69l9OYdYEUWpOpuM/IklIna JIZeGN2cTFv1Hp48h1Vl7xXJ0IrL19kULVWS2ZnXpifbdMGY2ktI+Xn6fE0IOKGSnMFZA pagU5MdPPq0LEgOhxfgN2wMWhgsEO6+EenjKXQVYaZKry5/+mNyMPMQDLjvKRvCybn+0m MRw/h73wmoL3D3/J2Sj2upBZwT43pExdekw0o4/81fpO9gIZDmyUErgSRHTVfdNeYKA== X-ARC-Authentication-Results: i=3; smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" header.d=laveeshbansal.com header.i=@laveeshbansal.com header.a=rsa-sha256 header.s=zoho header.b=J0B9ZpQ0; dmarc=none header.from=laveeshbansal.com; spf=pass smtp.mailfrom=laveeshbansal.com; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 825CE606A322; Tue, 1 Dec 2026 15:43:39 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=laveeshbansal.com header.i=@laveeshbansal.com header.a=rsa-sha256 header.s=zoho header.b=J0B9ZpQ0 X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.182.16 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796139818; b=t3rDRZDiGDFvfJ8zRKxQAAhWanzfjV2fwsLCP2xUmmtxyv/aMqA77RbznqdLsh2QV+QA sKPQSKpwIVXnnDoFZ+SituhN9dy0QiimgWKTupU/yWcj5bfoy9czRI87AbSs8fzEYAvP1 qqB5t1wt7U2difDrhtkG0+twdvypjh5N2QK5Q9MjL7pxoMCwth5QbZ6xXAbO6F2I4mvl3 shEkL3CW6pCM5Tg2LJgfOkUnJb4t0H4Bl7Q6X/nBFdYIN1feoHzoKbs1JX0xYR/0Vl7M2 56e0lse6QwGM9cnUm5fYSavT9mwEV3PVyb7rBPLqTfK3gXNonAQlBqmgo9FK4U/ZCog== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796139818; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-ZohoMailClient; bh=aSRmDWcnvqGgpiHgzXWEaNwrBOGWaao64urTXxYjlfo=; b=i1dNzPrqlAGwvq2q1TgHYnbsWI436o2X9vJHj8DXbJBQPJsiMTjt0BwZBfvZNZu7wQFL GOz4W8MWWq7cmRvYG7Gd/8pUTQrDt9+ZyseGKC5yZDLy2W9nfobFQvbOzRPeRVTFZ1Q3I Gzncw0JS91b1Gyj9KKT/T7mF4N/35WOPOANJUTOG3N+hH8CSXulQh0YgFgWrlRjY+6VVG fXqvrO9ajCG1MDKeP45pOy/ToUskjsw8C3UNQlors/Kux7W/d5vgOTd9rnXce+WAsP+hU sO/K98idKmeDaH2ddCmjmktkFUVd0WGyrwChJ4qeWViKlMof7HL+vDKrzLyvBPCSVXw== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none header.from=laveeshbansal.com; spf=pass smtp.mailfrom=laveeshbansal.com; dkim=pass header.d=laveeshbansal.com header.i=@laveeshbansal.com header.a=rsa-sha256 header.s=zoho header.b=J0B9ZpQ0; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.182.16 X-Received: from sender5-op-o16.zoho.com (sender5-op-o16.zoho.com [165.173.182.16]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 48E11606A320 for <git@vger.kernel.org>; Tue, 1 Dec 2026 15:43:38 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 48E11606A320 X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=laveeshbansal.com X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=laveeshbansal.com X-ARC-Seal: i=1; a=rsa-sha256; t=1796139801; cv=none; d=zohomail.com; s=zohoarc; b=X8OOsmUFRN1AY5gl1AtzTtqtsDOuwop2Nk750GVl0QrBzOtFC6joH5n5zue+2JDMqZUY/8I2bkVsxzCyopbcuBGcbTpCg65OveSsWAwz+aFF99WvnEppe15kJKC/SZ5pHnoqmwbMjz2YRE1NFqDC9MTm0IWYDy6zzUezR5NnrvA= X-ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1796139801; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=aSRmDWcnvqGgpiHgzXWEaNwrBOGWaao64urTXxYjlfo=; b=CdrmWDa7Le39vhfnJUo5sDMb+11HAp6Xd9o9uigphCbEisema/y867gb212KZJ3xRqZI/0eC78++FS1tXZy3yZuI2ZxgtYoPT3eoHQu7cZK0axuwS9s/yUhepS0IjZFQpSbOABk+PtnzxH1l6hh/Zh5oE8WmgW2zkntor0vMLhk= X-ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=laveeshbansal.com; spf=pass smtp.mailfrom=laveeshb@laveeshbansal.com; dmarc=pass header.from=<laveeshb@laveeshbansal.com> X-DKIM-Signature: a=rsa-sha256; b=J0B9ZpQ0ye8kXL/eYLN+Ie+dmgssZH46+t0Q24RMX7MMvhTfg+unKSEU/Bqeb6Elm9RCKFAiAmyrVywRWrruPKmQ82qXTeWBg125MdOdLxCJKSZwSgfXH5Rew0KUJKMvJl/Pxb+i6Ffvp65ScNNxgztvKAMun8HH4oTdUEIP3pI=; c=relaxed/relaxed; s=zoho; d=laveeshbansal.com; v=1; bh=aSRmDWcnvqGgpiHgzXWEaNwrBOGWaao64urTXxYjlfo=; h=Content-Description:Content-ID:Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Resent-Sender:Resent-Cc:Resent-Date:Resent-To:Reply-To:Resent-Message-ID:Resent-From:Sender:Subject:To:Date:From:Message-Id:Reply-To:Subject:To:Cc; X-Received: by smtp.zohomail.com with SMTPS id 1796139796408874.6975260632073; Tue, 1 Dec 2026 07:43:16 -0800 (PST) X-Cc: Junio C Hamano <gitster@pobox.com>, Patrick Steinhardt <ps@pks.im>, Karthik Nayak <karthik.188@gmail.com>, Ævar Arnfjörð Bjarmason <avarab@gmail.com> X-Mailer: git-send-email 2.54.0 X-In-Reply-To: <cover.1796138650.git.laveeshb@laveeshbansal.com> X-Mailing-List: git@vger.kernel.org X-List-Post: <mailto:git@vger.kernel.org> X-List-Help: <mailto:git+help@vger.kernel.org> X-List-Subscribe: <mailto:git+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:git+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:git+owner@vger.kernel.org> X-Precedence: list X-ZohoMailClient: External