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
13 #include "parse-options.h"
14 #include "resolve-undo.h"
15 #include "string-list.h"
17 #include "run-command.h"
18 #include "submodule.h"
21 static int show_deleted;
22 static int show_cached;
23 static int show_others;
24 static int show_stage;
25 static int show_unmerged;
26 static int show_resolve_undo;
27 static int show_modified;
28 static int show_killed;
29 static int show_valid_bit;
30 static int line_terminator = '\n';
31 static int debug_mode;
33 static int recurse_submodules;
34 static struct argv_array submodule_options = ARGV_ARRAY_INIT;
36 static const char *prefix;
37 static const char *super_prefix;
38 static int max_prefix_len;
39 static int prefix_len;
40 static struct pathspec pathspec;
41 static int error_unmatch;
42 static char *ps_matched;
43 static const char *with_tree;
45 static int exclude_args;
47 static const char *tag_cached = "";
48 static const char *tag_unmerged = "";
49 static const char *tag_removed = "";
50 static const char *tag_other = "";
51 static const char *tag_killed = "";
52 static const char *tag_modified = "";
53 static const char *tag_skip_worktree = "";
54 static const char *tag_resolve_undo = "";
56 static void write_eolinfo(const struct index_state *istate,
57 const struct cache_entry *ce, const char *path)
61 const char *i_txt = "";
62 const char *w_txt = "";
63 const char *a_txt = get_convert_attr_ascii(path);
64 if (ce && S_ISREG(ce->ce_mode))
65 i_txt = get_cached_convert_stats_ascii(istate,
67 if (!lstat(path, &st) && S_ISREG(st.st_mode))
68 w_txt = get_wt_convert_stats_ascii(path);
69 printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
73 static void write_name(const char *name)
76 * Prepend the super_prefix to name to construct the full_name to be
79 struct strbuf full_name = STRBUF_INIT;
81 strbuf_addstr(&full_name, super_prefix);
82 strbuf_addstr(&full_name, name);
87 * With "--full-name", prefix_len=0; this caller needs to pass
88 * an empty string in that case (a NULL is good for "").
90 write_name_quoted_relative(name, prefix_len ? prefix : NULL,
91 stdout, line_terminator);
93 strbuf_release(&full_name);
96 static void print_debug(const struct cache_entry *ce)
99 const struct stat_data *sd = &ce->ce_stat_data;
101 printf(" ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
102 printf(" mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
103 printf(" dev: %d\tino: %d\n", sd->sd_dev, sd->sd_ino);
104 printf(" uid: %d\tgid: %d\n", sd->sd_uid, sd->sd_gid);
105 printf(" size: %d\tflags: %x\n", sd->sd_size, ce->ce_flags);
109 static void show_dir_entry(const char *tag, struct dir_entry *ent)
111 int len = max_prefix_len;
114 die("git ls-files: internal error - directory entry not superset of prefix");
116 if (!dir_path_match(ent, &pathspec, len, ps_matched))
120 write_eolinfo(NULL, NULL, ent->name);
121 write_name(ent->name);
124 static void show_other_files(const struct index_state *istate,
125 const struct dir_struct *dir)
129 for (i = 0; i < dir->nr; i++) {
130 struct dir_entry *ent = dir->entries[i];
131 if (!index_name_is_other(istate, ent->name, ent->len))
133 show_dir_entry(tag_other, ent);
137 static void show_killed_files(const struct index_state *istate,
138 const struct dir_struct *dir)
141 for (i = 0; i < dir->nr; i++) {
142 struct dir_entry *ent = dir->entries[i];
144 int pos, len, killed = 0;
146 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
147 sp = strchr(cp, '/');
149 /* If ent->name is prefix of an entry in the
150 * cache, it will be killed.
152 pos = index_name_pos(istate, ent->name, ent->len);
154 die("BUG: killed-file %.*s not found",
155 ent->len, ent->name);
157 while (pos < istate->cache_nr &&
158 ce_stage(istate->cache[pos]))
159 pos++; /* skip unmerged */
160 if (istate->cache_nr <= pos)
162 /* pos points at a name immediately after
163 * ent->name in the cache. Does it expect
164 * ent->name to be a directory?
166 len = ce_namelen(istate->cache[pos]);
167 if ((ent->len < len) &&
168 !strncmp(istate->cache[pos]->name,
169 ent->name, ent->len) &&
170 istate->cache[pos]->name[ent->len] == '/')
174 if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
175 /* If any of the leading directories in
176 * ent->name is registered in the cache,
177 * ent->name will be killed.
184 show_dir_entry(tag_killed, dir->entries[i]);
189 * Compile an argv_array with all of the options supported by --recurse_submodules
191 static void compile_submodule_options(const char **argv,
192 const struct dir_struct *dir,
195 if (line_terminator == '\0')
196 argv_array_push(&submodule_options, "-z");
198 argv_array_push(&submodule_options, "-t");
200 argv_array_push(&submodule_options, "-v");
202 argv_array_push(&submodule_options, "--cached");
204 argv_array_push(&submodule_options, "--eol");
206 argv_array_push(&submodule_options, "--debug");
209 argv_array_push(&submodule_options, "--");
210 for (; *argv; argv++)
211 argv_array_push(&submodule_options, *argv);
215 * Recursively call ls-files on a submodule
217 static void show_gitlink(const struct cache_entry *ce)
219 struct child_process cp = CHILD_PROCESS_INIT;
223 prepare_submodule_repo_env(&cp.env_array);
224 argv_array_push(&cp.env_array, GIT_DIR_ENVIRONMENT);
227 argv_array_pushf(&cp.env_array, "%s=%s",
228 GIT_TOPLEVEL_PREFIX_ENVIRONMENT,
230 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
231 super_prefix ? super_prefix : "",
233 argv_array_push(&cp.args, "ls-files");
234 argv_array_push(&cp.args, "--recurse-submodules");
236 /* add supported options */
237 argv_array_pushv(&cp.args, submodule_options.argv);
240 dir = mkpathdup("%s/%s", get_git_work_tree(), ce->name);
242 status = run_command(&cp);
248 static void show_ce_entry(const struct index_state *istate,
249 const char *tag, const struct cache_entry *ce)
251 struct strbuf name = STRBUF_INIT;
252 int len = max_prefix_len;
254 strbuf_addstr(&name, super_prefix);
255 strbuf_addstr(&name, ce->name);
257 if (len > ce_namelen(ce))
258 die("git ls-files: internal error - cache entry not superset of prefix");
260 if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
261 submodule_path_match(&pathspec, name.buf, ps_matched)) {
263 } else if (match_pathspec(&pathspec, name.buf, name.len,
265 S_ISDIR(ce->ce_mode) ||
266 S_ISGITLINK(ce->ce_mode))) {
267 if (tag && *tag && show_valid_bit &&
268 (ce->ce_flags & CE_VALID)) {
269 static char alttag[4];
270 memcpy(alttag, tag, 3);
272 alttag[0] = tolower(tag[0]);
273 else if (tag[0] == '?')
287 printf("%s%06o %s %d\t",
290 find_unique_abbrev(ce->oid.hash, abbrev),
293 write_eolinfo(istate, ce, ce->name);
294 write_name(ce->name);
298 strbuf_release(&name);
301 static void show_ru_info(const struct index_state *istate)
303 struct string_list_item *item;
305 if (!istate->resolve_undo)
308 for_each_string_list_item(item, istate->resolve_undo) {
309 const char *path = item->string;
310 struct resolve_undo_info *ui = item->util;
314 if (len < max_prefix_len)
315 continue; /* outside of the prefix */
316 if (!match_pathspec(&pathspec, path, len,
317 max_prefix_len, ps_matched, 0))
318 continue; /* uninterested */
319 for (i = 0; i < 3; i++) {
322 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
323 find_unique_abbrev(ui->sha1[i], abbrev),
330 static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
331 const struct cache_entry *ce)
333 int dtype = ce_to_dtype(ce);
334 return is_excluded(dir, istate, ce->name, &dtype);
337 static void show_files(struct index_state *istate, struct dir_struct *dir)
341 /* For cached/deleted files we don't need to even do the readdir */
342 if (show_others || show_killed) {
344 dir->flags |= DIR_COLLECT_KILLED_ONLY;
345 fill_directory(dir, istate, &pathspec);
347 show_other_files(istate, dir);
349 show_killed_files(istate, dir);
351 if (show_cached || show_stage) {
352 for (i = 0; i < istate->cache_nr; i++) {
353 const struct cache_entry *ce = istate->cache[i];
354 if ((dir->flags & DIR_SHOW_IGNORED) &&
355 !ce_excluded(dir, istate, ce))
357 if (show_unmerged && !ce_stage(ce))
359 if (ce->ce_flags & CE_UPDATE)
361 show_ce_entry(istate, ce_stage(ce) ? tag_unmerged :
362 (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce);
365 if (show_deleted || show_modified) {
366 for (i = 0; i < istate->cache_nr; i++) {
367 const struct cache_entry *ce = istate->cache[i];
370 if ((dir->flags & DIR_SHOW_IGNORED) &&
371 !ce_excluded(dir, istate, ce))
373 if (ce->ce_flags & CE_UPDATE)
375 if (ce_skip_worktree(ce))
377 err = lstat(ce->name, &st);
378 if (show_deleted && err)
379 show_ce_entry(istate, tag_removed, ce);
380 if (show_modified && ie_modified(istate, ce, &st, 0))
381 show_ce_entry(istate, tag_modified, ce);
387 * Prune the index to only contain stuff starting with "prefix"
389 static void prune_index(struct index_state *istate,
390 const char *prefix, size_t prefixlen)
393 unsigned int first, last;
397 pos = index_name_pos(istate, prefix, prefixlen);
401 last = istate->cache_nr;
402 while (last > first) {
403 int next = (last + first) >> 1;
404 const struct cache_entry *ce = istate->cache[next];
405 if (!strncmp(ce->name, prefix, prefixlen)) {
411 memmove(istate->cache, istate->cache + pos,
412 (last - pos) * sizeof(struct cache_entry *));
413 istate->cache_nr = last - pos;
416 static int get_common_prefix_len(const char *common_prefix)
418 int common_prefix_len;
423 common_prefix_len = strlen(common_prefix);
426 * If the prefix has a trailing slash, strip it so that submodules wont
427 * be pruned from the index.
429 if (common_prefix[common_prefix_len - 1] == '/')
432 return common_prefix_len;
436 * Read the tree specified with --with-tree option
437 * (typically, HEAD) into stage #1 and then
438 * squash them down to stage #0. This is used for
439 * --error-unmatch to list and check the path patterns
440 * that were given from the command line. We are not
441 * going to write this index out.
443 void overlay_tree_on_index(struct index_state *istate,
444 const char *tree_name, const char *prefix)
447 struct object_id oid;
448 struct pathspec pathspec;
449 struct cache_entry *last_stage0 = NULL;
452 if (get_oid(tree_name, &oid))
453 die("tree-ish %s not found.", tree_name);
454 tree = parse_tree_indirect(&oid);
456 die("bad tree-ish %s", tree_name);
458 /* Hoist the unmerged entries up to stage #3 to make room */
459 for (i = 0; i < istate->cache_nr; i++) {
460 struct cache_entry *ce = istate->cache[i];
463 ce->ce_flags |= CE_STAGEMASK;
467 static const char *(matchbuf[1]);
469 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
470 PATHSPEC_PREFER_CWD, prefix, matchbuf);
472 memset(&pathspec, 0, sizeof(pathspec));
473 if (read_tree(tree, 1, &pathspec, istate))
474 die("unable to read tree entries %s", tree_name);
476 for (i = 0; i < istate->cache_nr; i++) {
477 struct cache_entry *ce = istate->cache[i];
478 switch (ce_stage(ce)) {
486 * If there is stage #0 entry for this, we do not
487 * need to show it. We use CE_UPDATE bit to mark
491 !strcmp(last_stage0->name, ce->name))
492 ce->ce_flags |= CE_UPDATE;
497 static const char * const ls_files_usage[] = {
498 N_("git ls-files [<options>] [<file>...]"),
502 static int option_parse_exclude(const struct option *opt,
503 const char *arg, int unset)
505 struct string_list *exclude_list = opt->value;
508 string_list_append(exclude_list, arg);
513 static int option_parse_exclude_from(const struct option *opt,
514 const char *arg, int unset)
516 struct dir_struct *dir = opt->value;
519 add_excludes_from_file(dir, arg);
524 static int option_parse_exclude_standard(const struct option *opt,
525 const char *arg, int unset)
527 struct dir_struct *dir = opt->value;
530 setup_standard_excludes(dir);
535 int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
537 int require_work_tree = 0, show_tag = 0, i;
538 const char *max_prefix;
539 struct dir_struct dir;
540 struct exclude_list *el;
541 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
542 struct option builtin_ls_files_options[] = {
543 /* Think twice before adding "--nul" synonym to this */
544 OPT_SET_INT('z', NULL, &line_terminator,
545 N_("paths are separated with NUL character"), '\0'),
546 OPT_BOOL('t', NULL, &show_tag,
547 N_("identify the file status with tags")),
548 OPT_BOOL('v', NULL, &show_valid_bit,
549 N_("use lowercase letters for 'assume unchanged' files")),
550 OPT_BOOL('c', "cached", &show_cached,
551 N_("show cached files in the output (default)")),
552 OPT_BOOL('d', "deleted", &show_deleted,
553 N_("show deleted files in the output")),
554 OPT_BOOL('m', "modified", &show_modified,
555 N_("show modified files in the output")),
556 OPT_BOOL('o', "others", &show_others,
557 N_("show other files in the output")),
558 OPT_BIT('i', "ignored", &dir.flags,
559 N_("show ignored files in the output"),
561 OPT_BOOL('s', "stage", &show_stage,
562 N_("show staged contents' object name in the output")),
563 OPT_BOOL('k', "killed", &show_killed,
564 N_("show files on the filesystem that need to be removed")),
565 OPT_BIT(0, "directory", &dir.flags,
566 N_("show 'other' directories' names only"),
567 DIR_SHOW_OTHER_DIRECTORIES),
568 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
569 OPT_NEGBIT(0, "empty-directory", &dir.flags,
570 N_("don't show empty directories"),
571 DIR_HIDE_EMPTY_DIRECTORIES),
572 OPT_BOOL('u', "unmerged", &show_unmerged,
573 N_("show unmerged files in the output")),
574 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
575 N_("show resolve-undo information")),
576 { OPTION_CALLBACK, 'x', "exclude", &exclude_list, N_("pattern"),
577 N_("skip files matching pattern"),
578 0, option_parse_exclude },
579 { OPTION_CALLBACK, 'X', "exclude-from", &dir, N_("file"),
580 N_("exclude patterns are read from <file>"),
581 0, option_parse_exclude_from },
582 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
583 N_("read additional per-directory exclude patterns in <file>")),
584 { OPTION_CALLBACK, 0, "exclude-standard", &dir, NULL,
585 N_("add the standard git exclusions"),
586 PARSE_OPT_NOARG, option_parse_exclude_standard },
587 { OPTION_SET_INT, 0, "full-name", &prefix_len, NULL,
588 N_("make the output relative to the project top directory"),
589 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL },
590 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
591 N_("recurse through submodules")),
592 OPT_BOOL(0, "error-unmatch", &error_unmatch,
593 N_("if any <file> is not in the index, treat this as an error")),
594 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
595 N_("pretend that paths removed since <tree-ish> are still present")),
596 OPT__ABBREV(&abbrev),
597 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
601 if (argc == 2 && !strcmp(argv[1], "-h"))
602 usage_with_options(ls_files_usage, builtin_ls_files_options);
604 memset(&dir, 0, sizeof(dir));
607 prefix_len = strlen(prefix);
608 super_prefix = get_super_prefix();
609 git_config(git_default_config, NULL);
611 if (read_cache() < 0)
612 die("index file corrupt");
614 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
616 el = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
617 for (i = 0; i < exclude_list.nr; i++) {
618 add_exclude(exclude_list.items[i].string, "", 0, el, --exclude_args);
620 if (show_tag || show_valid_bit) {
627 tag_skip_worktree = "S ";
628 tag_resolve_undo = "U ";
630 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
631 require_work_tree = 1;
634 * There's no point in showing unmerged unless
635 * you also show the stage information.
638 if (dir.exclude_per_dir)
641 if (require_work_tree && !is_inside_work_tree())
644 if (recurse_submodules)
645 compile_submodule_options(argv, &dir, show_tag);
647 if (recurse_submodules &&
648 (show_stage || show_deleted || show_others || show_unmerged ||
649 show_killed || show_modified || show_resolve_undo || with_tree))
650 die("ls-files --recurse-submodules unsupported mode");
652 if (recurse_submodules && error_unmatch)
653 die("ls-files --recurse-submodules does not support "
656 parse_pathspec(&pathspec, 0,
661 * Find common prefix for all pathspec's
662 * This is used as a performance optimization which unfortunately cannot
663 * be done when recursing into submodules
665 if (recurse_submodules)
668 max_prefix = common_prefix(&pathspec);
669 max_prefix_len = get_common_prefix_len(max_prefix);
671 prune_index(&the_index, max_prefix, max_prefix_len);
673 /* Treat unmatching pathspec elements as errors */
674 if (pathspec.nr && error_unmatch)
675 ps_matched = xcalloc(pathspec.nr, 1);
677 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
678 die("ls-files --ignored needs some exclude pattern");
680 /* With no flags, we default to showing the cached files */
681 if (!(show_stage || show_deleted || show_others || show_unmerged ||
682 show_killed || show_modified || show_resolve_undo))
687 * Basic sanity check; show-stages and show-unmerged
688 * would not make any sense with this option.
690 if (show_stage || show_unmerged)
691 die("ls-files --with-tree is incompatible with -s or -u");
692 overlay_tree_on_index(&the_index, with_tree, max_prefix);
694 show_files(&the_index, &dir);
695 if (show_resolve_undo)
696 show_ru_info(&the_index);
700 bad = report_path_error(ps_matched, &pathspec, prefix);
702 fprintf(stderr, "Did you forget to 'git add'?\n");