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