setup.h: move declarations for setup.c functions from cache.h
[git.git] / builtin / ls-files.c
blob4a8de95ddc456982a8211e53bca1247f0afd8d61
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 "cache.h"
9 #include "repository.h"
10 #include "config.h"
11 #include "quote.h"
12 #include "dir.h"
13 #include "builtin.h"
14 #include "gettext.h"
15 #include "strbuf.h"
16 #include "tree.h"
17 #include "cache-tree.h"
18 #include "parse-options.h"
19 #include "resolve-undo.h"
20 #include "string-list.h"
21 #include "pathspec.h"
22 #include "run-command.h"
23 #include "setup.h"
24 #include "submodule.h"
25 #include "submodule-config.h"
27 static int abbrev;
28 static int show_deleted;
29 static int show_cached;
30 static int show_others;
31 static int show_stage;
32 static int show_unmerged;
33 static int show_resolve_undo;
34 static int show_modified;
35 static int show_killed;
36 static int show_valid_bit;
37 static int show_fsmonitor_bit;
38 static int line_terminator = '\n';
39 static int debug_mode;
40 static int show_eol;
41 static int recurse_submodules;
42 static int skipping_duplicates;
43 static int show_sparse_dirs;
45 static const char *prefix;
46 static int max_prefix_len;
47 static int prefix_len;
48 static struct pathspec pathspec;
49 static int error_unmatch;
50 static char *ps_matched;
51 static const char *with_tree;
52 static int exc_given;
53 static int exclude_args;
54 static const char *format;
56 static const char *tag_cached = "";
57 static const char *tag_unmerged = "";
58 static const char *tag_removed = "";
59 static const char *tag_other = "";
60 static const char *tag_killed = "";
61 static const char *tag_modified = "";
62 static const char *tag_skip_worktree = "";
63 static const char *tag_resolve_undo = "";
65 static void write_eolinfo(struct index_state *istate,
66 const struct cache_entry *ce, const char *path)
68 if (show_eol) {
69 struct stat st;
70 const char *i_txt = "";
71 const char *w_txt = "";
72 const char *a_txt = get_convert_attr_ascii(istate, path);
73 if (ce && S_ISREG(ce->ce_mode))
74 i_txt = get_cached_convert_stats_ascii(istate,
75 ce->name);
76 if (!lstat(path, &st) && S_ISREG(st.st_mode))
77 w_txt = get_wt_convert_stats_ascii(path);
78 printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
82 static void write_name(const char *name)
85 * With "--full-name", prefix_len=0; this caller needs to pass
86 * an empty string in that case (a NULL is good for "").
88 write_name_quoted_relative(name, prefix_len ? prefix : NULL,
89 stdout, line_terminator);
92 static void write_name_to_buf(struct strbuf *sb, const char *name)
94 const char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);
96 if (line_terminator)
97 quote_c_style(rel, sb, NULL, 0);
98 else
99 strbuf_addstr(sb, rel);
102 static const char *get_tag(const struct cache_entry *ce, const char *tag)
104 static char alttag[4];
106 if (tag && *tag && ((show_valid_bit && (ce->ce_flags & CE_VALID)) ||
107 (show_fsmonitor_bit && (ce->ce_flags & CE_FSMONITOR_VALID)))) {
108 memcpy(alttag, tag, 3);
110 if (isalpha(tag[0])) {
111 alttag[0] = tolower(tag[0]);
112 } else if (tag[0] == '?') {
113 alttag[0] = '!';
114 } else {
115 alttag[0] = 'v';
116 alttag[1] = tag[0];
117 alttag[2] = ' ';
118 alttag[3] = 0;
121 tag = alttag;
124 return tag;
127 static void print_debug(const struct cache_entry *ce)
129 if (debug_mode) {
130 const struct stat_data *sd = &ce->ce_stat_data;
132 printf(" ctime: %u:%u\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
133 printf(" mtime: %u:%u\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
134 printf(" dev: %u\tino: %u\n", sd->sd_dev, sd->sd_ino);
135 printf(" uid: %u\tgid: %u\n", sd->sd_uid, sd->sd_gid);
136 printf(" size: %u\tflags: %x\n", sd->sd_size, ce->ce_flags);
140 static void show_dir_entry(struct index_state *istate,
141 const char *tag, struct dir_entry *ent)
143 int len = max_prefix_len;
145 if (len > ent->len)
146 die("git ls-files: internal error - directory entry not superset of prefix");
148 /* If ps_matches is non-NULL, figure out which pathspec(s) match. */
149 if (ps_matched)
150 dir_path_match(istate, ent, &pathspec, len, ps_matched);
152 fputs(tag, stdout);
153 write_eolinfo(istate, NULL, ent->name);
154 write_name(ent->name);
157 static void show_other_files(struct index_state *istate,
158 const struct dir_struct *dir)
160 int i;
162 for (i = 0; i < dir->nr; i++) {
163 struct dir_entry *ent = dir->entries[i];
164 if (!index_name_is_other(istate, ent->name, ent->len))
165 continue;
166 show_dir_entry(istate, tag_other, ent);
170 static void show_killed_files(struct index_state *istate,
171 const struct dir_struct *dir)
173 int i;
174 for (i = 0; i < dir->nr; i++) {
175 struct dir_entry *ent = dir->entries[i];
176 char *cp, *sp;
177 int pos, len, killed = 0;
179 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
180 sp = strchr(cp, '/');
181 if (!sp) {
182 /* If ent->name is prefix of an entry in the
183 * cache, it will be killed.
185 pos = index_name_pos(istate, ent->name, ent->len);
186 if (0 <= pos)
187 BUG("killed-file %.*s not found",
188 ent->len, ent->name);
189 pos = -pos - 1;
190 while (pos < istate->cache_nr &&
191 ce_stage(istate->cache[pos]))
192 pos++; /* skip unmerged */
193 if (istate->cache_nr <= pos)
194 break;
195 /* pos points at a name immediately after
196 * ent->name in the cache. Does it expect
197 * ent->name to be a directory?
199 len = ce_namelen(istate->cache[pos]);
200 if ((ent->len < len) &&
201 !strncmp(istate->cache[pos]->name,
202 ent->name, ent->len) &&
203 istate->cache[pos]->name[ent->len] == '/')
204 killed = 1;
205 break;
207 if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
208 /* If any of the leading directories in
209 * ent->name is registered in the cache,
210 * ent->name will be killed.
212 killed = 1;
213 break;
216 if (killed)
217 show_dir_entry(istate, tag_killed, dir->entries[i]);
221 static void show_files(struct repository *repo, struct dir_struct *dir);
223 static void show_submodule(struct repository *superproject,
224 struct dir_struct *dir, const char *path)
226 struct repository subrepo;
228 if (repo_submodule_init(&subrepo, superproject, path, null_oid()))
229 return;
231 if (repo_read_index(&subrepo) < 0)
232 die("index file corrupt");
234 show_files(&subrepo, dir);
236 repo_clear(&subrepo);
239 struct show_index_data {
240 const char *pathname;
241 struct index_state *istate;
242 const struct cache_entry *ce;
245 static size_t expand_show_index(struct strbuf *sb, const char *start,
246 void *context)
248 struct show_index_data *data = context;
249 const char *end;
250 const char *p;
251 size_t len = strbuf_expand_literal_cb(sb, start, NULL);
252 struct stat st;
254 if (len)
255 return len;
256 if (*start != '(')
257 die(_("bad ls-files format: element '%s' "
258 "does not start with '('"), start);
260 end = strchr(start + 1, ')');
261 if (!end)
262 die(_("bad ls-files format: element '%s' "
263 "does not end in ')'"), start);
265 len = end - start + 1;
266 if (skip_prefix(start, "(objectmode)", &p))
267 strbuf_addf(sb, "%06o", data->ce->ce_mode);
268 else if (skip_prefix(start, "(objectname)", &p))
269 strbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);
270 else if (skip_prefix(start, "(stage)", &p))
271 strbuf_addf(sb, "%d", ce_stage(data->ce));
272 else if (skip_prefix(start, "(eolinfo:index)", &p))
273 strbuf_addstr(sb, S_ISREG(data->ce->ce_mode) ?
274 get_cached_convert_stats_ascii(data->istate,
275 data->ce->name) : "");
276 else if (skip_prefix(start, "(eolinfo:worktree)", &p))
277 strbuf_addstr(sb, !lstat(data->pathname, &st) &&
278 S_ISREG(st.st_mode) ?
279 get_wt_convert_stats_ascii(data->pathname) : "");
280 else if (skip_prefix(start, "(eolattr)", &p))
281 strbuf_addstr(sb, get_convert_attr_ascii(data->istate,
282 data->pathname));
283 else if (skip_prefix(start, "(path)", &p))
284 write_name_to_buf(sb, data->pathname);
285 else
286 die(_("bad ls-files format: %%%.*s"), (int)len, start);
288 return len;
291 static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
292 const char *format, const char *fullname) {
293 struct show_index_data data = {
294 .pathname = fullname,
295 .istate = repo->index,
296 .ce = ce,
298 struct strbuf sb = STRBUF_INIT;
300 strbuf_expand(&sb, format, expand_show_index, &data);
301 strbuf_addch(&sb, line_terminator);
302 fwrite(sb.buf, sb.len, 1, stdout);
303 strbuf_release(&sb);
306 static void show_ce(struct repository *repo, struct dir_struct *dir,
307 const struct cache_entry *ce, const char *fullname,
308 const char *tag)
310 if (max_prefix_len > strlen(fullname))
311 die("git ls-files: internal error - cache entry not superset of prefix");
313 if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
314 is_submodule_active(repo, ce->name)) {
315 show_submodule(repo, dir, ce->name);
316 } else if (match_pathspec(repo->index, &pathspec, fullname, strlen(fullname),
317 max_prefix_len, ps_matched,
318 S_ISDIR(ce->ce_mode) ||
319 S_ISGITLINK(ce->ce_mode))) {
320 if (format) {
321 show_ce_fmt(repo, ce, format, fullname);
322 print_debug(ce);
323 return;
326 tag = get_tag(ce, tag);
328 if (!show_stage) {
329 fputs(tag, stdout);
330 } else {
331 printf("%s%06o %s %d\t",
332 tag,
333 ce->ce_mode,
334 repo_find_unique_abbrev(repo, &ce->oid, abbrev),
335 ce_stage(ce));
337 write_eolinfo(repo->index, ce, fullname);
338 write_name(fullname);
339 print_debug(ce);
343 static void show_ru_info(struct index_state *istate)
345 struct string_list_item *item;
347 if (!istate->resolve_undo)
348 return;
350 for_each_string_list_item(item, istate->resolve_undo) {
351 const char *path = item->string;
352 struct resolve_undo_info *ui = item->util;
353 int i, len;
355 len = strlen(path);
356 if (len < max_prefix_len)
357 continue; /* outside of the prefix */
358 if (!match_pathspec(istate, &pathspec, path, len,
359 max_prefix_len, ps_matched, 0))
360 continue; /* uninterested */
361 for (i = 0; i < 3; i++) {
362 if (!ui->mode[i])
363 continue;
364 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
365 find_unique_abbrev(&ui->oid[i], abbrev),
366 i + 1);
367 write_name(path);
372 static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
373 const char *fullname, const struct cache_entry *ce)
375 int dtype = ce_to_dtype(ce);
376 return is_excluded(dir, istate, fullname, &dtype);
379 static void construct_fullname(struct strbuf *out, const struct repository *repo,
380 const struct cache_entry *ce)
382 strbuf_reset(out);
383 if (repo->submodule_prefix)
384 strbuf_addstr(out, repo->submodule_prefix);
385 strbuf_addstr(out, ce->name);
388 static void show_files(struct repository *repo, struct dir_struct *dir)
390 int i;
391 struct strbuf fullname = STRBUF_INIT;
393 /* For cached/deleted files we don't need to even do the readdir */
394 if (show_others || show_killed) {
395 if (!show_others)
396 dir->flags |= DIR_COLLECT_KILLED_ONLY;
397 fill_directory(dir, repo->index, &pathspec);
398 if (show_others)
399 show_other_files(repo->index, dir);
400 if (show_killed)
401 show_killed_files(repo->index, dir);
404 if (!(show_cached || show_stage || show_deleted || show_modified))
405 return;
407 if (!show_sparse_dirs)
408 ensure_full_index(repo->index);
410 for (i = 0; i < repo->index->cache_nr; i++) {
411 const struct cache_entry *ce = repo->index->cache[i];
412 struct stat st;
413 int stat_err;
415 construct_fullname(&fullname, repo, ce);
417 if ((dir->flags & DIR_SHOW_IGNORED) &&
418 !ce_excluded(dir, repo->index, fullname.buf, ce))
419 continue;
420 if (ce->ce_flags & CE_UPDATE)
421 continue;
422 if ((show_cached || show_stage) &&
423 (!show_unmerged || ce_stage(ce))) {
424 show_ce(repo, dir, ce, fullname.buf,
425 ce_stage(ce) ? tag_unmerged :
426 (ce_skip_worktree(ce) ? tag_skip_worktree :
427 tag_cached));
428 if (skipping_duplicates)
429 goto skip_to_next_name;
432 if (!(show_deleted || show_modified))
433 continue;
434 if (ce_skip_worktree(ce))
435 continue;
436 stat_err = lstat(fullname.buf, &st);
437 if (stat_err && (errno != ENOENT && errno != ENOTDIR))
438 error_errno("cannot lstat '%s'", fullname.buf);
439 if (stat_err && show_deleted) {
440 show_ce(repo, dir, ce, fullname.buf, tag_removed);
441 if (skipping_duplicates)
442 goto skip_to_next_name;
444 if (show_modified &&
445 (stat_err || ie_modified(repo->index, ce, &st, 0))) {
446 show_ce(repo, dir, ce, fullname.buf, tag_modified);
447 if (skipping_duplicates)
448 goto skip_to_next_name;
450 continue;
452 skip_to_next_name:
454 int j;
455 struct cache_entry **cache = repo->index->cache;
456 for (j = i + 1; j < repo->index->cache_nr; j++)
457 if (strcmp(ce->name, cache[j]->name))
458 break;
459 i = j - 1; /* compensate for the for loop */
463 strbuf_release(&fullname);
467 * Prune the index to only contain stuff starting with "prefix"
469 static void prune_index(struct index_state *istate,
470 const char *prefix, size_t prefixlen)
472 int pos;
473 unsigned int first, last;
475 if (!prefix || !istate->cache_nr)
476 return;
477 pos = index_name_pos(istate, prefix, prefixlen);
478 if (pos < 0)
479 pos = -pos-1;
480 first = pos;
481 last = istate->cache_nr;
482 while (last > first) {
483 int next = first + ((last - first) >> 1);
484 const struct cache_entry *ce = istate->cache[next];
485 if (!strncmp(ce->name, prefix, prefixlen)) {
486 first = next+1;
487 continue;
489 last = next;
491 MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos);
492 istate->cache_nr = last - pos;
495 static int get_common_prefix_len(const char *common_prefix)
497 int common_prefix_len;
499 if (!common_prefix)
500 return 0;
502 common_prefix_len = strlen(common_prefix);
505 * If the prefix has a trailing slash, strip it so that submodules wont
506 * be pruned from the index.
508 if (common_prefix[common_prefix_len - 1] == '/')
509 common_prefix_len--;
511 return common_prefix_len;
514 static int read_one_entry_opt(struct index_state *istate,
515 const struct object_id *oid,
516 struct strbuf *base,
517 const char *pathname,
518 unsigned mode, int opt)
520 int len;
521 struct cache_entry *ce;
523 if (S_ISDIR(mode))
524 return READ_TREE_RECURSIVE;
526 len = strlen(pathname);
527 ce = make_empty_cache_entry(istate, base->len + len);
529 ce->ce_mode = create_ce_mode(mode);
530 ce->ce_flags = create_ce_flags(1);
531 ce->ce_namelen = base->len + len;
532 memcpy(ce->name, base->buf, base->len);
533 memcpy(ce->name + base->len, pathname, len+1);
534 oidcpy(&ce->oid, oid);
535 return add_index_entry(istate, ce, opt);
538 static int read_one_entry(const struct object_id *oid, struct strbuf *base,
539 const char *pathname, unsigned mode,
540 void *context)
542 struct index_state *istate = context;
543 return read_one_entry_opt(istate, oid, base, pathname,
544 mode,
545 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
549 * This is used when the caller knows there is no existing entries at
550 * the stage that will conflict with the entry being added.
552 static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
553 const char *pathname, unsigned mode,
554 void *context)
556 struct index_state *istate = context;
557 return read_one_entry_opt(istate, oid, base, pathname,
558 mode, ADD_CACHE_JUST_APPEND);
562 * Read the tree specified with --with-tree option
563 * (typically, HEAD) into stage #1 and then
564 * squash them down to stage #0. This is used for
565 * --error-unmatch to list and check the path patterns
566 * that were given from the command line. We are not
567 * going to write this index out.
569 void overlay_tree_on_index(struct index_state *istate,
570 const char *tree_name, const char *prefix)
572 struct tree *tree;
573 struct object_id oid;
574 struct pathspec pathspec;
575 struct cache_entry *last_stage0 = NULL;
576 int i;
577 read_tree_fn_t fn = NULL;
578 int err;
580 if (get_oid(tree_name, &oid))
581 die("tree-ish %s not found.", tree_name);
582 tree = parse_tree_indirect(&oid);
583 if (!tree)
584 die("bad tree-ish %s", tree_name);
586 /* Hoist the unmerged entries up to stage #3 to make room */
587 /* TODO: audit for interaction with sparse-index. */
588 ensure_full_index(istate);
589 for (i = 0; i < istate->cache_nr; i++) {
590 struct cache_entry *ce = istate->cache[i];
591 if (!ce_stage(ce))
592 continue;
593 ce->ce_flags |= CE_STAGEMASK;
596 if (prefix) {
597 static const char *(matchbuf[1]);
598 matchbuf[0] = NULL;
599 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
600 PATHSPEC_PREFER_CWD, prefix, matchbuf);
601 } else
602 memset(&pathspec, 0, sizeof(pathspec));
605 * See if we have cache entry at the stage. If so,
606 * do it the original slow way, otherwise, append and then
607 * sort at the end.
609 for (i = 0; !fn && i < istate->cache_nr; i++) {
610 const struct cache_entry *ce = istate->cache[i];
611 if (ce_stage(ce) == 1)
612 fn = read_one_entry;
615 if (!fn)
616 fn = read_one_entry_quick;
617 err = read_tree(the_repository, tree, &pathspec, fn, istate);
618 clear_pathspec(&pathspec);
619 if (err)
620 die("unable to read tree entries %s", tree_name);
623 * Sort the cache entry -- we need to nuke the cache tree, though.
625 if (fn == read_one_entry_quick) {
626 cache_tree_free(&istate->cache_tree);
627 QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
630 for (i = 0; i < istate->cache_nr; i++) {
631 struct cache_entry *ce = istate->cache[i];
632 switch (ce_stage(ce)) {
633 case 0:
634 last_stage0 = ce;
635 /* fallthru */
636 default:
637 continue;
638 case 1:
640 * If there is stage #0 entry for this, we do not
641 * need to show it. We use CE_UPDATE bit to mark
642 * such an entry.
644 if (last_stage0 &&
645 !strcmp(last_stage0->name, ce->name))
646 ce->ce_flags |= CE_UPDATE;
651 static const char * const ls_files_usage[] = {
652 N_("git ls-files [<options>] [<file>...]"),
653 NULL
656 static int option_parse_exclude(const struct option *opt,
657 const char *arg, int unset)
659 struct string_list *exclude_list = opt->value;
661 BUG_ON_OPT_NEG(unset);
663 exc_given = 1;
664 string_list_append(exclude_list, arg);
666 return 0;
669 static int option_parse_exclude_from(const struct option *opt,
670 const char *arg, int unset)
672 struct dir_struct *dir = opt->value;
674 BUG_ON_OPT_NEG(unset);
676 exc_given = 1;
677 add_patterns_from_file(dir, arg);
679 return 0;
682 static int option_parse_exclude_standard(const struct option *opt,
683 const char *arg, int unset)
685 struct dir_struct *dir = opt->value;
687 BUG_ON_OPT_NEG(unset);
688 BUG_ON_OPT_ARG(arg);
690 exc_given = 1;
691 setup_standard_excludes(dir);
693 return 0;
696 int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
698 int require_work_tree = 0, show_tag = 0, i;
699 char *max_prefix;
700 struct dir_struct dir = DIR_INIT;
701 struct pattern_list *pl;
702 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
703 struct option builtin_ls_files_options[] = {
704 /* Think twice before adding "--nul" synonym to this */
705 OPT_SET_INT('z', NULL, &line_terminator,
706 N_("separate paths with the NUL character"), '\0'),
707 OPT_BOOL('t', NULL, &show_tag,
708 N_("identify the file status with tags")),
709 OPT_BOOL('v', NULL, &show_valid_bit,
710 N_("use lowercase letters for 'assume unchanged' files")),
711 OPT_BOOL('f', NULL, &show_fsmonitor_bit,
712 N_("use lowercase letters for 'fsmonitor clean' files")),
713 OPT_BOOL('c', "cached", &show_cached,
714 N_("show cached files in the output (default)")),
715 OPT_BOOL('d', "deleted", &show_deleted,
716 N_("show deleted files in the output")),
717 OPT_BOOL('m', "modified", &show_modified,
718 N_("show modified files in the output")),
719 OPT_BOOL('o', "others", &show_others,
720 N_("show other files in the output")),
721 OPT_BIT('i', "ignored", &dir.flags,
722 N_("show ignored files in the output"),
723 DIR_SHOW_IGNORED),
724 OPT_BOOL('s', "stage", &show_stage,
725 N_("show staged contents' object name in the output")),
726 OPT_BOOL('k', "killed", &show_killed,
727 N_("show files on the filesystem that need to be removed")),
728 OPT_BIT(0, "directory", &dir.flags,
729 N_("show 'other' directories' names only"),
730 DIR_SHOW_OTHER_DIRECTORIES),
731 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
732 OPT_NEGBIT(0, "empty-directory", &dir.flags,
733 N_("don't show empty directories"),
734 DIR_HIDE_EMPTY_DIRECTORIES),
735 OPT_BOOL('u', "unmerged", &show_unmerged,
736 N_("show unmerged files in the output")),
737 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
738 N_("show resolve-undo information")),
739 OPT_CALLBACK_F('x', "exclude", &exclude_list, N_("pattern"),
740 N_("skip files matching pattern"),
741 PARSE_OPT_NONEG, option_parse_exclude),
742 OPT_CALLBACK_F('X', "exclude-from", &dir, N_("file"),
743 N_("read exclude patterns from <file>"),
744 PARSE_OPT_NONEG, option_parse_exclude_from),
745 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
746 N_("read additional per-directory exclude patterns in <file>")),
747 OPT_CALLBACK_F(0, "exclude-standard", &dir, NULL,
748 N_("add the standard git exclusions"),
749 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
750 option_parse_exclude_standard),
751 OPT_SET_INT_F(0, "full-name", &prefix_len,
752 N_("make the output relative to the project top directory"),
753 0, PARSE_OPT_NONEG),
754 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
755 N_("recurse through submodules")),
756 OPT_BOOL(0, "error-unmatch", &error_unmatch,
757 N_("if any <file> is not in the index, treat this as an error")),
758 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
759 N_("pretend that paths removed since <tree-ish> are still present")),
760 OPT__ABBREV(&abbrev),
761 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
762 OPT_BOOL(0, "deduplicate", &skipping_duplicates,
763 N_("suppress duplicate entries")),
764 OPT_BOOL(0, "sparse", &show_sparse_dirs,
765 N_("show sparse directories in the presence of a sparse index")),
766 OPT_STRING_F(0, "format", &format, N_("format"),
767 N_("format to use for the output"),
768 PARSE_OPT_NONEG),
769 OPT_END()
771 int ret = 0;
773 if (argc == 2 && !strcmp(argv[1], "-h"))
774 usage_with_options(ls_files_usage, builtin_ls_files_options);
776 prepare_repo_settings(the_repository);
777 the_repository->settings.command_requires_full_index = 0;
779 prefix = cmd_prefix;
780 if (prefix)
781 prefix_len = strlen(prefix);
782 git_config(git_default_config, NULL);
784 if (repo_read_index(the_repository) < 0)
785 die("index file corrupt");
787 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
788 ls_files_usage, 0);
789 pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
790 for (i = 0; i < exclude_list.nr; i++) {
791 add_pattern(exclude_list.items[i].string, "", 0, pl, --exclude_args);
794 if (format && (show_stage || show_others || show_killed ||
795 show_resolve_undo || skipping_duplicates || show_eol || show_tag))
796 usage_msg_opt(_("--format cannot be used with -s, -o, -k, -t, "
797 "--resolve-undo, --deduplicate, --eol"),
798 ls_files_usage, builtin_ls_files_options);
800 if (show_tag || show_valid_bit || show_fsmonitor_bit) {
801 tag_cached = "H ";
802 tag_unmerged = "M ";
803 tag_removed = "R ";
804 tag_modified = "C ";
805 tag_other = "? ";
806 tag_killed = "K ";
807 tag_skip_worktree = "S ";
808 tag_resolve_undo = "U ";
810 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
811 require_work_tree = 1;
812 if (show_unmerged)
814 * There's no point in showing unmerged unless
815 * you also show the stage information.
817 show_stage = 1;
818 if (show_tag || show_stage)
819 skipping_duplicates = 0;
820 if (dir.exclude_per_dir)
821 exc_given = 1;
823 if (require_work_tree && !is_inside_work_tree())
824 setup_work_tree();
826 if (recurse_submodules &&
827 (show_deleted || show_others || show_unmerged ||
828 show_killed || show_modified || show_resolve_undo || with_tree))
829 die("ls-files --recurse-submodules unsupported mode");
831 if (recurse_submodules && error_unmatch)
832 die("ls-files --recurse-submodules does not support "
833 "--error-unmatch");
835 parse_pathspec(&pathspec, 0,
836 PATHSPEC_PREFER_CWD,
837 prefix, argv);
840 * Find common prefix for all pathspec's
841 * This is used as a performance optimization which unfortunately cannot
842 * be done when recursing into submodules because when a pathspec is
843 * given which spans repository boundaries you can't simply remove the
844 * submodule entry because the pathspec may match something inside the
845 * submodule.
847 if (recurse_submodules)
848 max_prefix = NULL;
849 else
850 max_prefix = common_prefix(&pathspec);
851 max_prefix_len = get_common_prefix_len(max_prefix);
853 prune_index(the_repository->index, max_prefix, max_prefix_len);
855 /* Treat unmatching pathspec elements as errors */
856 if (pathspec.nr && error_unmatch)
857 ps_matched = xcalloc(pathspec.nr, 1);
859 if ((dir.flags & DIR_SHOW_IGNORED) && !show_others && !show_cached)
860 die("ls-files -i must be used with either -o or -c");
862 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
863 die("ls-files --ignored needs some exclude pattern");
865 /* With no flags, we default to showing the cached files */
866 if (!(show_stage || show_deleted || show_others || show_unmerged ||
867 show_killed || show_modified || show_resolve_undo))
868 show_cached = 1;
870 if (with_tree) {
872 * Basic sanity check; show-stages and show-unmerged
873 * would not make any sense with this option.
875 if (show_stage || show_unmerged)
876 die(_("options '%s' and '%s' cannot be used together"), "ls-files --with-tree", "-s/-u");
877 overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
880 show_files(the_repository, &dir);
882 if (show_resolve_undo)
883 show_ru_info(the_repository->index);
885 if (ps_matched && report_path_error(ps_matched, &pathspec)) {
886 fprintf(stderr, "Did you forget to 'git add'?\n");
887 ret = 1;
890 string_list_clear(&exclude_list, 0);
891 dir_clear(&dir);
892 free(max_prefix);
893 return ret;