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 "cache.h"
9#include "quote.h"
10#include "dir.h"
11#include "builtin.h"
12#include "tree.h"
13
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;
23static int line_terminator = '\n';
24
25static int prefix_len;
26static int prefix_offset;
27static const char **pathspec;
28static int error_unmatch;
29static char *ps_matched;
30static const char *with_tree;
31
32static const char *tag_cached = "";
33static const char *tag_unmerged = "";
34static const char *tag_removed = "";
35static const char *tag_other = "";
36static const char *tag_killed = "";
37static const char *tag_modified = "";
38
39static void show_dir_entry(const char *tag, struct dir_entry *ent)
40{
41 int len = prefix_len;
42 int offset = prefix_offset;
43
44 if (len >= ent->len)
45 die("git ls-files: internal error - directory entry not superset of prefix");
46
47 if (!match_pathspec(pathspec, ent->name, ent->len, len, ps_matched))
48 return;
49
50 fputs(tag, stdout);
51 write_name_quoted(ent->name + offset, stdout, line_terminator);
52}
53
54static void show_other_files(struct dir_struct *dir)
55{
56 int i;
57
58 for (i = 0; i < dir->nr; i++) {
59 struct dir_entry *ent = dir->entries[i];
60 if (!cache_name_is_other(ent->name, ent->len))
61 continue;
62 show_dir_entry(tag_other, ent);
63 }
64}
65
66static void show_killed_files(struct dir_struct *dir)
67{
68 int i;
69 for (i = 0; i < dir->nr; i++) {
70 struct dir_entry *ent = dir->entries[i];
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)
111 show_dir_entry(tag_killed, dir->entries[i]);
112 }
113}
114
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))
121 die("git ls-files: internal error - cache entry not superset of prefix");
122
123 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), len, ps_matched))
124 return;
125
126 if (tag && *tag && show_valid_bit &&
127 (ce->ce_flags & CE_VALID)) {
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
143 if (!show_stage) {
144 fputs(tag, stdout);
145 } else {
146 printf("%s%06o %s %d\t",
147 tag,
148 ce->ce_mode,
149 abbrev ? find_unique_abbrev(ce->sha1,abbrev)
150 : sha1_to_hex(ce->sha1),
151 ce_stage(ce));
152 }
153 write_name_quoted(ce->name + offset, stdout, line_terminator);
154}
155
156static void show_files(struct dir_struct *dir, const char *prefix)
157{
158 int i;
159
160 /* For cached/deleted files we don't need to even do the readdir */
161 if (show_others || show_killed) {
162 const char *path = ".", *base = "";
163 int baselen = prefix_len;
164
165 if (baselen)
166 path = base = prefix;
167 read_directory(dir, path, base, baselen, pathspec);
168 if (show_others)
169 show_other_files(dir);
170 if (show_killed)
171 show_killed_files(dir);
172 }
173 if (show_cached | show_stage) {
174 for (i = 0; i < active_nr; i++) {
175 struct cache_entry *ce = active_cache[i];
176 int dtype = ce_to_dtype(ce);
177 if (excluded(dir, ce->name, &dtype) != dir->show_ignored)
178 continue;
179 if (show_unmerged && !ce_stage(ce))
180 continue;
181 if (ce->ce_flags & CE_UPDATE)
182 continue;
183 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
184 }
185 }
186 if (show_deleted | show_modified) {
187 for (i = 0; i < active_nr; i++) {
188 struct cache_entry *ce = active_cache[i];
189 struct stat st;
190 int err;
191 int dtype = ce_to_dtype(ce);
192 if (excluded(dir, ce->name, &dtype) != dir->show_ignored)
193 continue;
194 if (ce->ce_flags & CE_UPDATE)
195 continue;
196 err = lstat(ce->name, &st);
197 if (show_deleted && err)
198 show_ce_entry(tag_removed, ce);
199 if (show_modified && ce_modified(ce, &st, 0))
200 show_ce_entry(tag_modified, ce);
201 }
202 }
203}
204
205/*
206 * Prune the index to only contain stuff starting with "prefix"
207 */
208static void prune_cache(const char *prefix)
209{
210 int pos = cache_name_pos(prefix, prefix_len);
211 unsigned int first, last;
212
213 if (pos < 0)
214 pos = -pos-1;
215 memmove(active_cache, active_cache + pos,
216 (active_nr - pos) * sizeof(struct cache_entry *));
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;
226 }
227 last = next;
228 }
229 active_nr = last;
230}
231
232static const char *verify_pathspec(const char *prefix)
233{
234 const char **p, *n, *prev;
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;
245 if (!c || c == '*' || c == '?')
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 }
256 }
257
258 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
259 die("git ls-files: cannot generate relative filenames containing '..'");
260
261 prefix_len = max;
262 return max ? xmemdupz(prev, max) : NULL;
263}
264
265/*
266 * Read the tree specified with --with-tree option
267 * (typically, HEAD) into stage #1 and then
268 * squash them down to stage #0. This is used for
269 * --error-unmatch to list and check the path patterns
270 * that were given from the command line. We are not
271 * going to write this index out.
272 */
273void overlay_tree_on_cache(const char *tree_name, const char *prefix)
274{
275 struct tree *tree;
276 unsigned char sha1[20];
277 const char **match;
278 struct cache_entry *last_stage0 = NULL;
279 int i;
280
281 if (get_sha1(tree_name, sha1))
282 die("tree-ish %s not found.", tree_name);
283 tree = parse_tree_indirect(sha1);
284 if (!tree)
285 die("bad tree-ish %s", tree_name);
286
287 /* Hoist the unmerged entries up to stage #3 to make room */
288 for (i = 0; i < active_nr; i++) {
289 struct cache_entry *ce = active_cache[i];
290 if (!ce_stage(ce))
291 continue;
292 ce->ce_flags |= CE_STAGEMASK;
293 }
294
295 if (prefix) {
296 static const char *(matchbuf[2]);
297 matchbuf[0] = prefix;
298 matchbuf[1] = NULL;
299 match = matchbuf;
300 } else
301 match = NULL;
302 if (read_tree(tree, 1, match))
303 die("unable to read tree entries %s", tree_name);
304
305 for (i = 0; i < active_nr; i++) {
306 struct cache_entry *ce = active_cache[i];
307 switch (ce_stage(ce)) {
308 case 0:
309 last_stage0 = ce;
310 /* fallthru */
311 default:
312 continue;
313 case 1:
314 /*
315 * If there is stage #0 entry for this, we do not
316 * need to show it. We use CE_UPDATE bit to mark
317 * such an entry.
318 */
319 if (last_stage0 &&
320 !strcmp(last_stage0->name, ce->name))
321 ce->ce_flags |= CE_UPDATE;
322 }
323 }
324}
325
326int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset)
327{
328 /*
329 * Make sure all pathspec matched; otherwise it is an error.
330 */
331 int num, errors = 0;
332 for (num = 0; pathspec[num]; num++) {
333 int other, found_dup;
334
335 if (ps_matched[num])
336 continue;
337 /*
338 * The caller might have fed identical pathspec
339 * twice. Do not barf on such a mistake.
340 */
341 for (found_dup = other = 0;
342 !found_dup && pathspec[other];
343 other++) {
344 if (other == num || !ps_matched[other])
345 continue;
346 if (!strcmp(pathspec[other], pathspec[num]))
347 /*
348 * Ok, we have a match already.
349 */
350 found_dup = 1;
351 }
352 if (found_dup)
353 continue;
354
355 error("pathspec '%s' did not match any file(s) known to git.",
356 pathspec[num] + prefix_offset);
357 errors++;
358 }
359 return errors;
360}
361
362static const char ls_files_usage[] =
363 "git ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
364 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
365 "[ --exclude-per-directory=<filename> ] [--exclude-standard] "
366 "[--full-name] [--abbrev] [--] [<file>]*";
367
368int cmd_ls_files(int argc, const char **argv, const char *prefix)
369{
370 int i;
371 int exc_given = 0, require_work_tree = 0;
372 struct dir_struct dir;
373
374 memset(&dir, 0, sizeof(dir));
375 if (prefix)
376 prefix_offset = strlen(prefix);
377 git_config(git_default_config, NULL);
378
379 for (i = 1; i < argc; i++) {
380 const char *arg = argv[i];
381
382 if (!strcmp(arg, "--")) {
383 i++;
384 break;
385 }
386 if (!strcmp(arg, "-z")) {
387 line_terminator = 0;
388 continue;
389 }
390 if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) {
391 tag_cached = "H ";
392 tag_unmerged = "M ";
393 tag_removed = "R ";
394 tag_modified = "C ";
395 tag_other = "? ";
396 tag_killed = "K ";
397 if (arg[1] == 'v')
398 show_valid_bit = 1;
399 continue;
400 }
401 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
402 show_cached = 1;
403 continue;
404 }
405 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
406 show_deleted = 1;
407 continue;
408 }
409 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
410 show_modified = 1;
411 require_work_tree = 1;
412 continue;
413 }
414 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
415 show_others = 1;
416 require_work_tree = 1;
417 continue;
418 }
419 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
420 dir.show_ignored = 1;
421 require_work_tree = 1;
422 continue;
423 }
424 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
425 show_stage = 1;
426 continue;
427 }
428 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
429 show_killed = 1;
430 require_work_tree = 1;
431 continue;
432 }
433 if (!strcmp(arg, "--directory")) {
434 dir.show_other_directories = 1;
435 continue;
436 }
437 if (!strcmp(arg, "--no-empty-directory")) {
438 dir.hide_empty_directories = 1;
439 continue;
440 }
441 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
442 /* There's no point in showing unmerged unless
443 * you also show the stage information.
444 */
445 show_stage = 1;
446 show_unmerged = 1;
447 continue;
448 }
449 if (!strcmp(arg, "-x") && i+1 < argc) {
450 exc_given = 1;
451 add_exclude(argv[++i], "", 0, &dir.exclude_list[EXC_CMDL]);
452 continue;
453 }
454 if (!prefixcmp(arg, "--exclude=")) {
455 exc_given = 1;
456 add_exclude(arg+10, "", 0, &dir.exclude_list[EXC_CMDL]);
457 continue;
458 }
459 if (!strcmp(arg, "-X") && i+1 < argc) {
460 exc_given = 1;
461 add_excludes_from_file(&dir, argv[++i]);
462 continue;
463 }
464 if (!prefixcmp(arg, "--exclude-from=")) {
465 exc_given = 1;
466 add_excludes_from_file(&dir, arg+15);
467 continue;
468 }
469 if (!prefixcmp(arg, "--exclude-per-directory=")) {
470 exc_given = 1;
471 dir.exclude_per_dir = arg + 24;
472 continue;
473 }
474 if (!strcmp(arg, "--exclude-standard")) {
475 exc_given = 1;
476 setup_standard_excludes(&dir);
477 continue;
478 }
479 if (!strcmp(arg, "--full-name")) {
480 prefix_offset = 0;
481 continue;
482 }
483 if (!strcmp(arg, "--error-unmatch")) {
484 error_unmatch = 1;
485 continue;
486 }
487 if (!prefixcmp(arg, "--with-tree=")) {
488 with_tree = arg + 12;
489 continue;
490 }
491 if (!prefixcmp(arg, "--abbrev=")) {
492 abbrev = strtoul(arg+9, NULL, 10);
493 if (abbrev && abbrev < MINIMUM_ABBREV)
494 abbrev = MINIMUM_ABBREV;
495 else if (abbrev > 40)
496 abbrev = 40;
497 continue;
498 }
499 if (!strcmp(arg, "--abbrev")) {
500 abbrev = DEFAULT_ABBREV;
501 continue;
502 }
503 if (*arg == '-')
504 usage(ls_files_usage);
505 break;
506 }
507
508 if (require_work_tree && !is_inside_work_tree())
509 setup_work_tree();
510
511 pathspec = get_pathspec(prefix, argv + i);
512
513 /* Verify that the pathspec matches the prefix */
514 if (pathspec)
515 prefix = verify_pathspec(prefix);
516
517 /* Treat unmatching pathspec elements as errors */
518 if (pathspec && error_unmatch) {
519 int num;
520 for (num = 0; pathspec[num]; num++)
521 ;
522 ps_matched = xcalloc(1, num);
523 }
524
525 if (dir.show_ignored && !exc_given) {
526 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
527 argv[0]);
528 exit(1);
529 }
530
531 /* With no flags, we default to showing the cached files */
532 if (!(show_stage | show_deleted | show_others | show_unmerged |
533 show_killed | show_modified))
534 show_cached = 1;
535
536 read_cache();
537 if (prefix)
538 prune_cache(prefix);
539 if (with_tree) {
540 /*
541 * Basic sanity check; show-stages and show-unmerged
542 * would not make any sense with this option.
543 */
544 if (show_stage || show_unmerged)
545 die("ls-files --with-tree is incompatible with -s or -u");
546 overlay_tree_on_cache(with_tree, prefix);
547 }
548 show_files(&dir, prefix);
549
550 if (ps_matched) {
551 int bad;
552 bad = report_path_error(ps_matched, pathspec, prefix_offset);
553 if (bad)
554 fprintf(stderr, "Did you forget to 'git add'?\n");
555
556 return bad ? 1 : 0;
557 }
558
559 return 0;
560}