builtin: also setup gently for --help-all

Git experts often check the help summary of a command to make sure they spell options right when suggesting advice to colleagues. Further, they might check hidden options when responding to queries about deprecated options like git-rebase(1)'s "preserve merges" option. But some commands don't support "--help-all" outside of a git directory. Running (for example) git rebase --help-all outside a directory fails in "setup_git_directory", erroring with the localized form of fatal: not a git repository (or any of the parent directories): .git Like 99caeed05d (Let 'git <command> -h' show usage without a git dir, 2009-11-09), we want to show the "--help-all" output even without a git dir. Make "--help-all" where we expect "-h" to mean "setup_git_directory_gently", and interpose early in the natural place ("show_usage_with_options_if_asked"). Do the same for usage callers with show_usage_if_asked. The exception is merge-recursive, whose help block doesn't use newer APIs. Best-viewed-with: --ignore-space-change Signed-off-by: D. Ben Knoble <ben.knoble+github@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

D. Ben Knoble committed Aug 3, 2025 at 12:10 UTC 129b3632f35a1c46fb30d9e6f275a95119a9d521
5 files changed +19 -7
builtin/merge-recursive.c
+2 -1
@@ -38,7 +38,8 @@ int cmd_merge_recursive(int argc,
38 if (argv[0] && ends_with(argv[0], "-subtree"))
39 o.subtree_shift = "";
40
41 - if (argc == 2 && !strcmp(argv[1], "-h")) {
41 + if (argc == 2 && (!strcmp(argv[1], "-h") ||
42 + !strcmp(argv[1], "--help-all"))) {
43 struct strbuf msg = STRBUF_INIT;
44 strbuf_addf(&msg, builtin_merge_recursive_usage, argv[0]);
45 show_usage_if_asked(argc, argv, msg.buf);
git.c
+1 -1
@@ -445,7 +445,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv, struct
445 const char *prefix;
446 int run_setup = (p->option & (RUN_SETUP | RUN_SETUP_GENTLY));
447
448 - help = argc == 2 && !strcmp(argv[1], "-h");
448 + help = argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help-all"));
449 if (help && (run_setup & RUN_SETUP))
450 /* demote to GENTLY to allow 'git cmd -h' outside repo */
451 run_setup = RUN_SETUP_GENTLY;
parse-options.c
+10 -4
@@ -1461,10 +1461,16 @@ void show_usage_with_options_if_asked(int ac, const char **av,
1461 const char * const *usagestr,
1462 const struct option *opts)
1463 {
1464 - if (ac == 2 && !strcmp(av[1], "-h")) {
1465 - usage_with_options_internal(NULL, usagestr, opts,
1466 - USAGE_NORMAL, USAGE_TO_STDOUT);
1467 - exit(129);
1464 + if (ac == 2) {
1465 + if (!strcmp(av[1], "-h")) {
1466 + usage_with_options_internal(NULL, usagestr, opts,
1467 + USAGE_NORMAL, USAGE_TO_STDOUT);
1468 + exit(129);
1469 + } else if (!strcmp(av[1], "--help-all")) {
1470 + usage_with_options_internal(NULL, usagestr, opts,
1471 + USAGE_FULL, USAGE_TO_STDOUT);
1472 + exit(129);
1473 + }
1474 }
1475 }
1476
t/t1517-outside-repo.sh
+4
@@ -133,6 +133,10 @@ do
133 test_expect_code 129 nongit git $cmd -h >usage &&
134 test_grep "[Uu]sage: git $cmd " usage
135 '
136 + test_$expect_outcome $prereq "'git $cmd --help-all' outside a repository" '
137 + test_expect_code 129 nongit git $cmd --help-all >usage &&
138 + test_grep "[Uu]sage: git $cmd " usage
139 + '
140 done
141
142 test_done
usage.c
+2 -1
@@ -192,7 +192,8 @@ static void show_usage_if_asked_helper(const char *err, ...)
192
193 void show_usage_if_asked(int ac, const char **av, const char *err)
194 {
195 - if (ac == 2 && !strcmp(av[1], "-h"))
195 + if (ac == 2 && (!strcmp(av[1], "-h") ||
196 + !strcmp(av[1], "--help-all")))
197 show_usage_if_asked_helper(err);
198 }
199