notes.c: use designated initializers for clarity
[git/debian.git] / builtin / ls-files.c
blobed35fa8d8e84b57bbc7bc2b3e62a2b4d437c992f
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 struct strbuf buf = STRBUF_INIT;
95 const char *rel = relative_path(name, prefix_len ? prefix : NULL, &buf);
97 if (line_terminator)
98 quote_c_style(rel, sb, NULL, 0);
99 else
100 strbuf_addstr(sb, rel);
102 strbuf_release(&buf);
105 static const char *get_tag(const struct cache_entry *ce, const char *tag)
107 static char alttag[4];
109 if (tag && *tag && ((show_valid_bit && (ce->ce_flags & CE_VALID)) ||
110 (show_fsmonitor_bit && (ce->ce_flags & CE_FSMONITOR_VALID)))) {
111 memcpy(alttag, tag, 3);
113 if (isalpha(tag[0])) {
114 alttag[0] = tolower(tag[0]);
115 } else if (tag[0] == '?') {
116 alttag[0] = '!';
117 } else {
118 alttag[0] = 'v';
119 alttag[1] = tag[0];
120 alttag[2] = ' ';
121 alttag[3] = 0;
124 tag = alttag;
127 return tag;
130 static void print_debug(const struct cache_entry *ce)
132 if (debug_mode) {
133 const struct stat_data *sd = &ce->ce_stat_data;
135 printf(" ctime: %u:%u\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
136 printf(" mtime: %u:%u\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
137 printf(" dev: %u\tino: %u\n", sd->sd_dev, sd->sd_ino);
138 printf(" uid: %u\tgid: %u\n", sd->sd_uid, sd->sd_gid);
139 printf(" size: %u\tflags: %x\n", sd->sd_size, ce->ce_flags);
143 static void show_dir_entry(struct index_state *istate,
144 const char *tag, struct dir_entry *ent)
146 int len = max_prefix_len;
148 if (len > ent->len)
149 die("git ls-files: internal error - directory entry not superset of prefix");
151 /* If ps_matches is non-NULL, figure out which pathspec(s) match. */
152 if (ps_matched)
153 dir_path_match(istate, ent, &pathspec, len, ps_matched);
155 fputs(tag, stdout);
156 write_eolinfo(istate, NULL, ent->name);
157 write_name(ent->name);
160 static void show_other_files(struct index_state *istate,
161 const struct dir_struct *dir)
163 int i;
165 for (i = 0; i < dir->nr; i++) {
166 struct dir_entry *ent = dir->entries[i];
167 if (!index_name_is_other(istate, ent->name, ent->len))
168 continue;
169 show_dir_entry(istate, tag_other, ent);
173 static void show_killed_files(struct index_state *istate,
174 const struct dir_struct *dir)
176 int i;
177 for (i = 0; i < dir->nr; i++) {
178 struct dir_entry *ent = dir->entries[i];
179 char *cp, *sp;
180 int pos, len, killed = 0;
182 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
183 sp = strchr(cp, '/');
184 if (!sp) {
185 /* If ent->name is prefix of an entry in the
186 * cache, it will be killed.
188 pos = index_name_pos(istate, ent->name, ent->len);
189 if (0 <= pos)
190 BUG("killed-file %.*s not found",
191 ent->len, ent->name);
192 pos = -pos - 1;
193 while (pos < istate->cache_nr &&
194 ce_stage(istate->cache[pos]))
195 pos++; /* skip unmerged */
196 if (istate->cache_nr <= pos)
197 break;
198 /* pos points at a name immediately after
199 * ent->name in the cache. Does it expect
200 * ent->name to be a directory?
202 len = ce_namelen(istate->cache[pos]);
203 if ((ent->len < len) &&
204 !strncmp(istate->cache[pos]->name,
205 ent->name, ent->len) &&
206 istate->cache[pos]->name[ent->len] == '/')
207 killed = 1;
208 break;
210 if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
211 /* If any of the leading directories in
212 * ent->name is registered in the cache,
213 * ent->name will be killed.
215 killed = 1;
216 break;
219 if (killed)
220 show_dir_entry(istate, tag_killed, dir->entries[i]);
224 static void show_files(struct repository *repo, struct dir_struct *dir);
226 static void show_submodule(struct repository *superproject,
227 struct dir_struct *dir, const char *path)
229 struct repository subrepo;
231 if (repo_submodule_init(&subrepo, superproject, path, null_oid()))
232 return;
234 if (repo_read_index(&subrepo) < 0)
235 die("index file corrupt");
237 show_files(&subrepo, dir);
239 repo_clear(&subrepo);
242 struct show_index_data {
243 const char *pathname;
244 struct index_state *istate;
245 const struct cache_entry *ce;
248 static size_t expand_show_index(struct strbuf *sb, const char *start,
249 void *context)
251 struct show_index_data *data = context;
252 const char *end;
253 const char *p;
254 size_t len = strbuf_expand_literal_cb(sb, start, NULL);
255 struct stat st;
257 if (len)
258 return len;
259 if (*start != '(')
260 die(_("bad ls-files format: element '%s' "
261 "does not start with '('"), start);
263 end = strchr(start + 1, ')');
264 if (!end)
265 die(_("bad ls-files format: element '%s' "
266 "does not end in ')'"), start);
268 len = end - start + 1;
269 if (skip_prefix(start, "(objectmode)", &p))
270 strbuf_addf(sb, "%06o", data->ce->ce_mode);
271 else if (skip_prefix(start, "(objectname)", &p))
272 strbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);
273 else if (skip_prefix(start, "(stage)", &p))
274 strbuf_addf(sb, "%d", ce_stage(data->ce));
275 else if (skip_prefix(start, "(eolinfo:index)", &p))
276 strbuf_addstr(sb, S_ISREG(data->ce->ce_mode) ?
277 get_cached_convert_stats_ascii(data->istate,
278 data->ce->name) : "");
279 else if (skip_prefix(start, "(eolinfo:worktree)", &p))
280 strbuf_addstr(sb, !lstat(data->pathname, &st) &&
281 S_ISREG(st.st_mode) ?
282 get_wt_convert_stats_ascii(data->pathname) : "");
283 else if (skip_prefix(start, "(eolattr)", &p))
284 strbuf_addstr(sb, get_convert_attr_ascii(data->istate,
285 data->pathname));
286 else if (skip_prefix(start, "(path)", &p))
287 write_name_to_buf(sb, data->pathname);
288 else
289 die(_("bad ls-files format: %%%.*s"), (int)len, start);
291 return len;
294 static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
295 const char *format, const char *fullname) {
296 struct show_index_data data = {
297 .pathname = fullname,
298 .istate = repo->index,
299 .ce = ce,
301 struct strbuf sb = STRBUF_INIT;
303 strbuf_expand(&sb, format, expand_show_index, &data);
304 strbuf_addch(&sb, line_terminator);
305 fwrite(sb.buf, sb.len, 1, stdout);
306 strbuf_release(&sb);
309 static void show_ce(struct repository *repo, struct dir_struct *dir,
310 const struct cache_entry *ce, const char *fullname,
311 const char *tag)
313 if (max_prefix_len > strlen(fullname))
314 die("git ls-files: internal error - cache entry not superset of prefix");
316 if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
317 is_submodule_active(repo, ce->name)) {
318 show_submodule(repo, dir, ce->name);
319 } else if (match_pathspec(repo->index, &pathspec, fullname, strlen(fullname),
320 max_prefix_len, ps_matched,
321 S_ISDIR(ce->ce_mode) ||
322 S_ISGITLINK(ce->ce_mode))) {
323 if (format) {
324 show_ce_fmt(repo, ce, format, fullname);
325 print_debug(ce);
326 return;
329 tag = get_tag(ce, tag);
331 if (!show_stage) {
332 fputs(tag, stdout);
333 } else {
334 printf("%s%06o %s %d\t",
335 tag,
336 ce->ce_mode,
337 repo_find_unique_abbrev(repo, &ce->oid, abbrev),
338 ce_stage(ce));
340 write_eolinfo(repo->index, ce, fullname);
341 write_name(fullname);
342 print_debug(ce);
346 static void show_ru_info(struct index_state *istate)
348 struct string_list_item *item;
350 if (!istate->resolve_undo)
351 return;
353 for_each_string_list_item(item, istate->resolve_undo) {
354 const char *path = item->string;
355 struct resolve_undo_info *ui = item->util;
356 int i, len;
358 len = strlen(path);
359 if (len < max_prefix_len)
360 continue; /* outside of the prefix */
361 if (!match_pathspec(istate, &pathspec, path, len,
362 max_prefix_len, ps_matched, 0))
363 continue; /* uninterested */
364 for (i = 0; i < 3; i++) {
365 if (!ui->mode[i])
366 continue;
367 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
368 repo_find_unique_abbrev(the_repository, &ui->oid[i], abbrev),
369 i + 1);
370 write_name(path);
375 static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
376 const char *fullname, const struct cache_entry *ce)
378 int dtype = ce_to_dtype(ce);
379 return is_excluded(dir, istate, fullname, &dtype);
382 static void construct_fullname(struct strbuf *out, const struct repository *repo,
383 const struct cache_entry *ce)
385 strbuf_reset(out);
386 if (repo->submodule_prefix)
387 strbuf_addstr(out, repo->submodule_prefix);
388 strbuf_addstr(out, ce->name);
391 static void show_files(struct repository *repo, struct dir_struct *dir)
393 int i;
394 struct strbuf fullname = STRBUF_INIT;
396 /* For cached/deleted files we don't need to even do the readdir */
397 if (show_others || show_killed) {
398 if (!show_others)
399 dir->flags |= DIR_COLLECT_KILLED_ONLY;
400 fill_directory(dir, repo->index, &pathspec);
401 if (show_others)
402 show_other_files(repo->index, dir);
403 if (show_killed)
404 show_killed_files(repo->index, dir);
407 if (!(show_cached || show_stage || show_deleted || show_modified))
408 return;
410 if (!show_sparse_dirs)
411 ensure_full_index(repo->index);
413 for (i = 0; i < repo->index->cache_nr; i++) {
414 const struct cache_entry *ce = repo->index->cache[i];
415 struct stat st;
416 int stat_err;
418 construct_fullname(&fullname, repo, ce);
420 if ((dir->flags & DIR_SHOW_IGNORED) &&
421 !ce_excluded(dir, repo->index, fullname.buf, ce))
422 continue;
423 if (ce->ce_flags & CE_UPDATE)
424 continue;
425 if ((show_cached || show_stage) &&
426 (!show_unmerged || ce_stage(ce))) {
427 show_ce(repo, dir, ce, fullname.buf,
428 ce_stage(ce) ? tag_unmerged :
429 (ce_skip_worktree(ce) ? tag_skip_worktree :
430 tag_cached));
431 if (skipping_duplicates)
432 goto skip_to_next_name;
435 if (!(show_deleted || show_modified))
436 continue;
437 if (ce_skip_worktree(ce))
438 continue;
439 stat_err = lstat(fullname.buf, &st);
440 if (stat_err && (errno != ENOENT && errno != ENOTDIR))
441 error_errno("cannot lstat '%s'", fullname.buf);
442 if (stat_err && show_deleted) {
443 show_ce(repo, dir, ce, fullname.buf, tag_removed);
444 if (skipping_duplicates)
445 goto skip_to_next_name;
447 if (show_modified &&
448 (stat_err || ie_modified(repo->index, ce, &st, 0))) {
449 show_ce(repo, dir, ce, fullname.buf, tag_modified);
450 if (skipping_duplicates)
451 goto skip_to_next_name;
453 continue;
455 skip_to_next_name:
457 int j;
458 struct cache_entry **cache = repo->index->cache;
459 for (j = i + 1; j < repo->index->cache_nr; j++)
460 if (strcmp(ce->name, cache[j]->name))
461 break;
462 i = j - 1; /* compensate for the for loop */
466 strbuf_release(&fullname);
470 * Prune the index to only contain stuff starting with "prefix"
472 static void prune_index(struct index_state *istate,
473 const char *prefix, size_t prefixlen)
475 int pos;
476 unsigned int first, last;
478 if (!prefix || !istate->cache_nr)
479 return;
480 pos = index_name_pos(istate, prefix, prefixlen);
481 if (pos < 0)
482 pos = -pos-1;
483 first = pos;
484 last = istate->cache_nr;
485 while (last > first) {
486 int next = first + ((last - first) >> 1);
487 const struct cache_entry *ce = istate->cache[next];
488 if (!strncmp(ce->name, prefix, prefixlen)) {
489 first = next+1;
490 continue;
492 last = next;
494 MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos);
495 istate->cache_nr = last - pos;
498 static int get_common_prefix_len(const char *common_prefix)
500 int common_prefix_len;
502 if (!common_prefix)
503 return 0;
505 common_prefix_len = strlen(common_prefix);
508 * If the prefix has a trailing slash, strip it so that submodules wont
509 * be pruned from the index.
511 if (common_prefix[common_prefix_len - 1] == '/')
512 common_prefix_len--;
514 return common_prefix_len;
517 static int read_one_entry_opt(struct index_state *istate,
518 const struct object_id *oid,
519 struct strbuf *base,
520 const char *pathname,
521 unsigned mode, int opt)
523 int len;
524 struct cache_entry *ce;
526 if (S_ISDIR(mode))
527 return READ_TREE_RECURSIVE;
529 len = strlen(pathname);
530 ce = make_empty_cache_entry(istate, base->len + len);
532 ce->ce_mode = create_ce_mode(mode);
533 ce->ce_flags = create_ce_flags(1);
534 ce->ce_namelen = base->len + len;
535 memcpy(ce->name, base->buf, base->len);
536 memcpy(ce->name + base->len, pathname, len+1);
537 oidcpy(&ce->oid, oid);
538 return add_index_entry(istate, ce, opt);
541 static int read_one_entry(const struct object_id *oid, struct strbuf *base,
542 const char *pathname, unsigned mode,
543 void *context)
545 struct index_state *istate = context;
546 return read_one_entry_opt(istate, oid, base, pathname,
547 mode,
548 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
552 * This is used when the caller knows there is no existing entries at
553 * the stage that will conflict with the entry being added.
555 static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
556 const char *pathname, unsigned mode,
557 void *context)
559 struct index_state *istate = context;
560 return read_one_entry_opt(istate, oid, base, pathname,
561 mode, ADD_CACHE_JUST_APPEND);
565 * Read the tree specified with --with-tree option
566 * (typically, HEAD) into stage #1 and then
567 * squash them down to stage #0. This is used for
568 * --error-unmatch to list and check the path patterns
569 * that were given from the command line. We are not
570 * going to write this index out.
572 void overlay_tree_on_index(struct index_state *istate,
573 const char *tree_name, const char *prefix)
575 struct tree *tree;
576 struct object_id oid;
577 struct pathspec pathspec;
578 struct cache_entry *last_stage0 = NULL;
579 int i;
580 read_tree_fn_t fn = NULL;
581 int err;
583 if (repo_get_oid(the_repository, tree_name, &oid))
584 die("tree-ish %s not found.", tree_name);
585 tree = parse_tree_indirect(&oid);
586 if (!tree)
587 die("bad tree-ish %s", tree_name);
589 /* Hoist the unmerged entries up to stage #3 to make room */
590 /* TODO: audit for interaction with sparse-index. */
591 ensure_full_index(istate);
592 for (i = 0; i < istate->cache_nr; i++) {
593 struct cache_entry *ce = istate->cache[i];
594 if (!ce_stage(ce))
595 continue;
596 ce->ce_flags |= CE_STAGEMASK;
599 if (prefix) {
600 static const char *(matchbuf[1]);
601 matchbuf[0] = NULL;
602 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
603 PATHSPEC_PREFER_CWD, prefix, matchbuf);
604 } else
605 memset(&pathspec, 0, sizeof(pathspec));
608 * See if we have cache entry at the stage. If so,
609 * do it the original slow way, otherwise, append and then
610 * sort at the end.
612 for (i = 0; !fn && i < istate->cache_nr; i++) {
613 const struct cache_entry *ce = istate->cache[i];
614 if (ce_stage(ce) == 1)
615 fn = read_one_entry;
618 if (!fn)
619 fn = read_one_entry_quick;
620 err = read_tree(the_repository, tree, &pathspec, fn, istate);
621 clear_pathspec(&pathspec);
622 if (err)
623 die("unable to read tree entries %s", tree_name);
626 * Sort the cache entry -- we need to nuke the cache tree, though.
628 if (fn == read_one_entry_quick) {
629 cache_tree_free(&istate->cache_tree);
630 QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
633 for (i = 0; i < istate->cache_nr; i++) {
634 struct cache_entry *ce = istate->cache[i];
635 switch (ce_stage(ce)) {
636 case 0:
637 last_stage0 = ce;
638 /* fallthru */
639 default:
640 continue;
641 case 1:
643 * If there is stage #0 entry for this, we do not
644 * need to show it. We use CE_UPDATE bit to mark
645 * such an entry.
647 if (last_stage0 &&
648 !strcmp(last_stage0->name, ce->name))
649 ce->ce_flags |= CE_UPDATE;
654 static const char * const ls_files_usage[] = {
655 N_("git ls-files [<options>] [<file>...]"),
656 NULL
659 static int option_parse_exclude(const struct option *opt,
660 const char *arg, int unset)
662 struct string_list *exclude_list = opt->value;
664 BUG_ON_OPT_NEG(unset);
666 exc_given = 1;
667 string_list_append(exclude_list, arg);
669 return 0;
672 static int option_parse_exclude_from(const struct option *opt,
673 const char *arg, int unset)
675 struct dir_struct *dir = opt->value;
677 BUG_ON_OPT_NEG(unset);
679 exc_given = 1;
680 add_patterns_from_file(dir, arg);
682 return 0;
685 static int option_parse_exclude_standard(const struct option *opt,
686 const char *arg, int unset)
688 struct dir_struct *dir = opt->value;
690 BUG_ON_OPT_NEG(unset);
691 BUG_ON_OPT_ARG(arg);
693 exc_given = 1;
694 setup_standard_excludes(dir);
696 return 0;
699 int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
701 int require_work_tree = 0, show_tag = 0, i;
702 char *max_prefix;
703 struct dir_struct dir = DIR_INIT;
704 struct pattern_list *pl;
705 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
706 struct option builtin_ls_files_options[] = {
707 /* Think twice before adding "--nul" synonym to this */
708 OPT_SET_INT('z', NULL, &line_terminator,
709 N_("separate paths with the NUL character"), '\0'),
710 OPT_BOOL('t', NULL, &show_tag,
711 N_("identify the file status with tags")),
712 OPT_BOOL('v', NULL, &show_valid_bit,
713 N_("use lowercase letters for 'assume unchanged' files")),
714 OPT_BOOL('f', NULL, &show_fsmonitor_bit,
715 N_("use lowercase letters for 'fsmonitor clean' files")),
716 OPT_BOOL('c', "cached", &show_cached,
717 N_("show cached files in the output (default)")),
718 OPT_BOOL('d', "deleted", &show_deleted,
719 N_("show deleted files in the output")),
720 OPT_BOOL('m', "modified", &show_modified,
721 N_("show modified files in the output")),
722 OPT_BOOL('o', "others", &show_others,
723 N_("show other files in the output")),
724 OPT_BIT('i', "ignored", &dir.flags,
725 N_("show ignored files in the output"),
726 DIR_SHOW_IGNORED),
727 OPT_BOOL('s', "stage", &show_stage,
728 N_("show staged contents' object name in the output")),
729 OPT_BOOL('k', "killed", &show_killed,
730 N_("show files on the filesystem that need to be removed")),
731 OPT_BIT(0, "directory", &dir.flags,
732 N_("show 'other' directories' names only"),
733 DIR_SHOW_OTHER_DIRECTORIES),
734 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
735 OPT_NEGBIT(0, "empty-directory", &dir.flags,
736 N_("don't show empty directories"),
737 DIR_HIDE_EMPTY_DIRECTORIES),
738 OPT_BOOL('u', "unmerged", &show_unmerged,
739 N_("show unmerged files in the output")),
740 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
741 N_("show resolve-undo information")),
742 OPT_CALLBACK_F('x', "exclude", &exclude_list, N_("pattern"),
743 N_("skip files matching pattern"),
744 PARSE_OPT_NONEG, option_parse_exclude),
745 OPT_CALLBACK_F('X', "exclude-from", &dir, N_("file"),
746 N_("read exclude patterns from <file>"),
747 PARSE_OPT_NONEG, option_parse_exclude_from),
748 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
749 N_("read additional per-directory exclude patterns in <file>")),
750 OPT_CALLBACK_F(0, "exclude-standard", &dir, NULL,
751 N_("add the standard git exclusions"),
752 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
753 option_parse_exclude_standard),
754 OPT_SET_INT_F(0, "full-name", &prefix_len,
755 N_("make the output relative to the project top directory"),
756 0, PARSE_OPT_NONEG),
757 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
758 N_("recurse through submodules")),
759 OPT_BOOL(0, "error-unmatch", &error_unmatch,
760 N_("if any <file> is not in the index, treat this as an error")),
761 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
762 N_("pretend that paths removed since <tree-ish> are still present")),
763 OPT__ABBREV(&abbrev),
764 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
765 OPT_BOOL(0, "deduplicate", &skipping_duplicates,
766 N_("suppress duplicate entries")),
767 OPT_BOOL(0, "sparse", &show_sparse_dirs,
768 N_("show sparse directories in the presence of a sparse index")),
769 OPT_STRING_F(0, "format", &format, N_("format"),
770 N_("format to use for the output"),
771 PARSE_OPT_NONEG),
772 OPT_END()
774 int ret = 0;
776 if (argc == 2 && !strcmp(argv[1], "-h"))
777 usage_with_options(ls_files_usage, builtin_ls_files_options);
779 prepare_repo_settings(the_repository);
780 the_repository->settings.command_requires_full_index = 0;
782 prefix = cmd_prefix;
783 if (prefix)
784 prefix_len = strlen(prefix);
785 git_config(git_default_config, NULL);
787 if (repo_read_index(the_repository) < 0)
788 die("index file corrupt");
790 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
791 ls_files_usage, 0);
792 pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
793 for (i = 0; i < exclude_list.nr; i++) {
794 add_pattern(exclude_list.items[i].string, "", 0, pl, --exclude_args);
797 if (format && (show_stage || show_others || show_killed ||
798 show_resolve_undo || skipping_duplicates || show_eol || show_tag))
799 usage_msg_opt(_("--format cannot be used with -s, -o, -k, -t, "
800 "--resolve-undo, --deduplicate, --eol"),
801 ls_files_usage, builtin_ls_files_options);
803 if (show_tag || show_valid_bit || show_fsmonitor_bit) {
804 tag_cached = "H ";
805 tag_unmerged = "M ";
806 tag_removed = "R ";
807 tag_modified = "C ";
808 tag_other = "? ";
809 tag_killed = "K ";
810 tag_skip_worktree = "S ";
811 tag_resolve_undo = "U ";
813 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
814 require_work_tree = 1;
815 if (show_unmerged)
817 * There's no point in showing unmerged unless
818 * you also show the stage information.
820 show_stage = 1;
821 if (show_tag || show_stage)
822 skipping_duplicates = 0;
823 if (dir.exclude_per_dir)
824 exc_given = 1;
826 if (require_work_tree && !is_inside_work_tree())
827 setup_work_tree();
829 if (recurse_submodules &&
830 (show_deleted || show_others || show_unmerged ||
831 show_killed || show_modified || show_resolve_undo || with_tree))
832 die("ls-files --recurse-submodules unsupported mode");
834 if (recurse_submodules && error_unmatch)
835 die("ls-files --recurse-submodules does not support "
836 "--error-unmatch");
838 parse_pathspec(&pathspec, 0,
839 PATHSPEC_PREFER_CWD,
840 prefix, argv);
843 * Find common prefix for all pathspec's
844 * This is used as a performance optimization which unfortunately cannot
845 * be done when recursing into submodules because when a pathspec is
846 * given which spans repository boundaries you can't simply remove the
847 * submodule entry because the pathspec may match something inside the
848 * submodule.
850 if (recurse_submodules)
851 max_prefix = NULL;
852 else
853 max_prefix = common_prefix(&pathspec);
854 max_prefix_len = get_common_prefix_len(max_prefix);
856 prune_index(the_repository->index, max_prefix, max_prefix_len);
858 /* Treat unmatching pathspec elements as errors */
859 if (pathspec.nr && error_unmatch)
860 ps_matched = xcalloc(pathspec.nr, 1);
862 if ((dir.flags & DIR_SHOW_IGNORED) && !show_others && !show_cached)
863 die("ls-files -i must be used with either -o or -c");
865 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
866 die("ls-files --ignored needs some exclude pattern");
868 /* With no flags, we default to showing the cached files */
869 if (!(show_stage || show_deleted || show_others || show_unmerged ||
870 show_killed || show_modified || show_resolve_undo))
871 show_cached = 1;
873 if (with_tree) {
875 * Basic sanity check; show-stages and show-unmerged
876 * would not make any sense with this option.
878 if (show_stage || show_unmerged)
879 die(_("options '%s' and '%s' cannot be used together"), "ls-files --with-tree", "-s/-u");
880 overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
883 show_files(the_repository, &dir);
885 if (show_resolve_undo)
886 show_ru_info(the_repository->index);
888 if (ps_matched && report_path_error(ps_matched, &pathspec)) {
889 fprintf(stderr, "Did you forget to 'git add'?\n");
890 ret = 1;
893 string_list_clear(&exclude_list, 0);
894 dir_clear(&dir);
895 free(max_prefix);
896 return ret;