gpg-interface: drop pointless config_error_nonbool() checks
[alt-git.git] / builtin / ls-files.c
bloba0229c3277874accc9edf0a4a5ac4e8186217903
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.
6 * Copyright (C) Linus Torvalds, 2005
7 */
8 #include "builtin.h"
9 #include "repository.h"
10 #include "config.h"
11 #include "convert.h"
12 #include "quote.h"
13 #include "dir.h"
14 #include "gettext.h"
15 #include "object-name.h"
16 #include "strbuf.h"
17 #include "tree.h"
18 #include "cache-tree.h"
19 #include "parse-options.h"
20 #include "resolve-undo.h"
21 #include "string-list.h"
22 #include "path.h"
23 #include "pathspec.h"
24 #include "read-cache.h"
25 #include "run-command.h"
26 #include "setup.h"
27 #include "sparse-index.h"
28 #include "submodule.h"
29 #include "submodule-config.h"
30 #include "object-store.h"
31 #include "hex.h"
34 static int abbrev;
35 static int show_deleted;
36 static int show_cached;
37 static int show_others;
38 static int show_stage;
39 static int show_unmerged;
40 static int show_resolve_undo;
41 static int show_modified;
42 static int show_killed;
43 static int show_valid_bit;
44 static int show_fsmonitor_bit;
45 static int line_terminator = '\n';
46 static int debug_mode;
47 static int show_eol;
48 static int recurse_submodules;
49 static int skipping_duplicates;
50 static int show_sparse_dirs;
52 static const char *prefix;
53 static int max_prefix_len;
54 static int prefix_len;
55 static struct pathspec pathspec;
56 static int error_unmatch;
57 static char *ps_matched;
58 static const char *with_tree;
59 static int exc_given;
60 static int exclude_args;
61 static const char *format;
63 static const char *tag_cached = "";
64 static const char *tag_unmerged = "";
65 static const char *tag_removed = "";
66 static const char *tag_other = "";
67 static const char *tag_killed = "";
68 static const char *tag_modified = "";
69 static const char *tag_skip_worktree = "";
70 static const char *tag_resolve_undo = "";
72 static void write_eolinfo(struct index_state *istate,
73 const struct cache_entry *ce, const char *path)
75 if (show_eol) {
76 struct stat st;
77 const char *i_txt = "";
78 const char *w_txt = "";
79 const char *a_txt = get_convert_attr_ascii(istate, path);
80 if (ce && S_ISREG(ce->ce_mode))
81 i_txt = get_cached_convert_stats_ascii(istate,
82 ce->name);
83 if (!lstat(path, &st) && S_ISREG(st.st_mode))
84 w_txt = get_wt_convert_stats_ascii(path);
85 printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
89 static void write_name(const char *name)
92 * With "--full-name", prefix_len=0; this caller needs to pass
93 * an empty string in that case (a NULL is good for "").
95 write_name_quoted_relative(name, prefix_len ? prefix : NULL,
96 stdout, line_terminator);
99 static void write_name_to_buf(struct strbuf *sb, const char *name)
101 struct strbuf buf = STRBUF_INIT;
102 const char *rel = relative_path(name, prefix_len ? prefix : NULL, &buf);
104 if (line_terminator)
105 quote_c_style(rel, sb, NULL, 0);
106 else
107 strbuf_addstr(sb, rel);
109 strbuf_release(&buf);
112 static const char *get_tag(const struct cache_entry *ce, const char *tag)
114 static char alttag[4];
116 if (tag && *tag && ((show_valid_bit && (ce->ce_flags & CE_VALID)) ||
117 (show_fsmonitor_bit && (ce->ce_flags & CE_FSMONITOR_VALID)))) {
118 memcpy(alttag, tag, 3);
120 if (isalpha(tag[0])) {
121 alttag[0] = tolower(tag[0]);
122 } else if (tag[0] == '?') {
123 alttag[0] = '!';
124 } else {
125 alttag[0] = 'v';
126 alttag[1] = tag[0];
127 alttag[2] = ' ';
128 alttag[3] = 0;
131 tag = alttag;
134 return tag;
137 static void print_debug(const struct cache_entry *ce)
139 if (debug_mode) {
140 const struct stat_data *sd = &ce->ce_stat_data;
142 printf(" ctime: %u:%u\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
143 printf(" mtime: %u:%u\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
144 printf(" dev: %u\tino: %u\n", sd->sd_dev, sd->sd_ino);
145 printf(" uid: %u\tgid: %u\n", sd->sd_uid, sd->sd_gid);
146 printf(" size: %u\tflags: %x\n", sd->sd_size, ce->ce_flags);
150 static void show_dir_entry(struct index_state *istate,
151 const char *tag, struct dir_entry *ent)
153 int len = max_prefix_len;
155 if (len > ent->len)
156 die("git ls-files: internal error - directory entry not superset of prefix");
158 /* If ps_matches is non-NULL, figure out which pathspec(s) match. */
159 if (ps_matched)
160 dir_path_match(istate, ent, &pathspec, len, ps_matched);
162 fputs(tag, stdout);
163 write_eolinfo(istate, NULL, ent->name);
164 write_name(ent->name);
167 static void show_other_files(struct index_state *istate,
168 const struct dir_struct *dir)
170 int i;
172 for (i = 0; i < dir->nr; i++) {
173 struct dir_entry *ent = dir->entries[i];
174 if (!index_name_is_other(istate, ent->name, ent->len))
175 continue;
176 show_dir_entry(istate, tag_other, ent);
180 static void show_killed_files(struct index_state *istate,
181 const struct dir_struct *dir)
183 int i;
184 for (i = 0; i < dir->nr; i++) {
185 struct dir_entry *ent = dir->entries[i];
186 char *cp, *sp;
187 int pos, len, killed = 0;
189 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
190 sp = strchr(cp, '/');
191 if (!sp) {
192 /* If ent->name is prefix of an entry in the
193 * cache, it will be killed.
195 pos = index_name_pos(istate, ent->name, ent->len);
196 if (0 <= pos)
197 BUG("killed-file %.*s not found",
198 ent->len, ent->name);
199 pos = -pos - 1;
200 while (pos < istate->cache_nr &&
201 ce_stage(istate->cache[pos]))
202 pos++; /* skip unmerged */
203 if (istate->cache_nr <= pos)
204 break;
205 /* pos points at a name immediately after
206 * ent->name in the cache. Does it expect
207 * ent->name to be a directory?
209 len = ce_namelen(istate->cache[pos]);
210 if ((ent->len < len) &&
211 !strncmp(istate->cache[pos]->name,
212 ent->name, ent->len) &&
213 istate->cache[pos]->name[ent->len] == '/')
214 killed = 1;
215 break;
217 if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
218 /* If any of the leading directories in
219 * ent->name is registered in the cache,
220 * ent->name will be killed.
222 killed = 1;
223 break;
226 if (killed)
227 show_dir_entry(istate, tag_killed, dir->entries[i]);
231 static void show_files(struct repository *repo, struct dir_struct *dir);
233 static void show_submodule(struct repository *superproject,
234 struct dir_struct *dir, const char *path)
236 struct repository subrepo;
238 if (repo_submodule_init(&subrepo, superproject, path, null_oid()))
239 return;
241 if (repo_read_index(&subrepo) < 0)
242 die("index file corrupt");
244 show_files(&subrepo, dir);
246 repo_clear(&subrepo);
249 static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
250 const enum object_type type, unsigned int padded)
252 if (type == OBJ_BLOB) {
253 unsigned long size;
254 if (oid_object_info(the_repository, oid, &size) < 0)
255 die(_("could not get object info about '%s'"),
256 oid_to_hex(oid));
257 if (padded)
258 strbuf_addf(line, "%7"PRIuMAX, (uintmax_t)size);
259 else
260 strbuf_addf(line, "%"PRIuMAX, (uintmax_t)size);
261 } else if (padded) {
262 strbuf_addf(line, "%7s", "-");
263 } else {
264 strbuf_addstr(line, "-");
268 static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
269 const char *format, const char *fullname) {
270 struct strbuf sb = STRBUF_INIT;
272 while (strbuf_expand_step(&sb, &format)) {
273 const char *end;
274 size_t len;
275 struct stat st;
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 (*format != '(')
282 die(_("bad ls-files format: element '%s' "
283 "does not start with '('"), format);
284 else if (!(end = strchr(format + 1, ')')))
285 die(_("bad ls-files format: element '%s' "
286 "does not end in ')'"), format);
287 else if (skip_prefix(format, "(objectmode)", &format))
288 strbuf_addf(&sb, "%06o", ce->ce_mode);
289 else if (skip_prefix(format, "(objectname)", &format))
290 strbuf_add_unique_abbrev(&sb, &ce->oid, abbrev);
291 else if (skip_prefix(format, "(objecttype)", &format))
292 strbuf_addstr(&sb, type_name(object_type(ce->ce_mode)));
293 else if (skip_prefix(format, "(objectsize:padded)", &format))
294 expand_objectsize(&sb, &ce->oid,
295 object_type(ce->ce_mode), 1);
296 else if (skip_prefix(format, "(objectsize)", &format))
297 expand_objectsize(&sb, &ce->oid,
298 object_type(ce->ce_mode), 0);
299 else if (skip_prefix(format, "(stage)", &format))
300 strbuf_addf(&sb, "%d", ce_stage(ce));
301 else if (skip_prefix(format, "(eolinfo:index)", &format))
302 strbuf_addstr(&sb, S_ISREG(ce->ce_mode) ?
303 get_cached_convert_stats_ascii(repo->index,
304 ce->name) : "");
305 else if (skip_prefix(format, "(eolinfo:worktree)", &format))
306 strbuf_addstr(&sb, !lstat(fullname, &st) &&
307 S_ISREG(st.st_mode) ?
308 get_wt_convert_stats_ascii(fullname) : "");
309 else if (skip_prefix(format, "(eolattr)", &format))
310 strbuf_addstr(&sb, get_convert_attr_ascii(repo->index,
311 fullname));
312 else if (skip_prefix(format, "(path)", &format))
313 write_name_to_buf(&sb, fullname);
314 else
315 die(_("bad ls-files format: %%%.*s"),
316 (int)(end - format + 1), format);
318 strbuf_addch(&sb, line_terminator);
319 fwrite(sb.buf, sb.len, 1, stdout);
320 strbuf_release(&sb);
323 static void show_ce(struct repository *repo, struct dir_struct *dir,
324 const struct cache_entry *ce, const char *fullname,
325 const char *tag)
327 if (max_prefix_len > strlen(fullname))
328 die("git ls-files: internal error - cache entry not superset of prefix");
330 if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
331 is_submodule_active(repo, ce->name)) {
332 show_submodule(repo, dir, ce->name);
333 } else if (match_pathspec(repo->index, &pathspec, fullname, strlen(fullname),
334 max_prefix_len, ps_matched,
335 S_ISDIR(ce->ce_mode) ||
336 S_ISGITLINK(ce->ce_mode))) {
337 if (format) {
338 show_ce_fmt(repo, ce, format, fullname);
339 print_debug(ce);
340 return;
343 tag = get_tag(ce, tag);
345 if (!show_stage) {
346 fputs(tag, stdout);
347 } else {
348 printf("%s%06o %s %d\t",
349 tag,
350 ce->ce_mode,
351 repo_find_unique_abbrev(repo, &ce->oid, abbrev),
352 ce_stage(ce));
354 write_eolinfo(repo->index, ce, fullname);
355 write_name(fullname);
356 print_debug(ce);
360 static void show_ru_info(struct index_state *istate)
362 struct string_list_item *item;
364 if (!istate->resolve_undo)
365 return;
367 for_each_string_list_item(item, istate->resolve_undo) {
368 const char *path = item->string;
369 struct resolve_undo_info *ui = item->util;
370 int i, len;
372 len = strlen(path);
373 if (len < max_prefix_len)
374 continue; /* outside of the prefix */
375 if (!match_pathspec(istate, &pathspec, path, len,
376 max_prefix_len, ps_matched, 0))
377 continue; /* uninterested */
378 for (i = 0; i < 3; i++) {
379 if (!ui->mode[i])
380 continue;
381 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
382 repo_find_unique_abbrev(the_repository, &ui->oid[i], abbrev),
383 i + 1);
384 write_name(path);
389 static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
390 const char *fullname, const struct cache_entry *ce)
392 int dtype = ce_to_dtype(ce);
393 return is_excluded(dir, istate, fullname, &dtype);
396 static void construct_fullname(struct strbuf *out, const struct repository *repo,
397 const struct cache_entry *ce)
399 strbuf_reset(out);
400 if (repo->submodule_prefix)
401 strbuf_addstr(out, repo->submodule_prefix);
402 strbuf_addstr(out, ce->name);
405 static void show_files(struct repository *repo, struct dir_struct *dir)
407 int i;
408 struct strbuf fullname = STRBUF_INIT;
410 /* For cached/deleted files we don't need to even do the readdir */
411 if (show_others || show_killed) {
412 if (!show_others)
413 dir->flags |= DIR_COLLECT_KILLED_ONLY;
414 fill_directory(dir, repo->index, &pathspec);
415 if (show_others)
416 show_other_files(repo->index, dir);
417 if (show_killed)
418 show_killed_files(repo->index, dir);
421 if (!(show_cached || show_stage || show_deleted || show_modified))
422 return;
424 if (!show_sparse_dirs)
425 ensure_full_index(repo->index);
427 for (i = 0; i < repo->index->cache_nr; i++) {
428 const struct cache_entry *ce = repo->index->cache[i];
429 struct stat st;
430 int stat_err;
432 construct_fullname(&fullname, repo, ce);
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;
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;
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;
467 continue;
469 skip_to_next_name:
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 */
480 strbuf_release(&fullname);
484 * Prune the index to only contain stuff starting with "prefix"
486 static void prune_index(struct index_state *istate,
487 const char *prefix, size_t prefixlen)
489 int pos;
490 unsigned int first, last;
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;
506 last = next;
508 MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos);
509 istate->cache_nr = last - pos;
512 static int get_common_prefix_len(const char *common_prefix)
514 int common_prefix_len;
516 if (!common_prefix)
517 return 0;
519 common_prefix_len = strlen(common_prefix);
522 * If the prefix has a trailing slash, strip it so that submodules wont
523 * be pruned from the index.
525 if (common_prefix[common_prefix_len - 1] == '/')
526 common_prefix_len--;
528 return common_prefix_len;
531 static const char * const ls_files_usage[] = {
532 N_("git ls-files [<options>] [<file>...]"),
533 NULL
536 static int option_parse_exclude(const struct option *opt,
537 const char *arg, int unset)
539 struct string_list *exclude_list = opt->value;
541 BUG_ON_OPT_NEG(unset);
543 exc_given = 1;
544 string_list_append(exclude_list, arg);
546 return 0;
549 static int option_parse_exclude_from(const struct option *opt,
550 const char *arg, int unset)
552 struct dir_struct *dir = opt->value;
554 BUG_ON_OPT_NEG(unset);
556 exc_given = 1;
557 add_patterns_from_file(dir, arg);
559 return 0;
562 static int option_parse_exclude_standard(const struct option *opt,
563 const char *arg, int unset)
565 struct dir_struct *dir = opt->value;
567 BUG_ON_OPT_NEG(unset);
568 BUG_ON_OPT_ARG(arg);
570 exc_given = 1;
571 setup_standard_excludes(dir);
573 return 0;
576 int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
578 int require_work_tree = 0, show_tag = 0, i;
579 char *max_prefix;
580 struct dir_struct dir = DIR_INIT;
581 struct pattern_list *pl;
582 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
583 struct option builtin_ls_files_options[] = {
584 /* Think twice before adding "--nul" synonym to this */
585 OPT_SET_INT('z', NULL, &line_terminator,
586 N_("separate paths with the NUL character"), '\0'),
587 OPT_BOOL('t', NULL, &show_tag,
588 N_("identify the file status with tags")),
589 OPT_BOOL('v', NULL, &show_valid_bit,
590 N_("use lowercase letters for 'assume unchanged' files")),
591 OPT_BOOL('f', NULL, &show_fsmonitor_bit,
592 N_("use lowercase letters for 'fsmonitor clean' files")),
593 OPT_BOOL('c', "cached", &show_cached,
594 N_("show cached files in the output (default)")),
595 OPT_BOOL('d', "deleted", &show_deleted,
596 N_("show deleted files in the output")),
597 OPT_BOOL('m', "modified", &show_modified,
598 N_("show modified files in the output")),
599 OPT_BOOL('o', "others", &show_others,
600 N_("show other files in the output")),
601 OPT_BIT('i', "ignored", &dir.flags,
602 N_("show ignored files in the output"),
603 DIR_SHOW_IGNORED),
604 OPT_BOOL('s', "stage", &show_stage,
605 N_("show staged contents' object name in the output")),
606 OPT_BOOL('k', "killed", &show_killed,
607 N_("show files on the filesystem that need to be removed")),
608 OPT_BIT(0, "directory", &dir.flags,
609 N_("show 'other' directories' names only"),
610 DIR_SHOW_OTHER_DIRECTORIES),
611 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
612 OPT_NEGBIT(0, "empty-directory", &dir.flags,
613 N_("don't show empty directories"),
614 DIR_HIDE_EMPTY_DIRECTORIES),
615 OPT_BOOL('u', "unmerged", &show_unmerged,
616 N_("show unmerged files in the output")),
617 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
618 N_("show resolve-undo information")),
619 OPT_CALLBACK_F('x', "exclude", &exclude_list, N_("pattern"),
620 N_("skip files matching pattern"),
621 PARSE_OPT_NONEG, option_parse_exclude),
622 OPT_CALLBACK_F('X', "exclude-from", &dir, N_("file"),
623 N_("read exclude patterns from <file>"),
624 PARSE_OPT_NONEG, option_parse_exclude_from),
625 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
626 N_("read additional per-directory exclude patterns in <file>")),
627 OPT_CALLBACK_F(0, "exclude-standard", &dir, NULL,
628 N_("add the standard git exclusions"),
629 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
630 option_parse_exclude_standard),
631 OPT_SET_INT_F(0, "full-name", &prefix_len,
632 N_("make the output relative to the project top directory"),
633 0, PARSE_OPT_NONEG),
634 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
635 N_("recurse through submodules")),
636 OPT_BOOL(0, "error-unmatch", &error_unmatch,
637 N_("if any <file> is not in the index, treat this as an error")),
638 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
639 N_("pretend that paths removed since <tree-ish> are still present")),
640 OPT__ABBREV(&abbrev),
641 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
642 OPT_BOOL(0, "deduplicate", &skipping_duplicates,
643 N_("suppress duplicate entries")),
644 OPT_BOOL(0, "sparse", &show_sparse_dirs,
645 N_("show sparse directories in the presence of a sparse index")),
646 OPT_STRING_F(0, "format", &format, N_("format"),
647 N_("format to use for the output"),
648 PARSE_OPT_NONEG),
649 OPT_END()
651 int ret = 0;
653 if (argc == 2 && !strcmp(argv[1], "-h"))
654 usage_with_options(ls_files_usage, builtin_ls_files_options);
656 prepare_repo_settings(the_repository);
657 the_repository->settings.command_requires_full_index = 0;
659 prefix = cmd_prefix;
660 if (prefix)
661 prefix_len = strlen(prefix);
662 git_config(git_default_config, NULL);
664 if (repo_read_index(the_repository) < 0)
665 die("index file corrupt");
667 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
668 ls_files_usage, 0);
669 pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
670 for (i = 0; i < exclude_list.nr; i++) {
671 add_pattern(exclude_list.items[i].string, "", 0, pl, --exclude_args);
674 if (format && (show_stage || show_others || show_killed ||
675 show_resolve_undo || skipping_duplicates || show_eol || show_tag))
676 usage_msg_opt(_("--format cannot be used with -s, -o, -k, -t, "
677 "--resolve-undo, --deduplicate, --eol"),
678 ls_files_usage, builtin_ls_files_options);
680 if (show_tag || show_valid_bit || show_fsmonitor_bit) {
681 tag_cached = "H ";
682 tag_unmerged = "M ";
683 tag_removed = "R ";
684 tag_modified = "C ";
685 tag_other = "? ";
686 tag_killed = "K ";
687 tag_skip_worktree = "S ";
688 tag_resolve_undo = "U ";
690 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
691 require_work_tree = 1;
692 if (show_unmerged)
694 * There's no point in showing unmerged unless
695 * you also show the stage information.
697 show_stage = 1;
698 if (show_tag || show_stage)
699 skipping_duplicates = 0;
700 if (dir.exclude_per_dir)
701 exc_given = 1;
703 if (require_work_tree && !is_inside_work_tree())
704 setup_work_tree();
706 if (recurse_submodules &&
707 (show_deleted || show_others || show_unmerged ||
708 show_killed || show_modified || show_resolve_undo || with_tree))
709 die("ls-files --recurse-submodules unsupported mode");
711 if (recurse_submodules && error_unmatch)
712 die("ls-files --recurse-submodules does not support "
713 "--error-unmatch");
715 parse_pathspec(&pathspec, 0,
716 PATHSPEC_PREFER_CWD,
717 prefix, argv);
720 * Find common prefix for all pathspec's
721 * This is used as a performance optimization which unfortunately cannot
722 * be done when recursing into submodules because when a pathspec is
723 * given which spans repository boundaries you can't simply remove the
724 * submodule entry because the pathspec may match something inside the
725 * submodule.
727 if (recurse_submodules)
728 max_prefix = NULL;
729 else
730 max_prefix = common_prefix(&pathspec);
731 max_prefix_len = get_common_prefix_len(max_prefix);
733 prune_index(the_repository->index, max_prefix, max_prefix_len);
735 /* Treat unmatching pathspec elements as errors */
736 if (pathspec.nr && error_unmatch)
737 ps_matched = xcalloc(pathspec.nr, 1);
739 if ((dir.flags & DIR_SHOW_IGNORED) && !show_others && !show_cached)
740 die("ls-files -i must be used with either -o or -c");
742 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
743 die("ls-files --ignored needs some exclude pattern");
745 /* With no flags, we default to showing the cached files */
746 if (!(show_stage || show_deleted || show_others || show_unmerged ||
747 show_killed || show_modified || show_resolve_undo))
748 show_cached = 1;
750 if (with_tree) {
752 * Basic sanity check; show-stages and show-unmerged
753 * would not make any sense with this option.
755 if (show_stage || show_unmerged)
756 die(_("options '%s' and '%s' cannot be used together"), "ls-files --with-tree", "-s/-u");
757 overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
760 show_files(the_repository, &dir);
762 if (show_resolve_undo)
763 show_ru_info(the_repository->index);
765 if (ps_matched && report_path_error(ps_matched, &pathspec)) {
766 fprintf(stderr, "Did you forget to 'git add'?\n");
767 ret = 1;
770 string_list_clear(&exclude_list, 0);
771 dir_clear(&dir);
772 free(max_prefix);
773 return ret;