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