git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/3] repack: introduce --force to force filtering

From
Christian Couder <christian.couder@gmail.com>
Date
Oct 12, 2022, 13:51 UTC
Message-ID
<20221012135114.294680-4-christian.couder@gmail.com>
In-Reply-To
<20221012135114.294680-1-christian.couder@gmail.com>

A previous commit introduced --filter=<filter-spec>, but disallowed it when stdin doesn't refer to a terminal, and asked the user when it does refer to a terminal. This is because this option is dangerous as it could lose data and corrupt the repo.

In some cases people might want to run this command automatically when stdin desn't refer to a terminal or when no question should be asked. Let's introduce --force for this purpose.

Unfortunately, we cannot use OPT__FORCE() to implement this because -f is already a repack option (to pass --no-reuse-delta to git-pack-objects), so we use OPT_BOOL() instead.

This is also a good time to test that --filter works properly as it wasn't done in the previous commit that introduced --filter.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 Documentation/git-repack.txt | 12 +++++++++++-
 builtin/repack.c             |  7 +++++--
 t/t7700-repack.sh            | 15 +++++++++++++++
 3 files changed, 31 insertions(+), 3 deletions(-)
diff --git a/Documentation/git-repack.txt b/Documentation/git-repack.txt
index 230f176e10..574f569fbe 100644
--- a/Documentation/git-repack.txt
+++ b/Documentation/git-repack.txt
@@ -142,7 +142,17 @@ depth is 4095.
 	packfile. WARNING: this could easily corrupt the current repo
 	and lose data if ANY of the omitted objects hasn't been
 	already pushed to a remote. See linkgit:git-rev-list[1] for
-	valid `<filter-spec>` forms.
+	valid `<filter-spec>` forms. See also `--force` option below.
+
+--force::
+	By default when using `--filter=<filter-spec>` and stdin
+	refers to a terminal, the user will be warned and asked if the
+	filtering repack should really be launched. This tries to
+	avoid possible data loss and repo corruption. See `--filter`
+	option above. If stdin doesn't refer to a terminal, the repack
+	is aborted. `--force` allows the repack to go on anyway when
+	stdin doesn't refer to a terminal or when no question should
+	be asked.
 
 -b::
 --write-bitmap-index::
diff --git a/builtin/repack.c b/builtin/repack.c
index 0a93f38da4..3660dbb7a7 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -56,6 +56,7 @@ struct pack_objects_args {
 	int no_reuse_object;
 	int quiet;
 	int local;
+	int force;
 };
 
 static int repack_config(const char *var, const char *value, void *cb)
@@ -793,6 +794,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
 				N_("maximum size of each packfile")),
 		OPT_STRING(0, "filter", &po_args.filter, N_("args"),
 				N_("object filtering")),
+		OPT_BOOL(0, "force", &po_args.force,
+				N_("force object filtering")),
 		OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
 				N_("repack objects in packs marked with .keep")),
 		OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
@@ -825,12 +828,12 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
 			die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-k");
 	}
 
-	if (po_args.filter) {
+	if (po_args.filter && !po_args.force) {
 		const char *yesno;
 
 		if (!isatty(STDIN_FILENO))
 			die (_("Repacking with a filter is not allowed "
-			       "yet unless a terminal is used!"));
+			       "unless a terminal is used or --force is passed!"));
 
 		/*
 		 * TRANSLATORS: Make sure to include [y] and [N] in your translation.
diff --git a/t/t7700-repack.sh b/t/t7700-repack.sh
index f8764d1dd9..20727112b5 100755
--- a/t/t7700-repack.sh
+++ b/t/t7700-repack.sh
@@ -242,6 +242,21 @@ test_expect_success 'repacking with a filter is not allowed' '
 	test_i18ngrep "Repacking with a filter is not allowed" actual
 '
 
+test_expect_success 'repacking with a filter works when --force is passed' '
+	test_when_finished "rm -rf server client" &&
+	test_create_repo server &&
+	git -C server config uploadpack.allowFilter true &&
+	git -C server config uploadpack.allowAnySHA1InWant true &&
+	test_commit -C server 1 &&
+	git clone --bare --no-local server client &&
+	git -C client config remote.origin.promisor true &&
+	git -C client rev-list --objects --all --missing=print >objects &&
+	test $(grep "^?" objects | wc -l) = 0 &&
+	git -C client -c repack.writebitmaps=false repack -a -d --filter=blob:none --force &&
+	git -C client rev-list --objects --all --missing=print >objects &&
+	test $(grep "^?" objects | wc -l) = 1
+'
+
 objdir=.git/objects
 midx=$objdir/pack/multi-pack-index
 
-- 
2.38.0.4.g7f9724c7bf.dirty
Previous: Christian CouderNext: Junio C Hamano
Message 4 of 46 in “Implement filtering repacks”
  1. 0/3 Implement filtering repacksChristian Couder, Oct 12, 2022
  2. 1/3 pack-objects: allow --filter without --stdoutChristian Couder, Oct 12, 2022
  3. 2/3 repack: add --filter=<filter-spec> optionChristian Couder, Oct 12, 2022
  4. 3/3 repack: introduce --force to force filteringChristian Couder, Oct 12, 2022
  5. Junio C HamanoOct 14, 2022
  6. Christian CouderOct 20, 2022
  7. Taylor BlauOct 28, 2022
  8. Junio C HamanoOct 28, 2022
  9. Christian CouderNov 7, 2022
  10. Christian CouderNov 7, 2022
  11. 0/2 Implement filtering repacksChristian Couder, Oct 25, 2022
  12. 1/2 pack-objects: allow --filter without --stdoutChristian Couder, Oct 25, 2022
  13. 2/2 repack: add --filter=<filter-spec> optionChristian Couder, Oct 25, 2022
  14. Taylor BlauOct 28, 2022
  15. Christian CouderNov 7, 2022
  16. 0/2 Implement filtering repacksChristian Couder, Nov 22, 2022
  17. 1/2 pack-objects: allow --filter without --stdoutChristian Couder, Nov 22, 2022
  18. 2/2 repack: add --filter=<filter-spec> optionChristian Couder, Nov 22, 2022
  19. Junio C HamanoNov 23, 2022
  20. Christian CouderDec 21, 2022
  21. Junio C HamanoNov 23, 2022
  22. 0/3 Implement filtering repacksChristian Couder, Dec 21, 2022
  23. 1/3 pack-objects: allow --filter without --stdoutChristian Couder, Dec 21, 2022
  24. Patrick SteinhardtJan 4, 2023
  25. 2/3 repack: add --filter=<filter-spec> optionChristian Couder, Dec 21, 2022
  26. Patrick SteinhardtJan 4, 2023
  27. Junio C HamanoJan 5, 2023
  28. 3/3 gc: add gc.repackFilter config optionChristian Couder, Dec 21, 2022
  29. Patrick SteinhardtJan 4, 2023
  30. 0/3 upload-pack: support a missing-actionChristian Couder, May 15, 2024
  31. 1/3 rev-list: refactor --missing=<missing-action>Christian Couder, May 15, 2024
  32. Junio C HamanoMay 15, 2024
  33. 2/3 pack-objects: use the missing action APIChristian Couder, May 15, 2024
  34. Junio C HamanoMay 15, 2024
  35. Christian CouderMay 24, 2024
  36. 3/3 upload-pack: allow configuring a missing-actionChristian Couder, May 15, 2024
  37. Junio C HamanoMay 15, 2024
  38. Christian CouderMay 24, 2024
  39. Junio C HamanoMay 24, 2024
  40. Christian CouderMay 28, 2024
  41. Junio C HamanoMay 28, 2024
  42. Christian CouderMay 31, 2024
  43. Junio C HamanoJun 1, 2024
  44. Christian CouderJun 3, 2024
  45. Junio C HamanoJun 3, 2024
  46. Christian CouderMay 15, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.