Introducing git-cvsserver -- a CVS emulator for git.
[git/repo.git] / ls-files.c
blob90b289f03d987c6c90214fc12d00c30b4e28bb27
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.
6 * Copyright (C) Linus Torvalds, 2005
7 */
8 #include <dirent.h>
9 #include <fnmatch.h>
11 #include "cache.h"
12 #include "quote.h"
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 show_valid_bit = 0;
24 static int line_terminator = '\n';
26 static int prefix_len = 0, prefix_offset = 0;
27 static const char *prefix = NULL;
28 static const char **pathspec = NULL;
29 static int error_unmatch = 0;
30 static char *ps_matched = NULL;
32 static const char *tag_cached = "";
33 static const char *tag_unmerged = "";
34 static const char *tag_removed = "";
35 static const char *tag_other = "";
36 static const char *tag_killed = "";
37 static const char *tag_modified = "";
39 static const char *exclude_per_dir = NULL;
41 /* We maintain three exclude pattern lists:
42 * EXC_CMDL lists patterns explicitly given on the command line.
43 * EXC_DIRS lists patterns obtained from per-directory ignore files.
44 * EXC_FILE lists patterns from fallback ignore files.
46 #define EXC_CMDL 0
47 #define EXC_DIRS 1
48 #define EXC_FILE 2
49 static struct exclude_list {
50 int nr;
51 int alloc;
52 struct exclude {
53 const char *pattern;
54 const char *base;
55 int baselen;
56 } **excludes;
57 } exclude_list[3];
59 static void add_exclude(const char *string, const char *base,
60 int baselen, struct exclude_list *which)
62 struct exclude *x = xmalloc(sizeof (*x));
64 x->pattern = string;
65 x->base = base;
66 x->baselen = baselen;
67 if (which->nr == which->alloc) {
68 which->alloc = alloc_nr(which->alloc);
69 which->excludes = realloc(which->excludes,
70 which->alloc * sizeof(x));
72 which->excludes[which->nr++] = x;
75 static int add_excludes_from_file_1(const char *fname,
76 const char *base,
77 int baselen,
78 struct exclude_list *which)
80 int fd, i;
81 long size;
82 char *buf, *entry;
84 fd = open(fname, O_RDONLY);
85 if (fd < 0)
86 goto err;
87 size = lseek(fd, 0, SEEK_END);
88 if (size < 0)
89 goto err;
90 lseek(fd, 0, SEEK_SET);
91 if (size == 0) {
92 close(fd);
93 return 0;
95 buf = xmalloc(size);
96 if (read(fd, buf, size) != size)
97 goto err;
98 close(fd);
100 entry = buf;
101 for (i = 0; i < size; i++) {
102 if (buf[i] == '\n') {
103 if (entry != buf + i && entry[0] != '#') {
104 buf[i - (i && buf[i-1] == '\r')] = 0;
105 add_exclude(entry, base, baselen, which);
107 entry = buf + i + 1;
110 return 0;
112 err:
113 if (0 <= fd)
114 close(fd);
115 return -1;
118 static void add_excludes_from_file(const char *fname)
120 if (add_excludes_from_file_1(fname, "", 0,
121 &exclude_list[EXC_FILE]) < 0)
122 die("cannot use %s as an exclude file", fname);
125 static int push_exclude_per_directory(const char *base, int baselen)
127 char exclude_file[PATH_MAX];
128 struct exclude_list *el = &exclude_list[EXC_DIRS];
129 int current_nr = el->nr;
131 if (exclude_per_dir) {
132 memcpy(exclude_file, base, baselen);
133 strcpy(exclude_file + baselen, exclude_per_dir);
134 add_excludes_from_file_1(exclude_file, base, baselen, el);
136 return current_nr;
139 static void pop_exclude_per_directory(int stk)
141 struct exclude_list *el = &exclude_list[EXC_DIRS];
143 while (stk < el->nr)
144 free(el->excludes[--el->nr]);
147 /* Scan the list and let the last match determines the fate.
148 * Return 1 for exclude, 0 for include and -1 for undecided.
150 static int excluded_1(const char *pathname,
151 int pathlen,
152 struct exclude_list *el)
154 int i;
156 if (el->nr) {
157 for (i = el->nr - 1; 0 <= i; i--) {
158 struct exclude *x = el->excludes[i];
159 const char *exclude = x->pattern;
160 int to_exclude = 1;
162 if (*exclude == '!') {
163 to_exclude = 0;
164 exclude++;
167 if (!strchr(exclude, '/')) {
168 /* match basename */
169 const char *basename = strrchr(pathname, '/');
170 basename = (basename) ? basename+1 : pathname;
171 if (fnmatch(exclude, basename, 0) == 0)
172 return to_exclude;
174 else {
175 /* match with FNM_PATHNAME:
176 * exclude has base (baselen long) implicitly
177 * in front of it.
179 int baselen = x->baselen;
180 if (*exclude == '/')
181 exclude++;
183 if (pathlen < baselen ||
184 (baselen && pathname[baselen-1] != '/') ||
185 strncmp(pathname, x->base, baselen))
186 continue;
188 if (fnmatch(exclude, pathname+baselen,
189 FNM_PATHNAME) == 0)
190 return to_exclude;
194 return -1; /* undecided */
197 static int excluded(const char *pathname)
199 int pathlen = strlen(pathname);
200 int st;
202 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
203 switch (excluded_1(pathname, pathlen, &exclude_list[st])) {
204 case 0:
205 return 0;
206 case 1:
207 return 1;
210 return 0;
213 struct nond_on_fs {
214 int len;
215 char name[FLEX_ARRAY]; /* more */
218 static struct nond_on_fs **dir;
219 static int nr_dir;
220 static int dir_alloc;
222 static void add_name(const char *pathname, int len)
224 struct nond_on_fs *ent;
226 if (cache_name_pos(pathname, len) >= 0)
227 return;
229 if (nr_dir == dir_alloc) {
230 dir_alloc = alloc_nr(dir_alloc);
231 dir = xrealloc(dir, dir_alloc*sizeof(ent));
233 ent = xmalloc(sizeof(*ent) + len + 1);
234 ent->len = len;
235 memcpy(ent->name, pathname, len);
236 ent->name[len] = 0;
237 dir[nr_dir++] = ent;
240 static int dir_exists(const char *dirname, int len)
242 int pos = cache_name_pos(dirname, len);
243 if (pos >= 0)
244 return 1;
245 pos = -pos-1;
246 if (pos >= active_nr) /* can't */
247 return 0;
248 return !strncmp(active_cache[pos]->name, dirname, len);
252 * Read a directory tree. We currently ignore anything but
253 * directories, regular files and symlinks. That's because git
254 * doesn't handle them at all yet. Maybe that will change some
255 * day.
257 * Also, we ignore the name ".git" (even if it is not a directory).
258 * That likely will not change.
260 static void read_directory(const char *path, const char *base, int baselen)
262 DIR *dir = opendir(path);
264 if (dir) {
265 int exclude_stk;
266 struct dirent *de;
267 char fullname[MAXPATHLEN + 1];
268 memcpy(fullname, base, baselen);
270 exclude_stk = push_exclude_per_directory(base, baselen);
272 while ((de = readdir(dir)) != NULL) {
273 int len;
275 if ((de->d_name[0] == '.') &&
276 (de->d_name[1] == 0 ||
277 !strcmp(de->d_name + 1, ".") ||
278 !strcmp(de->d_name + 1, "git")))
279 continue;
280 len = strlen(de->d_name);
281 memcpy(fullname + baselen, de->d_name, len+1);
282 if (excluded(fullname) != show_ignored)
283 continue;
285 switch (DTYPE(de)) {
286 struct stat st;
287 default:
288 continue;
289 case DT_UNKNOWN:
290 if (lstat(fullname, &st))
291 continue;
292 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
293 break;
294 if (!S_ISDIR(st.st_mode))
295 continue;
296 /* fallthrough */
297 case DT_DIR:
298 memcpy(fullname + baselen + len, "/", 2);
299 len++;
300 if (show_other_directories &&
301 !dir_exists(fullname, baselen + len))
302 break;
303 read_directory(fullname, fullname,
304 baselen + len);
305 continue;
306 case DT_REG:
307 case DT_LNK:
308 break;
310 add_name(fullname, baselen + len);
312 closedir(dir);
314 pop_exclude_per_directory(exclude_stk);
318 static int cmp_name(const void *p1, const void *p2)
320 const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1;
321 const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2;
323 return cache_name_compare(e1->name, e1->len,
324 e2->name, e2->len);
328 * Match a pathspec against a filename. The first "len" characters
329 * are the common prefix
331 static int match(const char **spec, char *ps_matched,
332 const char *filename, int len)
334 const char *m;
336 while ((m = *spec++) != NULL) {
337 int matchlen = strlen(m + len);
339 if (!matchlen)
340 goto matched;
341 if (!strncmp(m + len, filename + len, matchlen)) {
342 if (m[len + matchlen - 1] == '/')
343 goto matched;
344 switch (filename[len + matchlen]) {
345 case '/': case '\0':
346 goto matched;
349 if (!fnmatch(m + len, filename + len, 0))
350 goto matched;
351 if (ps_matched)
352 ps_matched++;
353 continue;
354 matched:
355 if (ps_matched)
356 *ps_matched = 1;
357 return 1;
359 return 0;
362 static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
364 int len = prefix_len;
365 int offset = prefix_offset;
367 if (len >= ent->len)
368 die("git-ls-files: internal error - directory entry not superset of prefix");
370 if (pathspec && !match(pathspec, ps_matched, ent->name, len))
371 return;
373 fputs(tag, stdout);
374 write_name_quoted("", 0, ent->name + offset, line_terminator, stdout);
375 putchar(line_terminator);
378 static void show_other_files(void)
380 int i;
381 for (i = 0; i < nr_dir; i++) {
382 /* We should not have a matching entry, but we
383 * may have an unmerged entry for this path.
385 struct nond_on_fs *ent = dir[i];
386 int pos = cache_name_pos(ent->name, ent->len);
387 struct cache_entry *ce;
388 if (0 <= pos)
389 die("bug in show-other-files");
390 pos = -pos - 1;
391 if (pos < active_nr) {
392 ce = active_cache[pos];
393 if (ce_namelen(ce) == ent->len &&
394 !memcmp(ce->name, ent->name, ent->len))
395 continue; /* Yup, this one exists unmerged */
397 show_dir_entry(tag_other, ent);
401 static void show_killed_files(void)
403 int i;
404 for (i = 0; i < nr_dir; i++) {
405 struct nond_on_fs *ent = dir[i];
406 char *cp, *sp;
407 int pos, len, killed = 0;
409 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
410 sp = strchr(cp, '/');
411 if (!sp) {
412 /* If ent->name is prefix of an entry in the
413 * cache, it will be killed.
415 pos = cache_name_pos(ent->name, ent->len);
416 if (0 <= pos)
417 die("bug in show-killed-files");
418 pos = -pos - 1;
419 while (pos < active_nr &&
420 ce_stage(active_cache[pos]))
421 pos++; /* skip unmerged */
422 if (active_nr <= pos)
423 break;
424 /* pos points at a name immediately after
425 * ent->name in the cache. Does it expect
426 * ent->name to be a directory?
428 len = ce_namelen(active_cache[pos]);
429 if ((ent->len < len) &&
430 !strncmp(active_cache[pos]->name,
431 ent->name, ent->len) &&
432 active_cache[pos]->name[ent->len] == '/')
433 killed = 1;
434 break;
436 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
437 /* If any of the leading directories in
438 * ent->name is registered in the cache,
439 * ent->name will be killed.
441 killed = 1;
442 break;
445 if (killed)
446 show_dir_entry(tag_killed, dir[i]);
450 static void show_ce_entry(const char *tag, struct cache_entry *ce)
452 int len = prefix_len;
453 int offset = prefix_offset;
455 if (len >= ce_namelen(ce))
456 die("git-ls-files: internal error - cache entry not superset of prefix");
458 if (pathspec && !match(pathspec, ps_matched, ce->name, len))
459 return;
461 if (tag && *tag && show_valid_bit &&
462 (ce->ce_flags & htons(CE_VALID))) {
463 static char alttag[4];
464 memcpy(alttag, tag, 3);
465 if (isalpha(tag[0]))
466 alttag[0] = tolower(tag[0]);
467 else if (tag[0] == '?')
468 alttag[0] = '!';
469 else {
470 alttag[0] = 'v';
471 alttag[1] = tag[0];
472 alttag[2] = ' ';
473 alttag[3] = 0;
475 tag = alttag;
478 if (!show_stage) {
479 fputs(tag, stdout);
480 write_name_quoted("", 0, ce->name + offset,
481 line_terminator, stdout);
482 putchar(line_terminator);
484 else {
485 printf("%s%06o %s %d\t",
486 tag,
487 ntohl(ce->ce_mode),
488 sha1_to_hex(ce->sha1),
489 ce_stage(ce));
490 write_name_quoted("", 0, ce->name + offset,
491 line_terminator, stdout);
492 putchar(line_terminator);
496 static void show_files(void)
498 int i;
500 /* For cached/deleted files we don't need to even do the readdir */
501 if (show_others || show_killed) {
502 const char *path = ".", *base = "";
503 int baselen = prefix_len;
505 if (baselen) {
506 path = base = prefix;
507 if (exclude_per_dir) {
508 char *p, *pp = xmalloc(baselen+1);
509 memcpy(pp, prefix, baselen+1);
510 p = pp;
511 while (1) {
512 char save = *p;
513 *p = 0;
514 push_exclude_per_directory(pp, p-pp);
515 *p++ = save;
516 if (!save)
517 break;
518 p = strchr(p, '/');
519 if (p)
520 p++;
521 else
522 p = pp + baselen;
524 free(pp);
527 read_directory(path, base, baselen);
528 qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
529 if (show_others)
530 show_other_files();
531 if (show_killed)
532 show_killed_files();
534 if (show_cached | show_stage) {
535 for (i = 0; i < active_nr; i++) {
536 struct cache_entry *ce = active_cache[i];
537 if (excluded(ce->name) != show_ignored)
538 continue;
539 if (show_unmerged && !ce_stage(ce))
540 continue;
541 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
544 if (show_deleted | show_modified) {
545 for (i = 0; i < active_nr; i++) {
546 struct cache_entry *ce = active_cache[i];
547 struct stat st;
548 int err;
549 if (excluded(ce->name) != show_ignored)
550 continue;
551 err = lstat(ce->name, &st);
552 if (show_deleted && err)
553 show_ce_entry(tag_removed, ce);
554 if (show_modified && ce_modified(ce, &st, 0))
555 show_ce_entry(tag_modified, ce);
561 * Prune the index to only contain stuff starting with "prefix"
563 static void prune_cache(void)
565 int pos = cache_name_pos(prefix, prefix_len);
566 unsigned int first, last;
568 if (pos < 0)
569 pos = -pos-1;
570 active_cache += pos;
571 active_nr -= pos;
572 first = 0;
573 last = active_nr;
574 while (last > first) {
575 int next = (last + first) >> 1;
576 struct cache_entry *ce = active_cache[next];
577 if (!strncmp(ce->name, prefix, prefix_len)) {
578 first = next+1;
579 continue;
581 last = next;
583 active_nr = last;
586 static void verify_pathspec(void)
588 const char **p, *n, *prev;
589 char *real_prefix;
590 unsigned long max;
592 prev = NULL;
593 max = PATH_MAX;
594 for (p = pathspec; (n = *p) != NULL; p++) {
595 int i, len = 0;
596 for (i = 0; i < max; i++) {
597 char c = n[i];
598 if (prev && prev[i] != c)
599 break;
600 if (!c || c == '*' || c == '?')
601 break;
602 if (c == '/')
603 len = i+1;
605 prev = n;
606 if (len < max) {
607 max = len;
608 if (!max)
609 break;
613 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
614 die("git-ls-files: cannot generate relative filenames containing '..'");
616 real_prefix = NULL;
617 prefix_len = max;
618 if (max) {
619 real_prefix = xmalloc(max + 1);
620 memcpy(real_prefix, prev, max);
621 real_prefix[max] = 0;
623 prefix = real_prefix;
626 static const char ls_files_usage[] =
627 "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
628 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
629 "[ --exclude-per-directory=<filename> ] [--full-name] [--] [<file>]*";
631 int main(int argc, const char **argv)
633 int i;
634 int exc_given = 0;
636 prefix = setup_git_directory();
637 if (prefix)
638 prefix_offset = strlen(prefix);
639 git_config(git_default_config);
641 for (i = 1; i < argc; i++) {
642 const char *arg = argv[i];
644 if (!strcmp(arg, "--")) {
645 i++;
646 break;
648 if (!strcmp(arg, "-z")) {
649 line_terminator = 0;
650 continue;
652 if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) {
653 tag_cached = "H ";
654 tag_unmerged = "M ";
655 tag_removed = "R ";
656 tag_modified = "C ";
657 tag_other = "? ";
658 tag_killed = "K ";
659 if (arg[1] == 'v')
660 show_valid_bit = 1;
661 continue;
663 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
664 show_cached = 1;
665 continue;
667 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
668 show_deleted = 1;
669 continue;
671 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
672 show_modified = 1;
673 continue;
675 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
676 show_others = 1;
677 continue;
679 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
680 show_ignored = 1;
681 continue;
683 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
684 show_stage = 1;
685 continue;
687 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
688 show_killed = 1;
689 continue;
691 if (!strcmp(arg, "--directory")) {
692 show_other_directories = 1;
693 continue;
695 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
696 /* There's no point in showing unmerged unless
697 * you also show the stage information.
699 show_stage = 1;
700 show_unmerged = 1;
701 continue;
703 if (!strcmp(arg, "-x") && i+1 < argc) {
704 exc_given = 1;
705 add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]);
706 continue;
708 if (!strncmp(arg, "--exclude=", 10)) {
709 exc_given = 1;
710 add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]);
711 continue;
713 if (!strcmp(arg, "-X") && i+1 < argc) {
714 exc_given = 1;
715 add_excludes_from_file(argv[++i]);
716 continue;
718 if (!strncmp(arg, "--exclude-from=", 15)) {
719 exc_given = 1;
720 add_excludes_from_file(arg+15);
721 continue;
723 if (!strncmp(arg, "--exclude-per-directory=", 24)) {
724 exc_given = 1;
725 exclude_per_dir = arg + 24;
726 continue;
728 if (!strcmp(arg, "--full-name")) {
729 prefix_offset = 0;
730 continue;
732 if (!strcmp(arg, "--error-unmatch")) {
733 error_unmatch = 1;
734 continue;
736 if (*arg == '-')
737 usage(ls_files_usage);
738 break;
741 pathspec = get_pathspec(prefix, argv + i);
743 /* Verify that the pathspec matches the prefix */
744 if (pathspec)
745 verify_pathspec();
747 /* Treat unmatching pathspec elements as errors */
748 if (pathspec && error_unmatch) {
749 int num;
750 for (num = 0; pathspec[num]; num++)
752 ps_matched = xcalloc(1, num);
755 if (show_ignored && !exc_given) {
756 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
757 argv[0]);
758 exit(1);
761 /* With no flags, we default to showing the cached files */
762 if (!(show_stage | show_deleted | show_others | show_unmerged |
763 show_killed | show_modified))
764 show_cached = 1;
766 read_cache();
767 if (prefix)
768 prune_cache();
769 show_files();
771 if (ps_matched) {
772 /* We need to make sure all pathspec matched otherwise
773 * it is an error.
775 int num, errors = 0;
776 for (num = 0; pathspec[num]; num++) {
777 if (ps_matched[num])
778 continue;
779 error("pathspec '%s' did not match any.",
780 pathspec[num] + prefix_offset);
781 errors++;
783 return errors ? 1 : 0;
786 return 0;