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