| 1 | /* |
| 2 | * Builtin help command |
| 3 | */ |
| 4 | |
| 5 | #define USE_THE_REPOSITORY_VARIABLE |
| 6 | |
| 7 | #include "builtin.h" |
| 8 | #include "config.h" |
| 9 | #include "environment.h" |
| 10 | #include "exec-cmd.h" |
| 11 | #include "gettext.h" |
| 12 | #include "pager.h" |
| 13 | #include "parse-options.h" |
| 14 | #include "path.h" |
| 15 | #include "run-command.h" |
| 16 | #include "config-list.h" |
| 17 | #include "help.h" |
| 18 | #include "alias.h" |
| 19 | #include "setup.h" |
| 20 | |
| 21 | #ifndef DEFAULT_HELP_FORMAT |
| 22 | #define DEFAULT_HELP_FORMAT "man" |
| 23 | #endif |
| 24 | |
| 25 | static struct man_viewer_list { |
| 26 | struct man_viewer_list *next; |
| 27 | char name[FLEX_ARRAY]; |
| 28 | } *man_viewer_list; |
| 29 | |
| 30 | static struct man_viewer_info_list { |
| 31 | struct man_viewer_info_list *next; |
| 32 | const char *info; |
| 33 | char name[FLEX_ARRAY]; |
| 34 | } *man_viewer_info_list; |
| 35 | |
| 36 | enum help_format { |
| 37 | HELP_FORMAT_NONE, |
| 38 | HELP_FORMAT_MAN, |
| 39 | HELP_FORMAT_INFO, |
| 40 | HELP_FORMAT_WEB |
| 41 | }; |
| 42 | |
| 43 | enum show_config_type { |
| 44 | SHOW_CONFIG_HUMAN, |
| 45 | SHOW_CONFIG_VARS, |
| 46 | SHOW_CONFIG_SECTIONS, |
| 47 | }; |
| 48 | |
| 49 | static enum help_action { |
| 50 | HELP_ACTION_ALL = 1, |
| 51 | HELP_ACTION_GUIDES, |
| 52 | HELP_ACTION_CONFIG, |
| 53 | HELP_ACTION_USER_INTERFACES, |
| 54 | HELP_ACTION_DEVELOPER_INTERFACES, |
| 55 | HELP_ACTION_CONFIG_FOR_COMPLETION, |
| 56 | HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION, |
| 57 | HELP_ACTION_ALIASES_FOR_COMPLETION, |
| 58 | } cmd_mode; |
| 59 | |
| 60 | static char *html_path; |
| 61 | static int verbose = 1; |
| 62 | static enum help_format help_format = HELP_FORMAT_NONE; |
| 63 | static int exclude_guides; |
| 64 | static int show_external_commands = -1; |
| 65 | static int show_aliases = -1; |
| 66 | static struct option builtin_help_options[] = { |
| 67 | OPT_CMDMODE('a', "all", &cmd_mode, N_("print all available commands"), |
| 68 | HELP_ACTION_ALL), |
| 69 | OPT_BOOL(0, "external-commands", &show_external_commands, |
| 70 | N_("show external commands in --all")), |
| 71 | OPT_BOOL(0, "aliases", &show_aliases, N_("show aliases in --all")), |
| 72 | OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")), |
| 73 | OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN), |
| 74 | OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"), |
| 75 | HELP_FORMAT_WEB), |
| 76 | OPT_SET_INT('i', "info", &help_format, N_("show info page"), |
| 77 | HELP_FORMAT_INFO), |
| 78 | OPT__VERBOSE(&verbose, N_("print command description")), |
| 79 | |
| 80 | OPT_CMDMODE('g', "guides", &cmd_mode, N_("print list of useful guides"), |
| 81 | HELP_ACTION_GUIDES), |
| 82 | OPT_CMDMODE(0, "user-interfaces", &cmd_mode, |
| 83 | N_("print list of user-facing repository, command and file interfaces"), |
| 84 | HELP_ACTION_USER_INTERFACES), |
| 85 | OPT_CMDMODE(0, "developer-interfaces", &cmd_mode, |
| 86 | N_("print list of file formats, protocols and other developer interfaces"), |
| 87 | HELP_ACTION_DEVELOPER_INTERFACES), |
| 88 | OPT_CMDMODE('c', "config", &cmd_mode, N_("print all configuration variable names"), |
| 89 | HELP_ACTION_CONFIG), |
| 90 | OPT_CMDMODE_F(0, "config-for-completion", &cmd_mode, "", |
| 91 | HELP_ACTION_CONFIG_FOR_COMPLETION, PARSE_OPT_HIDDEN), |
| 92 | OPT_CMDMODE_F(0, "config-sections-for-completion", &cmd_mode, "", |
| 93 | HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION, PARSE_OPT_HIDDEN), |
| 94 | OPT_CMDMODE_F(0, "aliases-for-completion", &cmd_mode, "", |
| 95 | HELP_ACTION_ALIASES_FOR_COMPLETION, PARSE_OPT_HIDDEN), |
| 96 | |
| 97 | OPT_END(), |
| 98 | }; |
| 99 | |
| 100 | static const char * const builtin_help_usage[] = { |
| 101 | "git help [-a|--all] [--[no-]verbose] [--[no-]external-commands] [--[no-]aliases]", |
| 102 | N_("git help [[-i|--info] [-m|--man] [-w|--web]] [<command>|<doc>]"), |
| 103 | "git help [-g|--guides]", |
| 104 | "git help [-c|--config]", |
| 105 | "git help [--user-interfaces]", |
| 106 | "git help [--developer-interfaces]", |
| 107 | NULL |
| 108 | }; |
| 109 | |
| 110 | struct slot_expansion { |
| 111 | const char *prefix; |
| 112 | const char *placeholder; |
| 113 | void (*fn)(struct string_list *list, const char *prefix); |
| 114 | int found; |
| 115 | }; |
| 116 | |
| 117 | static void set_config_vars(struct string_list *keys_uniq, struct string_list_item *var) |
| 118 | { |
| 119 | struct strbuf sb = STRBUF_INIT; |
| 120 | const char *str = var->string; |
| 121 | const char *wildcard = strchr(str, '*'); |
| 122 | const char *tag = strchr(str, '<'); |
| 123 | const char *cut; |
| 124 | |
| 125 | if (wildcard && tag) |
| 126 | cut = wildcard < tag ? wildcard : tag; |
| 127 | else if (wildcard) |
| 128 | cut = wildcard; |
| 129 | else if (tag) |
| 130 | cut = tag; |
| 131 | else { |
| 132 | string_list_append(keys_uniq, str); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | strbuf_add(&sb, str, cut - str); |
| 137 | string_list_append(keys_uniq, sb.buf); |
| 138 | strbuf_release(&sb); |
| 139 | } |
| 140 | |
| 141 | static void set_config_sections(struct string_list *keys_uniq, struct string_list_item *var) |
| 142 | { |
| 143 | struct strbuf sb = STRBUF_INIT; |
| 144 | const char *str = var->string; |
| 145 | const char *dot = strchr(str, '.'); |
| 146 | const char *cut; |
| 147 | |
| 148 | if (dot) |
| 149 | cut = dot; |
| 150 | else { |
| 151 | set_config_vars(keys_uniq, var); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | strbuf_add(&sb, str, cut - str); |
| 156 | string_list_append(keys_uniq, sb.buf); |
| 157 | strbuf_release(&sb); |
| 158 | } |
| 159 | |
| 160 | static void list_config_help(enum show_config_type type) |
| 161 | { |
| 162 | struct slot_expansion slot_expansions[] = { |
| 163 | { "advice", "*", list_config_advices }, |
| 164 | { "color.branch", "<slot>", list_config_color_branch_slots }, |
| 165 | { "color.decorate", "<slot>", list_config_color_decorate_slots }, |
| 166 | { "color.diff", "<slot>", list_config_color_diff_slots }, |
| 167 | { "color.grep", "<slot>", list_config_color_grep_slots }, |
| 168 | { "color.interactive", "<slot>", list_config_color_interactive_slots }, |
| 169 | { "color.remote", "<slot>", list_config_color_sideband_slots }, |
| 170 | { "color.status", "<slot>", list_config_color_status_slots }, |
| 171 | { "fsck", "<msg-id>", list_config_fsck_msg_ids }, |
| 172 | { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids }, |
| 173 | { NULL, NULL, NULL } |
| 174 | }; |
| 175 | const char **p; |
| 176 | struct slot_expansion *e; |
| 177 | struct string_list keys = STRING_LIST_INIT_DUP; |
| 178 | struct string_list keys_uniq = STRING_LIST_INIT_DUP; |
| 179 | struct string_list_item *item; |
| 180 | struct strbuf sb = STRBUF_INIT; |
| 181 | |
| 182 | for (p = config_name_list; *p; p++) { |
| 183 | const char *var = *p; |
| 184 | |
| 185 | for (e = slot_expansions; e->prefix; e++) { |
| 186 | strbuf_reset(&sb); |
| 187 | strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder); |
| 188 | if (!strcasecmp(var, sb.buf)) { |
| 189 | e->fn(&keys, e->prefix); |
| 190 | e->found++; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (!e->prefix) |
| 196 | string_list_append(&keys, var); |
| 197 | } |
| 198 | |
| 199 | strbuf_release(&sb); |
| 200 | |
| 201 | for (e = slot_expansions; e->prefix; e++) |
| 202 | if (!e->found) |
| 203 | BUG("slot_expansion %s.%s is not used", |
| 204 | e->prefix, e->placeholder); |
| 205 | |
| 206 | for (size_t i = 0; i < keys.nr; i++) { |
| 207 | switch (type) { |
| 208 | case SHOW_CONFIG_HUMAN: |
| 209 | string_list_append(&keys_uniq, keys.items[i].string); |
| 210 | break; |
| 211 | case SHOW_CONFIG_SECTIONS: |
| 212 | set_config_sections(&keys_uniq, &keys.items[i]); |
| 213 | break; |
| 214 | case SHOW_CONFIG_VARS: |
| 215 | set_config_vars(&keys_uniq, &keys.items[i]); |
| 216 | break; |
| 217 | default: |
| 218 | BUG("%d: unexpected type", type); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | string_list_sort_u(&keys_uniq, 0); |
| 223 | for_each_string_list_item(item, &keys_uniq) |
| 224 | puts(item->string); |
| 225 | string_list_clear(&keys_uniq, 0); |
| 226 | string_list_clear(&keys, 0); |
| 227 | } |
| 228 | |
| 229 | static enum help_format parse_help_format(const char *format) |
| 230 | { |
| 231 | if (!strcmp(format, "man")) |
| 232 | return HELP_FORMAT_MAN; |
| 233 | if (!strcmp(format, "info")) |
| 234 | return HELP_FORMAT_INFO; |
| 235 | if (!strcmp(format, "web") || !strcmp(format, "html")) |
| 236 | return HELP_FORMAT_WEB; |
| 237 | /* |
| 238 | * Please update _repo_config() in git-completion.bash when you |
| 239 | * add new help formats. |
| 240 | */ |
| 241 | die(_("unrecognized help format '%s'"), format); |
| 242 | } |
| 243 | |
| 244 | static const char *get_man_viewer_info(const char *name) |
| 245 | { |
| 246 | struct man_viewer_info_list *viewer; |
| 247 | |
| 248 | for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) |
| 249 | { |
| 250 | if (!strcasecmp(name, viewer->name)) |
| 251 | return viewer->info; |
| 252 | } |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | static int check_emacsclient_version(void) |
| 257 | { |
| 258 | struct strbuf buffer = STRBUF_INIT; |
| 259 | struct child_process ec_process = CHILD_PROCESS_INIT; |
| 260 | int version; |
| 261 | |
| 262 | /* emacsclient prints its version number on stderr */ |
| 263 | strvec_pushl(&ec_process.args, "emacsclient", "--version", NULL); |
| 264 | ec_process.err = -1; |
| 265 | ec_process.stdout_to_stderr = 1; |
| 266 | if (start_command(&ec_process)) |
| 267 | return error(_("Failed to start emacsclient.")); |
| 268 | |
| 269 | strbuf_read(&buffer, ec_process.err, 20); |
| 270 | close(ec_process.err); |
| 271 | |
| 272 | /* |
| 273 | * Don't bother checking return value, because "emacsclient --version" |
| 274 | * seems to always exits with code 1. |
| 275 | */ |
| 276 | finish_command(&ec_process); |
| 277 | |
| 278 | if (!starts_with(buffer.buf, "emacsclient")) { |
| 279 | strbuf_release(&buffer); |
| 280 | return error(_("Failed to parse emacsclient version.")); |
| 281 | } |
| 282 | |
| 283 | strbuf_remove(&buffer, 0, strlen("emacsclient")); |
| 284 | version = atoi(buffer.buf); |
| 285 | |
| 286 | if (version < 22) { |
| 287 | strbuf_release(&buffer); |
| 288 | return error(_("emacsclient version '%d' too old (< 22)."), |
| 289 | version); |
| 290 | } |
| 291 | |
| 292 | strbuf_release(&buffer); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static void exec_woman_emacs(const char *path, const char *page) |
| 297 | { |
| 298 | if (!check_emacsclient_version()) { |
| 299 | /* This works only with emacsclient version >= 22. */ |
| 300 | struct strbuf man_page = STRBUF_INIT; |
| 301 | |
| 302 | if (!path) |
| 303 | path = "emacsclient"; |
| 304 | strbuf_addf(&man_page, "(woman \"%s\")", page); |
| 305 | execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL); |
| 306 | warning_errno(_("failed to exec '%s'"), path); |
| 307 | strbuf_release(&man_page); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | static void exec_man_konqueror(const char *path, const char *page) |
| 312 | { |
| 313 | const char *display = getenv("DISPLAY"); |
| 314 | if (display && *display) { |
| 315 | struct strbuf man_page = STRBUF_INIT; |
| 316 | const char *filename = "kfmclient"; |
| 317 | |
| 318 | /* It's simpler to launch konqueror using kfmclient. */ |
| 319 | if (path) { |
| 320 | size_t len; |
| 321 | if (strip_suffix(path, "/konqueror", &len)) |
| 322 | path = xstrfmt("%.*s/kfmclient", (int)len, path); |
| 323 | filename = basename((char *)path); |
| 324 | } else |
| 325 | path = "kfmclient"; |
| 326 | strbuf_addf(&man_page, "man:%s(1)", page); |
| 327 | execlp(path, filename, "newTab", man_page.buf, (char *)NULL); |
| 328 | warning_errno(_("failed to exec '%s'"), path); |
| 329 | strbuf_release(&man_page); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | static void exec_man_man(const char *path, const char *page) |
| 334 | { |
| 335 | if (!path) |
| 336 | path = "man"; |
| 337 | execlp(path, "man", page, (char *)NULL); |
| 338 | warning_errno(_("failed to exec '%s'"), path); |
| 339 | } |
| 340 | |
| 341 | static void exec_man_cmd(const char *cmd, const char *page) |
| 342 | { |
| 343 | struct strbuf shell_cmd = STRBUF_INIT; |
| 344 | strbuf_addf(&shell_cmd, "%s %s", cmd, page); |
| 345 | execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL); |
| 346 | warning(_("failed to exec '%s'"), cmd); |
| 347 | strbuf_release(&shell_cmd); |
| 348 | } |
| 349 | |
| 350 | static void add_man_viewer(const char *name) |
| 351 | { |
| 352 | struct man_viewer_list **p = &man_viewer_list; |
| 353 | |
| 354 | while (*p) |
| 355 | p = &((*p)->next); |
| 356 | FLEX_ALLOC_STR(*p, name, name); |
| 357 | } |
| 358 | |
| 359 | static int supported_man_viewer(const char *name, size_t len) |
| 360 | { |
| 361 | return (!strncasecmp("man", name, len) || |
| 362 | !strncasecmp("woman", name, len) || |
| 363 | !strncasecmp("konqueror", name, len)); |
| 364 | } |
| 365 | |
| 366 | static void do_add_man_viewer_info(const char *name, |
| 367 | size_t len, |
| 368 | const char *value) |
| 369 | { |
| 370 | struct man_viewer_info_list *new_man_viewer; |
| 371 | FLEX_ALLOC_MEM(new_man_viewer, name, name, len); |
| 372 | new_man_viewer->info = xstrdup(value); |
| 373 | new_man_viewer->next = man_viewer_info_list; |
| 374 | man_viewer_info_list = new_man_viewer; |
| 375 | } |
| 376 | |
| 377 | static int add_man_viewer_path(const char *name, |
| 378 | size_t len, |
| 379 | const char *value) |
| 380 | { |
| 381 | if (supported_man_viewer(name, len)) |
| 382 | do_add_man_viewer_info(name, len, value); |
| 383 | else |
| 384 | warning(_("'%s': path for unsupported man viewer.\n" |
| 385 | "Please consider using 'man.<tool>.cmd' instead."), |
| 386 | name); |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static int add_man_viewer_cmd(const char *name, |
| 392 | size_t len, |
| 393 | const char *value) |
| 394 | { |
| 395 | if (supported_man_viewer(name, len)) |
| 396 | warning(_("'%s': cmd for supported man viewer.\n" |
| 397 | "Please consider using 'man.<tool>.path' instead."), |
| 398 | name); |
| 399 | else |
| 400 | do_add_man_viewer_info(name, len, value); |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static int add_man_viewer_info(const char *var, const char *value) |
| 406 | { |
| 407 | const char *name, *subkey; |
| 408 | size_t namelen; |
| 409 | |
| 410 | if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name) |
| 411 | return 0; |
| 412 | |
| 413 | if (!strcmp(subkey, "path")) { |
| 414 | if (!value) |
| 415 | return config_error_nonbool(var); |
| 416 | return add_man_viewer_path(name, namelen, value); |
| 417 | } |
| 418 | if (!strcmp(subkey, "cmd")) { |
| 419 | if (!value) |
| 420 | return config_error_nonbool(var); |
| 421 | return add_man_viewer_cmd(name, namelen, value); |
| 422 | } |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static int git_help_config(const char *var, const char *value, |
| 428 | const struct config_context *ctx, void *cb) |
| 429 | { |
| 430 | if (!strcmp(var, "help.format")) { |
| 431 | if (!value) |
| 432 | return config_error_nonbool(var); |
| 433 | help_format = parse_help_format(value); |
| 434 | return 0; |
| 435 | } |
| 436 | if (!strcmp(var, "help.htmlpath")) { |
| 437 | if (!value) |
| 438 | return config_error_nonbool(var); |
| 439 | free(html_path); |
| 440 | html_path = xstrdup(value); |
| 441 | return 0; |
| 442 | } |
| 443 | if (!strcmp(var, "man.viewer")) { |
| 444 | if (!value) |
| 445 | return config_error_nonbool(var); |
| 446 | add_man_viewer(value); |
| 447 | return 0; |
| 448 | } |
| 449 | if (starts_with(var, "man.")) |
| 450 | return add_man_viewer_info(var, value); |
| 451 | |
| 452 | return git_default_config(var, value, ctx, cb); |
| 453 | } |
| 454 | |
| 455 | static struct cmdnames main_cmds, other_cmds; |
| 456 | |
| 457 | static int is_git_command(const char *s) |
| 458 | { |
| 459 | if (is_builtin(s)) |
| 460 | return 1; |
| 461 | |
| 462 | load_command_list("git-", &main_cmds, &other_cmds); |
| 463 | return is_in_cmdlist(&main_cmds, s) || |
| 464 | is_in_cmdlist(&other_cmds, s); |
| 465 | } |
| 466 | |
| 467 | static const char *cmd_to_page(const char *git_cmd) |
| 468 | { |
| 469 | if (!git_cmd) |
| 470 | return "git"; |
| 471 | else if (starts_with(git_cmd, "git")) |
| 472 | return git_cmd; |
| 473 | else if (is_git_command(git_cmd)) |
| 474 | return xstrfmt("git-%s", git_cmd); |
| 475 | else if (!strcmp("scalar", git_cmd)) |
| 476 | return xstrdup(git_cmd); |
| 477 | else |
| 478 | return xstrfmt("git%s", git_cmd); |
| 479 | } |
| 480 | |
| 481 | static void setup_man_path(void) |
| 482 | { |
| 483 | struct strbuf new_path = STRBUF_INIT; |
| 484 | const char *old_path = getenv("MANPATH"); |
| 485 | char *git_man_path = system_path(GIT_MAN_PATH); |
| 486 | |
| 487 | /* We should always put ':' after our path. If there is no |
| 488 | * old_path, the ':' at the end will let 'man' to try |
| 489 | * system-wide paths after ours to find the manual page. If |
| 490 | * there is old_path, we need ':' as delimiter. */ |
| 491 | strbuf_addstr(&new_path, git_man_path); |
| 492 | strbuf_addch(&new_path, ':'); |
| 493 | if (old_path) |
| 494 | strbuf_addstr(&new_path, old_path); |
| 495 | |
| 496 | free(git_man_path); |
| 497 | setenv("MANPATH", new_path.buf, 1); |
| 498 | |
| 499 | strbuf_release(&new_path); |
| 500 | } |
| 501 | |
| 502 | static void exec_viewer(const char *name, const char *page) |
| 503 | { |
| 504 | const char *info = get_man_viewer_info(name); |
| 505 | |
| 506 | if (!strcasecmp(name, "man")) |
| 507 | exec_man_man(info, page); |
| 508 | else if (!strcasecmp(name, "woman")) |
| 509 | exec_woman_emacs(info, page); |
| 510 | else if (!strcasecmp(name, "konqueror")) |
| 511 | exec_man_konqueror(info, page); |
| 512 | else if (info) |
| 513 | exec_man_cmd(info, page); |
| 514 | else |
| 515 | warning(_("'%s': unknown man viewer."), name); |
| 516 | } |
| 517 | |
| 518 | static void show_man_page(const char *page) |
| 519 | { |
| 520 | struct man_viewer_list *viewer; |
| 521 | const char *fallback = getenv("GIT_MAN_VIEWER"); |
| 522 | |
| 523 | setup_man_path(); |
| 524 | for (viewer = man_viewer_list; viewer; viewer = viewer->next) |
| 525 | { |
| 526 | exec_viewer(viewer->name, page); /* will return when unable */ |
| 527 | } |
| 528 | if (fallback) |
| 529 | exec_viewer(fallback, page); |
| 530 | exec_viewer("man", page); |
| 531 | die(_("no man viewer handled the request")); |
| 532 | } |
| 533 | |
| 534 | static void show_info_page(const char *page) |
| 535 | { |
| 536 | setenv("INFOPATH", system_path(GIT_INFO_PATH), 1); |
| 537 | execlp("info", "info", "gitman", page, (char *)NULL); |
| 538 | die(_("no info viewer handled the request")); |
| 539 | } |
| 540 | |
| 541 | static void get_html_page_path(struct strbuf *page_path, const char *page) |
| 542 | { |
| 543 | struct stat st; |
| 544 | const char *path = html_path; |
| 545 | char *to_free = NULL; |
| 546 | |
| 547 | if (!path) |
| 548 | path = to_free = system_path(GIT_HTML_PATH); |
| 549 | |
| 550 | /* |
| 551 | * Check that the page we're looking for exists. |
| 552 | */ |
| 553 | if (!strstr(path, "://")) { |
| 554 | if (stat(mkpath("%s/%s.html", path, page), &st) |
| 555 | || !S_ISREG(st.st_mode)) |
| 556 | die("'%s/%s.html': documentation file not found.", |
| 557 | path, page); |
| 558 | } |
| 559 | |
| 560 | strbuf_init(page_path, 0); |
| 561 | strbuf_addf(page_path, "%s/%s.html", path, page); |
| 562 | free(to_free); |
| 563 | } |
| 564 | |
| 565 | static void open_html(const char *path) |
| 566 | { |
| 567 | execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL); |
| 568 | } |
| 569 | |
| 570 | static void show_html_page(const char *page) |
| 571 | { |
| 572 | struct strbuf page_path; /* it leaks but we exec below */ |
| 573 | |
| 574 | get_html_page_path(&page_path, page); |
| 575 | |
| 576 | open_html(page_path.buf); |
| 577 | } |
| 578 | |
| 579 | static char *check_git_cmd(const char *cmd) |
| 580 | { |
| 581 | char *alias; |
| 582 | |
| 583 | if (is_git_command(cmd)) |
| 584 | return xstrdup(cmd); |
| 585 | |
| 586 | alias = alias_lookup(cmd); |
| 587 | if (alias) { |
| 588 | const char **argv; |
| 589 | int count; |
| 590 | |
| 591 | /* |
| 592 | * handle_builtin() in git.c rewrites "git cmd --help" |
| 593 | * to "git help --exclude-guides cmd", so we can use |
| 594 | * exclude_guides to distinguish "git cmd --help" from |
| 595 | * "git help cmd". In the latter case, or if cmd is an |
| 596 | * alias for a shell command, just print the alias |
| 597 | * definition. |
| 598 | */ |
| 599 | if (!exclude_guides || alias[0] == '!') { |
| 600 | printf_ln(_("'%s' is aliased to '%s'"), cmd, alias); |
| 601 | free(alias); |
| 602 | exit(0); |
| 603 | } |
| 604 | /* |
| 605 | * Otherwise, we pretend that the command was "git |
| 606 | * word0 --help". We use split_cmdline() to get the |
| 607 | * first word of the alias, to ensure that we use the |
| 608 | * same rules as when the alias is actually |
| 609 | * used. split_cmdline() modifies alias in-place. |
| 610 | */ |
| 611 | fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias); |
| 612 | count = split_cmdline(alias, &argv); |
| 613 | if (count < 0) |
| 614 | die(_("bad alias.%s string: %s"), cmd, |
| 615 | split_cmdline_strerror(count)); |
| 616 | free(argv); |
| 617 | return alias; |
| 618 | } |
| 619 | |
| 620 | if (exclude_guides) |
| 621 | return help_unknown_cmd(cmd); |
| 622 | |
| 623 | return xstrdup(cmd); |
| 624 | } |
| 625 | |
| 626 | static void no_help_format(const char *opt_mode, enum help_format fmt) |
| 627 | { |
| 628 | const char *opt_fmt; |
| 629 | |
| 630 | switch (fmt) { |
| 631 | case HELP_FORMAT_NONE: |
| 632 | return; |
| 633 | case HELP_FORMAT_MAN: |
| 634 | opt_fmt = "--man"; |
| 635 | break; |
| 636 | case HELP_FORMAT_INFO: |
| 637 | opt_fmt = "--info"; |
| 638 | break; |
| 639 | case HELP_FORMAT_WEB: |
| 640 | opt_fmt = "--web"; |
| 641 | break; |
| 642 | default: |
| 643 | BUG("unreachable"); |
| 644 | } |
| 645 | |
| 646 | usage_msg_optf(_("options '%s' and '%s' cannot be used together"), |
| 647 | builtin_help_usage, builtin_help_options, opt_mode, |
| 648 | opt_fmt); |
| 649 | } |
| 650 | |
| 651 | static void opt_mode_usage(int argc, const char *opt_mode, |
| 652 | enum help_format fmt) |
| 653 | { |
| 654 | if (argc) |
| 655 | usage_msg_optf(_("the '%s' option doesn't take any non-option arguments"), |
| 656 | builtin_help_usage, builtin_help_options, |
| 657 | opt_mode); |
| 658 | |
| 659 | no_help_format(opt_mode, fmt); |
| 660 | } |
| 661 | |
| 662 | int cmd_help(int argc, |
| 663 | const char **argv, |
| 664 | const char *prefix, |
| 665 | struct repository *repo UNUSED) |
| 666 | { |
| 667 | int nongit; |
| 668 | enum help_format parsed_help_format; |
| 669 | char *command = NULL; |
| 670 | const char *page; |
| 671 | |
| 672 | argc = parse_options(argc, argv, prefix, builtin_help_options, |
| 673 | builtin_help_usage, 0); |
| 674 | parsed_help_format = help_format; |
| 675 | |
| 676 | if (cmd_mode != HELP_ACTION_ALL && |
| 677 | (show_external_commands >= 0 || |
| 678 | show_aliases >= 0)) |
| 679 | usage_msg_opt(_("the '--no-[external-commands|aliases]' options can only be used with '--all'"), |
| 680 | builtin_help_usage, builtin_help_options); |
| 681 | |
| 682 | switch (cmd_mode) { |
| 683 | case HELP_ACTION_ALL: |
| 684 | opt_mode_usage(argc, "--all", help_format); |
| 685 | if (verbose) { |
| 686 | setup_pager(the_repository); |
| 687 | list_all_cmds_help(show_external_commands, |
| 688 | show_aliases); |
| 689 | return 0; |
| 690 | } |
| 691 | printf(_("usage: %s%s"), _(git_usage_string), "\n\n"); |
| 692 | load_command_list("git-", &main_cmds, &other_cmds); |
| 693 | list_commands(&main_cmds, &other_cmds); |
| 694 | printf("%s\n", _(git_more_info_string)); |
| 695 | break; |
| 696 | case HELP_ACTION_GUIDES: |
| 697 | opt_mode_usage(argc, "--guides", help_format); |
| 698 | list_guides_help(); |
| 699 | printf("%s\n", _(git_more_info_string)); |
| 700 | return 0; |
| 701 | case HELP_ACTION_CONFIG_FOR_COMPLETION: |
| 702 | opt_mode_usage(argc, "--config-for-completion", help_format); |
| 703 | list_config_help(SHOW_CONFIG_VARS); |
| 704 | return 0; |
| 705 | case HELP_ACTION_USER_INTERFACES: |
| 706 | opt_mode_usage(argc, "--user-interfaces", help_format); |
| 707 | list_user_interfaces_help(); |
| 708 | return 0; |
| 709 | case HELP_ACTION_DEVELOPER_INTERFACES: |
| 710 | opt_mode_usage(argc, "--developer-interfaces", help_format); |
| 711 | list_developer_interfaces_help(); |
| 712 | return 0; |
| 713 | case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION: |
| 714 | opt_mode_usage(argc, "--config-sections-for-completion", |
| 715 | help_format); |
| 716 | list_config_help(SHOW_CONFIG_SECTIONS); |
| 717 | return 0; |
| 718 | case HELP_ACTION_ALIASES_FOR_COMPLETION: { |
| 719 | struct string_list alias_list = STRING_LIST_INIT_DUP; |
| 720 | opt_mode_usage(argc, "--aliases-for-completion", help_format); |
| 721 | list_aliases(&alias_list); |
| 722 | for (size_t i = 0; i < alias_list.nr; i++) |
| 723 | printf("%s%c%s%c", alias_list.items[i].string, '\n', |
| 724 | (char *)alias_list.items[i].util, '\0'); |
| 725 | string_list_clear(&alias_list, 1); |
| 726 | return 0; |
| 727 | } |
| 728 | case HELP_ACTION_CONFIG: |
| 729 | opt_mode_usage(argc, "--config", help_format); |
| 730 | setup_pager(the_repository); |
| 731 | list_config_help(SHOW_CONFIG_HUMAN); |
| 732 | printf("\n%s\n", _("'git help config' for more information")); |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | if (!argv[0]) { |
| 737 | printf(_("usage: %s%s"), _(git_usage_string), "\n\n"); |
| 738 | list_common_cmds_help(); |
| 739 | printf("\n%s\n", _(git_more_info_string)); |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | setup_git_directory_gently(the_repository, &nongit); |
| 744 | repo_config(the_repository, git_help_config, NULL); |
| 745 | |
| 746 | if (parsed_help_format != HELP_FORMAT_NONE) |
| 747 | help_format = parsed_help_format; |
| 748 | if (help_format == HELP_FORMAT_NONE) |
| 749 | help_format = parse_help_format(DEFAULT_HELP_FORMAT); |
| 750 | |
| 751 | command = check_git_cmd(argv[0]); |
| 752 | |
| 753 | page = cmd_to_page(command); |
| 754 | switch (help_format) { |
| 755 | case HELP_FORMAT_NONE: |
| 756 | case HELP_FORMAT_MAN: |
| 757 | show_man_page(page); |
| 758 | break; |
| 759 | case HELP_FORMAT_INFO: |
| 760 | show_info_page(page); |
| 761 | break; |
| 762 | case HELP_FORMAT_WEB: |
| 763 | show_html_page(page); |
| 764 | break; |
| 765 | } |
| 766 | |
| 767 | free(command); |
| 768 | return 0; |
| 769 | } |