Subject: [PATCH v3] backfill: handle unexpected arguments

From: Siddharth Shrimali <[email-masked]>
Date: Sun, 22 Mar 2026 11:02:07 +0530

git backfill takes no non-option arguments. However, if extra
arguments are passed with git backfill, parse_options() leaves
them in argc and the command ignores them silently, giving the
user no indication that something is wrong.

Add a check after parse_options() to report an error if any unexpected
arguments remain. To ensure the user understands why the command
failed, print an error message specifying the unknown argument
followed by the short usage string. This matches the behavior of
other Git commands such as git bugreport.

Also, add a test in t5620 to ensure the unexpected arguments are
rejected with the correct error message and that the full option
descriptions are not printed.

Helped-by: Junio C Hamano <[email-masked]>
Signed-off-by: Siddharth Shrimali <[email-masked]>
---
Changes since v2:
- Replaced the backtick (`%s') with standard single quotes ('%s').
- Swapped `usage_with_options()` for `usage(builtin_backfill_usage[0])` 
  so the user only sees the short usage string instead of the full 
  option descriptions.
- Updated the test to also verify that the full option descriptions 
  are not printed.

 builtin/backfill.c  | 5 +++++
 t/t5620-backfill.sh | 6 ++++++
 2 files changed, 11 insertions(+)

diff --git a/builtin/backfill.c b/builtin/backfill.c
index e9a33e81be..6d90db3da0 100644
--- a/builtin/backfill.c
+++ b/builtin/backfill.c
@@ -135,6 +135,11 @@ int cmd_backfill(int argc, const char **argv, const char *prefix, struct reposit
 
 	argc = parse_options(argc, argv, prefix, options, builtin_backfill_usage,
 			     0);
+	
+	if (argc) {
+		error(_("unknown argument '%s'"), argv[0]);
+		usage(builtin_backfill_usage[0]);
+	}
 
 	repo_config(repo, git_default_config, NULL);
 
diff --git a/t/t5620-backfill.sh b/t/t5620-backfill.sh
index 58c81556e7..3f1eeb67e8 100755
--- a/t/t5620-backfill.sh
+++ b/t/t5620-backfill.sh
@@ -176,6 +176,12 @@ test_expect_success 'backfill --sparse without cone mode (negative)' '
 	test_line_count = 12 missing
 '
 
+test_expect_success 'backfill rejects unexpected arguments' '
+	test_must_fail git -C backfill1 backfill unexpected-arg >err 2>&1 &&
+	grep "unknown argument .*unexpected-arg" err &&
+	! grep "Minimum number of objects" err
+'
+
 . "$TEST_DIRECTORY"/lib-httpd.sh
 start_httpd
 
-- 
2.51.2