]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-ls-files.c
autoconf: Add tests for memmem, strtoumax and mkdtemp functions
[thirdparty/git.git] / builtin-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 */
8695c8bf 8#include "cache.h"
22ddf719 9#include "quote.h"
453ec4bd 10#include "dir.h"
0864f264 11#include "builtin.h"
64586e75 12#include "tree.h"
8695c8bf 13
96f1e58f
DR
14static int abbrev;
15static int show_deleted;
16static int show_cached;
17static int show_others;
18static int show_stage;
19static int show_unmerged;
20static int show_modified;
21static int show_killed;
22static int show_valid_bit;
b83c8345 23static int line_terminator = '\n';
8695c8bf 24
96f1e58f
DR
25static int prefix_len;
26static int prefix_offset;
27static const char **pathspec;
28static int error_unmatch;
29static char *ps_matched;
64586e75 30static const char *with_tree;
5be4efbe 31
20d37ef6
PB
32static const char *tag_cached = "";
33static const char *tag_unmerged = "";
34static const char *tag_removed = "";
35static const char *tag_other = "";
6ca45943 36static const char *tag_killed = "";
b0391890 37static const char *tag_modified = "";
20d37ef6 38
8695c8bf 39
56fc5108
LT
40/*
41 * Match a pathspec against a filename. The first "len" characters
42 * are the common prefix
43 */
bba319b5
JH
44static int match(const char **spec, char *ps_matched,
45 const char *filename, int len)
56fc5108
LT
46{
47 const char *m;
48
49 while ((m = *spec++) != NULL) {
50 int matchlen = strlen(m + len);
51
52 if (!matchlen)
bba319b5 53 goto matched;
56fc5108
LT
54 if (!strncmp(m + len, filename + len, matchlen)) {
55 if (m[len + matchlen - 1] == '/')
bba319b5 56 goto matched;
56fc5108
LT
57 switch (filename[len + matchlen]) {
58 case '/': case '\0':
bba319b5 59 goto matched;
56fc5108
LT
60 }
61 }
62 if (!fnmatch(m + len, filename + len, 0))
bba319b5
JH
63 goto matched;
64 if (ps_matched)
65 ps_matched++;
66 continue;
67 matched:
68 if (ps_matched)
69 *ps_matched = 1;
70 return 1;
56fc5108
LT
71 }
72 return 0;
73}
74
453ec4bd 75static void show_dir_entry(const char *tag, struct dir_entry *ent)
5be4efbe
LT
76{
77 int len = prefix_len;
78 int offset = prefix_offset;
79
80 if (len >= ent->len)
81 die("git-ls-files: internal error - directory entry not superset of prefix");
82
bba319b5 83 if (pathspec && !match(pathspec, ps_matched, ent->name, len))
5be4efbe
LT
84 return;
85
22ddf719 86 fputs(tag, stdout);
663af342 87 write_name_quoted(ent->name + offset, stdout, line_terminator);
5be4efbe
LT
88}
89
453ec4bd 90static void show_other_files(struct dir_struct *dir)
fcbc3083
JH
91{
92 int i;
5698454e
LT
93
94
95 /*
96 * Skip matching and unmerged entries for the paths,
97 * since we want just "others".
98 *
99 * (Matching entries are normally pruned during
100 * the directory tree walk, but will show up for
101 * gitlinks because we don't necessarily have
102 * dir->show_other_directories set to suppress
103 * them).
104 */
453ec4bd 105 for (i = 0; i < dir->nr; i++) {
453ec4bd 106 struct dir_entry *ent = dir->entries[i];
5698454e 107 int len, pos;
fcbc3083 108 struct cache_entry *ce;
5698454e
LT
109
110 /*
111 * Remove the '/' at the end that directory
112 * walking adds for directory entries.
113 */
114 len = ent->len;
115 if (len && ent->name[len-1] == '/')
116 len--;
117 pos = cache_name_pos(ent->name, len);
fcbc3083 118 if (0 <= pos)
5698454e 119 continue; /* exact match */
fcbc3083 120 pos = -pos - 1;
a6080a0a 121 if (pos < active_nr) {
fcbc3083 122 ce = active_cache[pos];
5698454e
LT
123 if (ce_namelen(ce) == len &&
124 !memcmp(ce->name, ent->name, len))
fcbc3083
JH
125 continue; /* Yup, this one exists unmerged */
126 }
127 show_dir_entry(tag_other, ent);
128 }
129}
130
453ec4bd 131static void show_killed_files(struct dir_struct *dir)
6ca45943
JH
132{
133 int i;
453ec4bd
LT
134 for (i = 0; i < dir->nr; i++) {
135 struct dir_entry *ent = dir->entries[i];
6ca45943
JH
136 char *cp, *sp;
137 int pos, len, killed = 0;
138
139 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
140 sp = strchr(cp, '/');
141 if (!sp) {
142 /* If ent->name is prefix of an entry in the
143 * cache, it will be killed.
144 */
145 pos = cache_name_pos(ent->name, ent->len);
146 if (0 <= pos)
147 die("bug in show-killed-files");
148 pos = -pos - 1;
149 while (pos < active_nr &&
150 ce_stage(active_cache[pos]))
151 pos++; /* skip unmerged */
152 if (active_nr <= pos)
153 break;
154 /* pos points at a name immediately after
155 * ent->name in the cache. Does it expect
156 * ent->name to be a directory?
157 */
158 len = ce_namelen(active_cache[pos]);
159 if ((ent->len < len) &&
160 !strncmp(active_cache[pos]->name,
161 ent->name, ent->len) &&
162 active_cache[pos]->name[ent->len] == '/')
163 killed = 1;
164 break;
165 }
166 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
167 /* If any of the leading directories in
168 * ent->name is registered in the cache,
169 * ent->name will be killed.
170 */
171 killed = 1;
172 break;
173 }
174 }
175 if (killed)
453ec4bd 176 show_dir_entry(tag_killed, dir->entries[i]);
6ca45943 177 }
8695c8bf
LT
178}
179
5be4efbe
LT
180static void show_ce_entry(const char *tag, struct cache_entry *ce)
181{
182 int len = prefix_len;
183 int offset = prefix_offset;
184
185 if (len >= ce_namelen(ce))
186 die("git-ls-files: internal error - cache entry not superset of prefix");
187
bba319b5 188 if (pathspec && !match(pathspec, ps_matched, ce->name, len))
5be4efbe
LT
189 return;
190
8bb2e03b
JH
191 if (tag && *tag && show_valid_bit &&
192 (ce->ce_flags & htons(CE_VALID))) {
2bcab240
JH
193 static char alttag[4];
194 memcpy(alttag, tag, 3);
195 if (isalpha(tag[0]))
196 alttag[0] = tolower(tag[0]);
197 else if (tag[0] == '?')
198 alttag[0] = '!';
199 else {
200 alttag[0] = 'v';
201 alttag[1] = tag[0];
202 alttag[2] = ' ';
203 alttag[3] = 0;
204 }
205 tag = alttag;
206 }
207
22ddf719
JH
208 if (!show_stage) {
209 fputs(tag, stdout);
663af342 210 } else {
22ddf719 211 printf("%s%06o %s %d\t",
5be4efbe
LT
212 tag,
213 ntohl(ce->ce_mode),
ad0cae4c
EW
214 abbrev ? find_unique_abbrev(ce->sha1,abbrev)
215 : sha1_to_hex(ce->sha1),
22ddf719 216 ce_stage(ce));
22ddf719 217 }
663af342 218 write_name_quoted(ce->name + offset, stdout, line_terminator);
5be4efbe
LT
219}
220
3e04228b 221static void show_files(struct dir_struct *dir, const char *prefix)
8695c8bf
LT
222{
223 int i;
224
225 /* For cached/deleted files we don't need to even do the readdir */
6ca45943 226 if (show_others || show_killed) {
5be4efbe
LT
227 const char *path = ".", *base = "";
228 int baselen = prefix_len;
229
b4189aa8 230 if (baselen)
5be4efbe 231 path = base = prefix;
9fc42d60 232 read_directory(dir, path, base, baselen, pathspec);
6ca45943 233 if (show_others)
453ec4bd 234 show_other_files(dir);
6ca45943 235 if (show_killed)
453ec4bd 236 show_killed_files(dir);
8695c8bf 237 }
aee46198 238 if (show_cached | show_stage) {
8695c8bf
LT
239 for (i = 0; i < active_nr; i++) {
240 struct cache_entry *ce = active_cache[i];
453ec4bd 241 if (excluded(dir, ce->name) != dir->show_ignored)
9ff768e9 242 continue;
eec8c633
LT
243 if (show_unmerged && !ce_stage(ce))
244 continue;
64586e75
JH
245 if (ce->ce_flags & htons(CE_UPDATE))
246 continue;
5be4efbe 247 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
8695c8bf
LT
248 }
249 }
b0391890 250 if (show_deleted | show_modified) {
8695c8bf
LT
251 for (i = 0; i < active_nr; i++) {
252 struct cache_entry *ce = active_cache[i];
253 struct stat st;
b0391890 254 int err;
453ec4bd 255 if (excluded(dir, ce->name) != dir->show_ignored)
9ff768e9 256 continue;
b0391890
JH
257 err = lstat(ce->name, &st);
258 if (show_deleted && err)
259 show_ce_entry(tag_removed, ce);
2bcab240 260 if (show_modified && ce_modified(ce, &st, 0))
b0391890 261 show_ce_entry(tag_modified, ce);
5be4efbe
LT
262 }
263 }
264}
265
266/*
267 * Prune the index to only contain stuff starting with "prefix"
268 */
3e04228b 269static void prune_cache(const char *prefix)
5be4efbe
LT
270{
271 int pos = cache_name_pos(prefix, prefix_len);
272 unsigned int first, last;
273
274 if (pos < 0)
275 pos = -pos-1;
95af39fc
KP
276 memmove(active_cache, active_cache + pos,
277 (active_nr - pos) * sizeof(struct cache_entry *));
5be4efbe
LT
278 active_nr -= pos;
279 first = 0;
280 last = active_nr;
281 while (last > first) {
282 int next = (last + first) >> 1;
283 struct cache_entry *ce = active_cache[next];
284 if (!strncmp(ce->name, prefix, prefix_len)) {
285 first = next+1;
286 continue;
8695c8bf 287 }
5be4efbe
LT
288 last = next;
289 }
290 active_nr = last;
291}
292
3e04228b 293static const char *verify_pathspec(const char *prefix)
5be4efbe 294{
56fc5108 295 const char **p, *n, *prev;
56fc5108
LT
296 unsigned long max;
297
298 prev = NULL;
299 max = PATH_MAX;
300 for (p = pathspec; (n = *p) != NULL; p++) {
301 int i, len = 0;
302 for (i = 0; i < max; i++) {
303 char c = n[i];
304 if (prev && prev[i] != c)
305 break;
56906143 306 if (!c || c == '*' || c == '?')
56fc5108
LT
307 break;
308 if (c == '/')
309 len = i+1;
310 }
311 prev = n;
312 if (len < max) {
313 max = len;
314 if (!max)
315 break;
316 }
5be4efbe 317 }
56fc5108
LT
318
319 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
320 die("git-ls-files: cannot generate relative filenames containing '..'");
321
56fc5108 322 prefix_len = max;
182af834 323 return max ? xmemdupz(prev, max) : NULL;
8695c8bf
LT
324}
325
64586e75
JH
326/*
327 * Read the tree specified with --with-tree option
328 * (typically, HEAD) into stage #1 and then
329 * squash them down to stage #0. This is used for
330 * --error-unmatch to list and check the path patterns
331 * that were given from the command line. We are not
332 * going to write this index out.
333 */
334static void overlay_tree(const char *tree_name, const char *prefix)
335{
336 struct tree *tree;
337 unsigned char sha1[20];
338 const char **match;
339 struct cache_entry *last_stage0 = NULL;
340 int i;
341
342 if (get_sha1(tree_name, sha1))
343 die("tree-ish %s not found.", tree_name);
344 tree = parse_tree_indirect(sha1);
345 if (!tree)
346 die("bad tree-ish %s", tree_name);
347
348 /* Hoist the unmerged entries up to stage #3 to make room */
349 for (i = 0; i < active_nr; i++) {
350 struct cache_entry *ce = active_cache[i];
351 if (!ce_stage(ce))
352 continue;
353 ce->ce_flags |= htons(CE_STAGEMASK);
354 }
355
356 if (prefix) {
357 static const char *(matchbuf[2]);
358 matchbuf[0] = prefix;
359 matchbuf [1] = NULL;
360 match = matchbuf;
361 } else
362 match = NULL;
363 if (read_tree(tree, 1, match))
364 die("unable to read tree entries %s", tree_name);
365
366 for (i = 0; i < active_nr; i++) {
367 struct cache_entry *ce = active_cache[i];
368 switch (ce_stage(ce)) {
369 case 0:
370 last_stage0 = ce;
371 /* fallthru */
372 default:
373 continue;
374 case 1:
375 /*
376 * If there is stage #0 entry for this, we do not
377 * need to show it. We use CE_UPDATE bit to mark
378 * such an entry.
379 */
380 if (last_stage0 &&
381 !strcmp(last_stage0->name, ce->name))
382 ce->ce_flags |= htons(CE_UPDATE);
383 }
384 }
385}
386
4d1f1190 387static const char ls_files_usage[] =
8bb2e03b 388 "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
f87f9497 389 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
8e7b07c8
JK
390 "[ --exclude-per-directory=<filename> ] [--exclude-standard] "
391 "[--full-name] [--abbrev] [--] [<file>]*";
cf9a113d 392
a633fca0 393int cmd_ls_files(int argc, const char **argv, const char *prefix)
8695c8bf
LT
394{
395 int i;
6d9ba67b 396 int exc_given = 0, require_work_tree = 0;
453ec4bd 397 struct dir_struct dir;
8695c8bf 398
453ec4bd 399 memset(&dir, 0, sizeof(dir));
5be4efbe 400 if (prefix)
56fc5108 401 prefix_offset = strlen(prefix);
39b4ac99 402 git_config(git_default_config);
5be4efbe 403
8695c8bf 404 for (i = 1; i < argc; i++) {
6b5ee137 405 const char *arg = argv[i];
8695c8bf 406
500b97e4
FK
407 if (!strcmp(arg, "--")) {
408 i++;
409 break;
410 }
b83c8345
JH
411 if (!strcmp(arg, "-z")) {
412 line_terminator = 0;
5be4efbe
LT
413 continue;
414 }
8bb2e03b 415 if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) {
20d37ef6
PB
416 tag_cached = "H ";
417 tag_unmerged = "M ";
418 tag_removed = "R ";
b0391890 419 tag_modified = "C ";
20d37ef6 420 tag_other = "? ";
6ca45943 421 tag_killed = "K ";
8bb2e03b
JH
422 if (arg[1] == 'v')
423 show_valid_bit = 1;
5be4efbe
LT
424 continue;
425 }
426 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
8695c8bf 427 show_cached = 1;
5be4efbe
LT
428 continue;
429 }
430 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
8695c8bf 431 show_deleted = 1;
5be4efbe
LT
432 continue;
433 }
b0391890
JH
434 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
435 show_modified = 1;
6d9ba67b 436 require_work_tree = 1;
b0391890
JH
437 continue;
438 }
5be4efbe 439 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
8695c8bf 440 show_others = 1;
6d9ba67b 441 require_work_tree = 1;
5be4efbe
LT
442 continue;
443 }
444 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
453ec4bd 445 dir.show_ignored = 1;
6d9ba67b 446 require_work_tree = 1;
5be4efbe
LT
447 continue;
448 }
449 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
aee46198 450 show_stage = 1;
5be4efbe
LT
451 continue;
452 }
453 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
6ca45943 454 show_killed = 1;
6d9ba67b 455 require_work_tree = 1;
5be4efbe
LT
456 continue;
457 }
9518eb26 458 if (!strcmp(arg, "--directory")) {
453ec4bd 459 dir.show_other_directories = 1;
9518eb26
LT
460 continue;
461 }
b0a3de42 462 if (!strcmp(arg, "--no-empty-directory")) {
453ec4bd 463 dir.hide_empty_directories = 1;
b0a3de42
PB
464 continue;
465 }
5be4efbe 466 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
20d37ef6
PB
467 /* There's no point in showing unmerged unless
468 * you also show the stage information.
469 */
eec8c633
LT
470 show_stage = 1;
471 show_unmerged = 1;
5be4efbe
LT
472 continue;
473 }
474 if (!strcmp(arg, "-x") && i+1 < argc) {
fee88256 475 exc_given = 1;
453ec4bd 476 add_exclude(argv[++i], "", 0, &dir.exclude_list[EXC_CMDL]);
5be4efbe
LT
477 continue;
478 }
cc44c765 479 if (!prefixcmp(arg, "--exclude=")) {
fee88256 480 exc_given = 1;
453ec4bd 481 add_exclude(arg+10, "", 0, &dir.exclude_list[EXC_CMDL]);
5be4efbe
LT
482 continue;
483 }
484 if (!strcmp(arg, "-X") && i+1 < argc) {
fee88256 485 exc_given = 1;
453ec4bd 486 add_excludes_from_file(&dir, argv[++i]);
5be4efbe
LT
487 continue;
488 }
cc44c765 489 if (!prefixcmp(arg, "--exclude-from=")) {
fee88256 490 exc_given = 1;
453ec4bd 491 add_excludes_from_file(&dir, arg+15);
5be4efbe
LT
492 continue;
493 }
cc44c765 494 if (!prefixcmp(arg, "--exclude-per-directory=")) {
fee88256 495 exc_given = 1;
453ec4bd 496 dir.exclude_per_dir = arg + 24;
5be4efbe
LT
497 continue;
498 }
8e7b07c8
JK
499 if (!strcmp(arg, "--exclude-standard")) {
500 exc_given = 1;
501 setup_standard_excludes(&dir);
502 continue;
503 }
5be4efbe
LT
504 if (!strcmp(arg, "--full-name")) {
505 prefix_offset = 0;
506 continue;
507 }
bba319b5
JH
508 if (!strcmp(arg, "--error-unmatch")) {
509 error_unmatch = 1;
510 continue;
511 }
64586e75
JH
512 if (!prefixcmp(arg, "--with-tree=")) {
513 with_tree = arg + 12;
514 continue;
515 }
cc44c765 516 if (!prefixcmp(arg, "--abbrev=")) {
ad0cae4c
EW
517 abbrev = strtoul(arg+9, NULL, 10);
518 if (abbrev && abbrev < MINIMUM_ABBREV)
519 abbrev = MINIMUM_ABBREV;
520 else if (abbrev > 40)
521 abbrev = 40;
522 continue;
523 }
524 if (!strcmp(arg, "--abbrev")) {
525 abbrev = DEFAULT_ABBREV;
526 continue;
527 }
56fc5108 528 if (*arg == '-')
17710391 529 usage(ls_files_usage);
56fc5108 530 break;
9ff768e9
NP
531 }
532
7d8ae932
MH
533 if (require_work_tree && !is_inside_work_tree())
534 setup_work_tree();
6d9ba67b 535
56fc5108
LT
536 pathspec = get_pathspec(prefix, argv + i);
537
538 /* Verify that the pathspec matches the prefix */
539 if (pathspec)
3e04228b 540 prefix = verify_pathspec(prefix);
5be4efbe 541
bba319b5
JH
542 /* Treat unmatching pathspec elements as errors */
543 if (pathspec && error_unmatch) {
544 int num;
545 for (num = 0; pathspec[num]; num++)
546 ;
547 ps_matched = xcalloc(1, num);
548 }
549
453ec4bd 550 if (dir.show_ignored && !exc_given) {
20d37ef6
PB
551 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
552 argv[0]);
9ff768e9 553 exit(1);
8695c8bf
LT
554 }
555
556 /* With no flags, we default to showing the cached files */
b0391890
JH
557 if (!(show_stage | show_deleted | show_others | show_unmerged |
558 show_killed | show_modified))
8695c8bf
LT
559 show_cached = 1;
560
561 read_cache();
5be4efbe 562 if (prefix)
3e04228b 563 prune_cache(prefix);
64586e75
JH
564 if (with_tree) {
565 /*
566 * Basic sanity check; show-stages and show-unmerged
567 * would not make any sense with this option.
568 */
569 if (show_stage || show_unmerged)
570 die("ls-files --with-tree is incompatible with -s or -u");
571 overlay_tree(with_tree, prefix);
572 }
3e04228b 573 show_files(&dir, prefix);
bba319b5
JH
574
575 if (ps_matched) {
576 /* We need to make sure all pathspec matched otherwise
577 * it is an error.
578 */
579 int num, errors = 0;
580 for (num = 0; pathspec[num]; num++) {
93e23fea
JH
581 int other, found_dup;
582
bba319b5
JH
583 if (ps_matched[num])
584 continue;
93e23fea
JH
585 /*
586 * The caller might have fed identical pathspec
587 * twice. Do not barf on such a mistake.
588 */
589 for (found_dup = other = 0;
590 !found_dup && pathspec[other];
591 other++) {
592 if (other == num || !ps_matched[other])
593 continue;
594 if (!strcmp(pathspec[other], pathspec[num]))
595 /*
596 * Ok, we have a match already.
597 */
598 found_dup = 1;
599 }
600 if (found_dup)
601 continue;
602
ced7b828 603 error("pathspec '%s' did not match any file(s) known to git.",
6becd7da 604 pathspec[num] + prefix_offset);
c8af25ca 605 errors++;
bba319b5 606 }
ced7b828
AE
607
608 if (errors)
609 fprintf(stderr, "Did you forget to 'git add'?\n");
610
bba319b5
JH
611 return errors ? 1 : 0;
612 }
613
8695c8bf
LT
614 return 0;
615}