builtin / help.con commit Merge branch 'sg/update-ref-stdin-cleanup' (9eb9727)
   1/*
   2 * Builtin help command
   3 */
   4#include "cache.h"
   5#include "config.h"
   6#include "builtin.h"
   7#include "exec-cmd.h"
   8#include "parse-options.h"
   9#include "run-command.h"
  10#include "column.h"
  11#include "help.h"
  12#include "alias.h"
  13
  14#ifndef DEFAULT_HELP_FORMAT
  15#define DEFAULT_HELP_FORMAT "man"
  16#endif
  17
  18static 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 {
  30        HELP_FORMAT_NONE,
  31        HELP_FORMAT_MAN,
  32        HELP_FORMAT_INFO,
  33        HELP_FORMAT_WEB
  34};
  35
  36static const char *html_path;
  37
  38static int show_all = 0;
  39static int show_guides = 0;
  40static int show_config;
  41static int verbose;
  42static unsigned int colopts;
  43static enum help_format help_format = HELP_FORMAT_NONE;
  44static int exclude_guides;
  45static struct option builtin_help_options[] = {
  46        OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
  47        OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
  48        OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
  49        OPT_BOOL('c', "config", &show_config, N_("print all configuration variable names")),
  50        OPT_SET_INT_F(0, "config-for-completion", &show_config, "", 2, PARSE_OPT_HIDDEN),
  51        OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
  52        OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
  53                        HELP_FORMAT_WEB),
  54        OPT_SET_INT('i', "info", &help_format, N_("show info page"),
  55                        HELP_FORMAT_INFO),
  56        OPT__VERBOSE(&verbose, N_("print command description")),
  57        OPT_END(),
  58};
  59
  60static const char * const builtin_help_usage[] = {
  61        N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"),
  62        NULL
  63};
  64
  65static enum help_format parse_help_format(const char *format)
  66{
  67        if (!strcmp(format, "man"))
  68                return HELP_FORMAT_MAN;
  69        if (!strcmp(format, "info"))
  70                return HELP_FORMAT_INFO;
  71        if (!strcmp(format, "web") || !strcmp(format, "html"))
  72                return HELP_FORMAT_WEB;
  73        die(_("unrecognized help format '%s'"), format);
  74}
  75
  76static const char *get_man_viewer_info(const char *name)
  77{
  78        struct man_viewer_info_list *viewer;
  79
  80        for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
  81        {
  82                if (!strcasecmp(name, viewer->name))
  83                        return viewer->info;
  84        }
  85        return NULL;
  86}
  87
  88static int check_emacsclient_version(void)
  89{
  90        struct strbuf buffer = STRBUF_INIT;
  91        struct child_process ec_process = CHILD_PROCESS_INIT;
  92        const char *argv_ec[] = { "emacsclient", "--version", NULL };
  93        int version;
  94
  95        /* emacsclient prints its version number on stderr */
  96        ec_process.argv = argv_ec;
  97        ec_process.err = -1;
  98        ec_process.stdout_to_stderr = 1;
  99        if (start_command(&ec_process))
 100                return error(_("Failed to start emacsclient."));
 101
 102        strbuf_read(&buffer, ec_process.err, 20);
 103        close(ec_process.err);
 104
 105        /*
 106         * Don't bother checking return value, because "emacsclient --version"
 107         * seems to always exits with code 1.
 108         */
 109        finish_command(&ec_process);
 110
 111        if (!starts_with(buffer.buf, "emacsclient")) {
 112                strbuf_release(&buffer);
 113                return error(_("Failed to parse emacsclient version."));
 114        }
 115
 116        strbuf_remove(&buffer, 0, strlen("emacsclient"));
 117        version = atoi(buffer.buf);
 118
 119        if (version < 22) {
 120                strbuf_release(&buffer);
 121                return error(_("emacsclient version '%d' too old (< 22)."),
 122                        version);
 123        }
 124
 125        strbuf_release(&buffer);
 126        return 0;
 127}
 128
 129static void exec_woman_emacs(const char *path, const char *page)
 130{
 131        if (!check_emacsclient_version()) {
 132                /* This works only with emacsclient version >= 22. */
 133                struct strbuf man_page = STRBUF_INIT;
 134
 135                if (!path)
 136                        path = "emacsclient";
 137                strbuf_addf(&man_page, "(woman \"%s\")", page);
 138                execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
 139                warning_errno(_("failed to exec '%s'"), path);
 140                strbuf_release(&man_page);
 141        }
 142}
 143
 144static void exec_man_konqueror(const char *path, const char *page)
 145{
 146        const char *display = getenv("DISPLAY");
 147        if (display && *display) {
 148                struct strbuf man_page = STRBUF_INIT;
 149                const char *filename = "kfmclient";
 150
 151                /* It's simpler to launch konqueror using kfmclient. */
 152                if (path) {
 153                        size_t len;
 154                        if (strip_suffix(path, "/konqueror", &len))
 155                                path = xstrfmt("%.*s/kfmclient", (int)len, path);
 156                        filename = basename((char *)path);
 157                } else
 158                        path = "kfmclient";
 159                strbuf_addf(&man_page, "man:%s(1)", page);
 160                execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
 161                warning_errno(_("failed to exec '%s'"), path);
 162                strbuf_release(&man_page);
 163        }
 164}
 165
 166static void exec_man_man(const char *path, const char *page)
 167{
 168        if (!path)
 169                path = "man";
 170        execlp(path, "man", page, (char *)NULL);
 171        warning_errno(_("failed to exec '%s'"), path);
 172}
 173
 174static void exec_man_cmd(const char *cmd, const char *page)
 175{
 176        struct strbuf shell_cmd = STRBUF_INIT;
 177        strbuf_addf(&shell_cmd, "%s %s", cmd, page);
 178        execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
 179        warning(_("failed to exec '%s'"), cmd);
 180        strbuf_release(&shell_cmd);
 181}
 182
 183static void add_man_viewer(const char *name)
 184{
 185        struct man_viewer_list **p = &man_viewer_list;
 186
 187        while (*p)
 188                p = &((*p)->next);
 189        FLEX_ALLOC_STR(*p, name, name);
 190}
 191
 192static int supported_man_viewer(const char *name, size_t len)
 193{
 194        return (!strncasecmp("man", name, len) ||
 195                !strncasecmp("woman", name, len) ||
 196                !strncasecmp("konqueror", name, len));
 197}
 198
 199static void do_add_man_viewer_info(const char *name,
 200                                   size_t len,
 201                                   const char *value)
 202{
 203        struct man_viewer_info_list *new_man_viewer;
 204        FLEX_ALLOC_MEM(new_man_viewer, name, name, len);
 205        new_man_viewer->info = xstrdup(value);
 206        new_man_viewer->next = man_viewer_info_list;
 207        man_viewer_info_list = new_man_viewer;
 208}
 209
 210static int add_man_viewer_path(const char *name,
 211                               size_t len,
 212                               const char *value)
 213{
 214        if (supported_man_viewer(name, len))
 215                do_add_man_viewer_info(name, len, value);
 216        else
 217                warning(_("'%s': path for unsupported man viewer.\n"
 218                          "Please consider using 'man.<tool>.cmd' instead."),
 219                        name);
 220
 221        return 0;
 222}
 223
 224static int add_man_viewer_cmd(const char *name,
 225                              size_t len,
 226                              const char *value)
 227{
 228        if (supported_man_viewer(name, len))
 229                warning(_("'%s': cmd for supported man viewer.\n"
 230                          "Please consider using 'man.<tool>.path' instead."),
 231                        name);
 232        else
 233                do_add_man_viewer_info(name, len, value);
 234
 235        return 0;
 236}
 237
 238static int add_man_viewer_info(const char *var, const char *value)
 239{
 240        const char *name, *subkey;
 241        int namelen;
 242
 243        if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
 244                return 0;
 245
 246        if (!strcmp(subkey, "path")) {
 247                if (!value)
 248                        return config_error_nonbool(var);
 249                return add_man_viewer_path(name, namelen, value);
 250        }
 251        if (!strcmp(subkey, "cmd")) {
 252                if (!value)
 253                        return config_error_nonbool(var);
 254                return add_man_viewer_cmd(name, namelen, value);
 255        }
 256
 257        return 0;
 258}
 259
 260static int git_help_config(const char *var, const char *value, void *cb)
 261{
 262        if (starts_with(var, "column."))
 263                return git_column_config(var, value, "help", &colopts);
 264        if (!strcmp(var, "help.format")) {
 265                if (!value)
 266                        return config_error_nonbool(var);
 267                help_format = parse_help_format(value);
 268                return 0;
 269        }
 270        if (!strcmp(var, "help.htmlpath")) {
 271                if (!value)
 272                        return config_error_nonbool(var);
 273                html_path = xstrdup(value);
 274                return 0;
 275        }
 276        if (!strcmp(var, "man.viewer")) {
 277                if (!value)
 278                        return config_error_nonbool(var);
 279                add_man_viewer(value);
 280                return 0;
 281        }
 282        if (starts_with(var, "man."))
 283                return add_man_viewer_info(var, value);
 284
 285        return git_default_config(var, value, cb);
 286}
 287
 288static struct cmdnames main_cmds, other_cmds;
 289
 290static int is_git_command(const char *s)
 291{
 292        if (is_builtin(s))
 293                return 1;
 294
 295        load_command_list("git-", &main_cmds, &other_cmds);
 296        return is_in_cmdlist(&main_cmds, s) ||
 297                is_in_cmdlist(&other_cmds, s);
 298}
 299
 300static const char *cmd_to_page(const char *git_cmd)
 301{
 302        if (!git_cmd)
 303                return "git";
 304        else if (starts_with(git_cmd, "git"))
 305                return git_cmd;
 306        else if (is_git_command(git_cmd))
 307                return xstrfmt("git-%s", git_cmd);
 308        else
 309                return xstrfmt("git%s", git_cmd);
 310}
 311
 312static void setup_man_path(void)
 313{
 314        struct strbuf new_path = STRBUF_INIT;
 315        const char *old_path = getenv("MANPATH");
 316        char *git_man_path = system_path(GIT_MAN_PATH);
 317
 318        /* We should always put ':' after our path. If there is no
 319         * old_path, the ':' at the end will let 'man' to try
 320         * system-wide paths after ours to find the manual page. If
 321         * there is old_path, we need ':' as delimiter. */
 322        strbuf_addstr(&new_path, git_man_path);
 323        strbuf_addch(&new_path, ':');
 324        if (old_path)
 325                strbuf_addstr(&new_path, old_path);
 326
 327        free(git_man_path);
 328        setenv("MANPATH", new_path.buf, 1);
 329
 330        strbuf_release(&new_path);
 331}
 332
 333static void exec_viewer(const char *name, const char *page)
 334{
 335        const char *info = get_man_viewer_info(name);
 336
 337        if (!strcasecmp(name, "man"))
 338                exec_man_man(info, page);
 339        else if (!strcasecmp(name, "woman"))
 340                exec_woman_emacs(info, page);
 341        else if (!strcasecmp(name, "konqueror"))
 342                exec_man_konqueror(info, page);
 343        else if (info)
 344                exec_man_cmd(info, page);
 345        else
 346                warning(_("'%s': unknown man viewer."), name);
 347}
 348
 349static void show_man_page(const char *git_cmd)
 350{
 351        struct man_viewer_list *viewer;
 352        const char *page = cmd_to_page(git_cmd);
 353        const char *fallback = getenv("GIT_MAN_VIEWER");
 354
 355        setup_man_path();
 356        for (viewer = man_viewer_list; viewer; viewer = viewer->next)
 357        {
 358                exec_viewer(viewer->name, page); /* will return when unable */
 359        }
 360        if (fallback)
 361                exec_viewer(fallback, page);
 362        exec_viewer("man", page);
 363        die(_("no man viewer handled the request"));
 364}
 365
 366static void show_info_page(const char *git_cmd)
 367{
 368        const char *page = cmd_to_page(git_cmd);
 369        setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
 370        execlp("info", "info", "gitman", page, (char *)NULL);
 371        die(_("no info viewer handled the request"));
 372}
 373
 374static void get_html_page_path(struct strbuf *page_path, const char *page)
 375{
 376        struct stat st;
 377        char *to_free = NULL;
 378
 379        if (!html_path)
 380                html_path = to_free = system_path(GIT_HTML_PATH);
 381
 382        /* Check that we have a git documentation directory. */
 383        if (!strstr(html_path, "://")) {
 384                if (stat(mkpath("%s/git.html", html_path), &st)
 385                    || !S_ISREG(st.st_mode))
 386                        die("'%s': not a documentation directory.", html_path);
 387        }
 388
 389        strbuf_init(page_path, 0);
 390        strbuf_addf(page_path, "%s/%s.html", html_path, page);
 391        free(to_free);
 392}
 393
 394static void open_html(const char *path)
 395{
 396        execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
 397}
 398
 399static void show_html_page(const char *git_cmd)
 400{
 401        const char *page = cmd_to_page(git_cmd);
 402        struct strbuf page_path; /* it leaks but we exec bellow */
 403
 404        get_html_page_path(&page_path, page);
 405
 406        open_html(page_path.buf);
 407}
 408
 409static const char *check_git_cmd(const char* cmd)
 410{
 411        char *alias;
 412
 413        if (is_git_command(cmd))
 414                return cmd;
 415
 416        alias = alias_lookup(cmd);
 417        if (alias) {
 418                printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
 419                free(alias);
 420                exit(0);
 421        }
 422
 423        if (exclude_guides)
 424                return help_unknown_cmd(cmd);
 425
 426        return cmd;
 427}
 428
 429int cmd_help(int argc, const char **argv, const char *prefix)
 430{
 431        int nongit;
 432        enum help_format parsed_help_format;
 433
 434        argc = parse_options(argc, argv, prefix, builtin_help_options,
 435                        builtin_help_usage, 0);
 436        parsed_help_format = help_format;
 437
 438        if (show_all) {
 439                git_config(git_help_config, NULL);
 440                if (verbose) {
 441                        setup_pager();
 442                        list_all_cmds_help();
 443                        return 0;
 444                }
 445                printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
 446                load_command_list("git-", &main_cmds, &other_cmds);
 447                list_commands(colopts, &main_cmds, &other_cmds);
 448        }
 449
 450        if (show_config) {
 451                int for_human = show_config == 1;
 452
 453                if (!for_human) {
 454                        list_config_help(for_human);
 455                        return 0;
 456                }
 457                setup_pager();
 458                list_config_help(for_human);
 459                printf("\n%s\n", _("'git help config' for more information"));
 460                return 0;
 461        }
 462
 463        if (show_guides)
 464                list_common_guides_help();
 465
 466        if (show_all || show_guides) {
 467                printf("%s\n", _(git_more_info_string));
 468                /*
 469                * We're done. Ignore any remaining args
 470                */
 471                return 0;
 472        }
 473
 474        if (!argv[0]) {
 475                printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
 476                list_common_cmds_help();
 477                printf("\n%s\n", _(git_more_info_string));
 478                return 0;
 479        }
 480
 481        setup_git_directory_gently(&nongit);
 482        git_config(git_help_config, NULL);
 483
 484        if (parsed_help_format != HELP_FORMAT_NONE)
 485                help_format = parsed_help_format;
 486        if (help_format == HELP_FORMAT_NONE)
 487                help_format = parse_help_format(DEFAULT_HELP_FORMAT);
 488
 489        argv[0] = check_git_cmd(argv[0]);
 490
 491        switch (help_format) {
 492        case HELP_FORMAT_NONE:
 493        case HELP_FORMAT_MAN:
 494                show_man_page(argv[0]);
 495                break;
 496        case HELP_FORMAT_INFO:
 497                show_info_page(argv[0]);
 498                break;
 499        case HELP_FORMAT_WEB:
 500                show_html_page(argv[0]);
 501                break;
 502        }
 503
 504        return 0;
 505}