Builtin git-show-branch.
[git.git] / builtin-ls-files.c
blob3a0c5f215003bda5cfa2109953cd556765f6ea2c
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"
13 #include "builtin.h"
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';
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;
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 = "";
42 static const char *exclude_per_dir = NULL;
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.
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];
62 static void add_exclude(const char *string, const char *base,
63 int baselen, struct exclude_list *which)
65 struct exclude *x = xmalloc(sizeof (*x));
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));
75 which->excludes[which->nr++] = x;
78 static int add_excludes_from_file_1(const char *fname,
79 const char *base,
80 int baselen,
81 struct exclude_list *which)
83 int fd, i;
84 long size;
85 char *buf, *entry;
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;
98 buf = xmalloc(size+1);
99 if (read(fd, buf, size) != size)
100 goto err;
101 close(fd);
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);
111 entry = buf + i + 1;
114 return 0;
116 err:
117 if (0 <= fd)
118 close(fd);
119 return -1;
122 static void add_excludes_from_file(const char *fname)
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);
129 static int push_exclude_per_directory(const char *base, int baselen)
131 char exclude_file[PATH_MAX];
132 struct exclude_list *el = &exclude_list[EXC_DIRS];
133 int current_nr = el->nr;
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);
140 return current_nr;
143 static void pop_exclude_per_directory(int stk)
145 struct exclude_list *el = &exclude_list[EXC_DIRS];
147 while (stk < el->nr)
148 free(el->excludes[--el->nr]);
151 /* Scan the list and let the last match determines the fate.
152 * Return 1 for exclude, 0 for include and -1 for undecided.
154 static int excluded_1(const char *pathname,
155 int pathlen,
156 struct exclude_list *el)
158 int i;
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;
166 if (*exclude == '!') {
167 to_exclude = 0;
168 exclude++;
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;
178 else {
179 /* match with FNM_PATHNAME:
180 * exclude has base (baselen long) implicitly
181 * in front of it.
183 int baselen = x->baselen;
184 if (*exclude == '/')
185 exclude++;
187 if (pathlen < baselen ||
188 (baselen && pathname[baselen-1] != '/') ||
189 strncmp(pathname, x->base, baselen))
190 continue;
192 if (fnmatch(exclude, pathname+baselen,
193 FNM_PATHNAME) == 0)
194 return to_exclude;
198 return -1; /* undecided */
201 static int excluded(const char *pathname)
203 int pathlen = strlen(pathname);
204 int st;
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;
214 return 0;
217 struct nond_on_fs {
218 int len;
219 char name[FLEX_ARRAY]; /* more */
222 static struct nond_on_fs **dir;
223 static int nr_dir;
224 static int dir_alloc;
226 static void add_name(const char *pathname, int len)
228 struct nond_on_fs *ent;
230 if (cache_name_pos(pathname, len) >= 0)
231 return;
233 if (nr_dir == dir_alloc) {
234 dir_alloc = alloc_nr(dir_alloc);
235 dir = xrealloc(dir, dir_alloc*sizeof(ent));
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;
244 static int dir_exists(const char *dirname, int len)
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);
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.
261 * Also, we ignore the name ".git" (even if it is not a directory).
262 * That likely will not change.
264 static int read_directory(const char *path, const char *base, int baselen)
266 DIR *fdir = opendir(path);
267 int contents = 0;
269 if (fdir) {
270 int exclude_stk;
271 struct dirent *de;
272 char fullname[MAXPATHLEN + 1];
273 memcpy(fullname, base, baselen);
275 exclude_stk = push_exclude_per_directory(base, baselen);
277 while ((de = readdir(fdir)) != NULL) {
278 int len;
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;
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;
320 contents += subdir;
321 continue;
322 case DT_REG:
323 case DT_LNK:
324 break;
326 add_name(fullname, baselen + len);
327 contents++;
329 closedir(fdir);
331 pop_exclude_per_directory(exclude_stk);
334 return contents;
337 static int cmp_name(const void *p1, const void *p2)
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;
342 return cache_name_compare(e1->name, e1->len,
343 e2->name, e2->len);
347 * Match a pathspec against a filename. The first "len" characters
348 * are the common prefix
350 static int match(const char **spec, char *ps_matched,
351 const char *filename, int len)
353 const char *m;
355 while ((m = *spec++) != NULL) {
356 int matchlen = strlen(m + len);
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;
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;
378 return 0;
381 static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
383 int len = prefix_len;
384 int offset = prefix_offset;
386 if (len >= ent->len)
387 die("git-ls-files: internal error - directory entry not superset of prefix");
389 if (pathspec && !match(pathspec, ps_matched, ent->name, len))
390 return;
392 fputs(tag, stdout);
393 write_name_quoted("", 0, ent->name + offset, line_terminator, stdout);
394 putchar(line_terminator);
397 static void show_other_files(void)
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.
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 */
416 show_dir_entry(tag_other, ent);
420 static void show_killed_files(void)
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;
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.
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?
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;
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.
460 killed = 1;
461 break;
464 if (killed)
465 show_dir_entry(tag_killed, dir[i]);
469 static void show_ce_entry(const char *tag, struct cache_entry *ce)
471 int len = prefix_len;
472 int offset = prefix_offset;
474 if (len >= ce_namelen(ce))
475 die("git-ls-files: internal error - cache entry not superset of prefix");
477 if (pathspec && !match(pathspec, ps_matched, ce->name, len))
478 return;
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;
494 tag = alttag;
497 if (!show_stage) {
498 fputs(tag, stdout);
499 write_name_quoted("", 0, ce->name + offset,
500 line_terminator, stdout);
501 putchar(line_terminator);
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);
516 static void show_files(void)
518 int i;
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;
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;
544 free(pp);
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();
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);
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);
581 * Prune the index to only contain stuff starting with "prefix"
583 static void prune_cache(void)
585 int pos = cache_name_pos(prefix, prefix_len);
586 unsigned int first, last;
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;
601 last = next;
603 active_nr = last;
606 static void verify_pathspec(void)
608 const char **p, *n, *prev;
609 char *real_prefix;
610 unsigned long max;
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;
625 prev = n;
626 if (len < max) {
627 max = len;
628 if (!max)
629 break;
633 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
634 die("git-ls-files: cannot generate relative filenames containing '..'");
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;
643 prefix = real_prefix;
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>]*";
652 int cmd_ls_files(int argc, const char **argv, char** envp)
654 int i;
655 int exc_given = 0;
657 prefix = setup_git_directory();
658 if (prefix)
659 prefix_offset = strlen(prefix);
660 git_config(git_default_config);
662 for (i = 1; i < argc; i++) {
663 const char *arg = argv[i];
665 if (!strcmp(arg, "--")) {
666 i++;
667 break;
669 if (!strcmp(arg, "-z")) {
670 line_terminator = 0;
671 continue;
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;
684 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
685 show_cached = 1;
686 continue;
688 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
689 show_deleted = 1;
690 continue;
692 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
693 show_modified = 1;
694 continue;
696 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
697 show_others = 1;
698 continue;
700 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
701 show_ignored = 1;
702 continue;
704 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
705 show_stage = 1;
706 continue;
708 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
709 show_killed = 1;
710 continue;
712 if (!strcmp(arg, "--directory")) {
713 show_other_directories = 1;
714 continue;
716 if (!strcmp(arg, "--no-empty-directory")) {
717 hide_empty_directories = 1;
718 continue;
720 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
721 /* There's no point in showing unmerged unless
722 * you also show the stage information.
724 show_stage = 1;
725 show_unmerged = 1;
726 continue;
728 if (!strcmp(arg, "-x") && i+1 < argc) {
729 exc_given = 1;
730 add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]);
731 continue;
733 if (!strncmp(arg, "--exclude=", 10)) {
734 exc_given = 1;
735 add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]);
736 continue;
738 if (!strcmp(arg, "-X") && i+1 < argc) {
739 exc_given = 1;
740 add_excludes_from_file(argv[++i]);
741 continue;
743 if (!strncmp(arg, "--exclude-from=", 15)) {
744 exc_given = 1;
745 add_excludes_from_file(arg+15);
746 continue;
748 if (!strncmp(arg, "--exclude-per-directory=", 24)) {
749 exc_given = 1;
750 exclude_per_dir = arg + 24;
751 continue;
753 if (!strcmp(arg, "--full-name")) {
754 prefix_offset = 0;
755 continue;
757 if (!strcmp(arg, "--error-unmatch")) {
758 error_unmatch = 1;
759 continue;
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;
769 if (!strcmp(arg, "--abbrev")) {
770 abbrev = DEFAULT_ABBREV;
771 continue;
773 if (*arg == '-')
774 usage(ls_files_usage);
775 break;
778 pathspec = get_pathspec(prefix, argv + i);
780 /* Verify that the pathspec matches the prefix */
781 if (pathspec)
782 verify_pathspec();
784 /* Treat unmatching pathspec elements as errors */
785 if (pathspec && error_unmatch) {
786 int num;
787 for (num = 0; pathspec[num]; num++)
789 ps_matched = xcalloc(1, num);
792 if (show_ignored && !exc_given) {
793 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
794 argv[0]);
795 exit(1);
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;
803 read_cache();
804 if (prefix)
805 prune_cache();
806 show_files();
808 if (ps_matched) {
809 /* We need to make sure all pathspec matched otherwise
810 * it is an error.
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++;
820 return errors ? 1 : 0;
823 return 0;