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