cli: add -v and -h shorthands

Change the behavior of "git -v" to be synonymous with "--version" / "version", and "git -h" to be synonymous with "--help", but not "help". These shorthands both display the "unknown option" message. Following this change, "-v" displays the version, and "-h" displays the help text of the "git" command. It should be noted that the "-v" shorthand could be misinterpreted by the user to mean "verbose" instead of "version", since some sub-commands make use of it in this context. The top-level "git" command does not have a "verbose" flag, so it's safe to introduce this shorthand unambiguously. Signed-off-by: Garrit Franke <garrit@slashdev.space> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Garrit Franke committed Mar 31, 2022 at 23:27 UTC 6b52f48b8f1cda8d428d3e4fe685bd28713f293f
3 files changed +11 -6
Documentation/git.txt
+3 -1
@@ -9,7 +9,7 @@ git - the stupid content tracker
9 SYNOPSIS
10 --------
11 [verse]
12 -'git' [--version] [--help] [-C <path>] [-c <name>=<value>]
12 +'git' [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
13 [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
14 [-p|--paginate|-P|--no-pager] [--no-replace-objects] [--bare]
15 [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
@@ -39,6 +39,7 @@ or https://git-scm.com/docs.
39
40 OPTIONS
41 -------
42 +-v::
43 --version::
44 Prints the Git suite version that the 'git' program came from.
45 +
@@ -46,6 +47,7 @@ This option is internally converted to `git version ...` and accepts
47 the same options as the linkgit:git-version[1] command. If `--help` is
48 also given, it takes precedence over `--version`.
49
50 +-h::
51 --help::
52 Prints the synopsis and a list of the most commonly used
53 commands. If the option `--all` or `-a` is given then all
git.c
+7 -4
@@ -25,7 +25,7 @@ struct cmd_struct {
25 };
26
27 const char git_usage_string[] =
28 - N_("git [--version] [--help] [-C <path>] [-c <name>=<value>]\n"
28 + N_("git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]\n"
29 " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
30 " [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]\n"
31 " [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
@@ -146,7 +146,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
146 * commands can be written with "--" prepended
147 * to make them look like flags.
148 */
149 - if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
149 + if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h") ||
150 + !strcmp(cmd, "--version") || !strcmp(cmd, "-v"))
151 break;
152
153 /*
@@ -892,8 +893,10 @@ int cmd_main(int argc, const char **argv)
893 argc--;
894 handle_options(&argv, &argc, NULL);
895 if (argc > 0) {
895 - /* translate --help and --version into commands */
896 - skip_prefix(argv[0], "--", &argv[0]);
896 + if (!strcmp("--version", argv[0]) || !strcmp("-v", argv[0]))
897 + argv[0] = "version";
898 + else if (!strcmp("--help", argv[0]) || !strcmp("-h", argv[0]))
899 + argv[0] = "help";
900 } else {
901 /* The user didn't specify a command; give them help */
902 commit_pager_choice();
t/t0012-help.sh
+1 -1
@@ -181,7 +181,7 @@ for cmd in git "git help"
181 do
182 test_expect_success "'$cmd' section spacing" '
183 test_section_spacing_trailer git help <<-\EOF &&
184 - usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
184 + usage: git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
185
186 These are common Git commands used in various situations:
187