OSDN Git Service

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