help.con commit git.c: convert --list-* to --list-cmds=* (0089521)
   1#include "cache.h"
   2#include "config.h"
   3#include "builtin.h"
   4#include "exec_cmd.h"
   5#include "run-command.h"
   6#include "levenshtein.h"
   7#include "help.h"
   8#include "command-list.h"
   9#include "string-list.h"
  10#include "column.h"
  11#include "version.h"
  12#include "refs.h"
  13#include "parse-options.h"
  14
  15struct category_description {
  16        uint32_t category;
  17        const char *desc;
  18};
  19static uint32_t common_mask =
  20        CAT_init | CAT_worktree | CAT_info |
  21        CAT_history | CAT_remote;
  22static struct category_description common_categories[] = {
  23        { CAT_init, N_("start a working area (see also: git help tutorial)") },
  24        { CAT_worktree, N_("work on the current change (see also: git help everyday)") },
  25        { CAT_info, N_("examine the history and state (see also: git help revisions)") },
  26        { CAT_history, N_("grow, mark and tweak your common history") },
  27        { CAT_remote, N_("collaborate (see also: git help workflows)") },
  28        { 0, NULL }
  29};
  30
  31static const char *drop_prefix(const char *name)
  32{
  33        const char *new_name;
  34
  35        if (skip_prefix(name, "git-", &new_name))
  36                return new_name;
  37        return name;
  38
  39}
  40
  41static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask)
  42{
  43        int i, nr = 0;
  44        struct cmdname_help *cmds;
  45
  46        if (ARRAY_SIZE(command_list) == 0)
  47                BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
  48
  49        ALLOC_ARRAY(cmds, ARRAY_SIZE(command_list) + 1);
  50
  51        for (i = 0; i < ARRAY_SIZE(command_list); i++) {
  52                const struct cmdname_help *cmd = command_list + i;
  53
  54                if (!(cmd->category & mask))
  55                        continue;
  56
  57                cmds[nr] = *cmd;
  58                cmds[nr].name = drop_prefix(cmd->name);
  59
  60                nr++;
  61        }
  62        cmds[nr].name = NULL;
  63        *p_cmds = cmds;
  64}
  65
  66static void print_command_list(const struct cmdname_help *cmds,
  67                               uint32_t mask, int longest)
  68{
  69        int i;
  70
  71        for (i = 0; cmds[i].name; i++) {
  72                if (cmds[i].category & mask) {
  73                        printf("   %s   ", cmds[i].name);
  74                        mput_char(' ', longest - strlen(cmds[i].name));
  75                        puts(_(cmds[i].help));
  76                }
  77        }
  78}
  79
  80static int cmd_name_cmp(const void *elem1, const void *elem2)
  81{
  82        const struct cmdname_help *e1 = elem1;
  83        const struct cmdname_help *e2 = elem2;
  84
  85        return strcmp(e1->name, e2->name);
  86}
  87
  88static void print_cmd_by_category(const struct category_description *catdesc)
  89{
  90        struct cmdname_help *cmds;
  91        int longest = 0;
  92        int i, nr = 0;
  93        uint32_t mask = 0;
  94
  95        for (i = 0; catdesc[i].desc; i++)
  96                mask |= catdesc[i].category;
  97
  98        extract_cmds(&cmds, mask);
  99
 100        for (i = 0; cmds[i].name; i++, nr++) {
 101                if (longest < strlen(cmds[i].name))
 102                        longest = strlen(cmds[i].name);
 103        }
 104        QSORT(cmds, nr, cmd_name_cmp);
 105
 106        for (i = 0; catdesc[i].desc; i++) {
 107                uint32_t mask = catdesc[i].category;
 108                const char *desc = catdesc[i].desc;
 109
 110                printf("\n%s\n", _(desc));
 111                print_command_list(cmds, mask, longest);
 112        }
 113        free(cmds);
 114}
 115
 116void add_cmdname(struct cmdnames *cmds, const char *name, int len)
 117{
 118        struct cmdname *ent;
 119        FLEX_ALLOC_MEM(ent, name, name, len);
 120        ent->len = len;
 121
 122        ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
 123        cmds->names[cmds->cnt++] = ent;
 124}
 125
 126static void clean_cmdnames(struct cmdnames *cmds)
 127{
 128        int i;
 129        for (i = 0; i < cmds->cnt; ++i)
 130                free(cmds->names[i]);
 131        free(cmds->names);
 132        cmds->cnt = 0;
 133        cmds->alloc = 0;
 134}
 135
 136static int cmdname_compare(const void *a_, const void *b_)
 137{
 138        struct cmdname *a = *(struct cmdname **)a_;
 139        struct cmdname *b = *(struct cmdname **)b_;
 140        return strcmp(a->name, b->name);
 141}
 142
 143static void uniq(struct cmdnames *cmds)
 144{
 145        int i, j;
 146
 147        if (!cmds->cnt)
 148                return;
 149
 150        for (i = j = 1; i < cmds->cnt; i++) {
 151                if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
 152                        free(cmds->names[i]);
 153                else
 154                        cmds->names[j++] = cmds->names[i];
 155        }
 156
 157        cmds->cnt = j;
 158}
 159
 160void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
 161{
 162        int ci, cj, ei;
 163        int cmp;
 164
 165        ci = cj = ei = 0;
 166        while (ci < cmds->cnt && ei < excludes->cnt) {
 167                cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
 168                if (cmp < 0)
 169                        cmds->names[cj++] = cmds->names[ci++];
 170                else if (cmp == 0) {
 171                        ei++;
 172                        free(cmds->names[ci++]);
 173                } else if (cmp > 0)
 174                        ei++;
 175        }
 176
 177        while (ci < cmds->cnt)
 178                cmds->names[cj++] = cmds->names[ci++];
 179
 180        cmds->cnt = cj;
 181}
 182
 183static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
 184{
 185        struct string_list list = STRING_LIST_INIT_NODUP;
 186        struct column_options copts;
 187        int i;
 188
 189        for (i = 0; i < cmds->cnt; i++)
 190                string_list_append(&list, cmds->names[i]->name);
 191        /*
 192         * always enable column display, we only consult column.*
 193         * about layout strategy and stuff
 194         */
 195        colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
 196        memset(&copts, 0, sizeof(copts));
 197        copts.indent = "  ";
 198        copts.padding = 2;
 199        print_columns(&list, colopts, &copts);
 200        string_list_clear(&list, 0);
 201}
 202
 203static void list_commands_in_dir(struct cmdnames *cmds,
 204                                         const char *path,
 205                                         const char *prefix)
 206{
 207        DIR *dir = opendir(path);
 208        struct dirent *de;
 209        struct strbuf buf = STRBUF_INIT;
 210        int len;
 211
 212        if (!dir)
 213                return;
 214        if (!prefix)
 215                prefix = "git-";
 216
 217        strbuf_addf(&buf, "%s/", path);
 218        len = buf.len;
 219
 220        while ((de = readdir(dir)) != NULL) {
 221                const char *ent;
 222                size_t entlen;
 223
 224                if (!skip_prefix(de->d_name, prefix, &ent))
 225                        continue;
 226
 227                strbuf_setlen(&buf, len);
 228                strbuf_addstr(&buf, de->d_name);
 229                if (!is_executable(buf.buf))
 230                        continue;
 231
 232                entlen = strlen(ent);
 233                strip_suffix(ent, ".exe", &entlen);
 234
 235                add_cmdname(cmds, ent, entlen);
 236        }
 237        closedir(dir);
 238        strbuf_release(&buf);
 239}
 240
 241void load_command_list(const char *prefix,
 242                struct cmdnames *main_cmds,
 243                struct cmdnames *other_cmds)
 244{
 245        const char *env_path = getenv("PATH");
 246        const char *exec_path = git_exec_path();
 247
 248        if (exec_path) {
 249                list_commands_in_dir(main_cmds, exec_path, prefix);
 250                QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
 251                uniq(main_cmds);
 252        }
 253
 254        if (env_path) {
 255                char *paths, *path, *colon;
 256                path = paths = xstrdup(env_path);
 257                while (1) {
 258                        if ((colon = strchr(path, PATH_SEP)))
 259                                *colon = 0;
 260                        if (!exec_path || strcmp(path, exec_path))
 261                                list_commands_in_dir(other_cmds, path, prefix);
 262
 263                        if (!colon)
 264                                break;
 265                        path = colon + 1;
 266                }
 267                free(paths);
 268
 269                QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
 270                uniq(other_cmds);
 271        }
 272        exclude_cmds(other_cmds, main_cmds);
 273}
 274
 275void list_commands(unsigned int colopts,
 276                   struct cmdnames *main_cmds, struct cmdnames *other_cmds)
 277{
 278        if (main_cmds->cnt) {
 279                const char *exec_path = git_exec_path();
 280                printf_ln(_("available git commands in '%s'"), exec_path);
 281                putchar('\n');
 282                pretty_print_cmdnames(main_cmds, colopts);
 283                putchar('\n');
 284        }
 285
 286        if (other_cmds->cnt) {
 287                printf_ln(_("git commands available from elsewhere on your $PATH"));
 288                putchar('\n');
 289                pretty_print_cmdnames(other_cmds, colopts);
 290                putchar('\n');
 291        }
 292}
 293
 294void list_common_cmds_help(void)
 295{
 296        puts(_("These are common Git commands used in various situations:"));
 297        print_cmd_by_category(common_categories);
 298}
 299
 300int is_in_cmdlist(struct cmdnames *c, const char *s)
 301{
 302        int i;
 303        for (i = 0; i < c->cnt; i++)
 304                if (!strcmp(s, c->names[i]->name))
 305                        return 1;
 306        return 0;
 307}
 308
 309static int autocorrect;
 310static struct cmdnames aliases;
 311
 312static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
 313{
 314        const char *p;
 315
 316        if (!strcmp(var, "help.autocorrect"))
 317                autocorrect = git_config_int(var,value);
 318        /* Also use aliases for command lookup */
 319        if (skip_prefix(var, "alias.", &p))
 320                add_cmdname(&aliases, p, strlen(p));
 321
 322        return git_default_config(var, value, cb);
 323}
 324
 325static int levenshtein_compare(const void *p1, const void *p2)
 326{
 327        const struct cmdname *const *c1 = p1, *const *c2 = p2;
 328        const char *s1 = (*c1)->name, *s2 = (*c2)->name;
 329        int l1 = (*c1)->len;
 330        int l2 = (*c2)->len;
 331        return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
 332}
 333
 334static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
 335{
 336        int i;
 337        ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
 338
 339        for (i = 0; i < old->cnt; i++)
 340                cmds->names[cmds->cnt++] = old->names[i];
 341        FREE_AND_NULL(old->names);
 342        old->cnt = 0;
 343}
 344
 345/* An empirically derived magic number */
 346#define SIMILARITY_FLOOR 7
 347#define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
 348
 349static const char bad_interpreter_advice[] =
 350        N_("'%s' appears to be a git command, but we were not\n"
 351        "able to execute it. Maybe git-%s is broken?");
 352
 353const char *help_unknown_cmd(const char *cmd)
 354{
 355        int i, n, best_similarity = 0;
 356        struct cmdnames main_cmds, other_cmds;
 357        struct cmdname_help *common_cmds;
 358
 359        memset(&main_cmds, 0, sizeof(main_cmds));
 360        memset(&other_cmds, 0, sizeof(other_cmds));
 361        memset(&aliases, 0, sizeof(aliases));
 362
 363        read_early_config(git_unknown_cmd_config, NULL);
 364
 365        load_command_list("git-", &main_cmds, &other_cmds);
 366
 367        add_cmd_list(&main_cmds, &aliases);
 368        add_cmd_list(&main_cmds, &other_cmds);
 369        QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
 370        uniq(&main_cmds);
 371
 372        extract_cmds(&common_cmds, common_mask);
 373
 374        /* This abuses cmdname->len for levenshtein distance */
 375        for (i = 0, n = 0; i < main_cmds.cnt; i++) {
 376                int cmp = 0; /* avoid compiler stupidity */
 377                const char *candidate = main_cmds.names[i]->name;
 378
 379                /*
 380                 * An exact match means we have the command, but
 381                 * for some reason exec'ing it gave us ENOENT; probably
 382                 * it's a bad interpreter in the #! line.
 383                 */
 384                if (!strcmp(candidate, cmd))
 385                        die(_(bad_interpreter_advice), cmd, cmd);
 386
 387                /* Does the candidate appear in common_cmds list? */
 388                while (common_cmds[n].name &&
 389                       (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
 390                        n++;
 391                if (common_cmds[n].name && !cmp) {
 392                        /* Yes, this is one of the common commands */
 393                        n++; /* use the entry from common_cmds[] */
 394                        if (starts_with(candidate, cmd)) {
 395                                /* Give prefix match a very good score */
 396                                main_cmds.names[i]->len = 0;
 397                                continue;
 398                        }
 399                }
 400
 401                main_cmds.names[i]->len =
 402                        levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
 403        }
 404        FREE_AND_NULL(common_cmds);
 405
 406        QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
 407
 408        if (!main_cmds.cnt)
 409                die(_("Uh oh. Your system reports no Git commands at all."));
 410
 411        /* skip and count prefix matches */
 412        for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
 413                ; /* still counting */
 414
 415        if (main_cmds.cnt <= n) {
 416                /* prefix matches with everything? that is too ambiguous */
 417                best_similarity = SIMILARITY_FLOOR + 1;
 418        } else {
 419                /* count all the most similar ones */
 420                for (best_similarity = main_cmds.names[n++]->len;
 421                     (n < main_cmds.cnt &&
 422                      best_similarity == main_cmds.names[n]->len);
 423                     n++)
 424                        ; /* still counting */
 425        }
 426        if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
 427                const char *assumed = main_cmds.names[0]->name;
 428                main_cmds.names[0] = NULL;
 429                clean_cmdnames(&main_cmds);
 430                fprintf_ln(stderr,
 431                           _("WARNING: You called a Git command named '%s', "
 432                             "which does not exist."),
 433                           cmd);
 434                if (autocorrect < 0)
 435                        fprintf_ln(stderr,
 436                                   _("Continuing under the assumption that "
 437                                     "you meant '%s'."),
 438                                   assumed);
 439                else {
 440                        fprintf_ln(stderr,
 441                                   _("Continuing in %0.1f seconds, "
 442                                     "assuming that you meant '%s'."),
 443                                   (float)autocorrect/10.0, assumed);
 444                        sleep_millisec(autocorrect * 100);
 445                }
 446                return assumed;
 447        }
 448
 449        fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
 450
 451        if (SIMILAR_ENOUGH(best_similarity)) {
 452                fprintf_ln(stderr,
 453                           Q_("\nThe most similar command is",
 454                              "\nThe most similar commands are",
 455                           n));
 456
 457                for (i = 0; i < n; i++)
 458                        fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
 459        }
 460
 461        exit(1);
 462}
 463
 464int cmd_version(int argc, const char **argv, const char *prefix)
 465{
 466        int build_options = 0;
 467        const char * const usage[] = {
 468                N_("git version [<options>]"),
 469                NULL
 470        };
 471        struct option options[] = {
 472                OPT_BOOL(0, "build-options", &build_options,
 473                         "also print build options"),
 474                OPT_END()
 475        };
 476
 477        argc = parse_options(argc, argv, prefix, options, usage, 0);
 478
 479        /*
 480         * The format of this string should be kept stable for compatibility
 481         * with external projects that rely on the output of "git version".
 482         *
 483         * Always show the version, even if other options are given.
 484         */
 485        printf("git version %s\n", git_version_string);
 486
 487        if (build_options) {
 488                printf("cpu: %s\n", GIT_HOST_CPU);
 489                if (git_built_from_commit_string[0])
 490                        printf("built from commit: %s\n",
 491                               git_built_from_commit_string);
 492                else
 493                        printf("no commit associated with this build\n");
 494                printf("sizeof-long: %d\n", (int)sizeof(long));
 495                /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
 496        }
 497        return 0;
 498}
 499
 500struct similar_ref_cb {
 501        const char *base_ref;
 502        struct string_list *similar_refs;
 503};
 504
 505static int append_similar_ref(const char *refname, const struct object_id *oid,
 506                              int flags, void *cb_data)
 507{
 508        struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
 509        char *branch = strrchr(refname, '/') + 1;
 510        const char *remote;
 511
 512        /* A remote branch of the same name is deemed similar */
 513        if (skip_prefix(refname, "refs/remotes/", &remote) &&
 514            !strcmp(branch, cb->base_ref))
 515                string_list_append(cb->similar_refs, remote);
 516        return 0;
 517}
 518
 519static struct string_list guess_refs(const char *ref)
 520{
 521        struct similar_ref_cb ref_cb;
 522        struct string_list similar_refs = STRING_LIST_INIT_NODUP;
 523
 524        ref_cb.base_ref = ref;
 525        ref_cb.similar_refs = &similar_refs;
 526        for_each_ref(append_similar_ref, &ref_cb);
 527        return similar_refs;
 528}
 529
 530void help_unknown_ref(const char *ref, const char *cmd, const char *error)
 531{
 532        int i;
 533        struct string_list suggested_refs = guess_refs(ref);
 534
 535        fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
 536
 537        if (suggested_refs.nr > 0) {
 538                fprintf_ln(stderr,
 539                           Q_("\nDid you mean this?",
 540                              "\nDid you mean one of these?",
 541                              suggested_refs.nr));
 542                for (i = 0; i < suggested_refs.nr; i++)
 543                        fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
 544        }
 545
 546        string_list_clear(&suggested_refs, 0);
 547        exit(1);
 548}