Skip to content

Commit 4c8db1e

Browse files
benknoblegitster
authored andcommitted
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 99caeed (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 397091d commit 4c8db1e

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

builtin/merge-recursive.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ int cmd_merge_recursive(int argc,
3838
if (argv[0] && ends_with(argv[0], "-subtree"))
3939
o.subtree_shift = "";
4040

41-
if (argc == 2 && !strcmp(argv[1], "-h")) {
41+
if (argc == 2 && (!strcmp(argv[1], "-h") ||
42+
!strcmp(argv[1], "--help-all"))) {
4243
struct strbuf msg = STRBUF_INIT;
4344
strbuf_addf(&msg, builtin_merge_recursive_usage, argv[0]);
4445
show_usage_if_asked(argc, argv, msg.buf);

git.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv, struct
445445
const char *prefix;
446446
int run_setup = (p->option & (RUN_SETUP | RUN_SETUP_GENTLY));
447447

448-
help = argc == 2 && !strcmp(argv[1], "-h");
448+
help = argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help-all"));
449449
if (help && (run_setup & RUN_SETUP))
450450
/* demote to GENTLY to allow 'git cmd -h' outside repo */
451451
run_setup = RUN_SETUP_GENTLY;

parse-options.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,10 +1461,16 @@ void show_usage_with_options_if_asked(int ac, const char **av,
14611461
const char * const *usagestr,
14621462
const struct option *opts)
14631463
{
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+
}
14681474
}
14691475
}
14701476

t/t1517-outside-repo.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ do
133133
test_expect_code 129 nongit git $cmd -h >usage &&
134134
test_grep "[Uu]sage: git $cmd " usage
135135
'
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+
'
136140
done
137141

138142
test_done

usage.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ static void show_usage_if_asked_helper(const char *err, ...)
192192

193193
void show_usage_if_asked(int ac, const char **av, const char *err)
194194
{
195-
if (ac == 2 && !strcmp(av[1], "-h"))
195+
if (ac == 2 && (!strcmp(av[1], "-h") ||
196+
!strcmp(av[1], "--help-all")))
196197
show_usage_if_asked_helper(err);
197198
}
198199

0 commit comments

Comments
 (0)