]> git.ipfire.org Git - thirdparty/git.git/blame - ls-files.c
Remove 'Previously this command was known as ...' messages.
[thirdparty/git.git] / ls-files.c
CommitLineData
8695c8bf
LT
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.
5 *
6 * Copyright (C) Linus Torvalds, 2005
7 */
8#include <dirent.h>
9ff768e9 9#include <fnmatch.h>
8695c8bf
LT
10
11#include "cache.h"
22ddf719 12#include "quote.h"
8695c8bf
LT
13
14static int show_deleted = 0;
15static int show_cached = 0;
16static int show_others = 0;
17static int show_ignored = 0;
aee46198 18static int show_stage = 0;
eec8c633 19static int show_unmerged = 0;
b0391890 20static int show_modified = 0;
6ca45943 21static int show_killed = 0;
b83c8345 22static int line_terminator = '\n';
8695c8bf 23
5be4efbe
LT
24static int prefix_len = 0, prefix_offset = 0;
25static const char *prefix = NULL;
56fc5108 26static const char **pathspec = NULL;
5be4efbe 27
20d37ef6
PB
28static const char *tag_cached = "";
29static const char *tag_unmerged = "";
30static const char *tag_removed = "";
31static const char *tag_other = "";
6ca45943 32static const char *tag_killed = "";
b0391890 33static const char *tag_modified = "";
20d37ef6 34
6b5ee137 35static const char *exclude_per_dir = NULL;
fee88256
JH
36
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.
41 */
42#define EXC_CMDL 0
43#define EXC_DIRS 1
44#define EXC_FILE 2
45static struct exclude_list {
46 int nr;
47 int alloc;
48 struct exclude {
49 const char *pattern;
50 const char *base;
51 int baselen;
52 } **excludes;
53} exclude_list[3];
54
55static void add_exclude(const char *string, const char *base,
56 int baselen, struct exclude_list *which)
9ff768e9 57{
f87f9497
JH
58 struct exclude *x = xmalloc(sizeof (*x));
59
60 x->pattern = string;
61 x->base = base;
62 x->baselen = baselen;
fee88256
JH
63 if (which->nr == which->alloc) {
64 which->alloc = alloc_nr(which->alloc);
65 which->excludes = realloc(which->excludes,
66 which->alloc * sizeof(x));
9ff768e9 67 }
fee88256 68 which->excludes[which->nr++] = x;
9ff768e9
NP
69}
70
f87f9497 71static int add_excludes_from_file_1(const char *fname,
fee88256
JH
72 const char *base,
73 int baselen,
74 struct exclude_list *which)
9ff768e9
NP
75{
76 int fd, i;
77 long size;
78 char *buf, *entry;
79
80 fd = open(fname, O_RDONLY);
81 if (fd < 0)
82 goto err;
83 size = lseek(fd, 0, SEEK_END);
84 if (size < 0)
85 goto err;
86 lseek(fd, 0, SEEK_SET);
87 if (size == 0) {
88 close(fd);
f87f9497 89 return 0;
9ff768e9
NP
90 }
91 buf = xmalloc(size);
92 if (read(fd, buf, size) != size)
93 goto err;
94 close(fd);
95
96 entry = buf;
97 for (i = 0; i < size; i++) {
98 if (buf[i] == '\n') {
f87f9497 99 if (entry != buf + i && entry[0] != '#') {
9ff768e9 100 buf[i] = 0;
fee88256 101 add_exclude(entry, base, baselen, which);
9ff768e9
NP
102 }
103 entry = buf + i + 1;
104 }
105 }
f87f9497
JH
106 return 0;
107
108 err:
109 if (0 <= fd)
110 close(fd);
111 return -1;
112}
113
114static void add_excludes_from_file(const char *fname)
115{
fee88256
JH
116 if (add_excludes_from_file_1(fname, "", 0,
117 &exclude_list[EXC_FILE]) < 0)
f87f9497
JH
118 die("cannot use %s as an exclude file", fname);
119}
120
121static int push_exclude_per_directory(const char *base, int baselen)
122{
123 char exclude_file[PATH_MAX];
fee88256
JH
124 struct exclude_list *el = &exclude_list[EXC_DIRS];
125 int current_nr = el->nr;
f87f9497
JH
126
127 if (exclude_per_dir) {
128 memcpy(exclude_file, base, baselen);
129 strcpy(exclude_file + baselen, exclude_per_dir);
fee88256 130 add_excludes_from_file_1(exclude_file, base, baselen, el);
f87f9497
JH
131 }
132 return current_nr;
133}
9ff768e9 134
f87f9497
JH
135static void pop_exclude_per_directory(int stk)
136{
fee88256
JH
137 struct exclude_list *el = &exclude_list[EXC_DIRS];
138
139 while (stk < el->nr)
140 free(el->excludes[--el->nr]);
9ff768e9
NP
141}
142
fee88256
JH
143/* Scan the list and let the last match determines the fate.
144 * Return 1 for exclude, 0 for include and -1 for undecided.
145 */
146static int excluded_1(const char *pathname,
147 int pathlen,
148 struct exclude_list *el)
9ff768e9
NP
149{
150 int i;
f87f9497 151
fee88256
JH
152 if (el->nr) {
153 for (i = el->nr - 1; 0 <= i; i--) {
154 struct exclude *x = el->excludes[i];
f87f9497
JH
155 const char *exclude = x->pattern;
156 int to_exclude = 1;
157
158 if (*exclude == '!') {
159 to_exclude = 0;
160 exclude++;
161 }
162
163 if (!strchr(exclude, '/')) {
164 /* match basename */
165 const char *basename = strrchr(pathname, '/');
166 basename = (basename) ? basename+1 : pathname;
167 if (fnmatch(exclude, basename, 0) == 0)
168 return to_exclude;
169 }
170 else {
171 /* match with FNM_PATHNAME:
172 * exclude has base (baselen long) inplicitly
173 * in front of it.
174 */
175 int baselen = x->baselen;
176 if (*exclude == '/')
177 exclude++;
178
179 if (pathlen < baselen ||
180 (baselen && pathname[baselen-1] != '/') ||
181 strncmp(pathname, x->base, baselen))
182 continue;
183
184 if (fnmatch(exclude, pathname+baselen,
185 FNM_PATHNAME) == 0)
186 return to_exclude;
187 }
188 }
9ff768e9 189 }
fee88256
JH
190 return -1; /* undecided */
191}
192
193static int excluded(const char *pathname)
194{
195 int pathlen = strlen(pathname);
196 int st;
197
198 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
199 switch (excluded_1(pathname, pathlen, &exclude_list[st])) {
200 case 0:
201 return 0;
202 case 1:
203 return 1;
204 }
205 }
9ff768e9
NP
206 return 0;
207}
208
6ca45943
JH
209struct nond_on_fs {
210 int len;
2c04662d 211 char name[0];
6ca45943
JH
212};
213
214static struct nond_on_fs **dir;
8695c8bf
LT
215static int nr_dir;
216static int dir_alloc;
217
218static void add_name(const char *pathname, int len)
219{
6ca45943 220 struct nond_on_fs *ent;
8695c8bf
LT
221
222 if (cache_name_pos(pathname, len) >= 0)
223 return;
224
225 if (nr_dir == dir_alloc) {
226 dir_alloc = alloc_nr(dir_alloc);
6ca45943 227 dir = xrealloc(dir, dir_alloc*sizeof(ent));
8695c8bf 228 }
6ca45943
JH
229 ent = xmalloc(sizeof(*ent) + len + 1);
230 ent->len = len;
231 memcpy(ent->name, pathname, len);
5be4efbe 232 ent->name[len] = 0;
6ca45943 233 dir[nr_dir++] = ent;
8695c8bf
LT
234}
235
236/*
237 * Read a directory tree. We currently ignore anything but
a15c1c60
JH
238 * directories, regular files and symlinks. That's because git
239 * doesn't handle them at all yet. Maybe that will change some
240 * day.
8695c8bf 241 *
f87f9497 242 * Also, we ignore the name ".git" (even if it is not a directory).
aebb2679 243 * That likely will not change.
8695c8bf
LT
244 */
245static void read_directory(const char *path, const char *base, int baselen)
246{
247 DIR *dir = opendir(path);
248
249 if (dir) {
f87f9497 250 int exclude_stk;
8695c8bf
LT
251 struct dirent *de;
252 char fullname[MAXPATHLEN + 1];
253 memcpy(fullname, base, baselen);
254
f87f9497
JH
255 exclude_stk = push_exclude_per_directory(base, baselen);
256
8695c8bf
LT
257 while ((de = readdir(dir)) != NULL) {
258 int len;
259
c4ee2952
JH
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")))
8695c8bf
LT
264 continue;
265 len = strlen(de->d_name);
266 memcpy(fullname + baselen, de->d_name, len+1);
f87f9497
JH
267 if (excluded(fullname) != show_ignored)
268 continue;
8695c8bf 269
b6829693 270 switch (DTYPE(de)) {
8695c8bf
LT
271 struct stat st;
272 default:
273 continue;
274 case DT_UNKNOWN:
275 if (lstat(fullname, &st))
276 continue;
a15c1c60 277 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
8695c8bf
LT
278 break;
279 if (!S_ISDIR(st.st_mode))
280 continue;
281 /* fallthrough */
282 case DT_DIR:
283 memcpy(fullname + baselen + len, "/", 2);
20d37ef6
PB
284 read_directory(fullname, fullname,
285 baselen + len + 1);
8695c8bf
LT
286 continue;
287 case DT_REG:
a15c1c60 288 case DT_LNK:
8695c8bf
LT
289 break;
290 }
291 add_name(fullname, baselen + len);
292 }
293 closedir(dir);
f87f9497
JH
294
295 pop_exclude_per_directory(exclude_stk);
8695c8bf
LT
296 }
297}
298
299static int cmp_name(const void *p1, const void *p2)
300{
6ca45943
JH
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;
303
304 return cache_name_compare(e1->name, e1->len,
305 e2->name, e2->len);
306}
8695c8bf 307
56fc5108
LT
308/*
309 * Match a pathspec against a filename. The first "len" characters
310 * are the common prefix
311 */
312static int match(const char **spec, const char *filename, int len)
313{
314 const char *m;
315
316 while ((m = *spec++) != NULL) {
317 int matchlen = strlen(m + len);
318
319 if (!matchlen)
320 return 1;
321 if (!strncmp(m + len, filename + len, matchlen)) {
322 if (m[len + matchlen - 1] == '/')
323 return 1;
324 switch (filename[len + matchlen]) {
325 case '/': case '\0':
326 return 1;
327 }
328 }
329 if (!fnmatch(m + len, filename + len, 0))
330 return 1;
331 }
332 return 0;
333}
334
5be4efbe
LT
335static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
336{
337 int len = prefix_len;
338 int offset = prefix_offset;
339
340 if (len >= ent->len)
341 die("git-ls-files: internal error - directory entry not superset of prefix");
342
56fc5108 343 if (pathspec && !match(pathspec, ent->name, len))
5be4efbe
LT
344 return;
345
22ddf719
JH
346 fputs(tag, stdout);
347 write_name_quoted("", ent->name + offset, line_terminator, stdout);
348 putchar(line_terminator);
5be4efbe
LT
349}
350
e99d59ff 351static void show_killed_files(void)
6ca45943
JH
352{
353 int i;
354 for (i = 0; i < nr_dir; i++) {
355 struct nond_on_fs *ent = dir[i];
356 char *cp, *sp;
357 int pos, len, killed = 0;
358
359 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
360 sp = strchr(cp, '/');
361 if (!sp) {
362 /* If ent->name is prefix of an entry in the
363 * cache, it will be killed.
364 */
365 pos = cache_name_pos(ent->name, ent->len);
366 if (0 <= pos)
367 die("bug in show-killed-files");
368 pos = -pos - 1;
369 while (pos < active_nr &&
370 ce_stage(active_cache[pos]))
371 pos++; /* skip unmerged */
372 if (active_nr <= pos)
373 break;
374 /* pos points at a name immediately after
375 * ent->name in the cache. Does it expect
376 * ent->name to be a directory?
377 */
378 len = ce_namelen(active_cache[pos]);
379 if ((ent->len < len) &&
380 !strncmp(active_cache[pos]->name,
381 ent->name, ent->len) &&
382 active_cache[pos]->name[ent->len] == '/')
383 killed = 1;
384 break;
385 }
386 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
387 /* If any of the leading directories in
388 * ent->name is registered in the cache,
389 * ent->name will be killed.
390 */
391 killed = 1;
392 break;
393 }
394 }
395 if (killed)
5be4efbe 396 show_dir_entry(tag_killed, dir[i]);
6ca45943 397 }
8695c8bf
LT
398}
399
5be4efbe
LT
400static void show_ce_entry(const char *tag, struct cache_entry *ce)
401{
402 int len = prefix_len;
403 int offset = prefix_offset;
404
405 if (len >= ce_namelen(ce))
406 die("git-ls-files: internal error - cache entry not superset of prefix");
407
56fc5108 408 if (pathspec && !match(pathspec, ce->name, len))
5be4efbe
LT
409 return;
410
22ddf719
JH
411 if (!show_stage) {
412 fputs(tag, stdout);
413 write_name_quoted("", ce->name + offset, line_terminator, stdout);
414 putchar(line_terminator);
415 }
416 else {
417 printf("%s%06o %s %d\t",
5be4efbe
LT
418 tag,
419 ntohl(ce->ce_mode),
420 sha1_to_hex(ce->sha1),
22ddf719
JH
421 ce_stage(ce));
422 write_name_quoted("", ce->name + offset, line_terminator, stdout);
423 putchar(line_terminator);
424 }
5be4efbe
LT
425}
426
8695c8bf
LT
427static void show_files(void)
428{
429 int i;
430
431 /* For cached/deleted files we don't need to even do the readdir */
6ca45943 432 if (show_others || show_killed) {
5be4efbe
LT
433 const char *path = ".", *base = "";
434 int baselen = prefix_len;
435
436 if (baselen)
437 path = base = prefix;
438 read_directory(path, base, baselen);
6ca45943
JH
439 qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
440 if (show_others)
441 for (i = 0; i < nr_dir; i++)
5be4efbe 442 show_dir_entry(tag_other, dir[i]);
6ca45943
JH
443 if (show_killed)
444 show_killed_files();
8695c8bf 445 }
aee46198 446 if (show_cached | show_stage) {
8695c8bf
LT
447 for (i = 0; i < active_nr; i++) {
448 struct cache_entry *ce = active_cache[i];
9ff768e9
NP
449 if (excluded(ce->name) != show_ignored)
450 continue;
eec8c633
LT
451 if (show_unmerged && !ce_stage(ce))
452 continue;
5be4efbe 453 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
8695c8bf
LT
454 }
455 }
b0391890 456 if (show_deleted | show_modified) {
8695c8bf
LT
457 for (i = 0; i < active_nr; i++) {
458 struct cache_entry *ce = active_cache[i];
459 struct stat st;
b0391890 460 int err;
9ff768e9
NP
461 if (excluded(ce->name) != show_ignored)
462 continue;
b0391890
JH
463 err = lstat(ce->name, &st);
464 if (show_deleted && err)
465 show_ce_entry(tag_removed, ce);
466 if (show_modified && ce_modified(ce, &st))
467 show_ce_entry(tag_modified, ce);
5be4efbe
LT
468 }
469 }
470}
471
472/*
473 * Prune the index to only contain stuff starting with "prefix"
474 */
475static void prune_cache(void)
476{
477 int pos = cache_name_pos(prefix, prefix_len);
478 unsigned int first, last;
479
480 if (pos < 0)
481 pos = -pos-1;
482 active_cache += pos;
483 active_nr -= pos;
484 first = 0;
485 last = active_nr;
486 while (last > first) {
487 int next = (last + first) >> 1;
488 struct cache_entry *ce = active_cache[next];
489 if (!strncmp(ce->name, prefix, prefix_len)) {
490 first = next+1;
491 continue;
8695c8bf 492 }
5be4efbe
LT
493 last = next;
494 }
495 active_nr = last;
496}
497
56fc5108 498static void verify_pathspec(void)
5be4efbe 499{
56fc5108
LT
500 const char **p, *n, *prev;
501 char *real_prefix;
502 unsigned long max;
503
504 prev = NULL;
505 max = PATH_MAX;
506 for (p = pathspec; (n = *p) != NULL; p++) {
507 int i, len = 0;
508 for (i = 0; i < max; i++) {
509 char c = n[i];
510 if (prev && prev[i] != c)
511 break;
56906143 512 if (!c || c == '*' || c == '?')
56fc5108
LT
513 break;
514 if (c == '/')
515 len = i+1;
516 }
517 prev = n;
518 if (len < max) {
519 max = len;
520 if (!max)
521 break;
522 }
5be4efbe 523 }
56fc5108
LT
524
525 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
526 die("git-ls-files: cannot generate relative filenames containing '..'");
527
528 real_prefix = NULL;
529 prefix_len = max;
530 if (max) {
531 real_prefix = xmalloc(max + 1);
532 memcpy(real_prefix, prev, max);
533 real_prefix[max] = 0;
8695c8bf 534 }
56fc5108 535 prefix = real_prefix;
8695c8bf
LT
536}
537
4d1f1190 538static const char ls_files_usage[] =
b0391890 539 "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
f87f9497 540 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
500b97e4 541 "[ --exclude-per-directory=<filename> ] [--] [<file>]*";
cf9a113d 542
6b5ee137 543int main(int argc, const char **argv)
8695c8bf
LT
544{
545 int i;
fee88256 546 int exc_given = 0;
8695c8bf 547
5be4efbe
LT
548 prefix = setup_git_directory();
549 if (prefix)
56fc5108 550 prefix_offset = strlen(prefix);
5be4efbe 551
8695c8bf 552 for (i = 1; i < argc; i++) {
6b5ee137 553 const char *arg = argv[i];
8695c8bf 554
500b97e4
FK
555 if (!strcmp(arg, "--")) {
556 i++;
557 break;
558 }
b83c8345
JH
559 if (!strcmp(arg, "-z")) {
560 line_terminator = 0;
5be4efbe
LT
561 continue;
562 }
563 if (!strcmp(arg, "-t")) {
20d37ef6
PB
564 tag_cached = "H ";
565 tag_unmerged = "M ";
566 tag_removed = "R ";
b0391890 567 tag_modified = "C ";
20d37ef6 568 tag_other = "? ";
6ca45943 569 tag_killed = "K ";
5be4efbe
LT
570 continue;
571 }
572 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
8695c8bf 573 show_cached = 1;
5be4efbe
LT
574 continue;
575 }
576 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
8695c8bf 577 show_deleted = 1;
5be4efbe
LT
578 continue;
579 }
b0391890
JH
580 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
581 show_modified = 1;
582 continue;
583 }
5be4efbe 584 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
8695c8bf 585 show_others = 1;
5be4efbe
LT
586 continue;
587 }
588 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
8695c8bf 589 show_ignored = 1;
5be4efbe
LT
590 continue;
591 }
592 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
aee46198 593 show_stage = 1;
5be4efbe
LT
594 continue;
595 }
596 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
6ca45943 597 show_killed = 1;
5be4efbe
LT
598 continue;
599 }
600 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
20d37ef6
PB
601 /* There's no point in showing unmerged unless
602 * you also show the stage information.
603 */
eec8c633
LT
604 show_stage = 1;
605 show_unmerged = 1;
5be4efbe
LT
606 continue;
607 }
608 if (!strcmp(arg, "-x") && i+1 < argc) {
fee88256
JH
609 exc_given = 1;
610 add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]);
5be4efbe
LT
611 continue;
612 }
613 if (!strncmp(arg, "--exclude=", 10)) {
fee88256
JH
614 exc_given = 1;
615 add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]);
5be4efbe
LT
616 continue;
617 }
618 if (!strcmp(arg, "-X") && i+1 < argc) {
fee88256 619 exc_given = 1;
9ff768e9 620 add_excludes_from_file(argv[++i]);
5be4efbe
LT
621 continue;
622 }
623 if (!strncmp(arg, "--exclude-from=", 15)) {
fee88256 624 exc_given = 1;
9ff768e9 625 add_excludes_from_file(arg+15);
5be4efbe
LT
626 continue;
627 }
628 if (!strncmp(arg, "--exclude-per-directory=", 24)) {
fee88256 629 exc_given = 1;
f87f9497 630 exclude_per_dir = arg + 24;
5be4efbe
LT
631 continue;
632 }
633 if (!strcmp(arg, "--full-name")) {
634 prefix_offset = 0;
635 continue;
636 }
56fc5108 637 if (*arg == '-')
17710391 638 usage(ls_files_usage);
56fc5108 639 break;
9ff768e9
NP
640 }
641
56fc5108
LT
642 pathspec = get_pathspec(prefix, argv + i);
643
644 /* Verify that the pathspec matches the prefix */
645 if (pathspec)
646 verify_pathspec();
5be4efbe 647
fee88256 648 if (show_ignored && !exc_given) {
20d37ef6
PB
649 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
650 argv[0]);
9ff768e9 651 exit(1);
8695c8bf
LT
652 }
653
654 /* With no flags, we default to showing the cached files */
b0391890
JH
655 if (!(show_stage | show_deleted | show_others | show_unmerged |
656 show_killed | show_modified))
8695c8bf
LT
657 show_cached = 1;
658
659 read_cache();
5be4efbe
LT
660 if (prefix)
661 prune_cache();
8695c8bf
LT
662 show_files();
663 return 0;
664}