Skip to content

Commit 99caeed

Browse files
jrngitster
authored andcommitted
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>
1 parent 548d346 commit 99caeed

File tree

8 files changed

+36
-15
lines changed

8 files changed

+36
-15
lines changed

builtin-log.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
5050
if (default_date_mode)
5151
rev->date_mode = parse_date_format(default_date_mode);
5252

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);
5359
argc = setup_revisions(argc, argv, rev, "HEAD");
5460

5561
if (rev->diffopt.pickaxe || rev->diffopt.filter)

builtin-mv.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
6464

6565
git_config(git_default_config, NULL);
6666

67-
newfd = hold_locked_index(&lock_file, 1);
68-
if (read_cache() < 0)
69-
die("index file corrupt");
70-
7167
argc = parse_options(argc, argv, prefix, builtin_mv_options,
7268
builtin_mv_usage, 0);
7369
if (--argc < 1)
7470
usage_with_options(builtin_mv_usage, builtin_mv_options);
7571

72+
newfd = hold_locked_index(&lock_file, 1);
73+
if (read_cache() < 0)
74+
die("index file corrupt");
75+
7676
source = copy_pathspec(prefix, argv, argc, 0);
7777
modes = xcalloc(argc, sizeof(enum update_mode));
7878
dest_path = copy_pathspec(prefix, argv + argc, 1, 0);

builtin-read-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
108108

109109
git_config(git_default_config, NULL);
110110

111-
newfd = hold_locked_index(&lock_file, 1);
112-
113111
argc = parse_options(argc, argv, unused_prefix, read_tree_options,
114112
read_tree_usage, 0);
115113

114+
newfd = hold_locked_index(&lock_file, 1);
115+
116116
prefix_set = opts.prefix ? 1 : 0;
117117
if (1 < opts.merge + opts.reset + prefix_set)
118118
die("Which one? -m, --reset, or --prefix?");

builtin-reflog.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,9 @@ static const char reflog_usage[] =
698698

699699
int cmd_reflog(int argc, const char **argv, const char *prefix)
700700
{
701+
if (argc > 1 && !strcmp(argv[1], "-h"))
702+
usage(reflog_usage);
703+
701704
/* With no command, we default to showing it. */
702705
if (argc < 2 || *argv[1] == '-')
703706
return cmd_log_reflog(argc, argv, prefix);

builtin-rerere.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ int cmd_rerere(int argc, const char **argv, const char *prefix)
106106
if (argc < 2)
107107
return rerere();
108108

109+
if (!strcmp(argv[1], "-h"))
110+
usage(git_rerere_usage);
111+
109112
fd = setup_rerere(&merge_rr);
110113
if (fd < 0)
111114
return 0;

git.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,21 +229,24 @@ struct cmd_struct {
229229

230230
static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
231231
{
232-
int status;
232+
int status, help;
233233
struct stat st;
234234
const char *prefix;
235235

236236
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+
}
244247
commit_pager_choice();
245248

246-
if (p->option & NEED_WORK_TREE)
249+
if (!help && p->option & NEED_WORK_TREE)
247250
setup_work_tree();
248251

249252
trace_argv_printf(argv, "trace: built-in: git");

index-pack.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,9 @@ int main(int argc, char **argv)
882882

883883
git_extract_argv0_path(argv[0]);
884884

885+
if (argc == 2 && !strcmp(argv[1], "-h"))
886+
usage(index_pack_usage);
887+
885888
/*
886889
* We wish to read the repository's config file if any, and
887890
* for that it is necessary to call setup_git_directory_gently().

pack-redundant.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,9 @@ int main(int argc, char **argv)
603603

604604
git_extract_argv0_path(argv[0]);
605605

606+
if (argc == 2 && !strcmp(argv[1], "-h"))
607+
usage(pack_redundant_usage);
608+
606609
setup_git_directory();
607610

608611
for (i = 1; i < argc; i++) {

0 commit comments

Comments
 (0)