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