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_killed = 0;
20 static int line_terminator = '\n';
22 static const char *tag_cached = "";
23 static const char *tag_unmerged = "";
24 static const char *tag_removed = "";
25 static const char *tag_other = "";
26 static const char *tag_killed = "";
28 static char *exclude_per_dir = NULL;
30 /* We maintain three exclude pattern lists:
31 * EXC_CMDL lists patterns explicitly given on the command line.
32 * EXC_DIRS lists patterns obtained from per-directory ignore files.
33 * EXC_FILE lists patterns from fallback ignore files.
38 static struct exclude_list {
48 static void add_exclude(const char *string, const char *base,
49 int baselen, struct exclude_list *which)
51 struct exclude *x = xmalloc(sizeof (*x));
56 if (which->nr == which->alloc) {
57 which->alloc = alloc_nr(which->alloc);
58 which->excludes = realloc(which->excludes,
59 which->alloc * sizeof(x));
61 which->excludes[which->nr++] = x;
64 static int add_excludes_from_file_1(const char *fname,
67 struct exclude_list *which)
73 fd = open(fname, O_RDONLY);
76 size = lseek(fd, 0, SEEK_END);
79 lseek(fd, 0, SEEK_SET);
85 if (read(fd, buf, size) != size)
90 for (i = 0; i < size; i++) {
92 if (entry != buf + i && entry[0] != '#') {
94 add_exclude(entry, base, baselen, which);
107 static void add_excludes_from_file(const char *fname)
109 if (add_excludes_from_file_1(fname, "", 0,
110 &exclude_list[EXC_FILE]) < 0)
111 die("cannot use %s as an exclude file", fname);
114 static int push_exclude_per_directory(const char *base, int baselen)
116 char exclude_file[PATH_MAX];
117 struct exclude_list *el = &exclude_list[EXC_DIRS];
118 int current_nr = el->nr;
120 if (exclude_per_dir) {
121 memcpy(exclude_file, base, baselen);
122 strcpy(exclude_file + baselen, exclude_per_dir);
123 add_excludes_from_file_1(exclude_file, base, baselen, el);
128 static void pop_exclude_per_directory(int stk)
130 struct exclude_list *el = &exclude_list[EXC_DIRS];
133 free(el->excludes[--el->nr]);
136 /* Scan the list and let the last match determines the fate.
137 * Return 1 for exclude, 0 for include and -1 for undecided.
139 static int excluded_1(const char *pathname,
141 struct exclude_list *el)
146 for (i = el->nr - 1; 0 <= i; i--) {
147 struct exclude *x = el->excludes[i];
148 const char *exclude = x->pattern;
151 if (*exclude == '!') {
156 if (!strchr(exclude, '/')) {
158 const char *basename = strrchr(pathname, '/');
159 basename = (basename) ? basename+1 : pathname;
160 if (fnmatch(exclude, basename, 0) == 0)
164 /* match with FNM_PATHNAME:
165 * exclude has base (baselen long) inplicitly
168 int baselen = x->baselen;
172 if (pathlen < baselen ||
173 (baselen && pathname[baselen-1] != '/') ||
174 strncmp(pathname, x->base, baselen))
177 if (fnmatch(exclude, pathname+baselen,
183 return -1; /* undecided */
186 static int excluded(const char *pathname)
188 int pathlen = strlen(pathname);
191 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
192 switch (excluded_1(pathname, pathlen, &exclude_list[st])) {
207 static struct nond_on_fs **dir;
209 static int dir_alloc;
211 static void add_name(const char *pathname, int len)
213 struct nond_on_fs *ent;
215 if (cache_name_pos(pathname, len) >= 0)
218 if (nr_dir == dir_alloc) {
219 dir_alloc = alloc_nr(dir_alloc);
220 dir = xrealloc(dir, dir_alloc*sizeof(ent));
222 ent = xmalloc(sizeof(*ent) + len + 1);
224 memcpy(ent->name, pathname, len);
229 * Read a directory tree. We currently ignore anything but
230 * directories, regular files and symlinks. That's because git
231 * doesn't handle them at all yet. Maybe that will change some
234 * Also, we ignore the name ".git" (even if it is not a directory).
235 * That likely will not change.
237 static void read_directory(const char *path, const char *base, int baselen)
239 DIR *dir = opendir(path);
244 char fullname[MAXPATHLEN + 1];
245 memcpy(fullname, base, baselen);
247 exclude_stk = push_exclude_per_directory(base, baselen);
249 while ((de = readdir(dir)) != NULL) {
252 if ((de->d_name[0] == '.') &&
253 (de->d_name[1] == 0 ||
254 !strcmp(de->d_name + 1, ".") ||
255 !strcmp(de->d_name + 1, "git")))
257 len = strlen(de->d_name);
258 memcpy(fullname + baselen, de->d_name, len+1);
259 if (excluded(fullname) != show_ignored)
267 if (lstat(fullname, &st))
269 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
271 if (!S_ISDIR(st.st_mode))
275 memcpy(fullname + baselen + len, "/", 2);
276 read_directory(fullname, fullname,
283 add_name(fullname, baselen + len);
287 pop_exclude_per_directory(exclude_stk);
291 static int cmp_name(const void *p1, const void *p2)
293 const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1;
294 const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2;
296 return cache_name_compare(e1->name, e1->len,
300 static void show_killed_files(void)
303 for (i = 0; i < nr_dir; i++) {
304 struct nond_on_fs *ent = dir[i];
306 int pos, len, killed = 0;
308 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
309 sp = strchr(cp, '/');
311 /* If ent->name is prefix of an entry in the
312 * cache, it will be killed.
314 pos = cache_name_pos(ent->name, ent->len);
316 die("bug in show-killed-files");
318 while (pos < active_nr &&
319 ce_stage(active_cache[pos]))
320 pos++; /* skip unmerged */
321 if (active_nr <= pos)
323 /* pos points at a name immediately after
324 * ent->name in the cache. Does it expect
325 * ent->name to be a directory?
327 len = ce_namelen(active_cache[pos]);
328 if ((ent->len < len) &&
329 !strncmp(active_cache[pos]->name,
330 ent->name, ent->len) &&
331 active_cache[pos]->name[ent->len] == '/')
335 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
336 /* If any of the leading directories in
337 * ent->name is registered in the cache,
338 * ent->name will be killed.
345 printf("%s%.*s%c", tag_killed,
346 dir[i]->len, dir[i]->name,
351 static void show_files(void)
355 /* For cached/deleted files we don't need to even do the readdir */
356 if (show_others || show_killed) {
357 read_directory(".", "", 0);
358 qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
360 for (i = 0; i < nr_dir; i++)
361 printf("%s%.*s%c", tag_other,
362 dir[i]->len, dir[i]->name,
367 if (show_cached | show_stage) {
368 for (i = 0; i < active_nr; i++) {
369 struct cache_entry *ce = active_cache[i];
370 if (excluded(ce->name) != show_ignored)
372 if (show_unmerged && !ce_stage(ce))
376 ce_stage(ce) ? tag_unmerged :
378 ce->name, line_terminator);
380 printf("%s%06o %s %d\t%s%c",
381 ce_stage(ce) ? tag_unmerged :
384 sha1_to_hex(ce->sha1),
386 ce->name, line_terminator);
390 for (i = 0; i < active_nr; i++) {
391 struct cache_entry *ce = active_cache[i];
393 if (excluded(ce->name) != show_ignored)
395 if (!lstat(ce->name, &st))
397 printf("%s%s%c", tag_removed, ce->name,
403 static const char *ls_files_usage =
404 "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed])* "
405 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
406 "[ --exclude-per-directory=<filename> ]";
409 int main(int argc, char **argv)
414 for (i = 1; i < argc; i++) {
417 if (!strcmp(arg, "-z")) {
419 } else if (!strcmp(arg, "-t")) {
425 } else if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
427 } else if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
429 } else if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
431 } else if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
433 } else if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
435 } else if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
437 } else if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
438 /* There's no point in showing unmerged unless
439 * you also show the stage information.
443 } else if (!strcmp(arg, "-x") && i+1 < argc) {
445 add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]);
446 } else if (!strncmp(arg, "--exclude=", 10)) {
448 add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]);
449 } else if (!strcmp(arg, "-X") && i+1 < argc) {
451 add_excludes_from_file(argv[++i]);
452 } else if (!strncmp(arg, "--exclude-from=", 15)) {
454 add_excludes_from_file(arg+15);
455 } else if (!strncmp(arg, "--exclude-per-directory=", 24)) {
457 exclude_per_dir = arg + 24;
459 usage(ls_files_usage);
462 if (show_ignored && !exc_given) {
463 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
468 /* With no flags, we default to showing the cached files */
469 if (!(show_stage | show_deleted | show_others | show_unmerged | show_killed))