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
13 static int show_deleted = 0;
14 static int show_cached = 0;
15 static int show_others = 0;
16 static int show_ignored = 0;
17 static int show_stage = 0;
18 static int show_unmerged = 0;
19 static int show_modified = 0;
20 static int show_killed = 0;
21 static int line_terminator = '\n';
23 static int prefix_len = 0, prefix_offset = 0;
24 static const char *prefix = NULL;
25 static const char **pathspec = NULL;
27 static const char *tag_cached = "";
28 static const char *tag_unmerged = "";
29 static const char *tag_removed = "";
30 static const char *tag_other = "";
31 static const char *tag_killed = "";
32 static const char *tag_modified = "";
34 static const char *exclude_per_dir = NULL;
36 /* We maintain three exclude pattern lists:
37 * EXC_CMDL lists patterns explicitly given on the command line.
38 * EXC_DIRS lists patterns obtained from per-directory ignore files.
39 * EXC_FILE lists patterns from fallback ignore files.
44 static struct exclude_list {
54 static void add_exclude(const char *string, const char *base,
55 int baselen, struct exclude_list *which)
57 struct exclude *x = xmalloc(sizeof (*x));
62 if (which->nr == which->alloc) {
63 which->alloc = alloc_nr(which->alloc);
64 which->excludes = realloc(which->excludes,
65 which->alloc * sizeof(x));
67 which->excludes[which->nr++] = x;
70 static int add_excludes_from_file_1(const char *fname,
73 struct exclude_list *which)
79 fd = open(fname, O_RDONLY);
82 size = lseek(fd, 0, SEEK_END);
85 lseek(fd, 0, SEEK_SET);
91 if (read(fd, buf, size) != size)
96 for (i = 0; i < size; i++) {
98 if (entry != buf + i && entry[0] != '#') {
100 add_exclude(entry, base, baselen, which);
113 static void add_excludes_from_file(const char *fname)
115 if (add_excludes_from_file_1(fname, "", 0,
116 &exclude_list[EXC_FILE]) < 0)
117 die("cannot use %s as an exclude file", fname);
120 static int push_exclude_per_directory(const char *base, int baselen)
122 char exclude_file[PATH_MAX];
123 struct exclude_list *el = &exclude_list[EXC_DIRS];
124 int current_nr = el->nr;
126 if (exclude_per_dir) {
127 memcpy(exclude_file, base, baselen);
128 strcpy(exclude_file + baselen, exclude_per_dir);
129 add_excludes_from_file_1(exclude_file, base, baselen, el);
134 static void pop_exclude_per_directory(int stk)
136 struct exclude_list *el = &exclude_list[EXC_DIRS];
139 free(el->excludes[--el->nr]);
142 /* Scan the list and let the last match determines the fate.
143 * Return 1 for exclude, 0 for include and -1 for undecided.
145 static int excluded_1(const char *pathname,
147 struct exclude_list *el)
152 for (i = el->nr - 1; 0 <= i; i--) {
153 struct exclude *x = el->excludes[i];
154 const char *exclude = x->pattern;
157 if (*exclude == '!') {
162 if (!strchr(exclude, '/')) {
164 const char *basename = strrchr(pathname, '/');
165 basename = (basename) ? basename+1 : pathname;
166 if (fnmatch(exclude, basename, 0) == 0)
170 /* match with FNM_PATHNAME:
171 * exclude has base (baselen long) inplicitly
174 int baselen = x->baselen;
178 if (pathlen < baselen ||
179 (baselen && pathname[baselen-1] != '/') ||
180 strncmp(pathname, x->base, baselen))
183 if (fnmatch(exclude, pathname+baselen,
189 return -1; /* undecided */
192 static int excluded(const char *pathname)
194 int pathlen = strlen(pathname);
197 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
198 switch (excluded_1(pathname, pathlen, &exclude_list[st])) {
213 static struct nond_on_fs **dir;
215 static int dir_alloc;
217 static void add_name(const char *pathname, int len)
219 struct nond_on_fs *ent;
221 if (cache_name_pos(pathname, len) >= 0)
224 if (nr_dir == dir_alloc) {
225 dir_alloc = alloc_nr(dir_alloc);
226 dir = xrealloc(dir, dir_alloc*sizeof(ent));
228 ent = xmalloc(sizeof(*ent) + len + 1);
230 memcpy(ent->name, pathname, len);
236 * Read a directory tree. We currently ignore anything but
237 * directories, regular files and symlinks. That's because git
238 * doesn't handle them at all yet. Maybe that will change some
241 * Also, we ignore the name ".git" (even if it is not a directory).
242 * That likely will not change.
244 static void read_directory(const char *path, const char *base, int baselen)
246 DIR *dir = opendir(path);
251 char fullname[MAXPATHLEN + 1];
252 memcpy(fullname, base, baselen);
254 exclude_stk = push_exclude_per_directory(base, baselen);
256 while ((de = readdir(dir)) != NULL) {
259 if ((de->d_name[0] == '.') &&
260 (de->d_name[1] == 0 ||
261 !strcmp(de->d_name + 1, ".") ||
262 !strcmp(de->d_name + 1, "git")))
264 len = strlen(de->d_name);
265 memcpy(fullname + baselen, de->d_name, len+1);
266 if (excluded(fullname) != show_ignored)
274 if (lstat(fullname, &st))
276 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
278 if (!S_ISDIR(st.st_mode))
282 memcpy(fullname + baselen + len, "/", 2);
283 read_directory(fullname, fullname,
290 add_name(fullname, baselen + len);
294 pop_exclude_per_directory(exclude_stk);
298 static int cmp_name(const void *p1, const void *p2)
300 const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1;
301 const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2;
303 return cache_name_compare(e1->name, e1->len,
308 * Match a pathspec against a filename. The first "len" characters
309 * are the common prefix
311 static int match(const char **spec, const char *filename, int len)
315 while ((m = *spec++) != NULL) {
316 int matchlen = strlen(m + len);
320 if (!strncmp(m + len, filename + len, matchlen)) {
321 if (m[len + matchlen - 1] == '/')
323 switch (filename[len + matchlen]) {
328 if (!fnmatch(m + len, filename + len, 0))
334 static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
336 int len = prefix_len;
337 int offset = prefix_offset;
340 die("git-ls-files: internal error - directory entry not superset of prefix");
342 if (pathspec && !match(pathspec, ent->name, len))
345 printf("%s%s%c", tag, ent->name + offset, line_terminator);
348 static void show_killed_files(void)
351 for (i = 0; i < nr_dir; i++) {
352 struct nond_on_fs *ent = dir[i];
354 int pos, len, killed = 0;
356 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
357 sp = strchr(cp, '/');
359 /* If ent->name is prefix of an entry in the
360 * cache, it will be killed.
362 pos = cache_name_pos(ent->name, ent->len);
364 die("bug in show-killed-files");
366 while (pos < active_nr &&
367 ce_stage(active_cache[pos]))
368 pos++; /* skip unmerged */
369 if (active_nr <= pos)
371 /* pos points at a name immediately after
372 * ent->name in the cache. Does it expect
373 * ent->name to be a directory?
375 len = ce_namelen(active_cache[pos]);
376 if ((ent->len < len) &&
377 !strncmp(active_cache[pos]->name,
378 ent->name, ent->len) &&
379 active_cache[pos]->name[ent->len] == '/')
383 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
384 /* If any of the leading directories in
385 * ent->name is registered in the cache,
386 * ent->name will be killed.
393 show_dir_entry(tag_killed, dir[i]);
397 static void show_ce_entry(const char *tag, struct cache_entry *ce)
399 int len = prefix_len;
400 int offset = prefix_offset;
402 if (len >= ce_namelen(ce))
403 die("git-ls-files: internal error - cache entry not superset of prefix");
405 if (pathspec && !match(pathspec, ce->name, len))
409 printf("%s%s%c", tag, ce->name + offset, line_terminator);
411 printf("%s%06o %s %d\t%s%c",
414 sha1_to_hex(ce->sha1),
416 ce->name + offset, line_terminator);
419 static void show_files(void)
423 /* For cached/deleted files we don't need to even do the readdir */
424 if (show_others || show_killed) {
425 const char *path = ".", *base = "";
426 int baselen = prefix_len;
429 path = base = prefix;
430 read_directory(path, base, baselen);
431 qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
433 for (i = 0; i < nr_dir; i++)
434 show_dir_entry(tag_other, dir[i]);
438 if (show_cached | show_stage) {
439 for (i = 0; i < active_nr; i++) {
440 struct cache_entry *ce = active_cache[i];
441 if (excluded(ce->name) != show_ignored)
443 if (show_unmerged && !ce_stage(ce))
445 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
448 if (show_deleted | show_modified) {
449 for (i = 0; i < active_nr; i++) {
450 struct cache_entry *ce = active_cache[i];
453 if (excluded(ce->name) != show_ignored)
455 err = lstat(ce->name, &st);
456 if (show_deleted && err)
457 show_ce_entry(tag_removed, ce);
458 if (show_modified && ce_modified(ce, &st))
459 show_ce_entry(tag_modified, ce);
465 * Prune the index to only contain stuff starting with "prefix"
467 static void prune_cache(void)
469 int pos = cache_name_pos(prefix, prefix_len);
470 unsigned int first, last;
478 while (last > first) {
479 int next = (last + first) >> 1;
480 struct cache_entry *ce = active_cache[next];
481 if (!strncmp(ce->name, prefix, prefix_len)) {
490 static void verify_pathspec(void)
492 const char **p, *n, *prev;
498 for (p = pathspec; (n = *p) != NULL; p++) {
500 for (i = 0; i < max; i++) {
502 if (prev && prev[i] != c)
504 if (!c || c == '*' || c == '?')
517 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
518 die("git-ls-files: cannot generate relative filenames containing '..'");
523 real_prefix = xmalloc(max + 1);
524 memcpy(real_prefix, prev, max);
525 real_prefix[max] = 0;
527 prefix = real_prefix;
530 static const char ls_files_usage[] =
531 "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
532 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
533 "[ --exclude-per-directory=<filename> ]";
535 int main(int argc, const char **argv)
540 prefix = setup_git_directory();
542 prefix_offset = strlen(prefix);
544 for (i = 1; i < argc; i++) {
545 const char *arg = argv[i];
547 if (!strcmp(arg, "-z")) {
551 if (!strcmp(arg, "-t")) {
560 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
564 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
568 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
572 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
576 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
580 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
584 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
588 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
589 /* There's no point in showing unmerged unless
590 * you also show the stage information.
596 if (!strcmp(arg, "-x") && i+1 < argc) {
598 add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]);
601 if (!strncmp(arg, "--exclude=", 10)) {
603 add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]);
606 if (!strcmp(arg, "-X") && i+1 < argc) {
608 add_excludes_from_file(argv[++i]);
611 if (!strncmp(arg, "--exclude-from=", 15)) {
613 add_excludes_from_file(arg+15);
616 if (!strncmp(arg, "--exclude-per-directory=", 24)) {
618 exclude_per_dir = arg + 24;
621 if (!strcmp(arg, "--full-name")) {
626 usage(ls_files_usage);
630 pathspec = get_pathspec(prefix, argv + i);
632 /* Verify that the pathspec matches the prefix */
636 if (show_ignored && !exc_given) {
637 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
642 /* With no flags, we default to showing the cached files */
643 if (!(show_stage | show_deleted | show_others | show_unmerged |
644 show_killed | show_modified))