help.con commit is_ntfs_dotgit: match other .git files (e7cb0b4)
   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        read_early_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."),
 359                           cmd);
 360                if (autocorrect < 0)
 361                        fprintf_ln(stderr,
 362                                   _("Continuing under the assumption that "
 363                                     "you meant '%s'."),
 364                                   assumed);
 365                else {
 366                        fprintf_ln(stderr,
 367                                   _("Continuing in %0.1f seconds, "
 368                                     "assuming that you meant '%s'."),
 369                                   (float)autocorrect/10.0, assumed);
 370                        sleep_millisec(autocorrect * 100);
 371                }
 372                return assumed;
 373        }
 374
 375        fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
 376
 377        if (SIMILAR_ENOUGH(best_similarity)) {
 378                fprintf_ln(stderr,
 379                           Q_("\nThe most similar command is",
 380                              "\nThe most similar commands are",
 381                           n));
 382
 383                for (i = 0; i < n; i++)
 384                        fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
 385        }
 386
 387        exit(1);
 388}
 389
 390int cmd_version(int argc, const char **argv, const char *prefix)
 391{
 392        /*
 393         * The format of this string should be kept stable for compatibility
 394         * with external projects that rely on the output of "git version".
 395         */
 396        printf("git version %s\n", git_version_string);
 397        while (*++argv) {
 398                if (!strcmp(*argv, "--build-options")) {
 399                        printf("sizeof-long: %d\n", (int)sizeof(long));
 400                        /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
 401                }
 402        }
 403        return 0;
 404}
 405
 406struct similar_ref_cb {
 407        const char *base_ref;
 408        struct string_list *similar_refs;
 409};
 410
 411static int append_similar_ref(const char *refname, const struct object_id *oid,
 412                              int flags, void *cb_data)
 413{
 414        struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
 415        char *branch = strrchr(refname, '/') + 1;
 416        const char *remote;
 417
 418        /* A remote branch of the same name is deemed similar */
 419        if (skip_prefix(refname, "refs/remotes/", &remote) &&
 420            !strcmp(branch, cb->base_ref))
 421                string_list_append(cb->similar_refs, remote);
 422        return 0;
 423}
 424
 425static struct string_list guess_refs(const char *ref)
 426{
 427        struct similar_ref_cb ref_cb;
 428        struct string_list similar_refs = STRING_LIST_INIT_NODUP;
 429
 430        ref_cb.base_ref = ref;
 431        ref_cb.similar_refs = &similar_refs;
 432        for_each_ref(append_similar_ref, &ref_cb);
 433        return similar_refs;
 434}
 435
 436void help_unknown_ref(const char *ref, const char *cmd, const char *error)
 437{
 438        int i;
 439        struct string_list suggested_refs = guess_refs(ref);
 440
 441        fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
 442
 443        if (suggested_refs.nr > 0) {
 444                fprintf_ln(stderr,
 445                           Q_("\nDid you mean this?",
 446                              "\nDid you mean one of these?",
 447                              suggested_refs.nr));
 448                for (i = 0; i < suggested_refs.nr; i++)
 449                        fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
 450        }
 451
 452        string_list_clear(&suggested_refs, 0);
 453        exit(1);
 454}