diff --no-index: fix -R with stdin
[git/debian.git] / builtin / ls-files.c
blob72012c0f0f7e88db7d88279ac8f005954a689744
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 "convert.h"
12 #include "quote.h"
13 #include "dir.h"
14 #include "builtin.h"
15 #include "gettext.h"
16 #include "object-name.h"
17 #include "strbuf.h"
18 #include "tree.h"
19 #include "cache-tree.h"
20 #include "parse-options.h"
21 #include "resolve-undo.h"
22 #include "string-list.h"
23 #include "pathspec.h"
24 #include "run-command.h"
25 #include "setup.h"
26 #include "submodule.h"
27 #include "submodule-config.h"
28 #include "object-store.h"
29 #include "hex.h"
32 static int abbrev;
33 static int show_deleted;
34 static int show_cached;
35 static int show_others;
36 static int show_stage;
37 static int show_unmerged;
38 static int show_resolve_undo;
39 static int show_modified;
40 static int show_killed;
41 static int show_valid_bit;
42 static int show_fsmonitor_bit;
43 static int line_terminator = '\n';
44 static int debug_mode;
45 static int show_eol;
46 static int recurse_submodules;
47 static int skipping_duplicates;
48 static int show_sparse_dirs;
50 static const char *prefix;
51 static int max_prefix_len;
52 static int prefix_len;
53 static struct pathspec pathspec;
54 static int error_unmatch;
55 static char *ps_matched;
56 static const char *with_tree;
57 static int exc_given;
58 static int exclude_args;
59 static const char *format;
61 static const char *tag_cached = "";
62 static const char *tag_unmerged = "";
63 static const char *tag_removed = "";
64 static const char *tag_other = "";
65 static const char *tag_killed = "";
66 static const char *tag_modified = "";
67 static const char *tag_skip_worktree = "";
68 static const char *tag_resolve_undo = "";
70 static void write_eolinfo(struct index_state *istate,
71 const struct cache_entry *ce, const char *path)
73 if (show_eol) {
74 struct stat st;
75 const char *i_txt = "";
76 const char *w_txt = "";
77 const char *a_txt = get_convert_attr_ascii(istate, path);
78 if (ce && S_ISREG(ce->ce_mode))
79 i_txt = get_cached_convert_stats_ascii(istate,
80 ce->name);
81 if (!lstat(path, &st) && S_ISREG(st.st_mode))
82 w_txt = get_wt_convert_stats_ascii(path);
83 printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
87 static void write_name(const char *name)
90 * With "--full-name", prefix_len=0; this caller needs to pass
91 * an empty string in that case (a NULL is good for "").
93 write_name_quoted_relative(name, prefix_len ? prefix : NULL,
94 stdout, line_terminator);
97 static void write_name_to_buf(struct strbuf *sb, const char *name)
99 struct strbuf buf = STRBUF_INIT;
100 const char *rel = relative_path(name, prefix_len ? prefix : NULL, &buf);
102 if (line_terminator)
103 quote_c_style(rel, sb, NULL, 0);
104 else
105 strbuf_addstr(sb, rel);
107 strbuf_release(&buf);
110 static const char *get_tag(const struct cache_entry *ce, const char *tag)
112 static char alttag[4];
114 if (tag && *tag && ((show_valid_bit && (ce->ce_flags & CE_VALID)) ||
115 (show_fsmonitor_bit && (ce->ce_flags & CE_FSMONITOR_VALID)))) {
116 memcpy(alttag, tag, 3);
118 if (isalpha(tag[0])) {
119 alttag[0] = tolower(tag[0]);
120 } else if (tag[0] == '?') {
121 alttag[0] = '!';
122 } else {
123 alttag[0] = 'v';
124 alttag[1] = tag[0];
125 alttag[2] = ' ';
126 alttag[3] = 0;
129 tag = alttag;
132 return tag;
135 static void print_debug(const struct cache_entry *ce)
137 if (debug_mode) {
138 const struct stat_data *sd = &ce->ce_stat_data;
140 printf(" ctime: %u:%u\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
141 printf(" mtime: %u:%u\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
142 printf(" dev: %u\tino: %u\n", sd->sd_dev, sd->sd_ino);
143 printf(" uid: %u\tgid: %u\n", sd->sd_uid, sd->sd_gid);
144 printf(" size: %u\tflags: %x\n", sd->sd_size, ce->ce_flags);
148 static void show_dir_entry(struct index_state *istate,
149 const char *tag, struct dir_entry *ent)
151 int len = max_prefix_len;
153 if (len > ent->len)
154 die("git ls-files: internal error - directory entry not superset of prefix");
156 /* If ps_matches is non-NULL, figure out which pathspec(s) match. */
157 if (ps_matched)
158 dir_path_match(istate, ent, &pathspec, len, ps_matched);
160 fputs(tag, stdout);
161 write_eolinfo(istate, NULL, ent->name);
162 write_name(ent->name);
165 static void show_other_files(struct index_state *istate,
166 const struct dir_struct *dir)
168 int i;
170 for (i = 0; i < dir->nr; i++) {
171 struct dir_entry *ent = dir->entries[i];
172 if (!index_name_is_other(istate, ent->name, ent->len))
173 continue;
174 show_dir_entry(istate, tag_other, ent);
178 static void show_killed_files(struct index_state *istate,
179 const struct dir_struct *dir)
181 int i;
182 for (i = 0; i < dir->nr; i++) {
183 struct dir_entry *ent = dir->entries[i];
184 char *cp, *sp;
185 int pos, len, killed = 0;
187 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
188 sp = strchr(cp, '/');
189 if (!sp) {
190 /* If ent->name is prefix of an entry in the
191 * cache, it will be killed.
193 pos = index_name_pos(istate, ent->name, ent->len);
194 if (0 <= pos)
195 BUG("killed-file %.*s not found",
196 ent->len, ent->name);
197 pos = -pos - 1;
198 while (pos < istate->cache_nr &&
199 ce_stage(istate->cache[pos]))
200 pos++; /* skip unmerged */
201 if (istate->cache_nr <= pos)
202 break;
203 /* pos points at a name immediately after
204 * ent->name in the cache. Does it expect
205 * ent->name to be a directory?
207 len = ce_namelen(istate->cache[pos]);
208 if ((ent->len < len) &&
209 !strncmp(istate->cache[pos]->name,
210 ent->name, ent->len) &&
211 istate->cache[pos]->name[ent->len] == '/')
212 killed = 1;
213 break;
215 if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
216 /* If any of the leading directories in
217 * ent->name is registered in the cache,
218 * ent->name will be killed.
220 killed = 1;
221 break;
224 if (killed)
225 show_dir_entry(istate, tag_killed, dir->entries[i]);
229 static void show_files(struct repository *repo, struct dir_struct *dir);
231 static void show_submodule(struct repository *superproject,
232 struct dir_struct *dir, const char *path)
234 struct repository subrepo;
236 if (repo_submodule_init(&subrepo, superproject, path, null_oid()))
237 return;
239 if (repo_read_index(&subrepo) < 0)
240 die("index file corrupt");
242 show_files(&subrepo, dir);
244 repo_clear(&subrepo);
247 static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
248 const enum object_type type, unsigned int padded)
250 if (type == OBJ_BLOB) {
251 unsigned long size;
252 if (oid_object_info(the_repository, oid, &size) < 0)
253 die(_("could not get object info about '%s'"),
254 oid_to_hex(oid));
255 if (padded)
256 strbuf_addf(line, "%7"PRIuMAX, (uintmax_t)size);
257 else
258 strbuf_addf(line, "%"PRIuMAX, (uintmax_t)size);
259 } else if (padded) {
260 strbuf_addf(line, "%7s", "-");
261 } else {
262 strbuf_addstr(line, "-");
265 struct show_index_data {
266 const char *pathname;
267 struct index_state *istate;
268 const struct cache_entry *ce;
271 static size_t expand_show_index(struct strbuf *sb, const char *start,
272 void *context)
274 struct show_index_data *data = context;
275 const char *end;
276 const char *p;
277 size_t len = strbuf_expand_literal_cb(sb, start, NULL);
278 struct stat st;
280 if (len)
281 return len;
282 if (*start != '(')
283 die(_("bad ls-files format: element '%s' "
284 "does not start with '('"), start);
286 end = strchr(start + 1, ')');
287 if (!end)
288 die(_("bad ls-files format: element '%s' "
289 "does not end in ')'"), start);
291 len = end - start + 1;
292 if (skip_prefix(start, "(objectmode)", &p))
293 strbuf_addf(sb, "%06o", data->ce->ce_mode);
294 else if (skip_prefix(start, "(objectname)", &p))
295 strbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);
296 else if (skip_prefix(start, "(objecttype)", &p))
297 strbuf_addstr(sb, type_name(object_type(data->ce->ce_mode)));
298 else if (skip_prefix(start, "(objectsize:padded)", &p))
299 expand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 1);
300 else if (skip_prefix(start, "(objectsize)", &p))
301 expand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 0);
302 else if (skip_prefix(start, "(stage)", &p))
303 strbuf_addf(sb, "%d", ce_stage(data->ce));
304 else if (skip_prefix(start, "(eolinfo:index)", &p))
305 strbuf_addstr(sb, S_ISREG(data->ce->ce_mode) ?
306 get_cached_convert_stats_ascii(data->istate,
307 data->ce->name) : "");
308 else if (skip_prefix(start, "(eolinfo:worktree)", &p))
309 strbuf_addstr(sb, !lstat(data->pathname, &st) &&
310 S_ISREG(st.st_mode) ?
311 get_wt_convert_stats_ascii(data->pathname) : "");
312 else if (skip_prefix(start, "(eolattr)", &p))
313 strbuf_addstr(sb, get_convert_attr_ascii(data->istate,
314 data->pathname));
315 else if (skip_prefix(start, "(path)", &p))
316 write_name_to_buf(sb, data->pathname);
317 else
318 die(_("bad ls-files format: %%%.*s"), (int)len, start);
320 return len;
323 static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
324 const char *format, const char *fullname) {
325 struct show_index_data data = {
326 .pathname = fullname,
327 .istate = repo->index,
328 .ce = ce,
330 struct strbuf sb = STRBUF_INIT;
332 strbuf_expand(&sb, format, expand_show_index, &data);
333 strbuf_addch(&sb, line_terminator);
334 fwrite(sb.buf, sb.len, 1, stdout);
335 strbuf_release(&sb);
338 static void show_ce(struct repository *repo, struct dir_struct *dir,
339 const struct cache_entry *ce, const char *fullname,
340 const char *tag)
342 if (max_prefix_len > strlen(fullname))
343 die("git ls-files: internal error - cache entry not superset of prefix");
345 if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
346 is_submodule_active(repo, ce->name)) {
347 show_submodule(repo, dir, ce->name);
348 } else if (match_pathspec(repo->index, &pathspec, fullname, strlen(fullname),
349 max_prefix_len, ps_matched,
350 S_ISDIR(ce->ce_mode) ||
351 S_ISGITLINK(ce->ce_mode))) {
352 if (format) {
353 show_ce_fmt(repo, ce, format, fullname);
354 print_debug(ce);
355 return;
358 tag = get_tag(ce, tag);
360 if (!show_stage) {
361 fputs(tag, stdout);
362 } else {
363 printf("%s%06o %s %d\t",
364 tag,
365 ce->ce_mode,
366 repo_find_unique_abbrev(repo, &ce->oid, abbrev),
367 ce_stage(ce));
369 write_eolinfo(repo->index, ce, fullname);
370 write_name(fullname);
371 print_debug(ce);
375 static void show_ru_info(struct index_state *istate)
377 struct string_list_item *item;
379 if (!istate->resolve_undo)
380 return;
382 for_each_string_list_item(item, istate->resolve_undo) {
383 const char *path = item->string;
384 struct resolve_undo_info *ui = item->util;
385 int i, len;
387 len = strlen(path);
388 if (len < max_prefix_len)
389 continue; /* outside of the prefix */
390 if (!match_pathspec(istate, &pathspec, path, len,
391 max_prefix_len, ps_matched, 0))
392 continue; /* uninterested */
393 for (i = 0; i < 3; i++) {
394 if (!ui->mode[i])
395 continue;
396 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
397 repo_find_unique_abbrev(the_repository, &ui->oid[i], abbrev),
398 i + 1);
399 write_name(path);
404 static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
405 const char *fullname, const struct cache_entry *ce)
407 int dtype = ce_to_dtype(ce);
408 return is_excluded(dir, istate, fullname, &dtype);
411 static void construct_fullname(struct strbuf *out, const struct repository *repo,
412 const struct cache_entry *ce)
414 strbuf_reset(out);
415 if (repo->submodule_prefix)
416 strbuf_addstr(out, repo->submodule_prefix);
417 strbuf_addstr(out, ce->name);
420 static void show_files(struct repository *repo, struct dir_struct *dir)
422 int i;
423 struct strbuf fullname = STRBUF_INIT;
425 /* For cached/deleted files we don't need to even do the readdir */
426 if (show_others || show_killed) {
427 if (!show_others)
428 dir->flags |= DIR_COLLECT_KILLED_ONLY;
429 fill_directory(dir, repo->index, &pathspec);
430 if (show_others)
431 show_other_files(repo->index, dir);
432 if (show_killed)
433 show_killed_files(repo->index, dir);
436 if (!(show_cached || show_stage || show_deleted || show_modified))
437 return;
439 if (!show_sparse_dirs)
440 ensure_full_index(repo->index);
442 for (i = 0; i < repo->index->cache_nr; i++) {
443 const struct cache_entry *ce = repo->index->cache[i];
444 struct stat st;
445 int stat_err;
447 construct_fullname(&fullname, repo, ce);
449 if ((dir->flags & DIR_SHOW_IGNORED) &&
450 !ce_excluded(dir, repo->index, fullname.buf, ce))
451 continue;
452 if (ce->ce_flags & CE_UPDATE)
453 continue;
454 if ((show_cached || show_stage) &&
455 (!show_unmerged || ce_stage(ce))) {
456 show_ce(repo, dir, ce, fullname.buf,
457 ce_stage(ce) ? tag_unmerged :
458 (ce_skip_worktree(ce) ? tag_skip_worktree :
459 tag_cached));
460 if (skipping_duplicates)
461 goto skip_to_next_name;
464 if (!(show_deleted || show_modified))
465 continue;
466 if (ce_skip_worktree(ce))
467 continue;
468 stat_err = lstat(fullname.buf, &st);
469 if (stat_err && (errno != ENOENT && errno != ENOTDIR))
470 error_errno("cannot lstat '%s'", fullname.buf);
471 if (stat_err && show_deleted) {
472 show_ce(repo, dir, ce, fullname.buf, tag_removed);
473 if (skipping_duplicates)
474 goto skip_to_next_name;
476 if (show_modified &&
477 (stat_err || ie_modified(repo->index, ce, &st, 0))) {
478 show_ce(repo, dir, ce, fullname.buf, tag_modified);
479 if (skipping_duplicates)
480 goto skip_to_next_name;
482 continue;
484 skip_to_next_name:
486 int j;
487 struct cache_entry **cache = repo->index->cache;
488 for (j = i + 1; j < repo->index->cache_nr; j++)
489 if (strcmp(ce->name, cache[j]->name))
490 break;
491 i = j - 1; /* compensate for the for loop */
495 strbuf_release(&fullname);
499 * Prune the index to only contain stuff starting with "prefix"
501 static void prune_index(struct index_state *istate,
502 const char *prefix, size_t prefixlen)
504 int pos;
505 unsigned int first, last;
507 if (!prefix || !istate->cache_nr)
508 return;
509 pos = index_name_pos(istate, prefix, prefixlen);
510 if (pos < 0)
511 pos = -pos-1;
512 first = pos;
513 last = istate->cache_nr;
514 while (last > first) {
515 int next = first + ((last - first) >> 1);
516 const struct cache_entry *ce = istate->cache[next];
517 if (!strncmp(ce->name, prefix, prefixlen)) {
518 first = next+1;
519 continue;
521 last = next;
523 MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos);
524 istate->cache_nr = last - pos;
527 static int get_common_prefix_len(const char *common_prefix)
529 int common_prefix_len;
531 if (!common_prefix)
532 return 0;
534 common_prefix_len = strlen(common_prefix);
537 * If the prefix has a trailing slash, strip it so that submodules wont
538 * be pruned from the index.
540 if (common_prefix[common_prefix_len - 1] == '/')
541 common_prefix_len--;
543 return common_prefix_len;
546 static int read_one_entry_opt(struct index_state *istate,
547 const struct object_id *oid,
548 struct strbuf *base,
549 const char *pathname,
550 unsigned mode, int opt)
552 int len;
553 struct cache_entry *ce;
555 if (S_ISDIR(mode))
556 return READ_TREE_RECURSIVE;
558 len = strlen(pathname);
559 ce = make_empty_cache_entry(istate, base->len + len);
561 ce->ce_mode = create_ce_mode(mode);
562 ce->ce_flags = create_ce_flags(1);
563 ce->ce_namelen = base->len + len;
564 memcpy(ce->name, base->buf, base->len);
565 memcpy(ce->name + base->len, pathname, len+1);
566 oidcpy(&ce->oid, oid);
567 return add_index_entry(istate, ce, opt);
570 static int read_one_entry(const struct object_id *oid, struct strbuf *base,
571 const char *pathname, unsigned mode,
572 void *context)
574 struct index_state *istate = context;
575 return read_one_entry_opt(istate, oid, base, pathname,
576 mode,
577 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
581 * This is used when the caller knows there is no existing entries at
582 * the stage that will conflict with the entry being added.
584 static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
585 const char *pathname, unsigned mode,
586 void *context)
588 struct index_state *istate = context;
589 return read_one_entry_opt(istate, oid, base, pathname,
590 mode, ADD_CACHE_JUST_APPEND);
594 * Read the tree specified with --with-tree option
595 * (typically, HEAD) into stage #1 and then
596 * squash them down to stage #0. This is used for
597 * --error-unmatch to list and check the path patterns
598 * that were given from the command line. We are not
599 * going to write this index out.
601 void overlay_tree_on_index(struct index_state *istate,
602 const char *tree_name, const char *prefix)
604 struct tree *tree;
605 struct object_id oid;
606 struct pathspec pathspec;
607 struct cache_entry *last_stage0 = NULL;
608 int i;
609 read_tree_fn_t fn = NULL;
610 int err;
612 if (repo_get_oid(the_repository, tree_name, &oid))
613 die("tree-ish %s not found.", tree_name);
614 tree = parse_tree_indirect(&oid);
615 if (!tree)
616 die("bad tree-ish %s", tree_name);
618 /* Hoist the unmerged entries up to stage #3 to make room */
619 /* TODO: audit for interaction with sparse-index. */
620 ensure_full_index(istate);
621 for (i = 0; i < istate->cache_nr; i++) {
622 struct cache_entry *ce = istate->cache[i];
623 if (!ce_stage(ce))
624 continue;
625 ce->ce_flags |= CE_STAGEMASK;
628 if (prefix) {
629 static const char *(matchbuf[1]);
630 matchbuf[0] = NULL;
631 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
632 PATHSPEC_PREFER_CWD, prefix, matchbuf);
633 } else
634 memset(&pathspec, 0, sizeof(pathspec));
637 * See if we have cache entry at the stage. If so,
638 * do it the original slow way, otherwise, append and then
639 * sort at the end.
641 for (i = 0; !fn && i < istate->cache_nr; i++) {
642 const struct cache_entry *ce = istate->cache[i];
643 if (ce_stage(ce) == 1)
644 fn = read_one_entry;
647 if (!fn)
648 fn = read_one_entry_quick;
649 err = read_tree(the_repository, tree, &pathspec, fn, istate);
650 clear_pathspec(&pathspec);
651 if (err)
652 die("unable to read tree entries %s", tree_name);
655 * Sort the cache entry -- we need to nuke the cache tree, though.
657 if (fn == read_one_entry_quick) {
658 cache_tree_free(&istate->cache_tree);
659 QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
662 for (i = 0; i < istate->cache_nr; i++) {
663 struct cache_entry *ce = istate->cache[i];
664 switch (ce_stage(ce)) {
665 case 0:
666 last_stage0 = ce;
667 /* fallthru */
668 default:
669 continue;
670 case 1:
672 * If there is stage #0 entry for this, we do not
673 * need to show it. We use CE_UPDATE bit to mark
674 * such an entry.
676 if (last_stage0 &&
677 !strcmp(last_stage0->name, ce->name))
678 ce->ce_flags |= CE_UPDATE;
683 static const char * const ls_files_usage[] = {
684 N_("git ls-files [<options>] [<file>...]"),
685 NULL
688 static int option_parse_exclude(const struct option *opt,
689 const char *arg, int unset)
691 struct string_list *exclude_list = opt->value;
693 BUG_ON_OPT_NEG(unset);
695 exc_given = 1;
696 string_list_append(exclude_list, arg);
698 return 0;
701 static int option_parse_exclude_from(const struct option *opt,
702 const char *arg, int unset)
704 struct dir_struct *dir = opt->value;
706 BUG_ON_OPT_NEG(unset);
708 exc_given = 1;
709 add_patterns_from_file(dir, arg);
711 return 0;
714 static int option_parse_exclude_standard(const struct option *opt,
715 const char *arg, int unset)
717 struct dir_struct *dir = opt->value;
719 BUG_ON_OPT_NEG(unset);
720 BUG_ON_OPT_ARG(arg);
722 exc_given = 1;
723 setup_standard_excludes(dir);
725 return 0;
728 int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
730 int require_work_tree = 0, show_tag = 0, i;
731 char *max_prefix;
732 struct dir_struct dir = DIR_INIT;
733 struct pattern_list *pl;
734 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
735 struct option builtin_ls_files_options[] = {
736 /* Think twice before adding "--nul" synonym to this */
737 OPT_SET_INT('z', NULL, &line_terminator,
738 N_("separate paths with the NUL character"), '\0'),
739 OPT_BOOL('t', NULL, &show_tag,
740 N_("identify the file status with tags")),
741 OPT_BOOL('v', NULL, &show_valid_bit,
742 N_("use lowercase letters for 'assume unchanged' files")),
743 OPT_BOOL('f', NULL, &show_fsmonitor_bit,
744 N_("use lowercase letters for 'fsmonitor clean' files")),
745 OPT_BOOL('c', "cached", &show_cached,
746 N_("show cached files in the output (default)")),
747 OPT_BOOL('d', "deleted", &show_deleted,
748 N_("show deleted files in the output")),
749 OPT_BOOL('m', "modified", &show_modified,
750 N_("show modified files in the output")),
751 OPT_BOOL('o', "others", &show_others,
752 N_("show other files in the output")),
753 OPT_BIT('i', "ignored", &dir.flags,
754 N_("show ignored files in the output"),
755 DIR_SHOW_IGNORED),
756 OPT_BOOL('s', "stage", &show_stage,
757 N_("show staged contents' object name in the output")),
758 OPT_BOOL('k', "killed", &show_killed,
759 N_("show files on the filesystem that need to be removed")),
760 OPT_BIT(0, "directory", &dir.flags,
761 N_("show 'other' directories' names only"),
762 DIR_SHOW_OTHER_DIRECTORIES),
763 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
764 OPT_NEGBIT(0, "empty-directory", &dir.flags,
765 N_("don't show empty directories"),
766 DIR_HIDE_EMPTY_DIRECTORIES),
767 OPT_BOOL('u', "unmerged", &show_unmerged,
768 N_("show unmerged files in the output")),
769 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
770 N_("show resolve-undo information")),
771 OPT_CALLBACK_F('x', "exclude", &exclude_list, N_("pattern"),
772 N_("skip files matching pattern"),
773 PARSE_OPT_NONEG, option_parse_exclude),
774 OPT_CALLBACK_F('X', "exclude-from", &dir, N_("file"),
775 N_("read exclude patterns from <file>"),
776 PARSE_OPT_NONEG, option_parse_exclude_from),
777 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
778 N_("read additional per-directory exclude patterns in <file>")),
779 OPT_CALLBACK_F(0, "exclude-standard", &dir, NULL,
780 N_("add the standard git exclusions"),
781 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
782 option_parse_exclude_standard),
783 OPT_SET_INT_F(0, "full-name", &prefix_len,
784 N_("make the output relative to the project top directory"),
785 0, PARSE_OPT_NONEG),
786 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
787 N_("recurse through submodules")),
788 OPT_BOOL(0, "error-unmatch", &error_unmatch,
789 N_("if any <file> is not in the index, treat this as an error")),
790 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
791 N_("pretend that paths removed since <tree-ish> are still present")),
792 OPT__ABBREV(&abbrev),
793 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
794 OPT_BOOL(0, "deduplicate", &skipping_duplicates,
795 N_("suppress duplicate entries")),
796 OPT_BOOL(0, "sparse", &show_sparse_dirs,
797 N_("show sparse directories in the presence of a sparse index")),
798 OPT_STRING_F(0, "format", &format, N_("format"),
799 N_("format to use for the output"),
800 PARSE_OPT_NONEG),
801 OPT_END()
803 int ret = 0;
805 if (argc == 2 && !strcmp(argv[1], "-h"))
806 usage_with_options(ls_files_usage, builtin_ls_files_options);
808 prepare_repo_settings(the_repository);
809 the_repository->settings.command_requires_full_index = 0;
811 prefix = cmd_prefix;
812 if (prefix)
813 prefix_len = strlen(prefix);
814 git_config(git_default_config, NULL);
816 if (repo_read_index(the_repository) < 0)
817 die("index file corrupt");
819 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
820 ls_files_usage, 0);
821 pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
822 for (i = 0; i < exclude_list.nr; i++) {
823 add_pattern(exclude_list.items[i].string, "", 0, pl, --exclude_args);
826 if (format && (show_stage || show_others || show_killed ||
827 show_resolve_undo || skipping_duplicates || show_eol || show_tag))
828 usage_msg_opt(_("--format cannot be used with -s, -o, -k, -t, "
829 "--resolve-undo, --deduplicate, --eol"),
830 ls_files_usage, builtin_ls_files_options);
832 if (show_tag || show_valid_bit || show_fsmonitor_bit) {
833 tag_cached = "H ";
834 tag_unmerged = "M ";
835 tag_removed = "R ";
836 tag_modified = "C ";
837 tag_other = "? ";
838 tag_killed = "K ";
839 tag_skip_worktree = "S ";
840 tag_resolve_undo = "U ";
842 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
843 require_work_tree = 1;
844 if (show_unmerged)
846 * There's no point in showing unmerged unless
847 * you also show the stage information.
849 show_stage = 1;
850 if (show_tag || show_stage)
851 skipping_duplicates = 0;
852 if (dir.exclude_per_dir)
853 exc_given = 1;
855 if (require_work_tree && !is_inside_work_tree())
856 setup_work_tree();
858 if (recurse_submodules &&
859 (show_deleted || show_others || show_unmerged ||
860 show_killed || show_modified || show_resolve_undo || with_tree))
861 die("ls-files --recurse-submodules unsupported mode");
863 if (recurse_submodules && error_unmatch)
864 die("ls-files --recurse-submodules does not support "
865 "--error-unmatch");
867 parse_pathspec(&pathspec, 0,
868 PATHSPEC_PREFER_CWD,
869 prefix, argv);
872 * Find common prefix for all pathspec's
873 * This is used as a performance optimization which unfortunately cannot
874 * be done when recursing into submodules because when a pathspec is
875 * given which spans repository boundaries you can't simply remove the
876 * submodule entry because the pathspec may match something inside the
877 * submodule.
879 if (recurse_submodules)
880 max_prefix = NULL;
881 else
882 max_prefix = common_prefix(&pathspec);
883 max_prefix_len = get_common_prefix_len(max_prefix);
885 prune_index(the_repository->index, max_prefix, max_prefix_len);
887 /* Treat unmatching pathspec elements as errors */
888 if (pathspec.nr && error_unmatch)
889 ps_matched = xcalloc(pathspec.nr, 1);
891 if ((dir.flags & DIR_SHOW_IGNORED) && !show_others && !show_cached)
892 die("ls-files -i must be used with either -o or -c");
894 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
895 die("ls-files --ignored needs some exclude pattern");
897 /* With no flags, we default to showing the cached files */
898 if (!(show_stage || show_deleted || show_others || show_unmerged ||
899 show_killed || show_modified || show_resolve_undo))
900 show_cached = 1;
902 if (with_tree) {
904 * Basic sanity check; show-stages and show-unmerged
905 * would not make any sense with this option.
907 if (show_stage || show_unmerged)
908 die(_("options '%s' and '%s' cannot be used together"), "ls-files --with-tree", "-s/-u");
909 overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
912 show_files(the_repository, &dir);
914 if (show_resolve_undo)
915 show_ru_info(the_repository->index);
917 if (ps_matched && report_path_error(ps_matched, &pathspec)) {
918 fprintf(stderr, "Did you forget to 'git add'?\n");
919 ret = 1;
922 string_list_clear(&exclude_list, 0);
923 dir_clear(&dir);
924 free(max_prefix);
925 return ret;