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
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 line_terminator = '\n';
24 static int prefix_len = 0, prefix_offset = 0;
25 static const char *prefix = NULL;
26 static const char **pathspec = NULL;
28 static const char *tag_cached = "";
29 static const char *tag_unmerged = "";
30 static const char *tag_removed = "";
31 static const char *tag_other = "";
32 static const char *tag_killed = "";
33 static const char *tag_modified = "";
35 static const char *exclude_per_dir = NULL;
37 /* We maintain three exclude pattern lists:
38 * EXC_CMDL lists patterns explicitly given on the command line.
39 * EXC_DIRS lists patterns obtained from per-directory ignore files.
40 * EXC_FILE lists patterns from fallback ignore files.
45 static struct exclude_list {
55 static void add_exclude(const char *string, const char *base,
56 int baselen, struct exclude_list *which)
58 struct exclude *x = xmalloc(sizeof (*x));
63 if (which->nr == which->alloc) {
64 which->alloc = alloc_nr(which->alloc);
65 which->excludes = realloc(which->excludes,
66 which->alloc * sizeof(x));
68 which->excludes[which->nr++] = x;
71 static int add_excludes_from_file_1(const char *fname,
74 struct exclude_list *which)
80 fd = open(fname, O_RDONLY);
83 size = lseek(fd, 0, SEEK_END);
86 lseek(fd, 0, SEEK_SET);
92 if (read(fd, buf, size) != size)
97 for (i = 0; i < size; i++) {
99 if (entry != buf + i && entry[0] != '#') {
100 buf[i - (i && buf[i-1] == '\r')] = 0;
101 add_exclude(entry, base, baselen, which);
114 static void add_excludes_from_file(const char *fname)
116 if (add_excludes_from_file_1(fname, "", 0,
117 &exclude_list[EXC_FILE]) < 0)
118 die("cannot use %s as an exclude file", fname);
121 static int push_exclude_per_directory(const char *base, int baselen)
123 char exclude_file[PATH_MAX];
124 struct exclude_list *el = &exclude_list[EXC_DIRS];
125 int current_nr = el->nr;
127 if (exclude_per_dir) {
128 memcpy(exclude_file, base, baselen);
129 strcpy(exclude_file + baselen, exclude_per_dir);
130 add_excludes_from_file_1(exclude_file, base, baselen, el);
135 static void pop_exclude_per_directory(int stk)
137 struct exclude_list *el = &exclude_list[EXC_DIRS];
140 free(el->excludes[--el->nr]);
143 /* Scan the list and let the last match determines the fate.
144 * Return 1 for exclude, 0 for include and -1 for undecided.
146 static int excluded_1(const char *pathname,
148 struct exclude_list *el)
153 for (i = el->nr - 1; 0 <= i; i--) {
154 struct exclude *x = el->excludes[i];
155 const char *exclude = x->pattern;
158 if (*exclude == '!') {
163 if (!strchr(exclude, '/')) {
165 const char *basename = strrchr(pathname, '/');
166 basename = (basename) ? basename+1 : pathname;
167 if (fnmatch(exclude, basename, 0) == 0)
171 /* match with FNM_PATHNAME:
172 * exclude has base (baselen long) inplicitly
175 int baselen = x->baselen;
179 if (pathlen < baselen ||
180 (baselen && pathname[baselen-1] != '/') ||
181 strncmp(pathname, x->base, baselen))
184 if (fnmatch(exclude, pathname+baselen,
190 return -1; /* undecided */
193 static int excluded(const char *pathname)
195 int pathlen = strlen(pathname);
198 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
199 switch (excluded_1(pathname, pathlen, &exclude_list[st])) {
214 static struct nond_on_fs **dir;
216 static int dir_alloc;
218 static void add_name(const char *pathname, int len)
220 struct nond_on_fs *ent;
222 if (cache_name_pos(pathname, len) >= 0)
225 if (nr_dir == dir_alloc) {
226 dir_alloc = alloc_nr(dir_alloc);
227 dir = xrealloc(dir, dir_alloc*sizeof(ent));
229 ent = xmalloc(sizeof(*ent) + len + 1);
231 memcpy(ent->name, pathname, len);
237 * Read a directory tree. We currently ignore anything but
238 * directories, regular files and symlinks. That's because git
239 * doesn't handle them at all yet. Maybe that will change some
242 * Also, we ignore the name ".git" (even if it is not a directory).
243 * That likely will not change.
245 static void read_directory(const char *path, const char *base, int baselen)
247 DIR *dir = opendir(path);
252 char fullname[MAXPATHLEN + 1];
253 memcpy(fullname, base, baselen);
255 exclude_stk = push_exclude_per_directory(base, baselen);
257 while ((de = readdir(dir)) != NULL) {
260 if ((de->d_name[0] == '.') &&
261 (de->d_name[1] == 0 ||
262 !strcmp(de->d_name + 1, ".") ||
263 !strcmp(de->d_name + 1, "git")))
265 len = strlen(de->d_name);
266 memcpy(fullname + baselen, de->d_name, len+1);
267 if (excluded(fullname) != show_ignored)
275 if (lstat(fullname, &st))
277 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
279 if (!S_ISDIR(st.st_mode))
283 memcpy(fullname + baselen + len, "/", 2);
284 read_directory(fullname, fullname,
291 add_name(fullname, baselen + len);
295 pop_exclude_per_directory(exclude_stk);
299 static int cmp_name(const void *p1, const void *p2)
301 const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1;
302 const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2;
304 return cache_name_compare(e1->name, e1->len,
309 * Match a pathspec against a filename. The first "len" characters
310 * are the common prefix
312 static int match(const char **spec, const char *filename, int len)
316 while ((m = *spec++) != NULL) {
317 int matchlen = strlen(m + len);
321 if (!strncmp(m + len, filename + len, matchlen)) {
322 if (m[len + matchlen - 1] == '/')
324 switch (filename[len + matchlen]) {
329 if (!fnmatch(m + len, filename + len, 0))
335 static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
337 int len = prefix_len;
338 int offset = prefix_offset;
341 die("git-ls-files: internal error - directory entry not superset of prefix");
343 if (pathspec && !match(pathspec, ent->name, len))
347 write_name_quoted("", ent->name + offset, line_terminator, stdout);
348 putchar(line_terminator);
351 static void show_other_files(void)
354 for (i = 0; i < nr_dir; i++) {
355 /* We should not have a matching entry, but we
356 * may have an unmerged entry for this path.
358 struct nond_on_fs *ent = dir[i];
359 int pos = cache_name_pos(ent->name, ent->len);
360 struct cache_entry *ce;
362 die("bug in show-other-files");
364 if (pos < active_nr) {
365 ce = active_cache[pos];
366 if (ce_namelen(ce) == ent->len &&
367 !memcmp(ce->name, ent->name, ent->len))
368 continue; /* Yup, this one exists unmerged */
370 show_dir_entry(tag_other, ent);
374 static void show_killed_files(void)
377 for (i = 0; i < nr_dir; i++) {
378 struct nond_on_fs *ent = dir[i];
380 int pos, len, killed = 0;
382 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
383 sp = strchr(cp, '/');
385 /* If ent->name is prefix of an entry in the
386 * cache, it will be killed.
388 pos = cache_name_pos(ent->name, ent->len);
390 die("bug in show-killed-files");
392 while (pos < active_nr &&
393 ce_stage(active_cache[pos]))
394 pos++; /* skip unmerged */
395 if (active_nr <= pos)
397 /* pos points at a name immediately after
398 * ent->name in the cache. Does it expect
399 * ent->name to be a directory?
401 len = ce_namelen(active_cache[pos]);
402 if ((ent->len < len) &&
403 !strncmp(active_cache[pos]->name,
404 ent->name, ent->len) &&
405 active_cache[pos]->name[ent->len] == '/')
409 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
410 /* If any of the leading directories in
411 * ent->name is registered in the cache,
412 * ent->name will be killed.
419 show_dir_entry(tag_killed, dir[i]);
423 static void show_ce_entry(const char *tag, struct cache_entry *ce)
425 int len = prefix_len;
426 int offset = prefix_offset;
428 if (len >= ce_namelen(ce))
429 die("git-ls-files: internal error - cache entry not superset of prefix");
431 if (pathspec && !match(pathspec, ce->name, len))
436 write_name_quoted("", ce->name + offset, line_terminator, stdout);
437 putchar(line_terminator);
440 printf("%s%06o %s %d\t",
443 sha1_to_hex(ce->sha1),
445 write_name_quoted("", ce->name + offset, line_terminator, stdout);
446 putchar(line_terminator);
450 static void show_files(void)
454 /* For cached/deleted files we don't need to even do the readdir */
455 if (show_others || show_killed) {
456 const char *path = ".", *base = "";
457 int baselen = prefix_len;
460 path = base = prefix;
461 read_directory(path, base, baselen);
462 qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
468 if (show_cached | show_stage) {
469 for (i = 0; i < active_nr; i++) {
470 struct cache_entry *ce = active_cache[i];
471 if (excluded(ce->name) != show_ignored)
473 if (show_unmerged && !ce_stage(ce))
475 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
478 if (show_deleted | show_modified) {
479 for (i = 0; i < active_nr; i++) {
480 struct cache_entry *ce = active_cache[i];
483 if (excluded(ce->name) != show_ignored)
485 err = lstat(ce->name, &st);
486 if (show_deleted && err)
487 show_ce_entry(tag_removed, ce);
488 if (show_modified && ce_modified(ce, &st))
489 show_ce_entry(tag_modified, ce);
495 * Prune the index to only contain stuff starting with "prefix"
497 static void prune_cache(void)
499 int pos = cache_name_pos(prefix, prefix_len);
500 unsigned int first, last;
508 while (last > first) {
509 int next = (last + first) >> 1;
510 struct cache_entry *ce = active_cache[next];
511 if (!strncmp(ce->name, prefix, prefix_len)) {
520 static void verify_pathspec(void)
522 const char **p, *n, *prev;
528 for (p = pathspec; (n = *p) != NULL; p++) {
530 for (i = 0; i < max; i++) {
532 if (prev && prev[i] != c)
534 if (!c || c == '*' || c == '?')
547 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
548 die("git-ls-files: cannot generate relative filenames containing '..'");
553 real_prefix = xmalloc(max + 1);
554 memcpy(real_prefix, prev, max);
555 real_prefix[max] = 0;
557 prefix = real_prefix;
560 static const char ls_files_usage[] =
561 "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
562 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
563 "[ --exclude-per-directory=<filename> ] [--] [<file>]*";
565 int main(int argc, const char **argv)
570 prefix = setup_git_directory();
572 prefix_offset = strlen(prefix);
573 git_config(git_default_config);
575 for (i = 1; i < argc; i++) {
576 const char *arg = argv[i];
578 if (!strcmp(arg, "--")) {
582 if (!strcmp(arg, "-z")) {
586 if (!strcmp(arg, "-t")) {
595 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
599 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
603 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
607 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
611 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
615 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
619 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
623 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
624 /* There's no point in showing unmerged unless
625 * you also show the stage information.
631 if (!strcmp(arg, "-x") && i+1 < argc) {
633 add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]);
636 if (!strncmp(arg, "--exclude=", 10)) {
638 add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]);
641 if (!strcmp(arg, "-X") && i+1 < argc) {
643 add_excludes_from_file(argv[++i]);
646 if (!strncmp(arg, "--exclude-from=", 15)) {
648 add_excludes_from_file(arg+15);
651 if (!strncmp(arg, "--exclude-per-directory=", 24)) {
653 exclude_per_dir = arg + 24;
656 if (!strcmp(arg, "--full-name")) {
661 usage(ls_files_usage);
665 pathspec = get_pathspec(prefix, argv + i);
667 /* Verify that the pathspec matches the prefix */
671 if (show_ignored && !exc_given) {
672 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
677 /* With no flags, we default to showing the cached files */
678 if (!(show_stage | show_deleted | show_others | show_unmerged |
679 show_killed | show_modified))