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