@matheusbsbs / matheus-bagliano / commits / 99caeed05d
Fork 0

Let 'git <command> -h' show usage without a git dir

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>

Jonathan Nieder committed Nov 9, 2009 at 09:05 UTC 99caeed05d3e89176d352104a2b70a77aa7e5d81
8 files changed +36 -15
builtin-log.c
+6
@@ -50,6 +50,12 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
50 if (default_date_mode)
51 rev->date_mode = parse_date_format(default_date_mode);
52
53 + /*
54 + * Check for -h before setup_revisions(), or "git log -h" will
55 + * fail when run without a git directory.
56 + */
57 + if (argc == 2 && !strcmp(argv[1], "-h"))
58 + usage(builtin_log_usage);
59 argc = setup_revisions(argc, argv, rev, "HEAD");
60
61 if (rev->diffopt.pickaxe || rev->diffopt.filter)
builtin-mv.c
+4 -4
@@ -64,15 +64,15 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
64
65 git_config(git_default_config, NULL);
66
67 - newfd = hold_locked_index(&lock_file, 1);
68 - if (read_cache() < 0)
69 - die("index file corrupt");
70 -
67 argc = parse_options(argc, argv, prefix, builtin_mv_options,
68 builtin_mv_usage, 0);
69 if (--argc < 1)
70 usage_with_options(builtin_mv_usage, builtin_mv_options);
71
72 + newfd = hold_locked_index(&lock_file, 1);
73 + if (read_cache() < 0)
74 + die("index file corrupt");
75 +
76 source = copy_pathspec(prefix, argv, argc, 0);
77 modes = xcalloc(argc, sizeof(enum update_mode));
78 dest_path = copy_pathspec(prefix, argv + argc, 1, 0);
builtin-read-tree.c
+2 -2
@@ -108,11 +108,11 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
108
109 git_config(git_default_config, NULL);
110
111 - newfd = hold_locked_index(&lock_file, 1);
112 -
111 argc = parse_options(argc, argv, unused_prefix, read_tree_options,
112 read_tree_usage, 0);
113
114 + newfd = hold_locked_index(&lock_file, 1);
115 +
116 prefix_set = opts.prefix ? 1 : 0;
117 if (1 < opts.merge + opts.reset + prefix_set)
118 die("Which one? -m, --reset, or --prefix?");
builtin-reflog.c
+3
@@ -698,6 +698,9 @@ static const char reflog_usage[] =
698
699 int cmd_reflog(int argc, const char **argv, const char *prefix)
700 {
701 + if (argc > 1 && !strcmp(argv[1], "-h"))
702 + usage(reflog_usage);
703 +
704 /* With no command, we default to showing it. */
705 if (argc < 2 || *argv[1] == '-')
706 return cmd_log_reflog(argc, argv, prefix);
builtin-rerere.c
+3
@@ -106,6 +106,9 @@ int cmd_rerere(int argc, const char **argv, const char *prefix)
106 if (argc < 2)
107 return rerere();
108
109 + if (!strcmp(argv[1], "-h"))
110 + usage(git_rerere_usage);
111 +
112 fd = setup_rerere(&merge_rr);
113 if (fd < 0)
114 return 0;
git.c
+12 -9
@@ -229,21 +229,24 @@ struct cmd_struct {
229
230 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
231 {
232 - int status;
232 + int status, help;
233 struct stat st;
234 const char *prefix;
235
236 prefix = NULL;
237 - if (p->option & RUN_SETUP)
238 - prefix = setup_git_directory();
239 -
240 - if (use_pager == -1 && p->option & RUN_SETUP)
241 - use_pager = check_pager_config(p->cmd);
242 - if (use_pager == -1 && p->option & USE_PAGER)
243 - use_pager = 1;
237 + help = argc == 2 && !strcmp(argv[1], "-h");
238 + if (!help) {
239 + if (p->option & RUN_SETUP)
240 + prefix = setup_git_directory();
241 +
242 + if (use_pager == -1 && p->option & RUN_SETUP)
243 + use_pager = check_pager_config(p->cmd);
244 + if (use_pager == -1 && p->option & USE_PAGER)
245 + use_pager = 1;
246 + }
247 commit_pager_choice();
248
246 - if (p->option & NEED_WORK_TREE)
249 + if (!help && p->option & NEED_WORK_TREE)
250 setup_work_tree();
251
252 trace_argv_printf(argv, "trace: built-in: git");
index-pack.c
+3
@@ -882,6 +882,9 @@ int main(int argc, char **argv)
882
883 git_extract_argv0_path(argv[0]);
884
885 + if (argc == 2 && !strcmp(argv[1], "-h"))
886 + usage(index_pack_usage);
887 +
888 /*
889 * We wish to read the repository's config file if any, and
890 * for that it is necessary to call setup_git_directory_gently().
pack-redundant.c
+3
@@ -603,6 +603,9 @@ int main(int argc, char **argv)
603
604 git_extract_argv0_path(argv[0]);
605
606 + if (argc == 2 && !strcmp(argv[1], "-h"))
607 + usage(pack_redundant_usage);
608 +
609 setup_git_directory();
610
611 for (i = 1; i < argc; i++) {