ls-files.con commit Optionally do not list empty directories in git-ls-files --others (b0a3de4)
   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 show_deleted = 0;
  15static int show_cached = 0;
  16static int show_others = 0;
  17static int show_ignored = 0;
  18static int show_stage = 0;
  19static int show_unmerged = 0;
  20static int show_modified = 0;
  21static int show_killed = 0;
  22static int show_other_directories = 0;
  23static int hide_empty_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 int read_directory(const char *path, const char *base, int baselen)
 263{
 264        DIR *fdir = opendir(path);
 265        int contents = 0;
 266
 267        if (fdir) {
 268                int exclude_stk;
 269                struct dirent *de;
 270                char fullname[MAXPATHLEN + 1];
 271                memcpy(fullname, base, baselen);
 272
 273                exclude_stk = push_exclude_per_directory(base, baselen);
 274
 275                while ((de = readdir(fdir)) != NULL) {
 276                        int len;
 277
 278                        if ((de->d_name[0] == '.') &&
 279                            (de->d_name[1] == 0 ||
 280                             !strcmp(de->d_name + 1, ".") ||
 281                             !strcmp(de->d_name + 1, "git")))
 282                                continue;
 283                        len = strlen(de->d_name);
 284                        memcpy(fullname + baselen, de->d_name, len+1);
 285                        if (excluded(fullname) != show_ignored) {
 286                                if (!show_ignored || DTYPE(de) != DT_DIR) {
 287                                        continue;
 288                                }
 289                        }
 290
 291                        switch (DTYPE(de)) {
 292                        struct stat st;
 293                        int subdir, rewind_base;
 294                        default:
 295                                continue;
 296                        case DT_UNKNOWN:
 297                                if (lstat(fullname, &st))
 298                                        continue;
 299                                if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
 300                                        break;
 301                                if (!S_ISDIR(st.st_mode))
 302                                        continue;
 303                                /* fallthrough */
 304                        case DT_DIR:
 305                                memcpy(fullname + baselen + len, "/", 2);
 306                                len++;
 307                                rewind_base = nr_dir;
 308                                subdir = read_directory(fullname, fullname,
 309                                                        baselen + len);
 310                                if (show_other_directories &&
 311                                    (subdir || !hide_empty_directories) &&
 312                                    !dir_exists(fullname, baselen + len)) {
 313                                        // Rewind the read subdirectory
 314                                        while (nr_dir > rewind_base)
 315                                                free(dir[--nr_dir]);
 316                                        break;
 317                                }
 318                                contents += subdir;
 319                                continue;
 320                        case DT_REG:
 321                        case DT_LNK:
 322                                break;
 323                        }
 324                        add_name(fullname, baselen + len);
 325                        contents++;
 326                }
 327                closedir(fdir);
 328
 329                pop_exclude_per_directory(exclude_stk);
 330        }
 331
 332        return contents;
 333}
 334
 335static int cmp_name(const void *p1, const void *p2)
 336{
 337        const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1;
 338        const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2;
 339
 340        return cache_name_compare(e1->name, e1->len,
 341                                  e2->name, e2->len);
 342}
 343
 344/*
 345 * Match a pathspec against a filename. The first "len" characters
 346 * are the common prefix
 347 */
 348static int match(const char **spec, char *ps_matched,
 349                 const char *filename, int len)
 350{
 351        const char *m;
 352
 353        while ((m = *spec++) != NULL) {
 354                int matchlen = strlen(m + len);
 355
 356                if (!matchlen)
 357                        goto matched;
 358                if (!strncmp(m + len, filename + len, matchlen)) {
 359                        if (m[len + matchlen - 1] == '/')
 360                                goto matched;
 361                        switch (filename[len + matchlen]) {
 362                        case '/': case '\0':
 363                                goto matched;
 364                        }
 365                }
 366                if (!fnmatch(m + len, filename + len, 0))
 367                        goto matched;
 368                if (ps_matched)
 369                        ps_matched++;
 370                continue;
 371        matched:
 372                if (ps_matched)
 373                        *ps_matched = 1;
 374                return 1;
 375        }
 376        return 0;
 377}
 378
 379static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
 380{
 381        int len = prefix_len;
 382        int offset = prefix_offset;
 383
 384        if (len >= ent->len)
 385                die("git-ls-files: internal error - directory entry not superset of prefix");
 386
 387        if (pathspec && !match(pathspec, ps_matched, ent->name, len))
 388                return;
 389
 390        fputs(tag, stdout);
 391        write_name_quoted("", 0, ent->name + offset, line_terminator, stdout);
 392        putchar(line_terminator);
 393}
 394
 395static void show_other_files(void)
 396{
 397        int i;
 398        for (i = 0; i < nr_dir; i++) {
 399                /* We should not have a matching entry, but we
 400                 * may have an unmerged entry for this path.
 401                 */
 402                struct nond_on_fs *ent = dir[i];
 403                int pos = cache_name_pos(ent->name, ent->len);
 404                struct cache_entry *ce;
 405                if (0 <= pos)
 406                        die("bug in show-other-files");
 407                pos = -pos - 1;
 408                if (pos < active_nr) { 
 409                        ce = active_cache[pos];
 410                        if (ce_namelen(ce) == ent->len &&
 411                            !memcmp(ce->name, ent->name, ent->len))
 412                                continue; /* Yup, this one exists unmerged */
 413                }
 414                show_dir_entry(tag_other, ent);
 415        }
 416}
 417
 418static void show_killed_files(void)
 419{
 420        int i;
 421        for (i = 0; i < nr_dir; i++) {
 422                struct nond_on_fs *ent = dir[i];
 423                char *cp, *sp;
 424                int pos, len, killed = 0;
 425
 426                for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
 427                        sp = strchr(cp, '/');
 428                        if (!sp) {
 429                                /* If ent->name is prefix of an entry in the
 430                                 * cache, it will be killed.
 431                                 */
 432                                pos = cache_name_pos(ent->name, ent->len);
 433                                if (0 <= pos)
 434                                        die("bug in show-killed-files");
 435                                pos = -pos - 1;
 436                                while (pos < active_nr &&
 437                                       ce_stage(active_cache[pos]))
 438                                        pos++; /* skip unmerged */
 439                                if (active_nr <= pos)
 440                                        break;
 441                                /* pos points at a name immediately after
 442                                 * ent->name in the cache.  Does it expect
 443                                 * ent->name to be a directory?
 444                                 */
 445                                len = ce_namelen(active_cache[pos]);
 446                                if ((ent->len < len) &&
 447                                    !strncmp(active_cache[pos]->name,
 448                                             ent->name, ent->len) &&
 449                                    active_cache[pos]->name[ent->len] == '/')
 450                                        killed = 1;
 451                                break;
 452                        }
 453                        if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
 454                                /* If any of the leading directories in
 455                                 * ent->name is registered in the cache,
 456                                 * ent->name will be killed.
 457                                 */
 458                                killed = 1;
 459                                break;
 460                        }
 461                }
 462                if (killed)
 463                        show_dir_entry(tag_killed, dir[i]);
 464        }
 465}
 466
 467static void show_ce_entry(const char *tag, struct cache_entry *ce)
 468{
 469        int len = prefix_len;
 470        int offset = prefix_offset;
 471
 472        if (len >= ce_namelen(ce))
 473                die("git-ls-files: internal error - cache entry not superset of prefix");
 474
 475        if (pathspec && !match(pathspec, ps_matched, ce->name, len))
 476                return;
 477
 478        if (tag && *tag && show_valid_bit &&
 479            (ce->ce_flags & htons(CE_VALID))) {
 480                static char alttag[4];
 481                memcpy(alttag, tag, 3);
 482                if (isalpha(tag[0]))
 483                        alttag[0] = tolower(tag[0]);
 484                else if (tag[0] == '?')
 485                        alttag[0] = '!';
 486                else {
 487                        alttag[0] = 'v';
 488                        alttag[1] = tag[0];
 489                        alttag[2] = ' ';
 490                        alttag[3] = 0;
 491                }
 492                tag = alttag;
 493        }
 494
 495        if (!show_stage) {
 496                fputs(tag, stdout);
 497                write_name_quoted("", 0, ce->name + offset,
 498                                  line_terminator, stdout);
 499                putchar(line_terminator);
 500        }
 501        else {
 502                printf("%s%06o %s %d\t",
 503                       tag,
 504                       ntohl(ce->ce_mode),
 505                       sha1_to_hex(ce->sha1),
 506                       ce_stage(ce));
 507                write_name_quoted("", 0, ce->name + offset,
 508                                  line_terminator, stdout);
 509                putchar(line_terminator);
 510        }
 511}
 512
 513static void show_files(void)
 514{
 515        int i;
 516
 517        /* For cached/deleted files we don't need to even do the readdir */
 518        if (show_others || show_killed) {
 519                const char *path = ".", *base = "";
 520                int baselen = prefix_len;
 521
 522                if (baselen) {
 523                        path = base = prefix;
 524                        if (exclude_per_dir) {
 525                                char *p, *pp = xmalloc(baselen+1);
 526                                memcpy(pp, prefix, baselen+1);
 527                                p = pp;
 528                                while (1) {
 529                                        char save = *p;
 530                                        *p = 0;
 531                                        push_exclude_per_directory(pp, p-pp);
 532                                        *p++ = save;
 533                                        if (!save)
 534                                                break;
 535                                        p = strchr(p, '/');
 536                                        if (p)
 537                                                p++;
 538                                        else
 539                                                p = pp + baselen;
 540                                }
 541                                free(pp);
 542                        }
 543                }
 544                read_directory(path, base, baselen);
 545                qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
 546                if (show_others)
 547                        show_other_files();
 548                if (show_killed)
 549                        show_killed_files();
 550        }
 551        if (show_cached | show_stage) {
 552                for (i = 0; i < active_nr; i++) {
 553                        struct cache_entry *ce = active_cache[i];
 554                        if (excluded(ce->name) != show_ignored)
 555                                continue;
 556                        if (show_unmerged && !ce_stage(ce))
 557                                continue;
 558                        show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
 559                }
 560        }
 561        if (show_deleted | show_modified) {
 562                for (i = 0; i < active_nr; i++) {
 563                        struct cache_entry *ce = active_cache[i];
 564                        struct stat st;
 565                        int err;
 566                        if (excluded(ce->name) != show_ignored)
 567                                continue;
 568                        err = lstat(ce->name, &st);
 569                        if (show_deleted && err)
 570                                show_ce_entry(tag_removed, ce);
 571                        if (show_modified && ce_modified(ce, &st, 0))
 572                                show_ce_entry(tag_modified, ce);
 573                }
 574        }
 575}
 576
 577/*
 578 * Prune the index to only contain stuff starting with "prefix"
 579 */
 580static void prune_cache(void)
 581{
 582        int pos = cache_name_pos(prefix, prefix_len);
 583        unsigned int first, last;
 584
 585        if (pos < 0)
 586                pos = -pos-1;
 587        active_cache += pos;
 588        active_nr -= pos;
 589        first = 0;
 590        last = active_nr;
 591        while (last > first) {
 592                int next = (last + first) >> 1;
 593                struct cache_entry *ce = active_cache[next];
 594                if (!strncmp(ce->name, prefix, prefix_len)) {
 595                        first = next+1;
 596                        continue;
 597                }
 598                last = next;
 599        }
 600        active_nr = last;
 601}
 602
 603static void verify_pathspec(void)
 604{
 605        const char **p, *n, *prev;
 606        char *real_prefix;
 607        unsigned long max;
 608
 609        prev = NULL;
 610        max = PATH_MAX;
 611        for (p = pathspec; (n = *p) != NULL; p++) {
 612                int i, len = 0;
 613                for (i = 0; i < max; i++) {
 614                        char c = n[i];
 615                        if (prev && prev[i] != c)
 616                                break;
 617                        if (!c || c == '*' || c == '?')
 618                                break;
 619                        if (c == '/')
 620                                len = i+1;
 621                }
 622                prev = n;
 623                if (len < max) {
 624                        max = len;
 625                        if (!max)
 626                                break;
 627                }
 628        }
 629
 630        if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
 631                die("git-ls-files: cannot generate relative filenames containing '..'");
 632
 633        real_prefix = NULL;
 634        prefix_len = max;
 635        if (max) {
 636                real_prefix = xmalloc(max + 1);
 637                memcpy(real_prefix, prev, max);
 638                real_prefix[max] = 0;
 639        }
 640        prefix = real_prefix;
 641}
 642
 643static const char ls_files_usage[] =
 644        "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
 645        "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
 646        "[ --exclude-per-directory=<filename> ] [--full-name] [--] [<file>]*";
 647
 648int main(int argc, const char **argv)
 649{
 650        int i;
 651        int exc_given = 0;
 652
 653        prefix = setup_git_directory();
 654        if (prefix)
 655                prefix_offset = strlen(prefix);
 656        git_config(git_default_config);
 657
 658        for (i = 1; i < argc; i++) {
 659                const char *arg = argv[i];
 660
 661                if (!strcmp(arg, "--")) {
 662                        i++;
 663                        break;
 664                }
 665                if (!strcmp(arg, "-z")) {
 666                        line_terminator = 0;
 667                        continue;
 668                }
 669                if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) {
 670                        tag_cached = "H ";
 671                        tag_unmerged = "M ";
 672                        tag_removed = "R ";
 673                        tag_modified = "C ";
 674                        tag_other = "? ";
 675                        tag_killed = "K ";
 676                        if (arg[1] == 'v')
 677                                show_valid_bit = 1;
 678                        continue;
 679                }
 680                if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
 681                        show_cached = 1;
 682                        continue;
 683                }
 684                if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
 685                        show_deleted = 1;
 686                        continue;
 687                }
 688                if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
 689                        show_modified = 1;
 690                        continue;
 691                }
 692                if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
 693                        show_others = 1;
 694                        continue;
 695                }
 696                if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
 697                        show_ignored = 1;
 698                        continue;
 699                }
 700                if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
 701                        show_stage = 1;
 702                        continue;
 703                }
 704                if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
 705                        show_killed = 1;
 706                        continue;
 707                }
 708                if (!strcmp(arg, "--directory")) {
 709                        show_other_directories = 1;
 710                        continue;
 711                }
 712                if (!strcmp(arg, "--no-empty-directory")) {
 713                        hide_empty_directories = 1;
 714                        continue;
 715                }
 716                if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
 717                        /* There's no point in showing unmerged unless
 718                         * you also show the stage information.
 719                         */
 720                        show_stage = 1;
 721                        show_unmerged = 1;
 722                        continue;
 723                }
 724                if (!strcmp(arg, "-x") && i+1 < argc) {
 725                        exc_given = 1;
 726                        add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]);
 727                        continue;
 728                }
 729                if (!strncmp(arg, "--exclude=", 10)) {
 730                        exc_given = 1;
 731                        add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]);
 732                        continue;
 733                }
 734                if (!strcmp(arg, "-X") && i+1 < argc) {
 735                        exc_given = 1;
 736                        add_excludes_from_file(argv[++i]);
 737                        continue;
 738                }
 739                if (!strncmp(arg, "--exclude-from=", 15)) {
 740                        exc_given = 1;
 741                        add_excludes_from_file(arg+15);
 742                        continue;
 743                }
 744                if (!strncmp(arg, "--exclude-per-directory=", 24)) {
 745                        exc_given = 1;
 746                        exclude_per_dir = arg + 24;
 747                        continue;
 748                }
 749                if (!strcmp(arg, "--full-name")) {
 750                        prefix_offset = 0;
 751                        continue;
 752                }
 753                if (!strcmp(arg, "--error-unmatch")) {
 754                        error_unmatch = 1;
 755                        continue;
 756                }
 757                if (*arg == '-')
 758                        usage(ls_files_usage);
 759                break;
 760        }
 761
 762        pathspec = get_pathspec(prefix, argv + i);
 763
 764        /* Verify that the pathspec matches the prefix */
 765        if (pathspec)
 766                verify_pathspec();
 767
 768        /* Treat unmatching pathspec elements as errors */
 769        if (pathspec && error_unmatch) {
 770                int num;
 771                for (num = 0; pathspec[num]; num++)
 772                        ;
 773                ps_matched = xcalloc(1, num);
 774        }
 775
 776        if (show_ignored && !exc_given) {
 777                fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
 778                        argv[0]);
 779                exit(1);
 780        }
 781
 782        /* With no flags, we default to showing the cached files */
 783        if (!(show_stage | show_deleted | show_others | show_unmerged |
 784              show_killed | show_modified))
 785                show_cached = 1;
 786
 787        read_cache();
 788        if (prefix)
 789                prune_cache();
 790        show_files();
 791
 792        if (ps_matched) {
 793                /* We need to make sure all pathspec matched otherwise
 794                 * it is an error.
 795                 */
 796                int num, errors = 0;
 797                for (num = 0; pathspec[num]; num++) {
 798                        if (ps_matched[num])
 799                                continue;
 800                        error("pathspec '%s' did not match any.",
 801                              pathspec[num] + prefix_offset);
 802                        errors++;
 803                }
 804                return errors ? 1 : 0;
 805        }
 806
 807        return 0;
 808}