Code

Let 'git <command> -h' show usage without a git dir
authorJonathan Nieder <jrnieder@gmail.com>
Mon, 9 Nov 2009 15:05:01 +0000 (09:05 -0600)
committerJunio C Hamano <gitster@pobox.com>
Tue, 10 Nov 2009 20:00:21 +0000 (12:00 -0800)
There is no need for "git <command> -h" to depend on being inside
a repository.

Reported by Gerfried Fuchs through http://bugs.debian.org/462557

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin-log.c
builtin-mv.c
builtin-read-tree.c
builtin-reflog.c
builtin-rerere.c
git.c
index-pack.c
pack-redundant.c

index 524850735acd8dcbdf5c5c95332a3c721d8c8483..a0fa30c408090ba4ec5ceefff974a8008ce18aaa 100644 (file)
@@ -50,6 +50,12 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
        if (default_date_mode)
                rev->date_mode = parse_date_format(default_date_mode);
 
+       /*
+        * Check for -h before setup_revisions(), or "git log -h" will
+        * fail when run without a git directory.
+        */
+       if (argc == 2 && !strcmp(argv[1], "-h"))
+               usage(builtin_log_usage);
        argc = setup_revisions(argc, argv, rev, "HEAD");
 
        if (rev->diffopt.pickaxe || rev->diffopt.filter)
index 1b20028c67dec2f9954cb107620bd863b4b3a264..f633d81424f5e41cb85ac660dbdc9f4913852673 100644 (file)
@@ -64,15 +64,15 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 
        git_config(git_default_config, NULL);
 
-       newfd = hold_locked_index(&lock_file, 1);
-       if (read_cache() < 0)
-               die("index file corrupt");
-
        argc = parse_options(argc, argv, prefix, builtin_mv_options,
                             builtin_mv_usage, 0);
        if (--argc < 1)
                usage_with_options(builtin_mv_usage, builtin_mv_options);
 
+       newfd = hold_locked_index(&lock_file, 1);
+       if (read_cache() < 0)
+               die("index file corrupt");
+
        source = copy_pathspec(prefix, argv, argc, 0);
        modes = xcalloc(argc, sizeof(enum update_mode));
        dest_path = copy_pathspec(prefix, argv + argc, 1, 0);
index 14c836b1693317d5d834606e5613d7ceacb3189c..2a3a32cbfe83a0e0366d04a99bb49606f647f594 100644 (file)
@@ -108,11 +108,11 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
 
        git_config(git_default_config, NULL);
 
-       newfd = hold_locked_index(&lock_file, 1);
-
        argc = parse_options(argc, argv, unused_prefix, read_tree_options,
                             read_tree_usage, 0);
 
+       newfd = hold_locked_index(&lock_file, 1);
+
        prefix_set = opts.prefix ? 1 : 0;
        if (1 < opts.merge + opts.reset + prefix_set)
                die("Which one? -m, --reset, or --prefix?");
index e23b5ef979d98e5e44693345856a13485695246f..749821078df129cf13de34ebbd40a8cb7a38e00e 100644 (file)
@@ -698,6 +698,9 @@ static const char reflog_usage[] =
 
 int cmd_reflog(int argc, const char **argv, const char *prefix)
 {
+       if (argc > 1 && !strcmp(argv[1], "-h"))
+               usage(reflog_usage);
+
        /* With no command, we default to showing it. */
        if (argc < 2 || *argv[1] == '-')
                return cmd_log_reflog(argc, argv, prefix);
index adfb7b5f48597c19c23235b74cf1a0c779985dc6..343d6cde48266d63c0aa6826b973e55b48820745 100644 (file)
@@ -106,6 +106,9 @@ int cmd_rerere(int argc, const char **argv, const char *prefix)
        if (argc < 2)
                return rerere();
 
+       if (!strcmp(argv[1], "-h"))
+               usage(git_rerere_usage);
+
        fd = setup_rerere(&merge_rr);
        if (fd < 0)
                return 0;
diff --git a/git.c b/git.c
index f295561a933c264f869e9b94c948046d3a311be0..743ee57100ac49aca8d90892b1abec5f18eb18c1 100644 (file)
--- a/git.c
+++ b/git.c
@@ -229,21 +229,24 @@ struct cmd_struct {
 
 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 {
-       int status;
+       int status, help;
        struct stat st;
        const char *prefix;
 
        prefix = NULL;
-       if (p->option & RUN_SETUP)
-               prefix = setup_git_directory();
-
-       if (use_pager == -1 && p->option & RUN_SETUP)
-               use_pager = check_pager_config(p->cmd);
-       if (use_pager == -1 && p->option & USE_PAGER)
-               use_pager = 1;
+       help = argc == 2 && !strcmp(argv[1], "-h");
+       if (!help) {
+               if (p->option & RUN_SETUP)
+                       prefix = setup_git_directory();
+
+               if (use_pager == -1 && p->option & RUN_SETUP)
+                       use_pager = check_pager_config(p->cmd);
+               if (use_pager == -1 && p->option & USE_PAGER)
+                       use_pager = 1;
+       }
        commit_pager_choice();
 
-       if (p->option & NEED_WORK_TREE)
+       if (!help && p->option & NEED_WORK_TREE)
                setup_work_tree();
 
        trace_argv_printf(argv, "trace: built-in: git");
index b4f82786592e2e34392191e823d645e17d955fcf..190f372dd81f85d76068b166cc1688487640fdba 100644 (file)
@@ -882,6 +882,9 @@ int main(int argc, char **argv)
 
        git_extract_argv0_path(argv[0]);
 
+       if (argc == 2 && !strcmp(argv[1], "-h"))
+               usage(index_pack_usage);
+
        /*
         * We wish to read the repository's config file if any, and
         * for that it is necessary to call setup_git_directory_gently().
index 69a7ab2e27d39ecabca5c0cc25c4e372caafb46c..21c61dbbe9bd5e2f9770109bc4a9daaf2a2f6cc4 100644 (file)
@@ -603,6 +603,9 @@ int main(int argc, char **argv)
 
        git_extract_argv0_path(argv[0]);
 
+       if (argc == 2 && !strcmp(argv[1], "-h"))
+               usage(pack_redundant_usage);
+
        setup_git_directory();
 
        for (i = 1; i < argc; i++) {