Skip to content

Commit d87aa32

Browse files
committed
Merge branch 'jk/help-alias'
* jk/help-alias: help: respect aliases make alias lookup a public, procedural function help: use parseopt
2 parents f79ff5c + 2156435 commit d87aa32

File tree

5 files changed

+118
-78
lines changed

5 files changed

+118
-78
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ LIB_OBJS = \
327327
alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
328328
color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
329329
convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \
330-
transport.o bundle.o walker.o parse-options.o ws.o archive.o
330+
transport.o bundle.o walker.o parse-options.o ws.o archive.o \
331+
alias.o
331332

332333
BUILTIN_OBJS = \
333334
builtin-add.o \

alias.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "cache.h"
2+
3+
static const char *alias_key;
4+
static char *alias_val;
5+
static int alias_lookup_cb(const char *k, const char *v)
6+
{
7+
if (!prefixcmp(k, "alias.") && !strcmp(k+6, alias_key)) {
8+
if (!v)
9+
return config_error_nonbool(k);
10+
alias_val = xstrdup(v);
11+
return 0;
12+
}
13+
return 0;
14+
}
15+
16+
char *alias_lookup(const char *alias)
17+
{
18+
alias_key = alias;
19+
alias_val = NULL;
20+
git_config(alias_lookup_cb);
21+
return alias_val;
22+
}

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,4 +768,6 @@ int pathspec_match(const char **spec, char *matched, const char *filename, int s
768768
int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset);
769769
void overlay_tree_on_cache(const char *tree_name, const char *prefix);
770770

771+
char *alias_lookup(const char *alias);
772+
771773
#endif /* CACHE_H */

git.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,6 @@ static int handle_options(const char*** argv, int* argc, int* envchanged)
8787
return handled;
8888
}
8989

90-
static const char *alias_command;
91-
static char *alias_string;
92-
93-
static int git_alias_config(const char *var, const char *value)
94-
{
95-
if (!prefixcmp(var, "alias.") && !strcmp(var + 6, alias_command)) {
96-
if (!value)
97-
return config_error_nonbool(var);
98-
alias_string = xstrdup(value);
99-
}
100-
return 0;
101-
}
102-
10390
static int split_cmdline(char *cmdline, const char ***argv)
10491
{
10592
int src, dst, count = 0, size = 16;
@@ -159,11 +146,13 @@ static int handle_alias(int *argcp, const char ***argv)
159146
const char *subdir;
160147
int count, option_count;
161148
const char** new_argv;
149+
const char *alias_command;
150+
char *alias_string;
162151

163152
subdir = setup_git_directory_gently(&nongit);
164153

165154
alias_command = (*argv)[0];
166-
git_config(git_alias_config);
155+
alias_string = alias_lookup(alias_command);
167156
if (alias_string) {
168157
if (alias_string[0] == '!') {
169158
if (*argcp > 1) {

help.c

Lines changed: 89 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,49 @@
77
#include "builtin.h"
88
#include "exec_cmd.h"
99
#include "common-cmds.h"
10-
11-
static const char *help_default_format;
12-
13-
static enum help_format {
14-
man_format,
15-
info_format,
16-
web_format,
17-
} help_format = man_format;
18-
19-
static void parse_help_format(const char *format)
10+
#include "parse-options.h"
11+
12+
enum help_format {
13+
HELP_FORMAT_MAN,
14+
HELP_FORMAT_INFO,
15+
HELP_FORMAT_WEB,
16+
};
17+
18+
static int show_all = 0;
19+
static enum help_format help_format = HELP_FORMAT_MAN;
20+
static struct option builtin_help_options[] = {
21+
OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
22+
OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
23+
OPT_SET_INT('w', "web", &help_format, "show manual in web browser",
24+
HELP_FORMAT_WEB),
25+
OPT_SET_INT('i', "info", &help_format, "show info page",
26+
HELP_FORMAT_INFO),
27+
};
28+
29+
static const char * const builtin_help_usage[] = {
30+
"git-help [--all] [--man|--web|--info] [command]",
31+
NULL
32+
};
33+
34+
static enum help_format parse_help_format(const char *format)
2035
{
21-
if (!format) {
22-
help_format = man_format;
23-
return;
24-
}
25-
if (!strcmp(format, "man")) {
26-
help_format = man_format;
27-
return;
28-
}
29-
if (!strcmp(format, "info")) {
30-
help_format = info_format;
31-
return;
32-
}
33-
if (!strcmp(format, "web") || !strcmp(format, "html")) {
34-
help_format = web_format;
35-
return;
36-
}
36+
if (!strcmp(format, "man"))
37+
return HELP_FORMAT_MAN;
38+
if (!strcmp(format, "info"))
39+
return HELP_FORMAT_INFO;
40+
if (!strcmp(format, "web") || !strcmp(format, "html"))
41+
return HELP_FORMAT_WEB;
3742
die("unrecognized help format '%s'", format);
3843
}
3944

4045
static int git_help_config(const char *var, const char *value)
4146
{
42-
if (!strcmp(var, "help.format"))
43-
return git_config_string(&help_default_format, var, value);
47+
if (!strcmp(var, "help.format")) {
48+
if (!value)
49+
return config_error_nonbool(var);
50+
help_format = parse_help_format(value);
51+
return 0;
52+
}
4453
return git_default_config(var, value);
4554
}
4655

@@ -201,7 +210,7 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds,
201210
return longest;
202211
}
203212

204-
static void list_commands(void)
213+
static unsigned int load_command_list(void)
205214
{
206215
unsigned int longest = 0;
207216
unsigned int len;
@@ -241,6 +250,14 @@ static void list_commands(void)
241250
uniq(&other_cmds);
242251
exclude_cmds(&other_cmds, &main_cmds);
243252

253+
return longest;
254+
}
255+
256+
static void list_commands(void)
257+
{
258+
unsigned int longest = load_command_list();
259+
const char *exec_path = git_exec_path();
260+
244261
if (main_cmds.cnt) {
245262
printf("available git commands in '%s'\n", exec_path);
246263
printf("----------------------------");
@@ -275,6 +292,22 @@ void list_common_cmds_help(void)
275292
}
276293
}
277294

295+
static int is_in_cmdlist(struct cmdnames *c, const char *s)
296+
{
297+
int i;
298+
for (i = 0; i < c->cnt; i++)
299+
if (!strcmp(s, c->names[i]->name))
300+
return 1;
301+
return 0;
302+
}
303+
304+
static int is_git_command(const char *s)
305+
{
306+
load_command_list();
307+
return is_in_cmdlist(&main_cmds, s) ||
308+
is_in_cmdlist(&other_cmds, s);
309+
}
310+
278311
static const char *cmd_to_page(const char *git_cmd)
279312
{
280313
if (!git_cmd)
@@ -362,50 +395,43 @@ int cmd_version(int argc, const char **argv, const char *prefix)
362395

363396
int cmd_help(int argc, const char **argv, const char *prefix)
364397
{
365-
const char *help_cmd = argv[1];
398+
int nongit;
399+
const char *alias;
366400

367-
if (argc < 2) {
368-
printf("usage: %s\n\n", git_usage_string);
369-
list_common_cmds_help();
370-
exit(0);
371-
}
401+
setup_git_directory_gently(&nongit);
402+
git_config(git_help_config);
403+
404+
argc = parse_options(argc, argv, builtin_help_options,
405+
builtin_help_usage, 0);
372406

373-
if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
407+
if (show_all) {
374408
printf("usage: %s\n\n", git_usage_string);
375409
list_commands();
410+
return 0;
376411
}
377412

378-
else if (!strcmp(help_cmd, "--web") || !strcmp(help_cmd, "-w")) {
379-
show_html_page(argc > 2 ? argv[2] : NULL);
380-
}
381-
382-
else if (!strcmp(help_cmd, "--info") || !strcmp(help_cmd, "-i")) {
383-
show_info_page(argc > 2 ? argv[2] : NULL);
413+
if (!argv[0]) {
414+
printf("usage: %s\n\n", git_usage_string);
415+
list_common_cmds_help();
416+
return 0;
384417
}
385418

386-
else if (!strcmp(help_cmd, "--man") || !strcmp(help_cmd, "-m")) {
387-
show_man_page(argc > 2 ? argv[2] : NULL);
419+
alias = alias_lookup(argv[0]);
420+
if (alias && !is_git_command(argv[0])) {
421+
printf("`git %s' is aliased to `%s'\n", argv[0], alias);
422+
return 0;
388423
}
389424

390-
else {
391-
int nongit;
392-
393-
setup_git_directory_gently(&nongit);
394-
git_config(git_help_config);
395-
if (help_default_format)
396-
parse_help_format(help_default_format);
397-
398-
switch (help_format) {
399-
case man_format:
400-
show_man_page(help_cmd);
401-
break;
402-
case info_format:
403-
show_info_page(help_cmd);
404-
break;
405-
case web_format:
406-
show_html_page(help_cmd);
407-
break;
408-
}
425+
switch (help_format) {
426+
case HELP_FORMAT_MAN:
427+
show_man_page(argv[0]);
428+
break;
429+
case HELP_FORMAT_INFO:
430+
show_info_page(argv[0]);
431+
break;
432+
case HELP_FORMAT_WEB:
433+
show_html_page(argv[0]);
434+
break;
409435
}
410436

411437
return 0;

0 commit comments

Comments
 (0)