trace.c, git.c: remove unnecessary parameter to trace_repo_setup()
[git/debian.git] / builtin / ls-files.c
bloba03b559ecaab4caeba9a9a18bb268f22274e7b2a
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 "strbuf.h"
15 #include "tree.h"
16 #include "cache-tree.h"
17 #include "parse-options.h"
18 #include "resolve-undo.h"
19 #include "string-list.h"
20 #include "pathspec.h"
21 #include "run-command.h"
22 #include "submodule.h"
23 #include "submodule-config.h"
25 static int abbrev;
26 static int show_deleted;
27 static int show_cached;
28 static int show_others;
29 static int show_stage;
30 static int show_unmerged;
31 static int show_resolve_undo;
32 static int show_modified;
33 static int show_killed;
34 static int show_valid_bit;
35 static int show_fsmonitor_bit;
36 static int line_terminator = '\n';
37 static int debug_mode;
38 static int show_eol;
39 static int recurse_submodules;
40 static int skipping_duplicates;
41 static int show_sparse_dirs;
43 static const char *prefix;
44 static int max_prefix_len;
45 static int prefix_len;
46 static struct pathspec pathspec;
47 static int error_unmatch;
48 static char *ps_matched;
49 static const char *with_tree;
50 static int exc_given;
51 static int exclude_args;
52 static const char *format;
54 static const char *tag_cached = "";
55 static const char *tag_unmerged = "";
56 static const char *tag_removed = "";
57 static const char *tag_other = "";
58 static const char *tag_killed = "";
59 static const char *tag_modified = "";
60 static const char *tag_skip_worktree = "";
61 static const char *tag_resolve_undo = "";
63 static void write_eolinfo(struct index_state *istate,
64 const struct cache_entry *ce, const char *path)
66 if (show_eol) {
67 struct stat st;
68 const char *i_txt = "";
69 const char *w_txt = "";
70 const char *a_txt = get_convert_attr_ascii(istate, path);
71 if (ce && S_ISREG(ce->ce_mode))
72 i_txt = get_cached_convert_stats_ascii(istate,
73 ce->name);
74 if (!lstat(path, &st) && S_ISREG(st.st_mode))
75 w_txt = get_wt_convert_stats_ascii(path);
76 printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
80 static void write_name(const char *name)
83 * With "--full-name", prefix_len=0; this caller needs to pass
84 * an empty string in that case (a NULL is good for "").
86 write_name_quoted_relative(name, prefix_len ? prefix : NULL,
87 stdout, line_terminator);
90 static void write_name_to_buf(struct strbuf *sb, const char *name)
92 const char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);
94 if (line_terminator)
95 quote_c_style(rel, sb, NULL, 0);
96 else
97 strbuf_addstr(sb, rel);
100 static const char *get_tag(const struct cache_entry *ce, const char *tag)
102 static char alttag[4];
104 if (tag && *tag && ((show_valid_bit && (ce->ce_flags & CE_VALID)) ||
105 (show_fsmonitor_bit && (ce->ce_flags & CE_FSMONITOR_VALID)))) {
106 memcpy(alttag, tag, 3);
108 if (isalpha(tag[0])) {
109 alttag[0] = tolower(tag[0]);
110 } else if (tag[0] == '?') {
111 alttag[0] = '!';
112 } else {
113 alttag[0] = 'v';
114 alttag[1] = tag[0];
115 alttag[2] = ' ';
116 alttag[3] = 0;
119 tag = alttag;
122 return tag;
125 static void print_debug(const struct cache_entry *ce)
127 if (debug_mode) {
128 const struct stat_data *sd = &ce->ce_stat_data;
130 printf(" ctime: %u:%u\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
131 printf(" mtime: %u:%u\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
132 printf(" dev: %u\tino: %u\n", sd->sd_dev, sd->sd_ino);
133 printf(" uid: %u\tgid: %u\n", sd->sd_uid, sd->sd_gid);
134 printf(" size: %u\tflags: %x\n", sd->sd_size, ce->ce_flags);
138 static void show_dir_entry(struct index_state *istate,
139 const char *tag, struct dir_entry *ent)
141 int len = max_prefix_len;
143 if (len > ent->len)
144 die("git ls-files: internal error - directory entry not superset of prefix");
146 /* If ps_matches is non-NULL, figure out which pathspec(s) match. */
147 if (ps_matched)
148 dir_path_match(istate, ent, &pathspec, len, ps_matched);
150 fputs(tag, stdout);
151 write_eolinfo(istate, NULL, ent->name);
152 write_name(ent->name);
155 static void show_other_files(struct index_state *istate,
156 const struct dir_struct *dir)
158 int i;
160 for (i = 0; i < dir->nr; i++) {
161 struct dir_entry *ent = dir->entries[i];
162 if (!index_name_is_other(istate, ent->name, ent->len))
163 continue;
164 show_dir_entry(istate, tag_other, ent);
168 static void show_killed_files(struct index_state *istate,
169 const struct dir_struct *dir)
171 int i;
172 for (i = 0; i < dir->nr; i++) {
173 struct dir_entry *ent = dir->entries[i];
174 char *cp, *sp;
175 int pos, len, killed = 0;
177 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
178 sp = strchr(cp, '/');
179 if (!sp) {
180 /* If ent->name is prefix of an entry in the
181 * cache, it will be killed.
183 pos = index_name_pos(istate, ent->name, ent->len);
184 if (0 <= pos)
185 BUG("killed-file %.*s not found",
186 ent->len, ent->name);
187 pos = -pos - 1;
188 while (pos < istate->cache_nr &&
189 ce_stage(istate->cache[pos]))
190 pos++; /* skip unmerged */
191 if (istate->cache_nr <= pos)
192 break;
193 /* pos points at a name immediately after
194 * ent->name in the cache. Does it expect
195 * ent->name to be a directory?
197 len = ce_namelen(istate->cache[pos]);
198 if ((ent->len < len) &&
199 !strncmp(istate->cache[pos]->name,
200 ent->name, ent->len) &&
201 istate->cache[pos]->name[ent->len] == '/')
202 killed = 1;
203 break;
205 if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
206 /* If any of the leading directories in
207 * ent->name is registered in the cache,
208 * ent->name will be killed.
210 killed = 1;
211 break;
214 if (killed)
215 show_dir_entry(istate, tag_killed, dir->entries[i]);
219 static void show_files(struct repository *repo, struct dir_struct *dir);
221 static void show_submodule(struct repository *superproject,
222 struct dir_struct *dir, const char *path)
224 struct repository subrepo;
226 if (repo_submodule_init(&subrepo, superproject, path, null_oid()))
227 return;
229 if (repo_read_index(&subrepo) < 0)
230 die("index file corrupt");
232 show_files(&subrepo, dir);
234 repo_clear(&subrepo);
237 struct show_index_data {
238 const char *pathname;
239 struct index_state *istate;
240 const struct cache_entry *ce;
243 static size_t expand_show_index(struct strbuf *sb, const char *start,
244 void *context)
246 struct show_index_data *data = context;
247 const char *end;
248 const char *p;
249 size_t len = strbuf_expand_literal_cb(sb, start, NULL);
250 struct stat st;
252 if (len)
253 return len;
254 if (*start != '(')
255 die(_("bad ls-files format: element '%s' "
256 "does not start with '('"), start);
258 end = strchr(start + 1, ')');
259 if (!end)
260 die(_("bad ls-files format: element '%s' "
261 "does not end in ')'"), start);
263 len = end - start + 1;
264 if (skip_prefix(start, "(objectmode)", &p))
265 strbuf_addf(sb, "%06o", data->ce->ce_mode);
266 else if (skip_prefix(start, "(objectname)", &p))
267 strbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);
268 else if (skip_prefix(start, "(stage)", &p))
269 strbuf_addf(sb, "%d", ce_stage(data->ce));
270 else if (skip_prefix(start, "(eolinfo:index)", &p))
271 strbuf_addstr(sb, S_ISREG(data->ce->ce_mode) ?
272 get_cached_convert_stats_ascii(data->istate,
273 data->ce->name) : "");
274 else if (skip_prefix(start, "(eolinfo:worktree)", &p))
275 strbuf_addstr(sb, !lstat(data->pathname, &st) &&
276 S_ISREG(st.st_mode) ?
277 get_wt_convert_stats_ascii(data->pathname) : "");
278 else if (skip_prefix(start, "(eolattr)", &p))
279 strbuf_addstr(sb, get_convert_attr_ascii(data->istate,
280 data->pathname));
281 else if (skip_prefix(start, "(path)", &p))
282 write_name_to_buf(sb, data->pathname);
283 else
284 die(_("bad ls-files format: %%%.*s"), (int)len, start);
286 return len;
289 static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
290 const char *format, const char *fullname) {
291 struct show_index_data data = {
292 .pathname = fullname,
293 .istate = repo->index,
294 .ce = ce,
296 struct strbuf sb = STRBUF_INIT;
298 strbuf_expand(&sb, format, expand_show_index, &data);
299 strbuf_addch(&sb, line_terminator);
300 fwrite(sb.buf, sb.len, 1, stdout);
301 strbuf_release(&sb);
304 static void show_ce(struct repository *repo, struct dir_struct *dir,
305 const struct cache_entry *ce, const char *fullname,
306 const char *tag)
308 if (max_prefix_len > strlen(fullname))
309 die("git ls-files: internal error - cache entry not superset of prefix");
311 if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
312 is_submodule_active(repo, ce->name)) {
313 show_submodule(repo, dir, ce->name);
314 } else if (match_pathspec(repo->index, &pathspec, fullname, strlen(fullname),
315 max_prefix_len, ps_matched,
316 S_ISDIR(ce->ce_mode) ||
317 S_ISGITLINK(ce->ce_mode))) {
318 if (format) {
319 show_ce_fmt(repo, ce, format, fullname);
320 print_debug(ce);
321 return;
324 tag = get_tag(ce, tag);
326 if (!show_stage) {
327 fputs(tag, stdout);
328 } else {
329 printf("%s%06o %s %d\t",
330 tag,
331 ce->ce_mode,
332 repo_find_unique_abbrev(repo, &ce->oid, abbrev),
333 ce_stage(ce));
335 write_eolinfo(repo->index, ce, fullname);
336 write_name(fullname);
337 print_debug(ce);
341 static void show_ru_info(struct index_state *istate)
343 struct string_list_item *item;
345 if (!istate->resolve_undo)
346 return;
348 for_each_string_list_item(item, istate->resolve_undo) {
349 const char *path = item->string;
350 struct resolve_undo_info *ui = item->util;
351 int i, len;
353 len = strlen(path);
354 if (len < max_prefix_len)
355 continue; /* outside of the prefix */
356 if (!match_pathspec(istate, &pathspec, path, len,
357 max_prefix_len, ps_matched, 0))
358 continue; /* uninterested */
359 for (i = 0; i < 3; i++) {
360 if (!ui->mode[i])
361 continue;
362 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
363 find_unique_abbrev(&ui->oid[i], abbrev),
364 i + 1);
365 write_name(path);
370 static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
371 const char *fullname, const struct cache_entry *ce)
373 int dtype = ce_to_dtype(ce);
374 return is_excluded(dir, istate, fullname, &dtype);
377 static void construct_fullname(struct strbuf *out, const struct repository *repo,
378 const struct cache_entry *ce)
380 strbuf_reset(out);
381 if (repo->submodule_prefix)
382 strbuf_addstr(out, repo->submodule_prefix);
383 strbuf_addstr(out, ce->name);
386 static void show_files(struct repository *repo, struct dir_struct *dir)
388 int i;
389 struct strbuf fullname = STRBUF_INIT;
391 /* For cached/deleted files we don't need to even do the readdir */
392 if (show_others || show_killed) {
393 if (!show_others)
394 dir->flags |= DIR_COLLECT_KILLED_ONLY;
395 fill_directory(dir, repo->index, &pathspec);
396 if (show_others)
397 show_other_files(repo->index, dir);
398 if (show_killed)
399 show_killed_files(repo->index, dir);
402 if (!(show_cached || show_stage || show_deleted || show_modified))
403 return;
405 if (!show_sparse_dirs)
406 ensure_full_index(repo->index);
408 for (i = 0; i < repo->index->cache_nr; i++) {
409 const struct cache_entry *ce = repo->index->cache[i];
410 struct stat st;
411 int stat_err;
413 construct_fullname(&fullname, repo, ce);
415 if ((dir->flags & DIR_SHOW_IGNORED) &&
416 !ce_excluded(dir, repo->index, fullname.buf, ce))
417 continue;
418 if (ce->ce_flags & CE_UPDATE)
419 continue;
420 if ((show_cached || show_stage) &&
421 (!show_unmerged || ce_stage(ce))) {
422 show_ce(repo, dir, ce, fullname.buf,
423 ce_stage(ce) ? tag_unmerged :
424 (ce_skip_worktree(ce) ? tag_skip_worktree :
425 tag_cached));
426 if (skipping_duplicates)
427 goto skip_to_next_name;
430 if (!(show_deleted || show_modified))
431 continue;
432 if (ce_skip_worktree(ce))
433 continue;
434 stat_err = lstat(fullname.buf, &st);
435 if (stat_err && (errno != ENOENT && errno != ENOTDIR))
436 error_errno("cannot lstat '%s'", fullname.buf);
437 if (stat_err && show_deleted) {
438 show_ce(repo, dir, ce, fullname.buf, tag_removed);
439 if (skipping_duplicates)
440 goto skip_to_next_name;
442 if (show_modified &&
443 (stat_err || ie_modified(repo->index, ce, &st, 0))) {
444 show_ce(repo, dir, ce, fullname.buf, tag_modified);
445 if (skipping_duplicates)
446 goto skip_to_next_name;
448 continue;
450 skip_to_next_name:
452 int j;
453 struct cache_entry **cache = repo->index->cache;
454 for (j = i + 1; j < repo->index->cache_nr; j++)
455 if (strcmp(ce->name, cache[j]->name))
456 break;
457 i = j - 1; /* compensate for the for loop */
461 strbuf_release(&fullname);
465 * Prune the index to only contain stuff starting with "prefix"
467 static void prune_index(struct index_state *istate,
468 const char *prefix, size_t prefixlen)
470 int pos;
471 unsigned int first, last;
473 if (!prefix || !istate->cache_nr)
474 return;
475 pos = index_name_pos(istate, prefix, prefixlen);
476 if (pos < 0)
477 pos = -pos-1;
478 first = pos;
479 last = istate->cache_nr;
480 while (last > first) {
481 int next = first + ((last - first) >> 1);
482 const struct cache_entry *ce = istate->cache[next];
483 if (!strncmp(ce->name, prefix, prefixlen)) {
484 first = next+1;
485 continue;
487 last = next;
489 MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos);
490 istate->cache_nr = last - pos;
493 static int get_common_prefix_len(const char *common_prefix)
495 int common_prefix_len;
497 if (!common_prefix)
498 return 0;
500 common_prefix_len = strlen(common_prefix);
503 * If the prefix has a trailing slash, strip it so that submodules wont
504 * be pruned from the index.
506 if (common_prefix[common_prefix_len - 1] == '/')
507 common_prefix_len--;
509 return common_prefix_len;
512 static int read_one_entry_opt(struct index_state *istate,
513 const struct object_id *oid,
514 struct strbuf *base,
515 const char *pathname,
516 unsigned mode, int opt)
518 int len;
519 struct cache_entry *ce;
521 if (S_ISDIR(mode))
522 return READ_TREE_RECURSIVE;
524 len = strlen(pathname);
525 ce = make_empty_cache_entry(istate, base->len + len);
527 ce->ce_mode = create_ce_mode(mode);
528 ce->ce_flags = create_ce_flags(1);
529 ce->ce_namelen = base->len + len;
530 memcpy(ce->name, base->buf, base->len);
531 memcpy(ce->name + base->len, pathname, len+1);
532 oidcpy(&ce->oid, oid);
533 return add_index_entry(istate, ce, opt);
536 static int read_one_entry(const struct object_id *oid, struct strbuf *base,
537 const char *pathname, unsigned mode,
538 void *context)
540 struct index_state *istate = context;
541 return read_one_entry_opt(istate, oid, base, pathname,
542 mode,
543 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
547 * This is used when the caller knows there is no existing entries at
548 * the stage that will conflict with the entry being added.
550 static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
551 const char *pathname, unsigned mode,
552 void *context)
554 struct index_state *istate = context;
555 return read_one_entry_opt(istate, oid, base, pathname,
556 mode, ADD_CACHE_JUST_APPEND);
560 * Read the tree specified with --with-tree option
561 * (typically, HEAD) into stage #1 and then
562 * squash them down to stage #0. This is used for
563 * --error-unmatch to list and check the path patterns
564 * that were given from the command line. We are not
565 * going to write this index out.
567 void overlay_tree_on_index(struct index_state *istate,
568 const char *tree_name, const char *prefix)
570 struct tree *tree;
571 struct object_id oid;
572 struct pathspec pathspec;
573 struct cache_entry *last_stage0 = NULL;
574 int i;
575 read_tree_fn_t fn = NULL;
576 int err;
578 if (get_oid(tree_name, &oid))
579 die("tree-ish %s not found.", tree_name);
580 tree = parse_tree_indirect(&oid);
581 if (!tree)
582 die("bad tree-ish %s", tree_name);
584 /* Hoist the unmerged entries up to stage #3 to make room */
585 /* TODO: audit for interaction with sparse-index. */
586 ensure_full_index(istate);
587 for (i = 0; i < istate->cache_nr; i++) {
588 struct cache_entry *ce = istate->cache[i];
589 if (!ce_stage(ce))
590 continue;
591 ce->ce_flags |= CE_STAGEMASK;
594 if (prefix) {
595 static const char *(matchbuf[1]);
596 matchbuf[0] = NULL;
597 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
598 PATHSPEC_PREFER_CWD, prefix, matchbuf);
599 } else
600 memset(&pathspec, 0, sizeof(pathspec));
603 * See if we have cache entry at the stage. If so,
604 * do it the original slow way, otherwise, append and then
605 * sort at the end.
607 for (i = 0; !fn && i < istate->cache_nr; i++) {
608 const struct cache_entry *ce = istate->cache[i];
609 if (ce_stage(ce) == 1)
610 fn = read_one_entry;
613 if (!fn)
614 fn = read_one_entry_quick;
615 err = read_tree(the_repository, tree, &pathspec, fn, istate);
616 clear_pathspec(&pathspec);
617 if (err)
618 die("unable to read tree entries %s", tree_name);
621 * Sort the cache entry -- we need to nuke the cache tree, though.
623 if (fn == read_one_entry_quick) {
624 cache_tree_free(&istate->cache_tree);
625 QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
628 for (i = 0; i < istate->cache_nr; i++) {
629 struct cache_entry *ce = istate->cache[i];
630 switch (ce_stage(ce)) {
631 case 0:
632 last_stage0 = ce;
633 /* fallthru */
634 default:
635 continue;
636 case 1:
638 * If there is stage #0 entry for this, we do not
639 * need to show it. We use CE_UPDATE bit to mark
640 * such an entry.
642 if (last_stage0 &&
643 !strcmp(last_stage0->name, ce->name))
644 ce->ce_flags |= CE_UPDATE;
649 static const char * const ls_files_usage[] = {
650 N_("git ls-files [<options>] [<file>...]"),
651 NULL
654 static int option_parse_exclude(const struct option *opt,
655 const char *arg, int unset)
657 struct string_list *exclude_list = opt->value;
659 BUG_ON_OPT_NEG(unset);
661 exc_given = 1;
662 string_list_append(exclude_list, arg);
664 return 0;
667 static int option_parse_exclude_from(const struct option *opt,
668 const char *arg, int unset)
670 struct dir_struct *dir = opt->value;
672 BUG_ON_OPT_NEG(unset);
674 exc_given = 1;
675 add_patterns_from_file(dir, arg);
677 return 0;
680 static int option_parse_exclude_standard(const struct option *opt,
681 const char *arg, int unset)
683 struct dir_struct *dir = opt->value;
685 BUG_ON_OPT_NEG(unset);
686 BUG_ON_OPT_ARG(arg);
688 exc_given = 1;
689 setup_standard_excludes(dir);
691 return 0;
694 int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
696 int require_work_tree = 0, show_tag = 0, i;
697 char *max_prefix;
698 struct dir_struct dir = DIR_INIT;
699 struct pattern_list *pl;
700 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
701 struct option builtin_ls_files_options[] = {
702 /* Think twice before adding "--nul" synonym to this */
703 OPT_SET_INT('z', NULL, &line_terminator,
704 N_("separate paths with the NUL character"), '\0'),
705 OPT_BOOL('t', NULL, &show_tag,
706 N_("identify the file status with tags")),
707 OPT_BOOL('v', NULL, &show_valid_bit,
708 N_("use lowercase letters for 'assume unchanged' files")),
709 OPT_BOOL('f', NULL, &show_fsmonitor_bit,
710 N_("use lowercase letters for 'fsmonitor clean' files")),
711 OPT_BOOL('c', "cached", &show_cached,
712 N_("show cached files in the output (default)")),
713 OPT_BOOL('d', "deleted", &show_deleted,
714 N_("show deleted files in the output")),
715 OPT_BOOL('m', "modified", &show_modified,
716 N_("show modified files in the output")),
717 OPT_BOOL('o', "others", &show_others,
718 N_("show other files in the output")),
719 OPT_BIT('i', "ignored", &dir.flags,
720 N_("show ignored files in the output"),
721 DIR_SHOW_IGNORED),
722 OPT_BOOL('s', "stage", &show_stage,
723 N_("show staged contents' object name in the output")),
724 OPT_BOOL('k', "killed", &show_killed,
725 N_("show files on the filesystem that need to be removed")),
726 OPT_BIT(0, "directory", &dir.flags,
727 N_("show 'other' directories' names only"),
728 DIR_SHOW_OTHER_DIRECTORIES),
729 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
730 OPT_NEGBIT(0, "empty-directory", &dir.flags,
731 N_("don't show empty directories"),
732 DIR_HIDE_EMPTY_DIRECTORIES),
733 OPT_BOOL('u', "unmerged", &show_unmerged,
734 N_("show unmerged files in the output")),
735 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
736 N_("show resolve-undo information")),
737 OPT_CALLBACK_F('x', "exclude", &exclude_list, N_("pattern"),
738 N_("skip files matching pattern"),
739 PARSE_OPT_NONEG, option_parse_exclude),
740 OPT_CALLBACK_F('X', "exclude-from", &dir, N_("file"),
741 N_("read exclude patterns from <file>"),
742 PARSE_OPT_NONEG, option_parse_exclude_from),
743 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
744 N_("read additional per-directory exclude patterns in <file>")),
745 OPT_CALLBACK_F(0, "exclude-standard", &dir, NULL,
746 N_("add the standard git exclusions"),
747 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
748 option_parse_exclude_standard),
749 OPT_SET_INT_F(0, "full-name", &prefix_len,
750 N_("make the output relative to the project top directory"),
751 0, PARSE_OPT_NONEG),
752 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
753 N_("recurse through submodules")),
754 OPT_BOOL(0, "error-unmatch", &error_unmatch,
755 N_("if any <file> is not in the index, treat this as an error")),
756 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
757 N_("pretend that paths removed since <tree-ish> are still present")),
758 OPT__ABBREV(&abbrev),
759 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
760 OPT_BOOL(0, "deduplicate", &skipping_duplicates,
761 N_("suppress duplicate entries")),
762 OPT_BOOL(0, "sparse", &show_sparse_dirs,
763 N_("show sparse directories in the presence of a sparse index")),
764 OPT_STRING_F(0, "format", &format, N_("format"),
765 N_("format to use for the output"),
766 PARSE_OPT_NONEG),
767 OPT_END()
769 int ret = 0;
771 if (argc == 2 && !strcmp(argv[1], "-h"))
772 usage_with_options(ls_files_usage, builtin_ls_files_options);
774 prepare_repo_settings(the_repository);
775 the_repository->settings.command_requires_full_index = 0;
777 prefix = cmd_prefix;
778 if (prefix)
779 prefix_len = strlen(prefix);
780 git_config(git_default_config, NULL);
782 if (repo_read_index(the_repository) < 0)
783 die("index file corrupt");
785 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
786 ls_files_usage, 0);
787 pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
788 for (i = 0; i < exclude_list.nr; i++) {
789 add_pattern(exclude_list.items[i].string, "", 0, pl, --exclude_args);
792 if (format && (show_stage || show_others || show_killed ||
793 show_resolve_undo || skipping_duplicates || show_eol || show_tag))
794 usage_msg_opt(_("--format cannot be used with -s, -o, -k, -t, "
795 "--resolve-undo, --deduplicate, --eol"),
796 ls_files_usage, builtin_ls_files_options);
798 if (show_tag || show_valid_bit || show_fsmonitor_bit) {
799 tag_cached = "H ";
800 tag_unmerged = "M ";
801 tag_removed = "R ";
802 tag_modified = "C ";
803 tag_other = "? ";
804 tag_killed = "K ";
805 tag_skip_worktree = "S ";
806 tag_resolve_undo = "U ";
808 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
809 require_work_tree = 1;
810 if (show_unmerged)
812 * There's no point in showing unmerged unless
813 * you also show the stage information.
815 show_stage = 1;
816 if (show_tag || show_stage)
817 skipping_duplicates = 0;
818 if (dir.exclude_per_dir)
819 exc_given = 1;
821 if (require_work_tree && !is_inside_work_tree())
822 setup_work_tree();
824 if (recurse_submodules &&
825 (show_deleted || show_others || show_unmerged ||
826 show_killed || show_modified || show_resolve_undo || with_tree))
827 die("ls-files --recurse-submodules unsupported mode");
829 if (recurse_submodules && error_unmatch)
830 die("ls-files --recurse-submodules does not support "
831 "--error-unmatch");
833 parse_pathspec(&pathspec, 0,
834 PATHSPEC_PREFER_CWD,
835 prefix, argv);
838 * Find common prefix for all pathspec's
839 * This is used as a performance optimization which unfortunately cannot
840 * be done when recursing into submodules because when a pathspec is
841 * given which spans repository boundaries you can't simply remove the
842 * submodule entry because the pathspec may match something inside the
843 * submodule.
845 if (recurse_submodules)
846 max_prefix = NULL;
847 else
848 max_prefix = common_prefix(&pathspec);
849 max_prefix_len = get_common_prefix_len(max_prefix);
851 prune_index(the_repository->index, max_prefix, max_prefix_len);
853 /* Treat unmatching pathspec elements as errors */
854 if (pathspec.nr && error_unmatch)
855 ps_matched = xcalloc(pathspec.nr, 1);
857 if ((dir.flags & DIR_SHOW_IGNORED) && !show_others && !show_cached)
858 die("ls-files -i must be used with either -o or -c");
860 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
861 die("ls-files --ignored needs some exclude pattern");
863 /* With no flags, we default to showing the cached files */
864 if (!(show_stage || show_deleted || show_others || show_unmerged ||
865 show_killed || show_modified || show_resolve_undo))
866 show_cached = 1;
868 if (with_tree) {
870 * Basic sanity check; show-stages and show-unmerged
871 * would not make any sense with this option.
873 if (show_stage || show_unmerged)
874 die(_("options '%s' and '%s' cannot be used together"), "ls-files --with-tree", "-s/-u");
875 overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
878 show_files(the_repository, &dir);
880 if (show_resolve_undo)
881 show_ru_info(the_repository->index);
883 if (ps_matched && report_path_error(ps_matched, &pathspec)) {
884 fprintf(stderr, "Did you forget to 'git add'?\n");
885 ret = 1;
888 string_list_clear(&exclude_list, 0);
889 dir_clear(&dir);
890 free(max_prefix);
891 return ret;