All of lore.kernel.org
 help / color / mirror / Atom feed
From: Siddharth Shrimali <r.siddharth.shrimali@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, christian.couder@gmail.com,
	siddharthasthana31@gmail.com, ttaylorr@openai.com, ps@pks.im,
	johannes.schindelin@gmx.de, l.s.r@web.de,
	r.siddharth.shrimali@gmail.com
Subject: [GSoC PATCH v5 6/6] builtin/repack: add guards for --drop-filtered
Date: Fri, 14 Aug 2026 01:38:30 +0530	[thread overview]
Message-ID: <20260813200830.84348-7-r.siddharth.shrimali@gmail.com> (raw)
In-Reply-To: <20260813200830.84348-1-r.siddharth.shrimali@gmail.com>

--drop-filtered removes local promisor blobs. That is only safe when the
repository is not mid-operation and when the blobs are not actively in
use, so add two guards, both skipped for bare repositories which have
neither a worktree nor an index.

First, refuse to run while a merge, rebase, am, cherry-pick, revert, or
bisect is in progress. During these operations the working tree and
index are in an intermediate state, and rewriting packs and deleting
objects underneath a half-finished operation is unsafe.

Second, refuse to drop a blob that the current index references. Such a
blob is needed by the working tree, so dropping it would only cause the
next command that touches the worktree to lazy-fetch it straight back,
reclaiming nothing. The offending path is reported so the user can see
why the drop was refused.

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Siddharth Asthana <siddharthasthana31@gmail.com>
Signed-off-by: Siddharth Shrimali <r.siddharth.shrimali@gmail.com>
---
 Documentation/git-repack.adoc   |  9 ++++++
 builtin/repack.c                | 52 +++++++++++++++++++++++++++++++++
 t/t7706-repack-drop-filtered.sh | 35 ++++++++++++++++++++++
 3 files changed, 96 insertions(+)

diff --git a/Documentation/git-repack.adoc b/Documentation/git-repack.adoc
index 130249a139..a1f9e64f66 100644
--- a/Documentation/git-repack.adoc
+++ b/Documentation/git-repack.adoc
@@ -204,6 +204,15 @@ and with bitmap writing (`-b`/`--write-bitmap-index`), since filtering
 breaks the single-pack closure that bitmaps require. A bitmap setting
 coming from configuration is silently disabled for the duration of the
 command.
++
+As a convenience, since dropped objects remain recoverable by lazy fetch,
+`--drop-filtered` refuses to run while another operation
+(merge, rebase, am, cherry-pick, revert, or bisect) is in progress, to
+avoid a surprising network fetch mid-operation, and refuses to drop any
+blob that the current index references, since such a blob would only be
+lazily re-fetched by the next command that inspects the working tree.
+These checks are skipped in bare repositories, which have neither a
+working tree nor an index.
 
 --dry-run::
 	Only meaningful with `--drop-filtered`. List the objects that
diff --git a/builtin/repack.c b/builtin/repack.c
index a5f13fdd87..c4360382c1 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -17,6 +17,8 @@
 #include "list-objects-filter-options.h"
 #include "oidset.h"
 #include "hex.h"
+#include "wt-status.h"
+#include "read-cache-ll.h"
 
 #define ALL_INTO_ONE 1
 #define LOOSEN_UNREACHABLE 2
@@ -317,6 +319,33 @@ int cmd_repack(int argc,
 		if (!repo_has_promisor_remote(repo))
 			die(_("--drop-filtered requires a promisor remote"));
 
+		/*
+		 * Refuse to run while another operation is in progress. A
+		 * dropped object would just be lazily re-fetched when the
+		 * operation resumes, but triggering a network fetch in the
+		 * middle of a half-finished
+		 * merge/rebase/cherry-pick/revert/bisect is a poor
+		 * experience, so this is a UX convenience rather than a
+		 * safety measure. Bare repositories have no such state, so
+		 * the check is skipped there.
+		 */
+		if (!is_bare_repository(repo)) {
+			struct wt_status_state state = { 0 };
+
+			wt_status_get_state(repo, &state, 0);
+			if (state.merge_in_progress || state.revert_in_progress ||
+			    state.rebase_in_progress || state.bisect_in_progress ||
+			    state.cherry_pick_in_progress || state.am_in_progress ||
+			    state.rebase_interactive_in_progress) {
+				wt_status_state_free_buffers(&state);
+				die(_("--drop-filtered cannot be used while "
+				      "another operation (merge, rebase, am, "
+				      "cherry-pick, revert, or bisect) is in "
+				      "progress"));
+			}
+			wt_status_state_free_buffers(&state);
+		}
+
 		write_bitmaps = 0;
 
 		/*
@@ -332,6 +361,29 @@ int cmd_repack(int argc,
 		if (ret)
 			goto cleanup;
 
+		/*
+		 * Refuse to drop blobs that the current index references.
+		 * Such a blob would only be lazily re-fetched by the next
+		 * command that touches the worktree, so dropping it reclaims
+		 * nothing. This guard just avoids that churn. Bare
+		 * repositories have no index, so the check is skipped there.
+		 */
+		if (!is_bare_repository(repo) && oidset_size(&drop_oids)) {
+			struct index_state *istate = repo->index;
+			unsigned int i;
+
+			if (repo_read_index(repo) < 0)
+				die(_("could not read the index"));
+
+			for (i = 0; i < istate->cache_nr; i++) {
+				const struct cache_entry *ce = istate->cache[i];
+
+				if (oidset_contains(&drop_oids, &ce->oid))
+					die(_("cannot drop '%s' (%s): it is referenced by the current index"),
+						ce->name, oid_to_hex(&ce->oid));
+			}
+		}
+
 		if (dry_run) {
 			struct oidset_iter iter;
 			const struct object_id *oid;
diff --git a/t/t7706-repack-drop-filtered.sh b/t/t7706-repack-drop-filtered.sh
index 80c695742f..cb36115834 100755
--- a/t/t7706-repack-drop-filtered.sh
+++ b/t/t7706-repack-drop-filtered.sh
@@ -147,4 +147,39 @@ test_expect_success '--drop-filtered removes the promisor blob locally' '
 	test_grep "$SMALL" present
 '
 
+test_expect_success '--drop-filtered refuses when a merge is in progress' '
+	test_when_finished "git -C repo merge --abort || :" &&
+
+	# Create a conflicting merge so wt_status reports it.
+	git -C repo checkout -B mergebase base &&
+	echo one >repo/conflict.txt &&
+	git -C repo add conflict.txt &&
+	git -C repo commit -m one &&
+
+	git -C repo checkout -B mergeother base &&
+	echo two >repo/conflict.txt &&
+	git -C repo add conflict.txt &&
+	git -C repo commit -m two &&
+
+	test_must_fail git -C repo merge mergebase &&
+
+	test_must_fail git -C repo -c repack.writeBitmaps=false \
+		repack --drop-filtered --filter=blob:limit=1k --dry-run -a 2>err &&
+	test_grep "in progress" err
+'
+
+test_expect_success '--drop-filtered refuses to drop an index-referenced blob' '
+	# Create a large blob, add it to the index and make it a promisor object
+	# so the index references it and enumeration picks it up.
+	test-tool genrandom idx 4096 >repo/tracked-big.bin &&
+	git -C repo add tracked-big.bin &&
+	OID=$(git -C repo rev-parse :tracked-big.bin) &&
+	printf "%s\n" "$OID" | pack_as_from_promisor >/dev/null &&
+	delete_object repo "$OID" &&
+
+	test_must_fail git -C repo -c repack.writeBitmaps=false \
+		repack --drop-filtered --filter=blob:limit=1k --dry-run -a 2>err &&
+	test_grep "referenced by the current index" err
+'
+
 test_done
-- 
2.54.0


  parent reply	other threads:[~2026-08-13 20:09 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-16 13:28 [RFC PATCH 0/7] repack: add --drop-filtered to reclaim space in partial clones Siddharth Shrimali
2026-07-16 13:28 ` [RFC PATCH 1/7] builtin/repack.c: add --drop-filtered and --dry-run options Siddharth Shrimali
2026-07-16 21:08   ` Junio C Hamano
2026-07-17 18:00     ` Siddharth Shrimali
2026-07-23 19:31     ` Siddharth Asthana
2026-07-18 12:30   ` Christian Couder
2026-07-20 10:19     ` Siddharth Shrimali
2026-07-16 13:28 ` [RFC PATCH 2/7] list-objects-filter: add list_objects_filter__filter_oidset() Siddharth Shrimali
2026-07-16 13:28 ` [RFC PATCH 3/7] repack-promisor: allow excluding objects from the rebuilt promisor pack Siddharth Shrimali
2026-07-16 13:28 ` [RFC PATCH 4/7] builtin/repack: enumerate promisor blobs for --drop-filtered Siddharth Shrimali
2026-07-16 13:28 ` [RFC PATCH 5/7] t7706: test --drop-filtered enumeration and validation Siddharth Shrimali
2026-07-16 13:28 ` [RFC PATCH 6/7] builtin/repack: actually drop filtered promisor blobs Siddharth Shrimali
2026-07-23 19:42   ` Siddharth Asthana
2026-07-25 19:30     ` Siddharth Shrimali
2026-07-16 13:28 ` [RFC PATCH 7/7] repack-promisor: record dropped objects in a drop log Siddharth Shrimali
2026-07-23 19:41   ` Siddharth Asthana
2026-07-25 19:35     ` Siddharth Shrimali
2026-07-23 19:26 ` [RFC PATCH 0/7] repack: add --drop-filtered to reclaim space in partial clones Siddharth Asthana
2026-07-25 19:23   ` Siddharth Shrimali
2026-07-30 17:41 ` [GSoC PATCH v2 " Siddharth Shrimali
2026-07-30 17:41   ` [GSoC PATCH v2 1/7] builtin/repack.c: add --drop-filtered and --dry-run options Siddharth Shrimali
2026-08-04 21:13     ` Siddharth Asthana
2026-07-30 17:41   ` [GSoC PATCH v2 2/7] list-objects-filter: add list_objects_filter__filter_oidset() Siddharth Shrimali
2026-07-30 17:41   ` [GSoC PATCH v2 3/7] repack-promisor: allow excluding objects from the rebuilt promisor pack Siddharth Shrimali
2026-07-30 17:41   ` [GSoC PATCH v2 4/7] builtin/repack: enumerate promisor blobs for --drop-filtered Siddharth Shrimali
2026-07-30 17:41   ` [GSoC PATCH v2 5/7] builtin/repack: actually drop filtered promisor blobs Siddharth Shrimali
2026-07-30 17:41   ` [GSoC PATCH v2 6/7] builtin/repack: add safety guards for --drop-filtered Siddharth Shrimali
2026-08-04 21:13     ` Siddharth Asthana
2026-07-30 17:41   ` [GSoC PATCH v2 7/7] Documentation/git-repack: document --drop-filtered and --dry-run Siddharth Shrimali
2026-07-31 15:33   ` [GSoC PATCH v2 0/7] repack: add --drop-filtered to reclaim space in partial clones Junio C Hamano
2026-08-01 18:19     ` Siddharth Shrimali
2026-08-02  2:18       ` Junio C Hamano
2026-08-02 11:28         ` Siddharth Shrimali
2026-08-04 21:12   ` Siddharth Asthana
2026-08-06 11:21   ` [GSoC PATCH v3 " Siddharth Shrimali
2026-08-06 11:21     ` [GSoC PATCH v3 1/7] builtin/repack.c: add --drop-filtered and --dry-run options Siddharth Shrimali
2026-08-06 11:21     ` [GSoC PATCH v3 2/7] list-objects-filter: add list_objects_filter__filter_oidset() Siddharth Shrimali
2026-08-06 11:21     ` [GSoC PATCH v3 3/7] repack-promisor: allow excluding objects from the rebuilt promisor pack Siddharth Shrimali
2026-08-06 11:21     ` [GSoC PATCH v3 4/7] builtin/repack: enumerate promisor blobs for --drop-filtered Siddharth Shrimali
2026-08-06 11:22     ` [GSoC PATCH v3 5/7] builtin/repack: actually drop filtered promisor blobs Siddharth Shrimali
2026-08-06 11:22     ` [GSoC PATCH v3 6/7] builtin/repack: add guards for --drop-filtered Siddharth Shrimali
2026-08-06 11:22     ` [GSoC PATCH v3 7/7] Documentation/git-repack: document --drop-filtered and --dry-run Siddharth Shrimali
2026-08-06 21:34     ` [GSoC PATCH v3 0/7] repack: add --drop-filtered to reclaim space in partial clones Junio C Hamano
2026-08-06 22:19     ` Junio C Hamano
2026-08-07  9:06       ` Siddharth Shrimali
2026-08-07 20:52         ` Junio C Hamano
2026-08-08 20:07           ` Siddharth Shrimali
2026-08-10 17:40     ` [GSoC PATCH v4 " Siddharth Shrimali
2026-08-10 17:40       ` [GSoC PATCH v4 1/7] builtin/repack.c: add --drop-filtered and --dry-run options Siddharth Shrimali
2026-08-10 17:40       ` [GSoC PATCH v4 2/7] list-objects-filter: add list_objects_filter__filter_oidset() Siddharth Shrimali
2026-08-12 17:09         ` Christian Couder
2026-08-12 18:04         ` Junio C Hamano
2026-08-12 19:22           ` Siddharth Shrimali
2026-08-10 17:40       ` [GSoC PATCH v4 3/7] repack-promisor: allow excluding objects from the rebuilt promisor pack Siddharth Shrimali
2026-08-10 17:40       ` [GSoC PATCH v4 4/7] builtin/repack: enumerate promisor blobs for --drop-filtered Siddharth Shrimali
2026-08-12 17:21         ` Christian Couder
2026-08-10 17:40       ` [GSoC PATCH v4 5/7] builtin/repack: actually drop filtered promisor blobs Siddharth Shrimali
2026-08-10 17:40       ` [GSoC PATCH v4 6/7] builtin/repack: add guards for --drop-filtered Siddharth Shrimali
2026-08-12 17:41         ` Christian Couder
2026-08-12 19:45           ` Siddharth Shrimali
2026-08-10 17:40       ` [GSoC PATCH v4 7/7] Documentation/git-repack: document --drop-filtered and --dry-run Siddharth Shrimali
2026-08-12 17:51         ` Christian Couder
2026-08-11 17:50       ` [GSoC PATCH v4 0/7] repack: add --drop-filtered to reclaim space in partial clones Junio C Hamano
2026-08-12  7:35         ` Siddharth Shrimali
2026-08-13 20:08       ` [GSoC PATCH v5 0/6] " Siddharth Shrimali
2026-08-13 20:08         ` [GSoC PATCH v5 1/6] builtin/repack: add --drop-filtered and --dry-run options Siddharth Shrimali
2026-08-13 20:08         ` [GSoC PATCH v5 2/6] list-objects-filter: add list_objects_filter__filter_oidset() Siddharth Shrimali
2026-08-13 20:08         ` [GSoC PATCH v5 3/6] repack-promisor: allow excluding objects from the rebuilt promisor pack Siddharth Shrimali
2026-08-13 20:08         ` [GSoC PATCH v5 4/6] builtin/repack: enumerate promisor blobs for --drop-filtered Siddharth Shrimali
2026-08-13 20:08         ` [GSoC PATCH v5 5/6] builtin/repack: actually drop filtered promisor blobs Siddharth Shrimali
2026-08-13 20:08         ` Siddharth Shrimali [this message]
2026-08-14 17:12         ` [GSoC PATCH v5 0/6] repack: add --drop-filtered to reclaim space in partial clones Christian Couder
2026-08-14 17:17           ` Junio C Hamano
2026-08-14 19:32             ` Siddharth Shrimali

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260813200830.84348-7-r.siddharth.shrimali@gmail.com \
    --to=r.siddharth.shrimali@gmail.com \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=johannes.schindelin@gmx.de \
    --cc=l.s.r@web.de \
    --cc=ps@pks.im \
    --cc=siddharthasthana31@gmail.com \
    --cc=ttaylorr@openai.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.