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