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