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
9 #include "repository.h"
15 #include "cache-tree.h"
16 #include "parse-options.h"
17 #include "resolve-undo.h"
18 #include "string-list.h"
20 #include "run-command.h"
21 #include "submodule.h"
22 #include "submodule-config.h"
25 static int show_deleted;
26 static int show_cached;
27 static int show_others;
28 static int show_stage;
29 static int show_unmerged;
30 static int show_resolve_undo;
31 static int show_modified;
32 static int show_killed;
33 static int show_valid_bit;
34 static int show_fsmonitor_bit;
35 static int line_terminator = '\n';
36 static int debug_mode;
38 static int recurse_submodules;
39 static int skipping_duplicates;
41 static const char *prefix;
42 static int max_prefix_len;
43 static int prefix_len;
44 static struct pathspec pathspec;
45 static int error_unmatch;
46 static char *ps_matched;
47 static const char *with_tree;
49 static int exclude_args;
51 static const char *tag_cached = "";
52 static const char *tag_unmerged = "";
53 static const char *tag_removed = "";
54 static const char *tag_other = "";
55 static const char *tag_killed = "";
56 static const char *tag_modified = "";
57 static const char *tag_skip_worktree = "";
58 static const char *tag_resolve_undo = "";
60 static void write_eolinfo(const struct index_state *istate,
61 const struct cache_entry *ce, const char *path)
65 const char *i_txt = "";
66 const char *w_txt = "";
67 const char *a_txt = get_convert_attr_ascii(istate, path);
68 if (ce && S_ISREG(ce->ce_mode))
69 i_txt = get_cached_convert_stats_ascii(istate,
71 if (!lstat(path, &st) && S_ISREG(st.st_mode))
72 w_txt = get_wt_convert_stats_ascii(path);
73 printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
77 static void write_name(const char *name)
80 * With "--full-name", prefix_len=0; this caller needs to pass
81 * an empty string in that case (a NULL is good for "").
83 write_name_quoted_relative(name, prefix_len ? prefix : NULL,
84 stdout, line_terminator);
87 static const char *get_tag(const struct cache_entry *ce, const char *tag)
89 static char alttag[4];
91 if (tag && *tag && ((show_valid_bit && (ce->ce_flags & CE_VALID)) ||
92 (show_fsmonitor_bit && (ce->ce_flags & CE_FSMONITOR_VALID)))) {
93 memcpy(alttag, tag, 3);
95 if (isalpha(tag[0])) {
96 alttag[0] = tolower(tag[0]);
97 } else if (tag[0] == '?') {
112 static void print_debug(const struct cache_entry *ce)
115 const struct stat_data *sd = &ce->ce_stat_data;
117 printf(" ctime: %u:%u\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
118 printf(" mtime: %u:%u\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
119 printf(" dev: %u\tino: %u\n", sd->sd_dev, sd->sd_ino);
120 printf(" uid: %u\tgid: %u\n", sd->sd_uid, sd->sd_gid);
121 printf(" size: %u\tflags: %x\n", sd->sd_size, ce->ce_flags);
125 static void show_dir_entry(const struct index_state *istate,
126 const char *tag, struct dir_entry *ent)
128 int len = max_prefix_len;
131 die("git ls-files: internal error - directory entry not superset of prefix");
133 /* If ps_matches is non-NULL, figure out which pathspec(s) match. */
135 dir_path_match(istate, ent, &pathspec, len, ps_matched);
138 write_eolinfo(istate, NULL, ent->name);
139 write_name(ent->name);
142 static void show_other_files(const struct index_state *istate,
143 const struct dir_struct *dir)
147 for (i = 0; i < dir->nr; i++) {
148 struct dir_entry *ent = dir->entries[i];
149 if (!index_name_is_other(istate, ent->name, ent->len))
151 show_dir_entry(istate, tag_other, ent);
155 static void show_killed_files(const struct index_state *istate,
156 const struct dir_struct *dir)
159 for (i = 0; i < dir->nr; i++) {
160 struct dir_entry *ent = dir->entries[i];
162 int pos, len, killed = 0;
164 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
165 sp = strchr(cp, '/');
167 /* If ent->name is prefix of an entry in the
168 * cache, it will be killed.
170 pos = index_name_pos(istate, ent->name, ent->len);
172 BUG("killed-file %.*s not found",
173 ent->len, ent->name);
175 while (pos < istate->cache_nr &&
176 ce_stage(istate->cache[pos]))
177 pos++; /* skip unmerged */
178 if (istate->cache_nr <= pos)
180 /* pos points at a name immediately after
181 * ent->name in the cache. Does it expect
182 * ent->name to be a directory?
184 len = ce_namelen(istate->cache[pos]);
185 if ((ent->len < len) &&
186 !strncmp(istate->cache[pos]->name,
187 ent->name, ent->len) &&
188 istate->cache[pos]->name[ent->len] == '/')
192 if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
193 /* If any of the leading directories in
194 * ent->name is registered in the cache,
195 * ent->name will be killed.
202 show_dir_entry(istate, tag_killed, dir->entries[i]);
206 static void show_files(struct repository *repo, struct dir_struct *dir);
208 static void show_submodule(struct repository *superproject,
209 struct dir_struct *dir, const char *path)
211 struct repository subrepo;
212 const struct submodule *sub = submodule_from_path(superproject,
215 if (repo_submodule_init(&subrepo, superproject, sub))
218 if (repo_read_index(&subrepo) < 0)
219 die("index file corrupt");
221 show_files(&subrepo, dir);
223 repo_clear(&subrepo);
226 static void show_ce(struct repository *repo, struct dir_struct *dir,
227 const struct cache_entry *ce, const char *fullname,
230 if (max_prefix_len > strlen(fullname))
231 die("git ls-files: internal error - cache entry not superset of prefix");
233 if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
234 is_submodule_active(repo, ce->name)) {
235 show_submodule(repo, dir, ce->name);
236 } else if (match_pathspec(repo->index, &pathspec, fullname, strlen(fullname),
237 max_prefix_len, ps_matched,
238 S_ISDIR(ce->ce_mode) ||
239 S_ISGITLINK(ce->ce_mode))) {
240 tag = get_tag(ce, tag);
245 printf("%s%06o %s %d\t",
248 find_unique_abbrev(&ce->oid, abbrev),
251 write_eolinfo(repo->index, ce, fullname);
252 write_name(fullname);
257 static void show_ru_info(const struct index_state *istate)
259 struct string_list_item *item;
261 if (!istate->resolve_undo)
264 for_each_string_list_item(item, istate->resolve_undo) {
265 const char *path = item->string;
266 struct resolve_undo_info *ui = item->util;
270 if (len < max_prefix_len)
271 continue; /* outside of the prefix */
272 if (!match_pathspec(istate, &pathspec, path, len,
273 max_prefix_len, ps_matched, 0))
274 continue; /* uninterested */
275 for (i = 0; i < 3; i++) {
278 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
279 find_unique_abbrev(&ui->oid[i], abbrev),
286 static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
287 const char *fullname, const struct cache_entry *ce)
289 int dtype = ce_to_dtype(ce);
290 return is_excluded(dir, istate, fullname, &dtype);
293 static void construct_fullname(struct strbuf *out, const struct repository *repo,
294 const struct cache_entry *ce)
297 if (repo->submodule_prefix)
298 strbuf_addstr(out, repo->submodule_prefix);
299 strbuf_addstr(out, ce->name);
302 static void show_files(struct repository *repo, struct dir_struct *dir)
305 struct strbuf fullname = STRBUF_INIT;
307 /* For cached/deleted files we don't need to even do the readdir */
308 if (show_others || show_killed) {
310 dir->flags |= DIR_COLLECT_KILLED_ONLY;
311 fill_directory(dir, repo->index, &pathspec);
313 show_other_files(repo->index, dir);
315 show_killed_files(repo->index, dir);
318 if (!(show_cached || show_stage || show_deleted || show_modified))
320 for (i = 0; i < repo->index->cache_nr; i++) {
321 const struct cache_entry *ce = repo->index->cache[i];
325 construct_fullname(&fullname, repo, ce);
327 if ((dir->flags & DIR_SHOW_IGNORED) &&
328 !ce_excluded(dir, repo->index, fullname.buf, ce))
330 if (ce->ce_flags & CE_UPDATE)
332 if ((show_cached || show_stage) &&
333 (!show_unmerged || ce_stage(ce))) {
334 show_ce(repo, dir, ce, fullname.buf,
335 ce_stage(ce) ? tag_unmerged :
336 (ce_skip_worktree(ce) ? tag_skip_worktree :
338 if (skipping_duplicates)
339 goto skip_to_next_name;
342 if (!(show_deleted || show_modified))
344 if (ce_skip_worktree(ce))
346 stat_err = lstat(fullname.buf, &st);
347 if (stat_err && (errno != ENOENT && errno != ENOTDIR))
348 error_errno("cannot lstat '%s'", fullname.buf);
349 if (stat_err && show_deleted) {
350 show_ce(repo, dir, ce, fullname.buf, tag_removed);
351 if (skipping_duplicates)
352 goto skip_to_next_name;
355 (stat_err || ie_modified(repo->index, ce, &st, 0))) {
356 show_ce(repo, dir, ce, fullname.buf, tag_modified);
357 if (skipping_duplicates)
358 goto skip_to_next_name;
365 struct cache_entry **cache = repo->index->cache;
366 for (j = i + 1; j < repo->index->cache_nr; j++)
367 if (strcmp(ce->name, cache[j]->name))
369 i = j - 1; /* compensate for the for loop */
373 strbuf_release(&fullname);
377 * Prune the index to only contain stuff starting with "prefix"
379 static void prune_index(struct index_state *istate,
380 const char *prefix, size_t prefixlen)
383 unsigned int first, last;
385 if (!prefix || !istate->cache_nr)
387 pos = index_name_pos(istate, prefix, prefixlen);
391 last = istate->cache_nr;
392 while (last > first) {
393 int next = first + ((last - first) >> 1);
394 const struct cache_entry *ce = istate->cache[next];
395 if (!strncmp(ce->name, prefix, prefixlen)) {
401 MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos);
402 istate->cache_nr = last - pos;
405 static int get_common_prefix_len(const char *common_prefix)
407 int common_prefix_len;
412 common_prefix_len = strlen(common_prefix);
415 * If the prefix has a trailing slash, strip it so that submodules wont
416 * be pruned from the index.
418 if (common_prefix[common_prefix_len - 1] == '/')
421 return common_prefix_len;
424 static int read_one_entry_opt(struct index_state *istate,
425 const struct object_id *oid,
426 const char *base, int baselen,
427 const char *pathname,
428 unsigned mode, int stage, int opt)
431 struct cache_entry *ce;
434 return READ_TREE_RECURSIVE;
436 len = strlen(pathname);
437 ce = make_empty_cache_entry(istate, baselen + len);
439 ce->ce_mode = create_ce_mode(mode);
440 ce->ce_flags = create_ce_flags(stage);
441 ce->ce_namelen = baselen + len;
442 memcpy(ce->name, base, baselen);
443 memcpy(ce->name + baselen, pathname, len+1);
444 oidcpy(&ce->oid, oid);
445 return add_index_entry(istate, ce, opt);
448 static int read_one_entry(const struct object_id *oid, struct strbuf *base,
449 const char *pathname, unsigned mode, int stage,
452 struct index_state *istate = context;
453 return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
455 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
459 * This is used when the caller knows there is no existing entries at
460 * the stage that will conflict with the entry being added.
462 static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
463 const char *pathname, unsigned mode, int stage,
466 struct index_state *istate = context;
467 return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
469 ADD_CACHE_JUST_APPEND);
473 static int read_tree(struct repository *r, struct tree *tree, int stage,
474 struct pathspec *match, struct index_state *istate)
476 read_tree_fn_t fn = NULL;
480 * Currently the only existing callers of this function all
481 * call it with stage=1 and after making sure there is nothing
482 * at that stage; we could always use read_one_entry_quick().
484 * But when we decide to straighten out git-read-tree not to
485 * use unpack_trees() in some cases, this will probably start
490 * See if we have cache entry at the stage. If so,
491 * do it the original slow way, otherwise, append and then
494 for (i = 0; !fn && i < istate->cache_nr; i++) {
495 const struct cache_entry *ce = istate->cache[i];
496 if (ce_stage(ce) == stage)
501 fn = read_one_entry_quick;
502 err = read_tree_recursive(r, tree, "", 0, stage, match, fn, istate);
503 if (fn == read_one_entry || err)
507 * Sort the cache entry -- we need to nuke the cache tree, though.
509 cache_tree_free(&istate->cache_tree);
510 QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
515 * Read the tree specified with --with-tree option
516 * (typically, HEAD) into stage #1 and then
517 * squash them down to stage #0. This is used for
518 * --error-unmatch to list and check the path patterns
519 * that were given from the command line. We are not
520 * going to write this index out.
522 void overlay_tree_on_index(struct index_state *istate,
523 const char *tree_name, const char *prefix)
526 struct object_id oid;
527 struct pathspec pathspec;
528 struct cache_entry *last_stage0 = NULL;
531 if (get_oid(tree_name, &oid))
532 die("tree-ish %s not found.", tree_name);
533 tree = parse_tree_indirect(&oid);
535 die("bad tree-ish %s", tree_name);
537 /* Hoist the unmerged entries up to stage #3 to make room */
538 for (i = 0; i < istate->cache_nr; i++) {
539 struct cache_entry *ce = istate->cache[i];
542 ce->ce_flags |= CE_STAGEMASK;
546 static const char *(matchbuf[1]);
548 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
549 PATHSPEC_PREFER_CWD, prefix, matchbuf);
551 memset(&pathspec, 0, sizeof(pathspec));
552 if (read_tree(the_repository, tree, 1, &pathspec, istate))
553 die("unable to read tree entries %s", tree_name);
555 for (i = 0; i < istate->cache_nr; i++) {
556 struct cache_entry *ce = istate->cache[i];
557 switch (ce_stage(ce)) {
565 * If there is stage #0 entry for this, we do not
566 * need to show it. We use CE_UPDATE bit to mark
570 !strcmp(last_stage0->name, ce->name))
571 ce->ce_flags |= CE_UPDATE;
576 static const char * const ls_files_usage[] = {
577 N_("git ls-files [<options>] [<file>...]"),
581 static int option_parse_exclude(const struct option *opt,
582 const char *arg, int unset)
584 struct string_list *exclude_list = opt->value;
586 BUG_ON_OPT_NEG(unset);
589 string_list_append(exclude_list, arg);
594 static int option_parse_exclude_from(const struct option *opt,
595 const char *arg, int unset)
597 struct dir_struct *dir = opt->value;
599 BUG_ON_OPT_NEG(unset);
602 add_patterns_from_file(dir, arg);
607 static int option_parse_exclude_standard(const struct option *opt,
608 const char *arg, int unset)
610 struct dir_struct *dir = opt->value;
612 BUG_ON_OPT_NEG(unset);
616 setup_standard_excludes(dir);
621 int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
623 int require_work_tree = 0, show_tag = 0, i;
624 const char *max_prefix;
625 struct dir_struct dir;
626 struct pattern_list *pl;
627 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
628 struct option builtin_ls_files_options[] = {
629 /* Think twice before adding "--nul" synonym to this */
630 OPT_SET_INT('z', NULL, &line_terminator,
631 N_("paths are separated with NUL character"), '\0'),
632 OPT_BOOL('t', NULL, &show_tag,
633 N_("identify the file status with tags")),
634 OPT_BOOL('v', NULL, &show_valid_bit,
635 N_("use lowercase letters for 'assume unchanged' files")),
636 OPT_BOOL('f', NULL, &show_fsmonitor_bit,
637 N_("use lowercase letters for 'fsmonitor clean' files")),
638 OPT_BOOL('c', "cached", &show_cached,
639 N_("show cached files in the output (default)")),
640 OPT_BOOL('d', "deleted", &show_deleted,
641 N_("show deleted files in the output")),
642 OPT_BOOL('m', "modified", &show_modified,
643 N_("show modified files in the output")),
644 OPT_BOOL('o', "others", &show_others,
645 N_("show other files in the output")),
646 OPT_BIT('i', "ignored", &dir.flags,
647 N_("show ignored files in the output"),
649 OPT_BOOL('s', "stage", &show_stage,
650 N_("show staged contents' object name in the output")),
651 OPT_BOOL('k', "killed", &show_killed,
652 N_("show files on the filesystem that need to be removed")),
653 OPT_BIT(0, "directory", &dir.flags,
654 N_("show 'other' directories' names only"),
655 DIR_SHOW_OTHER_DIRECTORIES),
656 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
657 OPT_NEGBIT(0, "empty-directory", &dir.flags,
658 N_("don't show empty directories"),
659 DIR_HIDE_EMPTY_DIRECTORIES),
660 OPT_BOOL('u', "unmerged", &show_unmerged,
661 N_("show unmerged files in the output")),
662 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
663 N_("show resolve-undo information")),
664 OPT_CALLBACK_F('x', "exclude", &exclude_list, N_("pattern"),
665 N_("skip files matching pattern"),
666 PARSE_OPT_NONEG, option_parse_exclude),
667 OPT_CALLBACK_F('X', "exclude-from", &dir, N_("file"),
668 N_("exclude patterns are read from <file>"),
669 PARSE_OPT_NONEG, option_parse_exclude_from),
670 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
671 N_("read additional per-directory exclude patterns in <file>")),
672 OPT_CALLBACK_F(0, "exclude-standard", &dir, NULL,
673 N_("add the standard git exclusions"),
674 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
675 option_parse_exclude_standard),
676 OPT_SET_INT_F(0, "full-name", &prefix_len,
677 N_("make the output relative to the project top directory"),
679 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
680 N_("recurse through submodules")),
681 OPT_BOOL(0, "error-unmatch", &error_unmatch,
682 N_("if any <file> is not in the index, treat this as an error")),
683 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
684 N_("pretend that paths removed since <tree-ish> are still present")),
685 OPT__ABBREV(&abbrev),
686 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
687 OPT_BOOL(0, "deduplicate", &skipping_duplicates,
688 N_("suppress duplicate entries")),
692 if (argc == 2 && !strcmp(argv[1], "-h"))
693 usage_with_options(ls_files_usage, builtin_ls_files_options);
698 prefix_len = strlen(prefix);
699 git_config(git_default_config, NULL);
701 if (repo_read_index(the_repository) < 0)
702 die("index file corrupt");
704 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
706 pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
707 for (i = 0; i < exclude_list.nr; i++) {
708 add_pattern(exclude_list.items[i].string, "", 0, pl, --exclude_args);
710 if (show_tag || show_valid_bit || show_fsmonitor_bit) {
717 tag_skip_worktree = "S ";
718 tag_resolve_undo = "U ";
720 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
721 require_work_tree = 1;
724 * There's no point in showing unmerged unless
725 * you also show the stage information.
728 if (show_tag || show_stage)
729 skipping_duplicates = 0;
730 if (dir.exclude_per_dir)
733 if (require_work_tree && !is_inside_work_tree())
736 if (recurse_submodules &&
737 (show_stage || show_deleted || show_others || show_unmerged ||
738 show_killed || show_modified || show_resolve_undo || with_tree))
739 die("ls-files --recurse-submodules unsupported mode");
741 if (recurse_submodules && error_unmatch)
742 die("ls-files --recurse-submodules does not support "
745 parse_pathspec(&pathspec, 0,
750 * Find common prefix for all pathspec's
751 * This is used as a performance optimization which unfortunately cannot
752 * be done when recursing into submodules because when a pathspec is
753 * given which spans repository boundaries you can't simply remove the
754 * submodule entry because the pathspec may match something inside the
757 if (recurse_submodules)
760 max_prefix = common_prefix(&pathspec);
761 max_prefix_len = get_common_prefix_len(max_prefix);
763 prune_index(the_repository->index, max_prefix, max_prefix_len);
765 /* Treat unmatching pathspec elements as errors */
766 if (pathspec.nr && error_unmatch)
767 ps_matched = xcalloc(pathspec.nr, 1);
769 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
770 die("ls-files --ignored needs some exclude pattern");
772 /* With no flags, we default to showing the cached files */
773 if (!(show_stage || show_deleted || show_others || show_unmerged ||
774 show_killed || show_modified || show_resolve_undo))
779 * Basic sanity check; show-stages and show-unmerged
780 * would not make any sense with this option.
782 if (show_stage || show_unmerged)
783 die("ls-files --with-tree is incompatible with -s or -u");
784 overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
787 show_files(the_repository, &dir);
789 if (show_resolve_undo)
790 show_ru_info(the_repository->index);
794 bad = report_path_error(ps_matched, &pathspec);
796 fprintf(stderr, "Did you forget to 'git add'?\n");