ls-files.con commit Merge branch 'jc/name' into next (88d9405)
   1/*
   2 * This merges the file listing in the directory cache index
   3 * with the actual working directory list, and shows different
   4 * combinations of the two.
   5 *
   6 * Copyright (C) Linus Torvalds, 2005
   7 */
   8#include <dirent.h>
   9#include <fnmatch.h>
  10
  11#include "cache.h"
  12#include "quote.h"
  13
  14static int abbrev = 0;
  15static int show_deleted = 0;
  16static int show_cached = 0;
  17static int show_others = 0;
  18static int show_ignored = 0;
  19static int show_stage = 0;
  20static int show_unmerged = 0;
  21static int show_modified = 0;
  22static int show_killed = 0;
  23static int show_other_directories = 0;
  24static int show_valid_bit = 0;
  25static int line_terminator = '\n';
  26
  27static int prefix_len = 0, prefix_offset = 0;
  28static const char *prefix = NULL;
  29static const char **pathspec = NULL;
  30static int error_unmatch = 0;
  31static char *ps_matched = NULL;
  32
  33static const char *tag_cached = "";
  34static const char *tag_unmerged = "";
  35static const char *tag_removed = "";
  36static const char *tag_other = "";
  37static const char *tag_killed = "";
  38static const char *tag_modified = "";
  39
  40static const char *exclude_per_dir = NULL;
  41
  42/* We maintain three exclude pattern lists:
  43 * EXC_CMDL lists patterns explicitly given on the command line.
  44 * EXC_DIRS lists patterns obtained from per-directory ignore files.
  45 * EXC_FILE lists patterns from fallback ignore files.
  46 */
  47#define EXC_CMDL 0
  48#define EXC_DIRS 1
  49#define EXC_FILE 2
  50static struct exclude_list {
  51        int nr;
  52        int alloc;
  53        struct exclude {
  54                const char *pattern;
  55                const char *base;
  56                int baselen;
  57        } **excludes;
  58} exclude_list[3];
  59
  60static void add_exclude(const char *string, const char *base,
  61                        int baselen, struct exclude_list *which)
  62{
  63        struct exclude *x = xmalloc(sizeof (*x));
  64
  65        x->pattern = string;
  66        x->base = base;
  67        x->baselen = baselen;
  68        if (which->nr == which->alloc) {
  69                which->alloc = alloc_nr(which->alloc);
  70                which->excludes = realloc(which->excludes,
  71                                          which->alloc * sizeof(x));
  72        }
  73        which->excludes[which->nr++] = x;
  74}
  75
  76static int add_excludes_from_file_1(const char *fname,
  77                                    const char *base,
  78                                    int baselen,
  79                                    struct exclude_list *which)
  80{
  81        int fd, i;
  82        long size;
  83        char *buf, *entry;
  84
  85        fd = open(fname, O_RDONLY);
  86        if (fd < 0)
  87                goto err;
  88        size = lseek(fd, 0, SEEK_END);
  89        if (size < 0)
  90                goto err;
  91        lseek(fd, 0, SEEK_SET);
  92        if (size == 0) {
  93                close(fd);
  94                return 0;
  95        }
  96        buf = xmalloc(size+1);
  97        if (read(fd, buf, size) != size)
  98                goto err;
  99        close(fd);
 100
 101        buf[size++] = '\n';
 102        entry = buf;
 103        for (i = 0; i < size; i++) {
 104                if (buf[i] == '\n') {
 105                        if (entry != buf + i && entry[0] != '#') {
 106                                buf[i - (i && buf[i-1] == '\r')] = 0;
 107                                add_exclude(entry, base, baselen, which);
 108                        }
 109                        entry = buf + i + 1;
 110                }
 111        }
 112        return 0;
 113
 114 err:
 115        if (0 <= fd)
 116                close(fd);
 117        return -1;
 118}
 119
 120static void add_excludes_from_file(const char *fname)
 121{
 122        if (add_excludes_from_file_1(fname, "", 0,
 123                                     &exclude_list[EXC_FILE]) < 0)
 124                die("cannot use %s as an exclude file", fname);
 125}
 126
 127static int push_exclude_per_directory(const char *base, int baselen)
 128{
 129        char exclude_file[PATH_MAX];
 130        struct exclude_list *el = &exclude_list[EXC_DIRS];
 131        int current_nr = el->nr;
 132
 133        if (exclude_per_dir) {
 134                memcpy(exclude_file, base, baselen);
 135                strcpy(exclude_file + baselen, exclude_per_dir);
 136                add_excludes_from_file_1(exclude_file, base, baselen, el);
 137        }
 138        return current_nr;
 139}
 140
 141static void pop_exclude_per_directory(int stk)
 142{
 143        struct exclude_list *el = &exclude_list[EXC_DIRS];
 144
 145        while (stk < el->nr)
 146                free(el->excludes[--el->nr]);
 147}
 148
 149/* Scan the list and let the last match determines the fate.
 150 * Return 1 for exclude, 0 for include and -1 for undecided.
 151 */
 152static int excluded_1(const char *pathname,
 153                      int pathlen,
 154                      struct exclude_list *el)
 155{
 156        int i;
 157
 158        if (el->nr) {
 159                for (i = el->nr - 1; 0 <= i; i--) {
 160                        struct exclude *x = el->excludes[i];
 161                        const char *exclude = x->pattern;
 162                        int to_exclude = 1;
 163
 164                        if (*exclude == '!') {
 165                                to_exclude = 0;
 166                                exclude++;
 167                        }
 168
 169                        if (!strchr(exclude, '/')) {
 170                                /* match basename */
 171                                const char *basename = strrchr(pathname, '/');
 172                                basename = (basename) ? basename+1 : pathname;
 173                                if (fnmatch(exclude, basename, 0) == 0)
 174                                        return to_exclude;
 175                        }
 176                        else {
 177                                /* match with FNM_PATHNAME:
 178                                 * exclude has base (baselen long) implicitly
 179                                 * in front of it.
 180                                 */
 181                                int baselen = x->baselen;
 182                                if (*exclude == '/')
 183                                        exclude++;
 184
 185                                if (pathlen < baselen ||
 186                                    (baselen && pathname[baselen-1] != '/') ||
 187                                    strncmp(pathname, x->base, baselen))
 188                                    continue;
 189
 190                                if (fnmatch(exclude, pathname+baselen,
 191                                            FNM_PATHNAME) == 0)
 192                                        return to_exclude;
 193                        }
 194                }
 195        }
 196        return -1; /* undecided */
 197}
 198
 199static int excluded(const char *pathname)
 200{
 201        int pathlen = strlen(pathname);
 202        int st;
 203
 204        for (st = EXC_CMDL; st <= EXC_FILE; st++) {
 205                switch (excluded_1(pathname, pathlen, &exclude_list[st])) {
 206                case 0:
 207                        return 0;
 208                case 1:
 209                        return 1;
 210                }
 211        }
 212        return 0;
 213}
 214
 215struct nond_on_fs {
 216        int len;
 217        char name[FLEX_ARRAY]; /* more */
 218};
 219
 220static struct nond_on_fs **dir;
 221static int nr_dir;
 222static int dir_alloc;
 223
 224static void add_name(const char *pathname, int len)
 225{
 226        struct nond_on_fs *ent;
 227
 228        if (cache_name_pos(pathname, len) >= 0)
 229                return;
 230
 231        if (nr_dir == dir_alloc) {
 232                dir_alloc = alloc_nr(dir_alloc);
 233                dir = xrealloc(dir, dir_alloc*sizeof(ent));
 234        }
 235        ent = xmalloc(sizeof(*ent) + len + 1);
 236        ent->len = len;
 237        memcpy(ent->name, pathname, len);
 238        ent->name[len] = 0;
 239        dir[nr_dir++] = ent;
 240}
 241
 242static int dir_exists(const char *dirname, int len)
 243{
 244        int pos = cache_name_pos(dirname, len);
 245        if (pos >= 0)
 246                return 1;
 247        pos = -pos-1;
 248        if (pos >= active_nr) /* can't */
 249                return 0;
 250        return !strncmp(active_cache[pos]->name, dirname, len);
 251}
 252
 253/*
 254 * Read a directory tree. We currently ignore anything but
 255 * directories, regular files and symlinks. That's because git
 256 * doesn't handle them at all yet. Maybe that will change some
 257 * day.
 258 *
 259 * Also, we ignore the name ".git" (even if it is not a directory).
 260 * That likely will not change.
 261 */
 262static void read_directory(const char *path, const char *base, int baselen)
 263{
 264        DIR *dir = opendir(path);
 265
 266        if (dir) {
 267                int exclude_stk;
 268                struct dirent *de;
 269                char fullname[MAXPATHLEN + 1];
 270                memcpy(fullname, base, baselen);
 271
 272                exclude_stk = push_exclude_per_directory(base, baselen);
 273
 274                while ((de = readdir(dir)) != NULL) {
 275                        int len;
 276
 277                        if ((de->d_name[0] == '.') &&
 278                            (de->d_name[1] == 0 ||
 279                             !strcmp(de->d_name + 1, ".") ||
 280                             !strcmp(de->d_name + 1, "git")))
 281                                continue;
 282                        len = strlen(de->d_name);
 283                        memcpy(fullname + baselen, de->d_name, len+1);
 284                        if (excluded(fullname) != show_ignored) {
 285                                if (!show_ignored || DTYPE(de) != DT_DIR) {
 286                                        continue;
 287                                }
 288                        }
 289
 290                        switch (DTYPE(de)) {
 291                        struct stat st;
 292                        default:
 293                                continue;
 294                        case DT_UNKNOWN:
 295                                if (lstat(fullname, &st))
 296                                        continue;
 297                                if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
 298                                        break;
 299                                if (!S_ISDIR(st.st_mode))
 300                                        continue;
 301                                /* fallthrough */
 302                        case DT_DIR:
 303                                memcpy(fullname + baselen + len, "/", 2);
 304                                len++;
 305                                if (show_other_directories &&
 306                                    !dir_exists(fullname, baselen + len))
 307                                        break;
 308                                read_directory(fullname, fullname,
 309                                               baselen + len);
 310                                continue;
 311                        case DT_REG:
 312                        case DT_LNK:
 313                                break;
 314                        }
 315                        add_name(fullname, baselen + len);
 316                }
 317                closedir(dir);
 318
 319                pop_exclude_per_directory(exclude_stk);
 320        }
 321}
 322
 323static int cmp_name(const void *p1, const void *p2)
 324{
 325        const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1;
 326        const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2;
 327
 328        return cache_name_compare(e1->name, e1->len,
 329                                  e2->name, e2->len);
 330}
 331
 332/*
 333 * Match a pathspec against a filename. The first "len" characters
 334 * are the common prefix
 335 */
 336static int match(const char **spec, char *ps_matched,
 337                 const char *filename, int len)
 338{
 339        const char *m;
 340
 341        while ((m = *spec++) != NULL) {
 342                int matchlen = strlen(m + len);
 343
 344                if (!matchlen)
 345                        goto matched;
 346                if (!strncmp(m + len, filename + len, matchlen)) {
 347                        if (m[len + matchlen - 1] == '/')
 348                                goto matched;
 349                        switch (filename[len + matchlen]) {
 350                        case '/': case '\0':
 351                                goto matched;
 352                        }
 353                }
 354                if (!fnmatch(m + len, filename + len, 0))
 355                        goto matched;
 356                if (ps_matched)
 357                        ps_matched++;
 358                continue;
 359        matched:
 360                if (ps_matched)
 361                        *ps_matched = 1;
 362                return 1;
 363        }
 364        return 0;
 365}
 366
 367static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
 368{
 369        int len = prefix_len;
 370        int offset = prefix_offset;
 371
 372        if (len >= ent->len)
 373                die("git-ls-files: internal error - directory entry not superset of prefix");
 374
 375        if (pathspec && !match(pathspec, ps_matched, ent->name, len))
 376                return;
 377
 378        fputs(tag, stdout);
 379        write_name_quoted("", 0, ent->name + offset, line_terminator, stdout);
 380        putchar(line_terminator);
 381}
 382
 383static void show_other_files(void)
 384{
 385        int i;
 386        for (i = 0; i < nr_dir; i++) {
 387                /* We should not have a matching entry, but we
 388                 * may have an unmerged entry for this path.
 389                 */
 390                struct nond_on_fs *ent = dir[i];
 391                int pos = cache_name_pos(ent->name, ent->len);
 392                struct cache_entry *ce;
 393                if (0 <= pos)
 394                        die("bug in show-other-files");
 395                pos = -pos - 1;
 396                if (pos < active_nr) { 
 397                        ce = active_cache[pos];
 398                        if (ce_namelen(ce) == ent->len &&
 399                            !memcmp(ce->name, ent->name, ent->len))
 400                                continue; /* Yup, this one exists unmerged */
 401                }
 402                show_dir_entry(tag_other, ent);
 403        }
 404}
 405
 406static void show_killed_files(void)
 407{
 408        int i;
 409        for (i = 0; i < nr_dir; i++) {
 410                struct nond_on_fs *ent = dir[i];
 411                char *cp, *sp;
 412                int pos, len, killed = 0;
 413
 414                for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
 415                        sp = strchr(cp, '/');
 416                        if (!sp) {
 417                                /* If ent->name is prefix of an entry in the
 418                                 * cache, it will be killed.
 419                                 */
 420                                pos = cache_name_pos(ent->name, ent->len);
 421                                if (0 <= pos)
 422                                        die("bug in show-killed-files");
 423                                pos = -pos - 1;
 424                                while (pos < active_nr &&
 425                                       ce_stage(active_cache[pos]))
 426                                        pos++; /* skip unmerged */
 427                                if (active_nr <= pos)
 428                                        break;
 429                                /* pos points at a name immediately after
 430                                 * ent->name in the cache.  Does it expect
 431                                 * ent->name to be a directory?
 432                                 */
 433                                len = ce_namelen(active_cache[pos]);
 434                                if ((ent->len < len) &&
 435                                    !strncmp(active_cache[pos]->name,
 436                                             ent->name, ent->len) &&
 437                                    active_cache[pos]->name[ent->len] == '/')
 438                                        killed = 1;
 439                                break;
 440                        }
 441                        if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
 442                                /* If any of the leading directories in
 443                                 * ent->name is registered in the cache,
 444                                 * ent->name will be killed.
 445                                 */
 446                                killed = 1;
 447                                break;
 448                        }
 449                }
 450                if (killed)
 451                        show_dir_entry(tag_killed, dir[i]);
 452        }
 453}
 454
 455static void show_ce_entry(const char *tag, struct cache_entry *ce)
 456{
 457        int len = prefix_len;
 458        int offset = prefix_offset;
 459
 460        if (len >= ce_namelen(ce))
 461                die("git-ls-files: internal error - cache entry not superset of prefix");
 462
 463        if (pathspec && !match(pathspec, ps_matched, ce->name, len))
 464                return;
 465
 466        if (tag && *tag && show_valid_bit &&
 467            (ce->ce_flags & htons(CE_VALID))) {
 468                static char alttag[4];
 469                memcpy(alttag, tag, 3);
 470                if (isalpha(tag[0]))
 471                        alttag[0] = tolower(tag[0]);
 472                else if (tag[0] == '?')
 473                        alttag[0] = '!';
 474                else {
 475                        alttag[0] = 'v';
 476                        alttag[1] = tag[0];
 477                        alttag[2] = ' ';
 478                        alttag[3] = 0;
 479                }
 480                tag = alttag;
 481        }
 482
 483        if (!show_stage) {
 484                fputs(tag, stdout);
 485                write_name_quoted("", 0, ce->name + offset,
 486                                  line_terminator, stdout);
 487                putchar(line_terminator);
 488        }
 489        else {
 490                printf("%s%06o %s %d\t",
 491                       tag,
 492                       ntohl(ce->ce_mode),
 493                       abbrev ? find_unique_abbrev(ce->sha1,abbrev)
 494                                : sha1_to_hex(ce->sha1),
 495                       ce_stage(ce));
 496                write_name_quoted("", 0, ce->name + offset,
 497                                  line_terminator, stdout);
 498                putchar(line_terminator);
 499        }
 500}
 501
 502static void show_files(void)
 503{
 504        int i;
 505
 506        /* For cached/deleted files we don't need to even do the readdir */
 507        if (show_others || show_killed) {
 508                const char *path = ".", *base = "";
 509                int baselen = prefix_len;
 510
 511                if (baselen) {
 512                        path = base = prefix;
 513                        if (exclude_per_dir) {
 514                                char *p, *pp = xmalloc(baselen+1);
 515                                memcpy(pp, prefix, baselen+1);
 516                                p = pp;
 517                                while (1) {
 518                                        char save = *p;
 519                                        *p = 0;
 520                                        push_exclude_per_directory(pp, p-pp);
 521                                        *p++ = save;
 522                                        if (!save)
 523                                                break;
 524                                        p = strchr(p, '/');
 525                                        if (p)
 526                                                p++;
 527                                        else
 528                                                p = pp + baselen;
 529                                }
 530                                free(pp);
 531                        }
 532                }
 533                read_directory(path, base, baselen);
 534                qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
 535                if (show_others)
 536                        show_other_files();
 537                if (show_killed)
 538                        show_killed_files();
 539        }
 540        if (show_cached | show_stage) {
 541                for (i = 0; i < active_nr; i++) {
 542                        struct cache_entry *ce = active_cache[i];
 543                        if (excluded(ce->name) != show_ignored)
 544                                continue;
 545                        if (show_unmerged && !ce_stage(ce))
 546                                continue;
 547                        show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
 548                }
 549        }
 550        if (show_deleted | show_modified) {
 551                for (i = 0; i < active_nr; i++) {
 552                        struct cache_entry *ce = active_cache[i];
 553                        struct stat st;
 554                        int err;
 555                        if (excluded(ce->name) != show_ignored)
 556                                continue;
 557                        err = lstat(ce->name, &st);
 558                        if (show_deleted && err)
 559                                show_ce_entry(tag_removed, ce);
 560                        if (show_modified && ce_modified(ce, &st, 0))
 561                                show_ce_entry(tag_modified, ce);
 562                }
 563        }
 564}
 565
 566/*
 567 * Prune the index to only contain stuff starting with "prefix"
 568 */
 569static void prune_cache(void)
 570{
 571        int pos = cache_name_pos(prefix, prefix_len);
 572        unsigned int first, last;
 573
 574        if (pos < 0)
 575                pos = -pos-1;
 576        active_cache += pos;
 577        active_nr -= pos;
 578        first = 0;
 579        last = active_nr;
 580        while (last > first) {
 581                int next = (last + first) >> 1;
 582                struct cache_entry *ce = active_cache[next];
 583                if (!strncmp(ce->name, prefix, prefix_len)) {
 584                        first = next+1;
 585                        continue;
 586                }
 587                last = next;
 588        }
 589        active_nr = last;
 590}
 591
 592static void verify_pathspec(void)
 593{
 594        const char **p, *n, *prev;
 595        char *real_prefix;
 596        unsigned long max;
 597
 598        prev = NULL;
 599        max = PATH_MAX;
 600        for (p = pathspec; (n = *p) != NULL; p++) {
 601                int i, len = 0;
 602                for (i = 0; i < max; i++) {
 603                        char c = n[i];
 604                        if (prev && prev[i] != c)
 605                                break;
 606                        if (!c || c == '*' || c == '?')
 607                                break;
 608                        if (c == '/')
 609                                len = i+1;
 610                }
 611                prev = n;
 612                if (len < max) {
 613                        max = len;
 614                        if (!max)
 615                                break;
 616                }
 617        }
 618
 619        if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
 620                die("git-ls-files: cannot generate relative filenames containing '..'");
 621
 622        real_prefix = NULL;
 623        prefix_len = max;
 624        if (max) {
 625                real_prefix = xmalloc(max + 1);
 626                memcpy(real_prefix, prev, max);
 627                real_prefix[max] = 0;
 628        }
 629        prefix = real_prefix;
 630}
 631
 632static const char ls_files_usage[] =
 633        "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
 634        "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
 635        "[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] "
 636        "[--] [<file>]*";
 637
 638int main(int argc, const char **argv)
 639{
 640        int i;
 641        int exc_given = 0;
 642
 643        prefix = setup_git_directory();
 644        if (prefix)
 645                prefix_offset = strlen(prefix);
 646        git_config(git_default_config);
 647
 648        for (i = 1; i < argc; i++) {
 649                const char *arg = argv[i];
 650
 651                if (!strcmp(arg, "--")) {
 652                        i++;
 653                        break;
 654                }
 655                if (!strcmp(arg, "-z")) {
 656                        line_terminator = 0;
 657                        continue;
 658                }
 659                if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) {
 660                        tag_cached = "H ";
 661                        tag_unmerged = "M ";
 662                        tag_removed = "R ";
 663                        tag_modified = "C ";
 664                        tag_other = "? ";
 665                        tag_killed = "K ";
 666                        if (arg[1] == 'v')
 667                                show_valid_bit = 1;
 668                        continue;
 669                }
 670                if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
 671                        show_cached = 1;
 672                        continue;
 673                }
 674                if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
 675                        show_deleted = 1;
 676                        continue;
 677                }
 678                if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
 679                        show_modified = 1;
 680                        continue;
 681                }
 682                if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
 683                        show_others = 1;
 684                        continue;
 685                }
 686                if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
 687                        show_ignored = 1;
 688                        continue;
 689                }
 690                if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
 691                        show_stage = 1;
 692                        continue;
 693                }
 694                if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
 695                        show_killed = 1;
 696                        continue;
 697                }
 698                if (!strcmp(arg, "--directory")) {
 699                        show_other_directories = 1;
 700                        continue;
 701                }
 702                if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
 703                        /* There's no point in showing unmerged unless
 704                         * you also show the stage information.
 705                         */
 706                        show_stage = 1;
 707                        show_unmerged = 1;
 708                        continue;
 709                }
 710                if (!strcmp(arg, "-x") && i+1 < argc) {
 711                        exc_given = 1;
 712                        add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]);
 713                        continue;
 714                }
 715                if (!strncmp(arg, "--exclude=", 10)) {
 716                        exc_given = 1;
 717                        add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]);
 718                        continue;
 719                }
 720                if (!strcmp(arg, "-X") && i+1 < argc) {
 721                        exc_given = 1;
 722                        add_excludes_from_file(argv[++i]);
 723                        continue;
 724                }
 725                if (!strncmp(arg, "--exclude-from=", 15)) {
 726                        exc_given = 1;
 727                        add_excludes_from_file(arg+15);
 728                        continue;
 729                }
 730                if (!strncmp(arg, "--exclude-per-directory=", 24)) {
 731                        exc_given = 1;
 732                        exclude_per_dir = arg + 24;
 733                        continue;
 734                }
 735                if (!strcmp(arg, "--full-name")) {
 736                        prefix_offset = 0;
 737                        continue;
 738                }
 739                if (!strcmp(arg, "--error-unmatch")) {
 740                        error_unmatch = 1;
 741                        continue;
 742                }
 743                if (!strncmp(arg, "--abbrev=", 9)) {
 744                        abbrev = strtoul(arg+9, NULL, 10);
 745                        if (abbrev && abbrev < MINIMUM_ABBREV)
 746                                abbrev = MINIMUM_ABBREV;
 747                        else if (abbrev > 40)
 748                                abbrev = 40;
 749                        continue;
 750                }
 751                if (!strcmp(arg, "--abbrev")) {
 752                        abbrev = DEFAULT_ABBREV;
 753                        continue;
 754                }
 755                if (*arg == '-')
 756                        usage(ls_files_usage);
 757                break;
 758        }
 759
 760        pathspec = get_pathspec(prefix, argv + i);
 761
 762        /* Verify that the pathspec matches the prefix */
 763        if (pathspec)
 764                verify_pathspec();
 765
 766        /* Treat unmatching pathspec elements as errors */
 767        if (pathspec && error_unmatch) {
 768                int num;
 769                for (num = 0; pathspec[num]; num++)
 770                        ;
 771                ps_matched = xcalloc(1, num);
 772        }
 773
 774        if (show_ignored && !exc_given) {
 775                fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
 776                        argv[0]);
 777                exit(1);
 778        }
 779
 780        /* With no flags, we default to showing the cached files */
 781        if (!(show_stage | show_deleted | show_others | show_unmerged |
 782              show_killed | show_modified))
 783                show_cached = 1;
 784
 785        read_cache();
 786        if (prefix)
 787                prune_cache();
 788        show_files();
 789
 790        if (ps_matched) {
 791                /* We need to make sure all pathspec matched otherwise
 792                 * it is an error.
 793                 */
 794                int num, errors = 0;
 795                for (num = 0; pathspec[num]; num++) {
 796                        if (ps_matched[num])
 797                                continue;
 798                        error("pathspec '%s' did not match any.",
 799                              pathspec[num] + prefix_offset);
 800                        errors++;
 801                }
 802                return errors ? 1 : 0;
 803        }
 804
 805        return 0;
 806}