help.con commit help: use parseopt (41eb33b)
   1/*
   2 * builtin-help.c
   3 *
   4 * Builtin help-related commands (help, usage, version)
   5 */
   6#include "cache.h"
   7#include "builtin.h"
   8#include "exec_cmd.h"
   9#include "common-cmds.h"
  10#include "parse-options.h"
  11
  12enum help_format {
  13        HELP_FORMAT_MAN,
  14        HELP_FORMAT_INFO,
  15        HELP_FORMAT_WEB,
  16};
  17
  18static int show_all = 0;
  19static enum help_format help_format = HELP_FORMAT_MAN;
  20static 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
  29static const char * const builtin_help_usage[] = {
  30        "git-help [--all] [--man|--web|--info] [command]",
  31        NULL
  32};
  33
  34static enum help_format parse_help_format(const char *format)
  35{
  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;
  42        die("unrecognized help format '%s'", format);
  43}
  44
  45static int git_help_config(const char *var, const char *value)
  46{
  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        }
  53        return git_default_config(var, value);
  54}
  55
  56/* most GUI terminals set COLUMNS (although some don't export it) */
  57static int term_columns(void)
  58{
  59        char *col_string = getenv("COLUMNS");
  60        int n_cols;
  61
  62        if (col_string && (n_cols = atoi(col_string)) > 0)
  63                return n_cols;
  64
  65#ifdef TIOCGWINSZ
  66        {
  67                struct winsize ws;
  68                if (!ioctl(1, TIOCGWINSZ, &ws)) {
  69                        if (ws.ws_col)
  70                                return ws.ws_col;
  71                }
  72        }
  73#endif
  74
  75        return 80;
  76}
  77
  78static inline void mput_char(char c, unsigned int num)
  79{
  80        while(num--)
  81                putchar(c);
  82}
  83
  84static struct cmdnames {
  85        int alloc;
  86        int cnt;
  87        struct cmdname {
  88                size_t len;
  89                char name[1];
  90        } **names;
  91} main_cmds, other_cmds;
  92
  93static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
  94{
  95        struct cmdname *ent = xmalloc(sizeof(*ent) + len);
  96
  97        ent->len = len;
  98        memcpy(ent->name, name, len);
  99        ent->name[len] = 0;
 100
 101        ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
 102        cmds->names[cmds->cnt++] = ent;
 103}
 104
 105static int cmdname_compare(const void *a_, const void *b_)
 106{
 107        struct cmdname *a = *(struct cmdname **)a_;
 108        struct cmdname *b = *(struct cmdname **)b_;
 109        return strcmp(a->name, b->name);
 110}
 111
 112static void uniq(struct cmdnames *cmds)
 113{
 114        int i, j;
 115
 116        if (!cmds->cnt)
 117                return;
 118
 119        for (i = j = 1; i < cmds->cnt; i++)
 120                if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
 121                        cmds->names[j++] = cmds->names[i];
 122
 123        cmds->cnt = j;
 124}
 125
 126static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
 127{
 128        int ci, cj, ei;
 129        int cmp;
 130
 131        ci = cj = ei = 0;
 132        while (ci < cmds->cnt && ei < excludes->cnt) {
 133                cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
 134                if (cmp < 0)
 135                        cmds->names[cj++] = cmds->names[ci++];
 136                else if (cmp == 0)
 137                        ci++, ei++;
 138                else if (cmp > 0)
 139                        ei++;
 140        }
 141
 142        while (ci < cmds->cnt)
 143                cmds->names[cj++] = cmds->names[ci++];
 144
 145        cmds->cnt = cj;
 146}
 147
 148static void pretty_print_string_list(struct cmdnames *cmds, int longest)
 149{
 150        int cols = 1, rows;
 151        int space = longest + 1; /* min 1 SP between words */
 152        int max_cols = term_columns() - 1; /* don't print *on* the edge */
 153        int i, j;
 154
 155        if (space < max_cols)
 156                cols = max_cols / space;
 157        rows = (cmds->cnt + cols - 1) / cols;
 158
 159        for (i = 0; i < rows; i++) {
 160                printf("  ");
 161
 162                for (j = 0; j < cols; j++) {
 163                        int n = j * rows + i;
 164                        int size = space;
 165                        if (n >= cmds->cnt)
 166                                break;
 167                        if (j == cols-1 || n + rows >= cmds->cnt)
 168                                size = 1;
 169                        printf("%-*s", size, cmds->names[n]->name);
 170                }
 171                putchar('\n');
 172        }
 173}
 174
 175static unsigned int list_commands_in_dir(struct cmdnames *cmds,
 176                                         const char *path)
 177{
 178        unsigned int longest = 0;
 179        const char *prefix = "git-";
 180        int prefix_len = strlen(prefix);
 181        DIR *dir = opendir(path);
 182        struct dirent *de;
 183
 184        if (!dir || chdir(path))
 185                return 0;
 186
 187        while ((de = readdir(dir)) != NULL) {
 188                struct stat st;
 189                int entlen;
 190
 191                if (prefixcmp(de->d_name, prefix))
 192                        continue;
 193
 194                if (stat(de->d_name, &st) || /* stat, not lstat */
 195                    !S_ISREG(st.st_mode) ||
 196                    !(st.st_mode & S_IXUSR))
 197                        continue;
 198
 199                entlen = strlen(de->d_name) - prefix_len;
 200                if (has_extension(de->d_name, ".exe"))
 201                        entlen -= 4;
 202
 203                if (longest < entlen)
 204                        longest = entlen;
 205
 206                add_cmdname(cmds, de->d_name + prefix_len, entlen);
 207        }
 208        closedir(dir);
 209
 210        return longest;
 211}
 212
 213static void list_commands(void)
 214{
 215        unsigned int longest = 0;
 216        unsigned int len;
 217        const char *env_path = getenv("PATH");
 218        char *paths, *path, *colon;
 219        const char *exec_path = git_exec_path();
 220
 221        if (exec_path)
 222                longest = list_commands_in_dir(&main_cmds, exec_path);
 223
 224        if (!env_path) {
 225                fprintf(stderr, "PATH not set\n");
 226                exit(1);
 227        }
 228
 229        path = paths = xstrdup(env_path);
 230        while (1) {
 231                if ((colon = strchr(path, ':')))
 232                        *colon = 0;
 233
 234                len = list_commands_in_dir(&other_cmds, path);
 235                if (len > longest)
 236                        longest = len;
 237
 238                if (!colon)
 239                        break;
 240                path = colon + 1;
 241        }
 242        free(paths);
 243
 244        qsort(main_cmds.names, main_cmds.cnt,
 245              sizeof(*main_cmds.names), cmdname_compare);
 246        uniq(&main_cmds);
 247
 248        qsort(other_cmds.names, other_cmds.cnt,
 249              sizeof(*other_cmds.names), cmdname_compare);
 250        uniq(&other_cmds);
 251        exclude_cmds(&other_cmds, &main_cmds);
 252
 253        if (main_cmds.cnt) {
 254                printf("available git commands in '%s'\n", exec_path);
 255                printf("----------------------------");
 256                mput_char('-', strlen(exec_path));
 257                putchar('\n');
 258                pretty_print_string_list(&main_cmds, longest);
 259                putchar('\n');
 260        }
 261
 262        if (other_cmds.cnt) {
 263                printf("git commands available from elsewhere on your $PATH\n");
 264                printf("---------------------------------------------------\n");
 265                pretty_print_string_list(&other_cmds, longest);
 266                putchar('\n');
 267        }
 268}
 269
 270void list_common_cmds_help(void)
 271{
 272        int i, longest = 0;
 273
 274        for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
 275                if (longest < strlen(common_cmds[i].name))
 276                        longest = strlen(common_cmds[i].name);
 277        }
 278
 279        puts("The most commonly used git commands are:");
 280        for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
 281                printf("   %s   ", common_cmds[i].name);
 282                mput_char(' ', longest - strlen(common_cmds[i].name));
 283                puts(common_cmds[i].help);
 284        }
 285}
 286
 287static const char *cmd_to_page(const char *git_cmd)
 288{
 289        if (!git_cmd)
 290                return "git";
 291        else if (!prefixcmp(git_cmd, "git"))
 292                return git_cmd;
 293        else {
 294                int page_len = strlen(git_cmd) + 4;
 295                char *p = xmalloc(page_len + 1);
 296                strcpy(p, "git-");
 297                strcpy(p + 4, git_cmd);
 298                p[page_len] = 0;
 299                return p;
 300        }
 301}
 302
 303static void setup_man_path(void)
 304{
 305        struct strbuf new_path;
 306        const char *old_path = getenv("MANPATH");
 307
 308        strbuf_init(&new_path, 0);
 309
 310        /* We should always put ':' after our path. If there is no
 311         * old_path, the ':' at the end will let 'man' to try
 312         * system-wide paths after ours to find the manual page. If
 313         * there is old_path, we need ':' as delimiter. */
 314        strbuf_addstr(&new_path, GIT_MAN_PATH);
 315        strbuf_addch(&new_path, ':');
 316        if (old_path)
 317                strbuf_addstr(&new_path, old_path);
 318
 319        setenv("MANPATH", new_path.buf, 1);
 320
 321        strbuf_release(&new_path);
 322}
 323
 324static void show_man_page(const char *git_cmd)
 325{
 326        const char *page = cmd_to_page(git_cmd);
 327        setup_man_path();
 328        execlp("man", "man", page, NULL);
 329}
 330
 331static void show_info_page(const char *git_cmd)
 332{
 333        const char *page = cmd_to_page(git_cmd);
 334        setenv("INFOPATH", GIT_INFO_PATH, 1);
 335        execlp("info", "info", "gitman", page, NULL);
 336}
 337
 338static void get_html_page_path(struct strbuf *page_path, const char *page)
 339{
 340        struct stat st;
 341
 342        /* Check that we have a git documentation directory. */
 343        if (stat(GIT_HTML_PATH "/git.html", &st) || !S_ISREG(st.st_mode))
 344                die("'%s': not a documentation directory.", GIT_HTML_PATH);
 345
 346        strbuf_init(page_path, 0);
 347        strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
 348}
 349
 350static void show_html_page(const char *git_cmd)
 351{
 352        const char *page = cmd_to_page(git_cmd);
 353        struct strbuf page_path; /* it leaks but we exec bellow */
 354
 355        get_html_page_path(&page_path, page);
 356
 357        execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL);
 358}
 359
 360void help_unknown_cmd(const char *cmd)
 361{
 362        fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
 363        exit(1);
 364}
 365
 366int cmd_version(int argc, const char **argv, const char *prefix)
 367{
 368        printf("git version %s\n", git_version_string);
 369        return 0;
 370}
 371
 372int cmd_help(int argc, const char **argv, const char *prefix)
 373{
 374        int nongit;
 375
 376        setup_git_directory_gently(&nongit);
 377        git_config(git_help_config);
 378
 379        argc = parse_options(argc, argv, builtin_help_options,
 380                        builtin_help_usage, 0);
 381
 382        if (show_all) {
 383                printf("usage: %s\n\n", git_usage_string);
 384                list_commands();
 385                return 0;
 386        }
 387
 388        if (!argv[0]) {
 389                printf("usage: %s\n\n", git_usage_string);
 390                list_common_cmds_help();
 391                return 0;
 392        }
 393
 394        switch (help_format) {
 395        case HELP_FORMAT_MAN:
 396                show_man_page(argv[0]);
 397                break;
 398        case HELP_FORMAT_INFO:
 399                show_info_page(argv[0]);
 400                break;
 401        case HELP_FORMAT_WEB:
 402                show_html_page(argv[0]);
 403                break;
 404        }
 405
 406        return 0;
 407}