c9307f9ef4dc476a6f670ffc6b7eea332d89e4dc
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#include "parse-options.h"
14#include "resolve-undo.h"
15#include "string-list.h"
16#include "pathspec.h"
17#include "run-command.h"
18#include "submodule.h"
19
20static int abbrev;
21static int show_deleted;
22static int show_cached;
23static int show_others;
24static int show_stage;
25static int show_unmerged;
26static int show_resolve_undo;
27static int show_modified;
28static int show_killed;
29static int show_valid_bit;
30static int line_terminator = '\n';
31static int debug_mode;
32static int show_eol;
33static int recurse_submodules;
34static struct argv_array submodule_options = ARGV_ARRAY_INIT;
35
36static const char *prefix;
37static const char *super_prefix;
38static int max_prefix_len;
39static int prefix_len;
40static struct pathspec pathspec;
41static int error_unmatch;
42static char *ps_matched;
43static const char *with_tree;
44static int exc_given;
45static int exclude_args;
46
47static const char *tag_cached = "";
48static const char *tag_unmerged = "";
49static const char *tag_removed = "";
50static const char *tag_other = "";
51static const char *tag_killed = "";
52static const char *tag_modified = "";
53static const char *tag_skip_worktree = "";
54static const char *tag_resolve_undo = "";
55
56static void write_eolinfo(const struct index_state *istate,
57 const struct cache_entry *ce, const char *path)
58{
59 if (show_eol) {
60 struct stat st;
61 const char *i_txt = "";
62 const char *w_txt = "";
63 const char *a_txt = get_convert_attr_ascii(path);
64 if (ce && S_ISREG(ce->ce_mode))
65 i_txt = get_cached_convert_stats_ascii(istate,
66 ce->name);
67 if (!lstat(path, &st) && S_ISREG(st.st_mode))
68 w_txt = get_wt_convert_stats_ascii(path);
69 printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
70 }
71}
72
73static void write_name(const char *name)
74{
75 /*
76 * Prepend the super_prefix to name to construct the full_name to be
77 * written.
78 */
79 struct strbuf full_name = STRBUF_INIT;
80 if (super_prefix) {
81 strbuf_addstr(&full_name, super_prefix);
82 strbuf_addstr(&full_name, name);
83 name = full_name.buf;
84 }
85
86 /*
87 * With "--full-name", prefix_len=0; this caller needs to pass
88 * an empty string in that case (a NULL is good for "").
89 */
90 write_name_quoted_relative(name, prefix_len ? prefix : NULL,
91 stdout, line_terminator);
92
93 strbuf_release(&full_name);
94}
95
96static void print_debug(const struct cache_entry *ce)
97{
98 if (debug_mode) {
99 const struct stat_data *sd = &ce->ce_stat_data;
100
101 printf(" ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
102 printf(" mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
103 printf(" dev: %d\tino: %d\n", sd->sd_dev, sd->sd_ino);
104 printf(" uid: %d\tgid: %d\n", sd->sd_uid, sd->sd_gid);
105 printf(" size: %d\tflags: %x\n", sd->sd_size, ce->ce_flags);
106 }
107}
108
109static void show_dir_entry(const char *tag, struct dir_entry *ent)
110{
111 int len = max_prefix_len;
112
113 if (len > ent->len)
114 die("git ls-files: internal error - directory entry not superset of prefix");
115
116 if (!dir_path_match(ent, &pathspec, len, ps_matched))
117 return;
118
119 fputs(tag, stdout);
120 write_eolinfo(NULL, NULL, ent->name);
121 write_name(ent->name);
122}
123
124static void show_other_files(const struct index_state *istate,
125 const struct dir_struct *dir)
126{
127 int i;
128
129 for (i = 0; i < dir->nr; i++) {
130 struct dir_entry *ent = dir->entries[i];
131 if (!index_name_is_other(istate, ent->name, ent->len))
132 continue;
133 show_dir_entry(tag_other, ent);
134 }
135}
136
137static void show_killed_files(const struct index_state *istate,
138 const struct dir_struct *dir)
139{
140 int i;
141 for (i = 0; i < dir->nr; i++) {
142 struct dir_entry *ent = dir->entries[i];
143 char *cp, *sp;
144 int pos, len, killed = 0;
145
146 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
147 sp = strchr(cp, '/');
148 if (!sp) {
149 /* If ent->name is prefix of an entry in the
150 * cache, it will be killed.
151 */
152 pos = index_name_pos(istate, ent->name, ent->len);
153 if (0 <= pos)
154 die("BUG: killed-file %.*s not found",
155 ent->len, ent->name);
156 pos = -pos - 1;
157 while (pos < istate->cache_nr &&
158 ce_stage(istate->cache[pos]))
159 pos++; /* skip unmerged */
160 if (istate->cache_nr <= pos)
161 break;
162 /* pos points at a name immediately after
163 * ent->name in the cache. Does it expect
164 * ent->name to be a directory?
165 */
166 len = ce_namelen(istate->cache[pos]);
167 if ((ent->len < len) &&
168 !strncmp(istate->cache[pos]->name,
169 ent->name, ent->len) &&
170 istate->cache[pos]->name[ent->len] == '/')
171 killed = 1;
172 break;
173 }
174 if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
175 /* If any of the leading directories in
176 * ent->name is registered in the cache,
177 * ent->name will be killed.
178 */
179 killed = 1;
180 break;
181 }
182 }
183 if (killed)
184 show_dir_entry(tag_killed, dir->entries[i]);
185 }
186}
187
188/*
189 * Compile an argv_array with all of the options supported by --recurse_submodules
190 */
191static void compile_submodule_options(const char **argv,
192 const struct dir_struct *dir,
193 int show_tag)
194{
195 if (line_terminator == '\0')
196 argv_array_push(&submodule_options, "-z");
197 if (show_tag)
198 argv_array_push(&submodule_options, "-t");
199 if (show_valid_bit)
200 argv_array_push(&submodule_options, "-v");
201 if (show_cached)
202 argv_array_push(&submodule_options, "--cached");
203 if (show_eol)
204 argv_array_push(&submodule_options, "--eol");
205 if (debug_mode)
206 argv_array_push(&submodule_options, "--debug");
207
208 /* Add Pathspecs */
209 argv_array_push(&submodule_options, "--");
210 for (; *argv; argv++)
211 argv_array_push(&submodule_options, *argv);
212}
213
214/**
215 * Recursively call ls-files on a submodule
216 */
217static void show_gitlink(const struct cache_entry *ce)
218{
219 struct child_process cp = CHILD_PROCESS_INIT;
220 int status;
221 char *dir;
222
223 prepare_submodule_repo_env(&cp.env_array);
224 argv_array_push(&cp.env_array, GIT_DIR_ENVIRONMENT);
225
226 if (prefix_len)
227 argv_array_pushf(&cp.env_array, "%s=%s",
228 GIT_TOPLEVEL_PREFIX_ENVIRONMENT,
229 prefix);
230 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
231 super_prefix ? super_prefix : "",
232 ce->name);
233 argv_array_push(&cp.args, "ls-files");
234 argv_array_push(&cp.args, "--recurse-submodules");
235
236 /* add supported options */
237 argv_array_pushv(&cp.args, submodule_options.argv);
238
239 cp.git_cmd = 1;
240 dir = mkpathdup("%s/%s", get_git_work_tree(), ce->name);
241 cp.dir = dir;
242 status = run_command(&cp);
243 free(dir);
244 if (status)
245 exit(status);
246}
247
248static void show_ce_entry(const struct index_state *istate,
249 const char *tag, const struct cache_entry *ce)
250{
251 struct strbuf name = STRBUF_INIT;
252 int len = max_prefix_len;
253 if (super_prefix)
254 strbuf_addstr(&name, super_prefix);
255 strbuf_addstr(&name, ce->name);
256
257 if (len > ce_namelen(ce))
258 die("git ls-files: internal error - cache entry not superset of prefix");
259
260 if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
261 submodule_path_match(&pathspec, name.buf, ps_matched)) {
262 show_gitlink(ce);
263 } else if (match_pathspec(&pathspec, name.buf, name.len,
264 len, ps_matched,
265 S_ISDIR(ce->ce_mode) ||
266 S_ISGITLINK(ce->ce_mode))) {
267 if (tag && *tag && show_valid_bit &&
268 (ce->ce_flags & CE_VALID)) {
269 static char alttag[4];
270 memcpy(alttag, tag, 3);
271 if (isalpha(tag[0]))
272 alttag[0] = tolower(tag[0]);
273 else if (tag[0] == '?')
274 alttag[0] = '!';
275 else {
276 alttag[0] = 'v';
277 alttag[1] = tag[0];
278 alttag[2] = ' ';
279 alttag[3] = 0;
280 }
281 tag = alttag;
282 }
283
284 if (!show_stage) {
285 fputs(tag, stdout);
286 } else {
287 printf("%s%06o %s %d\t",
288 tag,
289 ce->ce_mode,
290 find_unique_abbrev(ce->oid.hash, abbrev),
291 ce_stage(ce));
292 }
293 write_eolinfo(istate, ce, ce->name);
294 write_name(ce->name);
295 print_debug(ce);
296 }
297
298 strbuf_release(&name);
299}
300
301static void show_ru_info(const struct index_state *istate)
302{
303 struct string_list_item *item;
304
305 if (!istate->resolve_undo)
306 return;
307
308 for_each_string_list_item(item, istate->resolve_undo) {
309 const char *path = item->string;
310 struct resolve_undo_info *ui = item->util;
311 int i, len;
312
313 len = strlen(path);
314 if (len < max_prefix_len)
315 continue; /* outside of the prefix */
316 if (!match_pathspec(&pathspec, path, len,
317 max_prefix_len, ps_matched, 0))
318 continue; /* uninterested */
319 for (i = 0; i < 3; i++) {
320 if (!ui->mode[i])
321 continue;
322 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
323 find_unique_abbrev(ui->sha1[i], abbrev),
324 i + 1);
325 write_name(path);
326 }
327 }
328}
329
330static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
331 const struct cache_entry *ce)
332{
333 int dtype = ce_to_dtype(ce);
334 return is_excluded(dir, istate, ce->name, &dtype);
335}
336
337static void show_files(struct index_state *istate, struct dir_struct *dir)
338{
339 int i;
340
341 /* For cached/deleted files we don't need to even do the readdir */
342 if (show_others || show_killed) {
343 if (!show_others)
344 dir->flags |= DIR_COLLECT_KILLED_ONLY;
345 fill_directory(dir, istate, &pathspec);
346 if (show_others)
347 show_other_files(istate, dir);
348 if (show_killed)
349 show_killed_files(istate, dir);
350 }
351 if (show_cached || show_stage) {
352 for (i = 0; i < istate->cache_nr; i++) {
353 const struct cache_entry *ce = istate->cache[i];
354 if ((dir->flags & DIR_SHOW_IGNORED) &&
355 !ce_excluded(dir, istate, ce))
356 continue;
357 if (show_unmerged && !ce_stage(ce))
358 continue;
359 if (ce->ce_flags & CE_UPDATE)
360 continue;
361 show_ce_entry(istate, ce_stage(ce) ? tag_unmerged :
362 (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce);
363 }
364 }
365 if (show_deleted || show_modified) {
366 for (i = 0; i < istate->cache_nr; i++) {
367 const struct cache_entry *ce = istate->cache[i];
368 struct stat st;
369 int err;
370 if ((dir->flags & DIR_SHOW_IGNORED) &&
371 !ce_excluded(dir, istate, ce))
372 continue;
373 if (ce->ce_flags & CE_UPDATE)
374 continue;
375 if (ce_skip_worktree(ce))
376 continue;
377 err = lstat(ce->name, &st);
378 if (show_deleted && err)
379 show_ce_entry(istate, tag_removed, ce);
380 if (show_modified && ie_modified(istate, ce, &st, 0))
381 show_ce_entry(istate, tag_modified, ce);
382 }
383 }
384}
385
386/*
387 * Prune the index to only contain stuff starting with "prefix"
388 */
389static void prune_index(struct index_state *istate,
390 const char *prefix, size_t prefixlen)
391{
392 int pos;
393 unsigned int first, last;
394
395 if (!prefix)
396 return;
397 pos = index_name_pos(istate, prefix, prefixlen);
398 if (pos < 0)
399 pos = -pos-1;
400 first = pos;
401 last = istate->cache_nr;
402 while (last > first) {
403 int next = (last + first) >> 1;
404 const struct cache_entry *ce = istate->cache[next];
405 if (!strncmp(ce->name, prefix, prefixlen)) {
406 first = next+1;
407 continue;
408 }
409 last = next;
410 }
411 memmove(istate->cache, istate->cache + pos,
412 (last - pos) * sizeof(struct cache_entry *));
413 istate->cache_nr = last - pos;
414}
415
416static int get_common_prefix_len(const char *common_prefix)
417{
418 int common_prefix_len;
419
420 if (!common_prefix)
421 return 0;
422
423 common_prefix_len = strlen(common_prefix);
424
425 /*
426 * If the prefix has a trailing slash, strip it so that submodules wont
427 * be pruned from the index.
428 */
429 if (common_prefix[common_prefix_len - 1] == '/')
430 common_prefix_len--;
431
432 return common_prefix_len;
433}
434
435/*
436 * Read the tree specified with --with-tree option
437 * (typically, HEAD) into stage #1 and then
438 * squash them down to stage #0. This is used for
439 * --error-unmatch to list and check the path patterns
440 * that were given from the command line. We are not
441 * going to write this index out.
442 */
443void overlay_tree_on_index(struct index_state *istate,
444 const char *tree_name, const char *prefix)
445{
446 struct tree *tree;
447 struct object_id oid;
448 struct pathspec pathspec;
449 struct cache_entry *last_stage0 = NULL;
450 int i;
451
452 if (get_oid(tree_name, &oid))
453 die("tree-ish %s not found.", tree_name);
454 tree = parse_tree_indirect(&oid);
455 if (!tree)
456 die("bad tree-ish %s", tree_name);
457
458 /* Hoist the unmerged entries up to stage #3 to make room */
459 for (i = 0; i < istate->cache_nr; i++) {
460 struct cache_entry *ce = istate->cache[i];
461 if (!ce_stage(ce))
462 continue;
463 ce->ce_flags |= CE_STAGEMASK;
464 }
465
466 if (prefix) {
467 static const char *(matchbuf[1]);
468 matchbuf[0] = NULL;
469 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
470 PATHSPEC_PREFER_CWD, prefix, matchbuf);
471 } else
472 memset(&pathspec, 0, sizeof(pathspec));
473 if (read_tree(tree, 1, &pathspec, istate))
474 die("unable to read tree entries %s", tree_name);
475
476 for (i = 0; i < istate->cache_nr; i++) {
477 struct cache_entry *ce = istate->cache[i];
478 switch (ce_stage(ce)) {
479 case 0:
480 last_stage0 = ce;
481 /* fallthru */
482 default:
483 continue;
484 case 1:
485 /*
486 * If there is stage #0 entry for this, we do not
487 * need to show it. We use CE_UPDATE bit to mark
488 * such an entry.
489 */
490 if (last_stage0 &&
491 !strcmp(last_stage0->name, ce->name))
492 ce->ce_flags |= CE_UPDATE;
493 }
494 }
495}
496
497static const char * const ls_files_usage[] = {
498 N_("git ls-files [<options>] [<file>...]"),
499 NULL
500};
501
502static int option_parse_exclude(const struct option *opt,
503 const char *arg, int unset)
504{
505 struct string_list *exclude_list = opt->value;
506
507 exc_given = 1;
508 string_list_append(exclude_list, arg);
509
510 return 0;
511}
512
513static int option_parse_exclude_from(const struct option *opt,
514 const char *arg, int unset)
515{
516 struct dir_struct *dir = opt->value;
517
518 exc_given = 1;
519 add_excludes_from_file(dir, arg);
520
521 return 0;
522}
523
524static int option_parse_exclude_standard(const struct option *opt,
525 const char *arg, int unset)
526{
527 struct dir_struct *dir = opt->value;
528
529 exc_given = 1;
530 setup_standard_excludes(dir);
531
532 return 0;
533}
534
535int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
536{
537 int require_work_tree = 0, show_tag = 0, i;
538 const char *max_prefix;
539 struct dir_struct dir;
540 struct exclude_list *el;
541 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
542 struct option builtin_ls_files_options[] = {
543 /* Think twice before adding "--nul" synonym to this */
544 OPT_SET_INT('z', NULL, &line_terminator,
545 N_("paths are separated with NUL character"), '\0'),
546 OPT_BOOL('t', NULL, &show_tag,
547 N_("identify the file status with tags")),
548 OPT_BOOL('v', NULL, &show_valid_bit,
549 N_("use lowercase letters for 'assume unchanged' files")),
550 OPT_BOOL('c', "cached", &show_cached,
551 N_("show cached files in the output (default)")),
552 OPT_BOOL('d', "deleted", &show_deleted,
553 N_("show deleted files in the output")),
554 OPT_BOOL('m', "modified", &show_modified,
555 N_("show modified files in the output")),
556 OPT_BOOL('o', "others", &show_others,
557 N_("show other files in the output")),
558 OPT_BIT('i', "ignored", &dir.flags,
559 N_("show ignored files in the output"),
560 DIR_SHOW_IGNORED),
561 OPT_BOOL('s', "stage", &show_stage,
562 N_("show staged contents' object name in the output")),
563 OPT_BOOL('k', "killed", &show_killed,
564 N_("show files on the filesystem that need to be removed")),
565 OPT_BIT(0, "directory", &dir.flags,
566 N_("show 'other' directories' names only"),
567 DIR_SHOW_OTHER_DIRECTORIES),
568 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
569 OPT_NEGBIT(0, "empty-directory", &dir.flags,
570 N_("don't show empty directories"),
571 DIR_HIDE_EMPTY_DIRECTORIES),
572 OPT_BOOL('u', "unmerged", &show_unmerged,
573 N_("show unmerged files in the output")),
574 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
575 N_("show resolve-undo information")),
576 { OPTION_CALLBACK, 'x', "exclude", &exclude_list, N_("pattern"),
577 N_("skip files matching pattern"),
578 0, option_parse_exclude },
579 { OPTION_CALLBACK, 'X', "exclude-from", &dir, N_("file"),
580 N_("exclude patterns are read from <file>"),
581 0, option_parse_exclude_from },
582 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
583 N_("read additional per-directory exclude patterns in <file>")),
584 { OPTION_CALLBACK, 0, "exclude-standard", &dir, NULL,
585 N_("add the standard git exclusions"),
586 PARSE_OPT_NOARG, option_parse_exclude_standard },
587 { OPTION_SET_INT, 0, "full-name", &prefix_len, NULL,
588 N_("make the output relative to the project top directory"),
589 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL },
590 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
591 N_("recurse through submodules")),
592 OPT_BOOL(0, "error-unmatch", &error_unmatch,
593 N_("if any <file> is not in the index, treat this as an error")),
594 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
595 N_("pretend that paths removed since <tree-ish> are still present")),
596 OPT__ABBREV(&abbrev),
597 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
598 OPT_END()
599 };
600
601 if (argc == 2 && !strcmp(argv[1], "-h"))
602 usage_with_options(ls_files_usage, builtin_ls_files_options);
603
604 memset(&dir, 0, sizeof(dir));
605 prefix = cmd_prefix;
606 if (prefix)
607 prefix_len = strlen(prefix);
608 super_prefix = get_super_prefix();
609 git_config(git_default_config, NULL);
610
611 if (read_cache() < 0)
612 die("index file corrupt");
613
614 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
615 ls_files_usage, 0);
616 el = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
617 for (i = 0; i < exclude_list.nr; i++) {
618 add_exclude(exclude_list.items[i].string, "", 0, el, --exclude_args);
619 }
620 if (show_tag || show_valid_bit) {
621 tag_cached = "H ";
622 tag_unmerged = "M ";
623 tag_removed = "R ";
624 tag_modified = "C ";
625 tag_other = "? ";
626 tag_killed = "K ";
627 tag_skip_worktree = "S ";
628 tag_resolve_undo = "U ";
629 }
630 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
631 require_work_tree = 1;
632 if (show_unmerged)
633 /*
634 * There's no point in showing unmerged unless
635 * you also show the stage information.
636 */
637 show_stage = 1;
638 if (dir.exclude_per_dir)
639 exc_given = 1;
640
641 if (require_work_tree && !is_inside_work_tree())
642 setup_work_tree();
643
644 if (recurse_submodules)
645 compile_submodule_options(argv, &dir, show_tag);
646
647 if (recurse_submodules &&
648 (show_stage || show_deleted || show_others || show_unmerged ||
649 show_killed || show_modified || show_resolve_undo || with_tree))
650 die("ls-files --recurse-submodules unsupported mode");
651
652 if (recurse_submodules && error_unmatch)
653 die("ls-files --recurse-submodules does not support "
654 "--error-unmatch");
655
656 parse_pathspec(&pathspec, 0,
657 PATHSPEC_PREFER_CWD,
658 prefix, argv);
659
660 /*
661 * Find common prefix for all pathspec's
662 * This is used as a performance optimization which unfortunately cannot
663 * be done when recursing into submodules
664 */
665 if (recurse_submodules)
666 max_prefix = NULL;
667 else
668 max_prefix = common_prefix(&pathspec);
669 max_prefix_len = get_common_prefix_len(max_prefix);
670
671 prune_index(&the_index, max_prefix, max_prefix_len);
672
673 /* Treat unmatching pathspec elements as errors */
674 if (pathspec.nr && error_unmatch)
675 ps_matched = xcalloc(pathspec.nr, 1);
676
677 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
678 die("ls-files --ignored needs some exclude pattern");
679
680 /* With no flags, we default to showing the cached files */
681 if (!(show_stage || show_deleted || show_others || show_unmerged ||
682 show_killed || show_modified || show_resolve_undo))
683 show_cached = 1;
684
685 if (with_tree) {
686 /*
687 * Basic sanity check; show-stages and show-unmerged
688 * would not make any sense with this option.
689 */
690 if (show_stage || show_unmerged)
691 die("ls-files --with-tree is incompatible with -s or -u");
692 overlay_tree_on_index(&the_index, with_tree, max_prefix);
693 }
694 show_files(&the_index, &dir);
695 if (show_resolve_undo)
696 show_ru_info(&the_index);
697
698 if (ps_matched) {
699 int bad;
700 bad = report_path_error(ps_matched, &pathspec, prefix);
701 if (bad)
702 fprintf(stderr, "Did you forget to 'git add'?\n");
703
704 return bad ? 1 : 0;
705 }
706
707 return 0;
708}