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