blob: d387131dd836c4eed62aa243ddbc1789934db8e0 [file] [log] [blame]
Miklos Vajna3d78d1f2008-08-02 10:08:38 +02001/*
Miklos Vajna3d78d1f2008-08-02 10:08:38 +02002 * Builtin help command
3 */
4#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07005#include "config.h"
Miklos Vajna3d78d1f2008-08-02 10:08:38 +02006#include "builtin.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -07007#include "exec-cmd.h"
Miklos Vajna3d78d1f2008-08-02 10:08:38 +02008#include "parse-options.h"
9#include "run-command.h"
Emily Shaffer709df952020-04-16 14:18:03 -070010#include "config-list.h"
Miklos Vajna3d78d1f2008-08-02 10:08:38 +020011#include "help.h"
Nguyễn Thái Ngọc Duy65b5f942018-05-20 20:40:06 +020012#include "alias.h"
Miklos Vajna3d78d1f2008-08-02 10:08:38 +020013
Vincent van Ravesteijn1cc8af02012-06-06 20:28:16 +000014#ifndef DEFAULT_HELP_FORMAT
15#define DEFAULT_HELP_FORMAT "man"
16#endif
17
Miklos Vajna3d78d1f2008-08-02 10:08:38 +020018static struct man_viewer_list {
19 struct man_viewer_list *next;
20 char name[FLEX_ARRAY];
21} *man_viewer_list;
22
23static struct man_viewer_info_list {
24 struct man_viewer_info_list *next;
25 const char *info;
26 char name[FLEX_ARRAY];
27} *man_viewer_info_list;
28
29enum help_format {
Christian Couder3caa8232010-01-09 06:10:05 +010030 HELP_FORMAT_NONE,
Miklos Vajna3d78d1f2008-08-02 10:08:38 +020031 HELP_FORMAT_MAN,
32 HELP_FORMAT_INFO,
Gary V. Vaughan4b055482010-05-14 09:31:35 +000033 HELP_FORMAT_WEB
Miklos Vajna3d78d1f2008-08-02 10:08:38 +020034};
35
Ævar Arnfjörð Bjarmasona9baccc2021-09-22 00:40:38 +020036enum show_config_type {
37 SHOW_CONFIG_HUMAN,
38 SHOW_CONFIG_VARS,
39 SHOW_CONFIG_SECTIONS,
40};
Chris Webb89a852e2012-06-28 07:58:02 +010041
Ævar Arnfjörð Bjarmasond35d03c2021-09-22 00:40:36 +020042static enum help_action {
43 HELP_ACTION_ALL = 1,
44 HELP_ACTION_GUIDES,
45 HELP_ACTION_CONFIG,
46 HELP_ACTION_CONFIG_FOR_COMPLETION,
Ævar Arnfjörð Bjarmasona9baccc2021-09-22 00:40:38 +020047 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION,
Ævar Arnfjörð Bjarmasond35d03c2021-09-22 00:40:36 +020048} cmd_mode;
Miklos Vajna3d78d1f2008-08-02 10:08:38 +020049
Ævar Arnfjörð Bjarmasond35d03c2021-09-22 00:40:36 +020050static const char *html_path;
Nguyễn Thái Ngọc Duy26c7d062018-09-29 08:08:14 +020051static int verbose = 1;
Christian Couder3caa8232010-01-09 06:10:05 +010052static enum help_format help_format = HELP_FORMAT_NONE;
Ralf Thielowaf741282016-08-26 19:58:35 +020053static int exclude_guides;
Miklos Vajna3d78d1f2008-08-02 10:08:38 +020054static struct option builtin_help_options[] = {
Ævar Arnfjörð Bjarmasond35d03c2021-09-22 00:40:36 +020055 OPT_CMDMODE('a', "all", &cmd_mode, N_("print all available commands"),
56 HELP_ACTION_ALL),
Ralf Thielowaf741282016-08-26 19:58:35 +020057 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
Nguyễn Thái Ngọc Duy68918692012-08-20 19:32:17 +070058 OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
59 OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
Miklos Vajna3d78d1f2008-08-02 10:08:38 +020060 HELP_FORMAT_WEB),
Nguyễn Thái Ngọc Duy68918692012-08-20 19:32:17 +070061 OPT_SET_INT('i', "info", &help_format, N_("show info page"),
Miklos Vajna3d78d1f2008-08-02 10:08:38 +020062 HELP_FORMAT_INFO),
Nguyễn Thái Ngọc Duy63eae832018-05-20 20:40:01 +020063 OPT__VERBOSE(&verbose, N_("print command description")),
Ævar Arnfjörð Bjarmasond35d03c2021-09-22 00:40:36 +020064
65 OPT_CMDMODE('g', "guides", &cmd_mode, N_("print list of useful guides"),
66 HELP_ACTION_GUIDES),
67 OPT_CMDMODE('c', "config", &cmd_mode, N_("print all configuration variable names"),
68 HELP_ACTION_CONFIG),
69 OPT_CMDMODE_F(0, "config-for-completion", &cmd_mode, "",
70 HELP_ACTION_CONFIG_FOR_COMPLETION, PARSE_OPT_HIDDEN),
Ævar Arnfjörð Bjarmasona9baccc2021-09-22 00:40:38 +020071 OPT_CMDMODE_F(0, "config-sections-for-completion", &cmd_mode, "",
72 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION, PARSE_OPT_HIDDEN),
Ævar Arnfjörð Bjarmasond35d03c2021-09-22 00:40:36 +020073
Miklos Vajna3d78d1f2008-08-02 10:08:38 +020074 OPT_END(),
75};
76
77static const char * const builtin_help_usage[] = {
Ævar Arnfjörð Bjarmason9856ea62021-09-22 00:40:32 +020078 N_("git help [-a|--all] [--[no-]verbose]]\n"
Ævar Arnfjörð Bjarmasonb4084522021-09-10 13:28:42 +020079 " [[-i|--info] [-m|--man] [-w|--web]] [<command>]"),
Ævar Arnfjörð Bjarmason9856ea62021-09-22 00:40:32 +020080 N_("git help [-g|--guides]"),
Ævar Arnfjörð Bjarmason1ed4bef2021-09-22 00:40:34 +020081 N_("git help [-c|--config]"),
Miklos Vajna3d78d1f2008-08-02 10:08:38 +020082 NULL
83};
84
Emily Shaffer709df952020-04-16 14:18:03 -070085struct slot_expansion {
86 const char *prefix;
87 const char *placeholder;
88 void (*fn)(struct string_list *list, const char *prefix);
89 int found;
90};
91
Ævar Arnfjörð Bjarmasona9baccc2021-09-22 00:40:38 +020092static void list_config_help(enum show_config_type type)
Emily Shaffer709df952020-04-16 14:18:03 -070093{
94 struct slot_expansion slot_expansions[] = {
95 { "advice", "*", list_config_advices },
96 { "color.branch", "<slot>", list_config_color_branch_slots },
97 { "color.decorate", "<slot>", list_config_color_decorate_slots },
98 { "color.diff", "<slot>", list_config_color_diff_slots },
99 { "color.grep", "<slot>", list_config_color_grep_slots },
100 { "color.interactive", "<slot>", list_config_color_interactive_slots },
101 { "color.remote", "<slot>", list_config_color_sideband_slots },
102 { "color.status", "<slot>", list_config_color_status_slots },
103 { "fsck", "<msg-id>", list_config_fsck_msg_ids },
104 { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids },
105 { NULL, NULL, NULL }
106 };
107 const char **p;
108 struct slot_expansion *e;
109 struct string_list keys = STRING_LIST_INIT_DUP;
Ævar Arnfjörð Bjarmasona9baccc2021-09-22 00:40:38 +0200110 struct string_list keys_uniq = STRING_LIST_INIT_DUP;
111 struct string_list_item *item;
Emily Shaffer709df952020-04-16 14:18:03 -0700112 int i;
113
114 for (p = config_name_list; *p; p++) {
115 const char *var = *p;
116 struct strbuf sb = STRBUF_INIT;
117
118 for (e = slot_expansions; e->prefix; e++) {
119
120 strbuf_reset(&sb);
121 strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder);
122 if (!strcasecmp(var, sb.buf)) {
123 e->fn(&keys, e->prefix);
124 e->found++;
125 break;
126 }
127 }
128 strbuf_release(&sb);
129 if (!e->prefix)
130 string_list_append(&keys, var);
131 }
132
133 for (e = slot_expansions; e->prefix; e++)
134 if (!e->found)
135 BUG("slot_expansion %s.%s is not used",
136 e->prefix, e->placeholder);
137
138 string_list_sort(&keys);
139 for (i = 0; i < keys.nr; i++) {
140 const char *var = keys.items[i].string;
141 const char *wildcard, *tag, *cut;
Ævar Arnfjörð Bjarmasona9baccc2021-09-22 00:40:38 +0200142 const char *dot = NULL;
143 struct strbuf sb = STRBUF_INIT;
Emily Shaffer709df952020-04-16 14:18:03 -0700144
Ævar Arnfjörð Bjarmasona9baccc2021-09-22 00:40:38 +0200145 switch (type) {
146 case SHOW_CONFIG_HUMAN:
Emily Shaffer709df952020-04-16 14:18:03 -0700147 puts(var);
148 continue;
Ævar Arnfjörð Bjarmasona9baccc2021-09-22 00:40:38 +0200149 case SHOW_CONFIG_SECTIONS:
150 dot = strchr(var, '.');
151 break;
152 case SHOW_CONFIG_VARS:
153 break;
Emily Shaffer709df952020-04-16 14:18:03 -0700154 }
Emily Shaffer709df952020-04-16 14:18:03 -0700155 wildcard = strchr(var, '*');
156 tag = strchr(var, '<');
157
Ævar Arnfjörð Bjarmasona9baccc2021-09-22 00:40:38 +0200158 if (!dot && !wildcard && !tag) {
159 string_list_append(&keys_uniq, var);
Emily Shaffer709df952020-04-16 14:18:03 -0700160 continue;
161 }
162
Ævar Arnfjörð Bjarmasona9baccc2021-09-22 00:40:38 +0200163 if (dot)
164 cut = dot;
165 else if (wildcard && !tag)
Emily Shaffer709df952020-04-16 14:18:03 -0700166 cut = wildcard;
167 else if (!wildcard && tag)
168 cut = tag;
169 else
170 cut = wildcard < tag ? wildcard : tag;
171
Ævar Arnfjörð Bjarmasona9baccc2021-09-22 00:40:38 +0200172 strbuf_add(&sb, var, cut - var);
173 string_list_append(&keys_uniq, sb.buf);
174 strbuf_release(&sb);
175
Emily Shaffer709df952020-04-16 14:18:03 -0700176 }
177 string_list_clear(&keys, 0);
Ævar Arnfjörð Bjarmasona9baccc2021-09-22 00:40:38 +0200178 string_list_remove_duplicates(&keys_uniq, 0);
179 for_each_string_list_item(item, &keys_uniq)
180 puts(item->string);
181 string_list_clear(&keys_uniq, 0);
Emily Shaffer709df952020-04-16 14:18:03 -0700182}
183
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200184static enum help_format parse_help_format(const char *format)
185{
186 if (!strcmp(format, "man"))
187 return HELP_FORMAT_MAN;
188 if (!strcmp(format, "info"))
189 return HELP_FORMAT_INFO;
190 if (!strcmp(format, "web") || !strcmp(format, "html"))
191 return HELP_FORMAT_WEB;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700192 /*
193 * Please update _git_config() in git-completion.bash when you
194 * add new help formats.
195 */
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700196 die(_("unrecognized help format '%s'"), format);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200197}
198
199static const char *get_man_viewer_info(const char *name)
200{
201 struct man_viewer_info_list *viewer;
202
203 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
204 {
205 if (!strcasecmp(name, viewer->name))
206 return viewer->info;
207 }
208 return NULL;
209}
210
211static int check_emacsclient_version(void)
212{
213 struct strbuf buffer = STRBUF_INIT;
René Scharfed3180272014-08-19 21:09:35 +0200214 struct child_process ec_process = CHILD_PROCESS_INIT;
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200215 int version;
216
217 /* emacsclient prints its version number on stderr */
Ævar Arnfjörð Bjarmason2b709892021-11-25 23:52:20 +0100218 strvec_pushl(&ec_process.args, "emacsclient", "--version", NULL);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200219 ec_process.err = -1;
220 ec_process.stdout_to_stderr = 1;
Johannes Sixt4e2715f2009-06-08 22:34:31 +0200221 if (start_command(&ec_process))
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700222 return error(_("Failed to start emacsclient."));
Johannes Sixt4e2715f2009-06-08 22:34:31 +0200223
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200224 strbuf_read(&buffer, ec_process.err, 20);
225 close(ec_process.err);
226
227 /*
228 * Don't bother checking return value, because "emacsclient --version"
229 * seems to always exits with code 1.
230 */
231 finish_command(&ec_process);
232
Christian Couder59556542013-11-30 21:55:40 +0100233 if (!starts_with(buffer.buf, "emacsclient")) {
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200234 strbuf_release(&buffer);
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700235 return error(_("Failed to parse emacsclient version."));
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200236 }
237
238 strbuf_remove(&buffer, 0, strlen("emacsclient"));
239 version = atoi(buffer.buf);
240
241 if (version < 22) {
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200242 strbuf_release(&buffer);
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700243 return error(_("emacsclient version '%d' too old (< 22)."),
Johannes Sixt4e2715f2009-06-08 22:34:31 +0200244 version);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200245 }
246
247 strbuf_release(&buffer);
248 return 0;
249}
250
Felipe Contreras4b25d092009-05-01 12:06:36 +0300251static void exec_woman_emacs(const char *path, const char *page)
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200252{
253 if (!check_emacsclient_version()) {
254 /* This works only with emacsclient version >= 22. */
255 struct strbuf man_page = STRBUF_INIT;
256
257 if (!path)
258 path = "emacsclient";
259 strbuf_addf(&man_page, "(woman \"%s\")", page);
Thomas Rast5d314752010-07-24 17:20:23 +0200260 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
Nguyễn Thái Ngọc Duy57477492016-05-08 16:47:27 +0700261 warning_errno(_("failed to exec '%s'"), path);
Rene Scharfebad0e2c2017-08-30 19:49:46 +0200262 strbuf_release(&man_page);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200263 }
264}
265
Felipe Contreras4b25d092009-05-01 12:06:36 +0300266static void exec_man_konqueror(const char *path, const char *page)
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200267{
268 const char *display = getenv("DISPLAY");
269 if (display && *display) {
270 struct strbuf man_page = STRBUF_INIT;
271 const char *filename = "kfmclient";
272
273 /* It's simpler to launch konqueror using kfmclient. */
274 if (path) {
Jeff King4c9ac3b2015-09-24 17:08:16 -0400275 size_t len;
276 if (strip_suffix(path, "/konqueror", &len))
277 path = xstrfmt("%.*s/kfmclient", (int)len, path);
278 filename = basename((char *)path);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200279 } else
280 path = "kfmclient";
281 strbuf_addf(&man_page, "man:%s(1)", page);
Thomas Rast5d314752010-07-24 17:20:23 +0200282 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
Nguyễn Thái Ngọc Duy57477492016-05-08 16:47:27 +0700283 warning_errno(_("failed to exec '%s'"), path);
Rene Scharfea981a9f2017-08-30 19:49:44 +0200284 strbuf_release(&man_page);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200285 }
286}
287
Felipe Contreras4b25d092009-05-01 12:06:36 +0300288static void exec_man_man(const char *path, const char *page)
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200289{
290 if (!path)
291 path = "man";
Thomas Rast5d314752010-07-24 17:20:23 +0200292 execlp(path, "man", page, (char *)NULL);
Nguyễn Thái Ngọc Duy57477492016-05-08 16:47:27 +0700293 warning_errno(_("failed to exec '%s'"), path);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200294}
295
296static void exec_man_cmd(const char *cmd, const char *page)
297{
298 struct strbuf shell_cmd = STRBUF_INIT;
299 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
Kyle J. McKayb680a862015-03-07 21:08:00 -0800300 execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
Nguyễn Thái Ngọc Duy57477492016-05-08 16:47:27 +0700301 warning(_("failed to exec '%s'"), cmd);
Rene Scharfe72462182017-08-30 19:49:45 +0200302 strbuf_release(&shell_cmd);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200303}
304
305static void add_man_viewer(const char *name)
306{
307 struct man_viewer_list **p = &man_viewer_list;
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200308
309 while (*p)
310 p = &((*p)->next);
Jeff King96ffc062016-02-22 17:44:32 -0500311 FLEX_ALLOC_STR(*p, name, name);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200312}
313
314static int supported_man_viewer(const char *name, size_t len)
315{
316 return (!strncasecmp("man", name, len) ||
317 !strncasecmp("woman", name, len) ||
318 !strncasecmp("konqueror", name, len));
319}
320
321static void do_add_man_viewer_info(const char *name,
322 size_t len,
323 const char *value)
324{
Brandon Williams8adda462018-02-14 10:59:32 -0800325 struct man_viewer_info_list *new_man_viewer;
326 FLEX_ALLOC_MEM(new_man_viewer, name, name, len);
327 new_man_viewer->info = xstrdup(value);
328 new_man_viewer->next = man_viewer_info_list;
329 man_viewer_info_list = new_man_viewer;
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200330}
331
332static int add_man_viewer_path(const char *name,
333 size_t len,
334 const char *value)
335{
336 if (supported_man_viewer(name, len))
337 do_add_man_viewer_info(name, len, value);
338 else
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700339 warning(_("'%s': path for unsupported man viewer.\n"
340 "Please consider using 'man.<tool>.cmd' instead."),
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200341 name);
342
343 return 0;
344}
345
346static int add_man_viewer_cmd(const char *name,
347 size_t len,
348 const char *value)
349{
350 if (supported_man_viewer(name, len))
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700351 warning(_("'%s': cmd for supported man viewer.\n"
352 "Please consider using 'man.<tool>.path' instead."),
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200353 name);
354 else
355 do_add_man_viewer_info(name, len, value);
356
357 return 0;
358}
359
360static int add_man_viewer_info(const char *var, const char *value)
361{
Jeff King4d5c6ce2013-01-23 01:27:09 -0500362 const char *name, *subkey;
Jeff Kingf5914f42020-04-10 15:44:28 -0400363 size_t namelen;
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200364
Jeff King4d5c6ce2013-01-23 01:27:09 -0500365 if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
Johannes Sixt178b5132009-04-23 15:49:06 +0200366 return 0;
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200367
Jeff King4d5c6ce2013-01-23 01:27:09 -0500368 if (!strcmp(subkey, "path")) {
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200369 if (!value)
370 return config_error_nonbool(var);
Jeff King4d5c6ce2013-01-23 01:27:09 -0500371 return add_man_viewer_path(name, namelen, value);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200372 }
Jeff King4d5c6ce2013-01-23 01:27:09 -0500373 if (!strcmp(subkey, "cmd")) {
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200374 if (!value)
375 return config_error_nonbool(var);
Jeff King4d5c6ce2013-01-23 01:27:09 -0500376 return add_man_viewer_cmd(name, namelen, value);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200377 }
378
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200379 return 0;
380}
381
382static int git_help_config(const char *var, const char *value, void *cb)
383{
384 if (!strcmp(var, "help.format")) {
385 if (!value)
386 return config_error_nonbool(var);
387 help_format = parse_help_format(value);
388 return 0;
389 }
Chris Webb89a852e2012-06-28 07:58:02 +0100390 if (!strcmp(var, "help.htmlpath")) {
391 if (!value)
392 return config_error_nonbool(var);
393 html_path = xstrdup(value);
394 return 0;
395 }
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200396 if (!strcmp(var, "man.viewer")) {
397 if (!value)
398 return config_error_nonbool(var);
399 add_man_viewer(value);
400 return 0;
401 }
Christian Couder59556542013-11-30 21:55:40 +0100402 if (starts_with(var, "man."))
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200403 return add_man_viewer_info(var, value);
404
405 return git_default_config(var, value, cb);
406}
407
Alex Riesenc7371e92008-08-28 19:17:46 +0200408static struct cmdnames main_cmds, other_cmds;
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200409
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200410static int is_git_command(const char *s)
411{
Sebastian Schuberthc6127fa2014-01-02 17:17:11 +0100412 if (is_builtin(s))
413 return 1;
414
Sebastian Schubertha3c52632014-01-02 17:16:30 +0100415 load_command_list("git-", &main_cmds, &other_cmds);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200416 return is_in_cmdlist(&main_cmds, s) ||
417 is_in_cmdlist(&other_cmds, s);
418}
419
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200420static const char *cmd_to_page(const char *git_cmd)
421{
422 if (!git_cmd)
423 return "git";
Christian Couder59556542013-11-30 21:55:40 +0100424 else if (starts_with(git_cmd, "git"))
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200425 return git_cmd;
426 else if (is_git_command(git_cmd))
Jeff Kingacd47ee2015-09-24 17:07:14 -0400427 return xstrfmt("git-%s", git_cmd);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200428 else
Jeff Kingacd47ee2015-09-24 17:07:14 -0400429 return xstrfmt("git%s", git_cmd);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200430}
431
432static void setup_man_path(void)
433{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500434 struct strbuf new_path = STRBUF_INIT;
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200435 const char *old_path = getenv("MANPATH");
Junio C Hamano59362e52014-11-24 11:33:54 -0800436 char *git_man_path = system_path(GIT_MAN_PATH);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200437
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200438 /* We should always put ':' after our path. If there is no
439 * old_path, the ':' at the end will let 'man' to try
440 * system-wide paths after ours to find the manual page. If
441 * there is old_path, we need ':' as delimiter. */
Junio C Hamano59362e52014-11-24 11:33:54 -0800442 strbuf_addstr(&new_path, git_man_path);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200443 strbuf_addch(&new_path, ':');
444 if (old_path)
445 strbuf_addstr(&new_path, old_path);
446
Junio C Hamano59362e52014-11-24 11:33:54 -0800447 free(git_man_path);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200448 setenv("MANPATH", new_path.buf, 1);
449
450 strbuf_release(&new_path);
451}
452
453static void exec_viewer(const char *name, const char *page)
454{
455 const char *info = get_man_viewer_info(name);
456
457 if (!strcasecmp(name, "man"))
458 exec_man_man(info, page);
459 else if (!strcasecmp(name, "woman"))
460 exec_woman_emacs(info, page);
461 else if (!strcasecmp(name, "konqueror"))
462 exec_man_konqueror(info, page);
463 else if (info)
464 exec_man_cmd(info, page);
465 else
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700466 warning(_("'%s': unknown man viewer."), name);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200467}
468
Andrei Rybakd5659f82021-07-04 17:39:12 +0200469static void show_man_page(const char *page)
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200470{
471 struct man_viewer_list *viewer;
Romain Francoise5059a422008-08-29 17:00:43 +0200472 const char *fallback = getenv("GIT_MAN_VIEWER");
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200473
474 setup_man_path();
475 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
476 {
477 exec_viewer(viewer->name, page); /* will return when unable */
478 }
Romain Francoise5059a422008-08-29 17:00:43 +0200479 if (fallback)
480 exec_viewer(fallback, page);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200481 exec_viewer("man", page);
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700482 die(_("no man viewer handled the request"));
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200483}
484
Andrei Rybakd5659f82021-07-04 17:39:12 +0200485static void show_info_page(const char *page)
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200486{
Steffen Prohaska026fa0d2009-01-18 13:00:09 +0100487 setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
Thomas Rast5d314752010-07-24 17:20:23 +0200488 execlp("info", "info", "gitman", page, (char *)NULL);
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700489 die(_("no info viewer handled the request"));
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200490}
491
492static void get_html_page_path(struct strbuf *page_path, const char *page)
493{
494 struct stat st;
Junio C Hamano59362e52014-11-24 11:33:54 -0800495 char *to_free = NULL;
496
Chris Webb89a852e2012-06-28 07:58:02 +0100497 if (!html_path)
Junio C Hamano59362e52014-11-24 11:33:54 -0800498 html_path = to_free = system_path(GIT_HTML_PATH);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200499
Matthias Aßhauera3952f82021-09-14 13:27:17 +0000500 /*
501 * Check that the page we're looking for exists.
502 */
Chris Webb86272b42012-06-28 07:58:03 +0100503 if (!strstr(html_path, "://")) {
Matthias Aßhauera3952f82021-09-14 13:27:17 +0000504 if (stat(mkpath("%s/%s.html", html_path, page), &st)
Chris Webb86272b42012-06-28 07:58:03 +0100505 || !S_ISREG(st.st_mode))
Matthias Aßhauera3952f82021-09-14 13:27:17 +0000506 die("'%s/%s.html': documentation file not found.",
507 html_path, page);
Chris Webb86272b42012-06-28 07:58:03 +0100508 }
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200509
510 strbuf_init(page_path, 0);
511 strbuf_addf(page_path, "%s/%s.html", html_path, page);
Junio C Hamano59362e52014-11-24 11:33:54 -0800512 free(to_free);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200513}
514
Linus Torvalds2af202b2009-06-18 10:28:43 -0700515static void open_html(const char *path)
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200516{
Thomas Rast5d314752010-07-24 17:20:23 +0200517 execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200518}
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200519
Andrei Rybakd5659f82021-07-04 17:39:12 +0200520static void show_html_page(const char *page)
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200521{
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200522 struct strbuf page_path; /* it leaks but we exec bellow */
523
524 get_html_page_path(&page_path, page);
525
526 open_html(page_path.buf);
527}
528
Ralf Thielowaf741282016-08-26 19:58:35 +0200529static const char *check_git_cmd(const char* cmd)
530{
531 char *alias;
532
533 if (is_git_command(cmd))
534 return cmd;
535
536 alias = alias_lookup(cmd);
537 if (alias) {
Rasmus Villemoese6e76ba2018-10-09 13:59:07 +0200538 const char **argv;
539 int count;
540
541 /*
542 * handle_builtin() in git.c rewrites "git cmd --help"
543 * to "git help --exclude-guides cmd", so we can use
544 * exclude_guides to distinguish "git cmd --help" from
545 * "git help cmd". In the latter case, or if cmd is an
546 * alias for a shell command, just print the alias
547 * definition.
548 */
549 if (!exclude_guides || alias[0] == '!') {
550 printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
551 free(alias);
552 exit(0);
553 }
554 /*
555 * Otherwise, we pretend that the command was "git
556 * word0 --help". We use split_cmdline() to get the
557 * first word of the alias, to ensure that we use the
558 * same rules as when the alias is actually
559 * used. split_cmdline() modifies alias in-place.
560 */
561 fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
562 count = split_cmdline(alias, &argv);
563 if (count < 0)
564 die(_("bad alias.%s string: %s"), cmd,
565 split_cmdline_strerror(count));
566 free(argv);
567 UNLEAK(alias);
568 return alias;
Ralf Thielowaf741282016-08-26 19:58:35 +0200569 }
570
571 if (exclude_guides)
572 return help_unknown_cmd(cmd);
573
574 return cmd;
575}
576
Ævar Arnfjörð Bjarmasond35d03c2021-09-22 00:40:36 +0200577static void no_extra_argc(int argc)
578{
579 if (argc)
580 usage_msg_opt(_("this option doesn't take any other arguments"),
581 builtin_help_usage, builtin_help_options);
582}
583
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200584int cmd_help(int argc, const char **argv, const char *prefix)
585{
586 int nongit;
Christian Couder3caa8232010-01-09 06:10:05 +0100587 enum help_format parsed_help_format;
Andrei Rybakd5659f82021-07-04 17:39:12 +0200588 const char *page;
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200589
Stephen Boyd37782922009-05-23 11:53:12 -0700590 argc = parse_options(argc, argv, prefix, builtin_help_options,
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200591 builtin_help_usage, 0);
Christian Couder3caa8232010-01-09 06:10:05 +0100592 parsed_help_format = help_format;
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200593
Ævar Arnfjörð Bjarmasond35d03c2021-09-22 00:40:36 +0200594 switch (cmd_mode) {
595 case HELP_ACTION_ALL:
Nguyễn Thái Ngọc Duy63eae832018-05-20 20:40:01 +0200596 if (verbose) {
597 setup_pager();
598 list_all_cmds_help();
599 return 0;
600 }
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700601 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
Sebastian Schubertha3c52632014-01-02 17:16:30 +0100602 load_command_list("git-", &main_cmds, &other_cmds);
Ævar Arnfjörð Bjarmason06fa4db2021-09-22 00:40:39 +0200603 list_commands(&main_cmds, &other_cmds);
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700604 printf("%s\n", _(git_more_info_string));
Ævar Arnfjörð Bjarmasond35d03c2021-09-22 00:40:36 +0200605 break;
606 case HELP_ACTION_GUIDES:
607 no_extra_argc(argc);
Ævar Arnfjörð Bjarmason1ed4bef2021-09-22 00:40:34 +0200608 list_guides_help();
Ævar Arnfjörð Bjarmason1ed4bef2021-09-22 00:40:34 +0200609 printf("%s\n", _(git_more_info_string));
610 return 0;
Ævar Arnfjörð Bjarmasond35d03c2021-09-22 00:40:36 +0200611 case HELP_ACTION_CONFIG_FOR_COMPLETION:
Ævar Arnfjörð Bjarmasona9baccc2021-09-22 00:40:38 +0200612 no_extra_argc(argc);
613 list_config_help(SHOW_CONFIG_VARS);
614 return 0;
615 case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION:
616 no_extra_argc(argc);
617 list_config_help(SHOW_CONFIG_SECTIONS);
Ævar Arnfjörð Bjarmasond35d03c2021-09-22 00:40:36 +0200618 return 0;
619 case HELP_ACTION_CONFIG:
620 no_extra_argc(argc);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200621 setup_pager();
Ævar Arnfjörð Bjarmasona9baccc2021-09-22 00:40:38 +0200622 list_config_help(SHOW_CONFIG_HUMAN);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200623 printf("\n%s\n", _("'git help config' for more information"));
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200624 return 0;
625 }
626
627 if (!argv[0]) {
628 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
629 list_common_cmds_help();
630 printf("\n%s\n", _(git_more_info_string));
631 return 0;
632 }
633
634 setup_git_directory_gently(&nongit);
635 git_config(git_help_config, NULL);
636
637 if (parsed_help_format != HELP_FORMAT_NONE)
638 help_format = parsed_help_format;
639 if (help_format == HELP_FORMAT_NONE)
640 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
641
Ralf Thielowaf741282016-08-26 19:58:35 +0200642 argv[0] = check_git_cmd(argv[0]);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200643
Andrei Rybakd5659f82021-07-04 17:39:12 +0200644 page = cmd_to_page(argv[0]);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200645 switch (help_format) {
Christian Couder3caa8232010-01-09 06:10:05 +0100646 case HELP_FORMAT_NONE:
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200647 case HELP_FORMAT_MAN:
Andrei Rybakd5659f82021-07-04 17:39:12 +0200648 show_man_page(page);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200649 break;
650 case HELP_FORMAT_INFO:
Andrei Rybakd5659f82021-07-04 17:39:12 +0200651 show_info_page(page);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200652 break;
653 case HELP_FORMAT_WEB:
Andrei Rybakd5659f82021-07-04 17:39:12 +0200654 show_html_page(page);
Miklos Vajna3d78d1f2008-08-02 10:08:38 +0200655 break;
656 }
657
658 return 0;
659}