help.con commit Add help.autocorrect to enable/disable autocorrecting (f0e9071)
   1#include "cache.h"
   2#include "builtin.h"
   3#include "exec_cmd.h"
   4#include "levenshtein.h"
   5#include "help.h"
   6
   7/* most GUI terminals set COLUMNS (although some don't export it) */
   8static int term_columns(void)
   9{
  10        char *col_string = getenv("COLUMNS");
  11        int n_cols;
  12
  13        if (col_string && (n_cols = atoi(col_string)) > 0)
  14                return n_cols;
  15
  16#ifdef TIOCGWINSZ
  17        {
  18                struct winsize ws;
  19                if (!ioctl(1, TIOCGWINSZ, &ws)) {
  20                        if (ws.ws_col)
  21                                return ws.ws_col;
  22                }
  23        }
  24#endif
  25
  26        return 80;
  27}
  28
  29void add_cmdname(struct cmdnames *cmds, const char *name, int len)
  30{
  31        struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
  32
  33        ent->len = len;
  34        memcpy(ent->name, name, len);
  35        ent->name[len] = 0;
  36
  37        ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
  38        cmds->names[cmds->cnt++] = ent;
  39}
  40
  41static void clean_cmdnames(struct cmdnames *cmds)
  42{
  43        int i;
  44        for (i = 0; i < cmds->cnt; ++i)
  45                free(cmds->names[i]);
  46        free(cmds->names);
  47        cmds->cnt = 0;
  48        cmds->alloc = 0;
  49}
  50
  51static int cmdname_compare(const void *a_, const void *b_)
  52{
  53        struct cmdname *a = *(struct cmdname **)a_;
  54        struct cmdname *b = *(struct cmdname **)b_;
  55        return strcmp(a->name, b->name);
  56}
  57
  58static void uniq(struct cmdnames *cmds)
  59{
  60        int i, j;
  61
  62        if (!cmds->cnt)
  63                return;
  64
  65        for (i = j = 1; i < cmds->cnt; i++)
  66                if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
  67                        cmds->names[j++] = cmds->names[i];
  68
  69        cmds->cnt = j;
  70}
  71
  72void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
  73{
  74        int ci, cj, ei;
  75        int cmp;
  76
  77        ci = cj = ei = 0;
  78        while (ci < cmds->cnt && ei < excludes->cnt) {
  79                cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
  80                if (cmp < 0)
  81                        cmds->names[cj++] = cmds->names[ci++];
  82                else if (cmp == 0)
  83                        ci++, ei++;
  84                else if (cmp > 0)
  85                        ei++;
  86        }
  87
  88        while (ci < cmds->cnt)
  89                cmds->names[cj++] = cmds->names[ci++];
  90
  91        cmds->cnt = cj;
  92}
  93
  94static void pretty_print_string_list(struct cmdnames *cmds, int longest)
  95{
  96        int cols = 1, rows;
  97        int space = longest + 1; /* min 1 SP between words */
  98        int max_cols = term_columns() - 1; /* don't print *on* the edge */
  99        int i, j;
 100
 101        if (space < max_cols)
 102                cols = max_cols / space;
 103        rows = (cmds->cnt + cols - 1) / cols;
 104
 105        for (i = 0; i < rows; i++) {
 106                printf("  ");
 107
 108                for (j = 0; j < cols; j++) {
 109                        int n = j * rows + i;
 110                        int size = space;
 111                        if (n >= cmds->cnt)
 112                                break;
 113                        if (j == cols-1 || n + rows >= cmds->cnt)
 114                                size = 1;
 115                        printf("%-*s", size, cmds->names[n]->name);
 116                }
 117                putchar('\n');
 118        }
 119}
 120
 121static int is_executable(const char *name)
 122{
 123        struct stat st;
 124
 125        if (stat(name, &st) || /* stat, not lstat */
 126            !S_ISREG(st.st_mode))
 127                return 0;
 128
 129#ifdef __MINGW32__
 130        /* cannot trust the executable bit, peek into the file instead */
 131        char buf[3] = { 0 };
 132        int n;
 133        int fd = open(name, O_RDONLY);
 134        st.st_mode &= ~S_IXUSR;
 135        if (fd >= 0) {
 136                n = read(fd, buf, 2);
 137                if (n == 2)
 138                        /* DOS executables start with "MZ" */
 139                        if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
 140                                st.st_mode |= S_IXUSR;
 141                close(fd);
 142        }
 143#endif
 144        return st.st_mode & S_IXUSR;
 145}
 146
 147static unsigned int list_commands_in_dir(struct cmdnames *cmds,
 148                                         const char *path,
 149                                         const char *prefix)
 150{
 151        unsigned int longest = 0;
 152        int prefix_len;
 153        DIR *dir = opendir(path);
 154        struct dirent *de;
 155        struct strbuf buf = STRBUF_INIT;
 156        int len;
 157
 158        if (!dir)
 159                return 0;
 160        if (!prefix)
 161                prefix = "git-";
 162        prefix_len = strlen(prefix);
 163
 164        strbuf_addf(&buf, "%s/", path);
 165        len = buf.len;
 166
 167        while ((de = readdir(dir)) != NULL) {
 168                int entlen;
 169
 170                if (prefixcmp(de->d_name, prefix))
 171                        continue;
 172
 173                strbuf_setlen(&buf, len);
 174                strbuf_addstr(&buf, de->d_name);
 175                if (!is_executable(buf.buf))
 176                        continue;
 177
 178                entlen = strlen(de->d_name) - prefix_len;
 179                if (has_extension(de->d_name, ".exe"))
 180                        entlen -= 4;
 181
 182                if (longest < entlen)
 183                        longest = entlen;
 184
 185                add_cmdname(cmds, de->d_name + prefix_len, entlen);
 186        }
 187        closedir(dir);
 188        strbuf_release(&buf);
 189
 190        return longest;
 191}
 192
 193unsigned int load_command_list(const char *prefix,
 194                struct cmdnames *main_cmds,
 195                struct cmdnames *other_cmds)
 196{
 197        unsigned int longest = 0;
 198        unsigned int len;
 199        const char *env_path = getenv("PATH");
 200        char *paths, *path, *colon;
 201        const char *exec_path = git_exec_path();
 202
 203        if (exec_path)
 204                longest = list_commands_in_dir(main_cmds, exec_path, prefix);
 205
 206        if (!env_path) {
 207                fprintf(stderr, "PATH not set\n");
 208                exit(1);
 209        }
 210
 211        path = paths = xstrdup(env_path);
 212        while (1) {
 213                if ((colon = strchr(path, PATH_SEP)))
 214                        *colon = 0;
 215
 216                len = list_commands_in_dir(other_cmds, path, prefix);
 217                if (len > longest)
 218                        longest = len;
 219
 220                if (!colon)
 221                        break;
 222                path = colon + 1;
 223        }
 224        free(paths);
 225
 226        qsort(main_cmds->names, main_cmds->cnt,
 227              sizeof(*main_cmds->names), cmdname_compare);
 228        uniq(main_cmds);
 229
 230        qsort(other_cmds->names, other_cmds->cnt,
 231              sizeof(*other_cmds->names), cmdname_compare);
 232        uniq(other_cmds);
 233        exclude_cmds(other_cmds, main_cmds);
 234
 235        return longest;
 236}
 237
 238void list_commands(const char *title, unsigned int longest,
 239                struct cmdnames *main_cmds, struct cmdnames *other_cmds)
 240{
 241        const char *exec_path = git_exec_path();
 242
 243        if (main_cmds->cnt) {
 244                printf("available %s in '%s'\n", title, exec_path);
 245                printf("----------------");
 246                mput_char('-', strlen(title) + strlen(exec_path));
 247                putchar('\n');
 248                pretty_print_string_list(main_cmds, longest);
 249                putchar('\n');
 250        }
 251
 252        if (other_cmds->cnt) {
 253                printf("%s available from elsewhere on your $PATH\n", title);
 254                printf("---------------------------------------");
 255                mput_char('-', strlen(title));
 256                putchar('\n');
 257                pretty_print_string_list(other_cmds, longest);
 258                putchar('\n');
 259        }
 260}
 261
 262int is_in_cmdlist(struct cmdnames *c, const char *s)
 263{
 264        int i;
 265        for (i = 0; i < c->cnt; i++)
 266                if (!strcmp(s, c->names[i]->name))
 267                        return 1;
 268        return 0;
 269}
 270
 271static int autocorrect;
 272
 273static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
 274{
 275        if (!strcmp(var, "help.autocorrect"))
 276                autocorrect = git_config_int(var,value);
 277
 278        return git_default_config(var, value, cb);
 279}
 280
 281static int levenshtein_compare(const void *p1, const void *p2)
 282{
 283        const struct cmdname *const *c1 = p1, *const *c2 = p2;
 284        const char *s1 = (*c1)->name, *s2 = (*c2)->name;
 285        int l1 = (*c1)->len;
 286        int l2 = (*c2)->len;
 287        return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
 288}
 289
 290const char *help_unknown_cmd(const char *cmd)
 291{
 292        int i, n, best_similarity = 0;
 293        struct cmdnames main_cmds, other_cmds;
 294
 295        memset(&main_cmds, 0, sizeof(main_cmds));
 296        memset(&other_cmds, 0, sizeof(main_cmds));
 297
 298        git_config(git_unknown_cmd_config, NULL);
 299
 300        load_command_list("git-", &main_cmds, &other_cmds);
 301
 302        ALLOC_GROW(main_cmds.names, main_cmds.cnt + other_cmds.cnt,
 303                   main_cmds.alloc);
 304        memcpy(main_cmds.names + main_cmds.cnt, other_cmds.names,
 305               other_cmds.cnt * sizeof(other_cmds.names[0]));
 306        main_cmds.cnt += other_cmds.cnt;
 307        free(other_cmds.names);
 308
 309        /* This reuses cmdname->len for similarity index */
 310        for (i = 0; i < main_cmds.cnt; ++i)
 311                main_cmds.names[i]->len =
 312                        levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4);
 313
 314        qsort(main_cmds.names, main_cmds.cnt,
 315              sizeof(*main_cmds.names), levenshtein_compare);
 316
 317        if (!main_cmds.cnt)
 318                die ("Uh oh. Your system reports no Git commands at all.");
 319
 320        best_similarity = main_cmds.names[0]->len;
 321        n = 1;
 322        while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
 323                ++n;
 324        if (autocorrect && n == 1) {
 325                const char *assumed = main_cmds.names[0]->name;
 326                main_cmds.names[0] = NULL;
 327                clean_cmdnames(&main_cmds);
 328                fprintf(stderr, "WARNING: You called a Git program named '%s', "
 329                        "which does not exist.\n"
 330                        "Continuing under the assumption that you meant '%s'\n",
 331                        cmd, assumed);
 332                if (autocorrect > 0) {
 333                        fprintf(stderr, "in %0.1f seconds automatically...\n",
 334                                (float)autocorrect/10.0);
 335                        poll(NULL, 0, autocorrect * 100);
 336                }
 337                return assumed;
 338        }
 339
 340        fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
 341
 342        if (best_similarity < 6) {
 343                fprintf(stderr, "\nDid you mean %s?\n",
 344                        n < 2 ? "this": "one of these");
 345
 346                for (i = 0; i < n; i++)
 347                        fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
 348        }
 349
 350        exit(1);
 351}
 352
 353int cmd_version(int argc, const char **argv, const char *prefix)
 354{
 355        printf("git version %s\n", git_version_string);
 356        return 0;
 357}