]> git.ipfire.org Git - thirdparty/git.git/commitdiff
cli: add -v and -h shorthands
authorGarrit Franke <garrit@slashdev.space>
Thu, 31 Mar 2022 21:27:09 +0000 (23:27 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 31 Mar 2022 22:57:10 +0000 (15:57 -0700)
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>
Documentation/git.txt
git.c
t/t0012-help.sh

index 13f83a2a3a12209791de1032839764a493ec4457..302607a49676d59aef4f1f8d39d3a302b7b66cc2 100644 (file)
@@ -9,7 +9,7 @@ git - the stupid content tracker
 SYNOPSIS
 --------
 [verse]
-'git' [--version] [--help] [-C <path>] [-c <name>=<value>]
+'git' [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
     [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
     [-p|--paginate|-P|--no-pager] [--no-replace-objects] [--bare]
     [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
@@ -39,6 +39,7 @@ or https://git-scm.com/docs.
 
 OPTIONS
 -------
+-v::
 --version::
        Prints the Git suite version that the 'git' program came from.
 +
@@ -46,6 +47,7 @@ This option is internally converted to `git version ...` and accepts
 the same options as the linkgit:git-version[1] command. If `--help` is
 also given, it takes precedence over `--version`.
 
+-h::
 --help::
        Prints the synopsis and a list of the most commonly used
        commands. If the option `--all` or `-a` is given then all
diff --git a/git.c b/git.c
index a25940d72e84e1ad6daba76a6c2845f320bc4df3..8e1a90490ec798f0fc1281e595b001f672f418bd 100644 (file)
--- a/git.c
+++ b/git.c
@@ -25,7 +25,7 @@ struct cmd_struct {
 };
 
 const char git_usage_string[] =
-       N_("git [--version] [--help] [-C <path>] [-c <name>=<value>]\n"
+       N_("git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]\n"
           "           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
           "           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]\n"
           "           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
@@ -146,7 +146,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
                 * commands can be written with "--" prepended
                 * to make them look like flags.
                 */
-               if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
+               if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h") ||
+                   !strcmp(cmd, "--version") || !strcmp(cmd, "-v"))
                        break;
 
                /*
@@ -892,8 +893,10 @@ int cmd_main(int argc, const char **argv)
        argc--;
        handle_options(&argv, &argc, NULL);
        if (argc > 0) {
-               /* translate --help and --version into commands */
-               skip_prefix(argv[0], "--", &argv[0]);
+               if (!strcmp("--version", argv[0]) || !strcmp("-v", argv[0]))
+                       argv[0] = "version";
+               else if (!strcmp("--help", argv[0]) || !strcmp("-h", argv[0]))
+                       argv[0] = "help";
        } else {
                /* The user didn't specify a command; give them help */
                commit_pager_choice();
index 6c3e1f7159d4dcc6a2e61228fa7c4f3cec42ef3b..6c33a4369015c813a73384ff68149463c5189105 100755 (executable)
@@ -181,7 +181,7 @@ for cmd in git "git help"
 do
        test_expect_success "'$cmd' section spacing" '
                test_section_spacing_trailer git help <<-\EOF &&
-               usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
+               usage: git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
 
                These are common Git commands used in various situations: