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
14 #include "parse-options.h"
15 #include "resolve-undo.h"
16 #include "string-list.h"
18 #include "run-command.h"
19 #include "submodule.h"
22 static int show_deleted;
23 static int show_cached;
24 static int show_others;
25 static int show_stage;
26 static int show_unmerged;
27 static int show_resolve_undo;
28 static int show_modified;
29 static int show_killed;
30 static int show_valid_bit;
31 static int line_terminator = '\n';
32 static int debug_mode;
34 static int recurse_submodules;
35 static struct argv_array submodule_options = ARGV_ARRAY_INIT;
37 static const char *prefix;
38 static const char *super_prefix;
39 static int max_prefix_len;
40 static int prefix_len;
41 static struct pathspec pathspec;
42 static int error_unmatch;
43 static char *ps_matched;
44 static const char *with_tree;
46 static int exclude_args;
48 static const char *tag_cached = "";
49 static const char *tag_unmerged = "";
50 static const char *tag_removed = "";
51 static const char *tag_other = "";
52 static const char *tag_killed = "";
53 static const char *tag_modified = "";
54 static const char *tag_skip_worktree = "";
55 static const char *tag_resolve_undo = "";
57 static void write_eolinfo(const struct index_state *istate,
58 const struct cache_entry *ce, const char *path)
62 const char *i_txt = "";
63 const char *w_txt = "";
64 const char *a_txt = get_convert_attr_ascii(path);
65 if (ce && S_ISREG(ce->ce_mode))
66 i_txt = get_cached_convert_stats_ascii(istate,
68 if (!lstat(path, &st) && S_ISREG(st.st_mode))
69 w_txt = get_wt_convert_stats_ascii(path);
70 printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
74 static void write_name(const char *name)
77 * Prepend the super_prefix to name to construct the full_name to be
80 struct strbuf full_name = STRBUF_INIT;
82 strbuf_addstr(&full_name, super_prefix);
83 strbuf_addstr(&full_name, name);
88 * With "--full-name", prefix_len=0; this caller needs to pass
89 * an empty string in that case (a NULL is good for "").
91 write_name_quoted_relative(name, prefix_len ? prefix : NULL,
92 stdout, line_terminator);
94 strbuf_release(&full_name);
97 static const char *get_tag(const struct cache_entry *ce, const char *tag)
99 static char alttag[4];
101 if (tag && *tag && show_valid_bit && (ce->ce_flags & CE_VALID)) {
102 memcpy(alttag, tag, 3);
104 if (isalpha(tag[0])) {
105 alttag[0] = tolower(tag[0]);
106 } else if (tag[0] == '?') {
121 static void print_debug(const struct cache_entry *ce)
124 const struct stat_data *sd = &ce->ce_stat_data;
126 printf(" ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
127 printf(" mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
128 printf(" dev: %d\tino: %d\n", sd->sd_dev, sd->sd_ino);
129 printf(" uid: %d\tgid: %d\n", sd->sd_uid, sd->sd_gid);
130 printf(" size: %d\tflags: %x\n", sd->sd_size, ce->ce_flags);
134 static void show_dir_entry(const char *tag, struct dir_entry *ent)
136 int len = max_prefix_len;
139 die("git ls-files: internal error - directory entry not superset of prefix");
141 if (!dir_path_match(ent, &pathspec, len, ps_matched))
145 write_eolinfo(NULL, NULL, ent->name);
146 write_name(ent->name);
149 static void show_other_files(const struct index_state *istate,
150 const struct dir_struct *dir)
154 for (i = 0; i < dir->nr; i++) {
155 struct dir_entry *ent = dir->entries[i];
156 if (!index_name_is_other(istate, ent->name, ent->len))
158 show_dir_entry(tag_other, ent);
162 static void show_killed_files(const struct index_state *istate,
163 const struct dir_struct *dir)
166 for (i = 0; i < dir->nr; i++) {
167 struct dir_entry *ent = dir->entries[i];
169 int pos, len, killed = 0;
171 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
172 sp = strchr(cp, '/');
174 /* If ent->name is prefix of an entry in the
175 * cache, it will be killed.
177 pos = index_name_pos(istate, ent->name, ent->len);
179 die("BUG: killed-file %.*s not found",
180 ent->len, ent->name);
182 while (pos < istate->cache_nr &&
183 ce_stage(istate->cache[pos]))
184 pos++; /* skip unmerged */
185 if (istate->cache_nr <= pos)
187 /* pos points at a name immediately after
188 * ent->name in the cache. Does it expect
189 * ent->name to be a directory?
191 len = ce_namelen(istate->cache[pos]);
192 if ((ent->len < len) &&
193 !strncmp(istate->cache[pos]->name,
194 ent->name, ent->len) &&
195 istate->cache[pos]->name[ent->len] == '/')
199 if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
200 /* If any of the leading directories in
201 * ent->name is registered in the cache,
202 * ent->name will be killed.
209 show_dir_entry(tag_killed, dir->entries[i]);
214 * Compile an argv_array with all of the options supported by --recurse_submodules
216 static void compile_submodule_options(const char **argv,
217 const struct dir_struct *dir,
220 if (line_terminator == '\0')
221 argv_array_push(&submodule_options, "-z");
223 argv_array_push(&submodule_options, "-t");
225 argv_array_push(&submodule_options, "-v");
227 argv_array_push(&submodule_options, "--cached");
229 argv_array_push(&submodule_options, "--eol");
231 argv_array_push(&submodule_options, "--debug");
234 argv_array_push(&submodule_options, "--");
235 for (; *argv; argv++)
236 argv_array_push(&submodule_options, *argv);
240 * Recursively call ls-files on a submodule
242 static void show_gitlink(const struct cache_entry *ce)
244 struct child_process cp = CHILD_PROCESS_INIT;
248 prepare_submodule_repo_env(&cp.env_array);
249 argv_array_push(&cp.env_array, GIT_DIR_ENVIRONMENT);
252 argv_array_pushf(&cp.env_array, "%s=%s",
253 GIT_TOPLEVEL_PREFIX_ENVIRONMENT,
255 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
256 super_prefix ? super_prefix : "",
258 argv_array_push(&cp.args, "ls-files");
259 argv_array_push(&cp.args, "--recurse-submodules");
261 /* add supported options */
262 argv_array_pushv(&cp.args, submodule_options.argv);
265 dir = mkpathdup("%s/%s", get_git_work_tree(), ce->name);
267 status = run_command(&cp);
273 static void show_ce_entry(const struct index_state *istate,
274 const char *tag, const struct cache_entry *ce)
276 struct strbuf name = STRBUF_INIT;
277 int len = max_prefix_len;
279 strbuf_addstr(&name, super_prefix);
280 strbuf_addstr(&name, ce->name);
282 if (len > ce_namelen(ce))
283 die("git ls-files: internal error - cache entry not superset of prefix");
285 if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
286 submodule_path_match(&pathspec, name.buf, ps_matched)) {
288 } else if (match_pathspec(&pathspec, name.buf, name.len,
290 S_ISDIR(ce->ce_mode) ||
291 S_ISGITLINK(ce->ce_mode))) {
292 tag = get_tag(ce, tag);
297 printf("%s%06o %s %d\t",
300 find_unique_abbrev(ce->oid.hash, abbrev),
303 write_eolinfo(istate, ce, ce->name);
304 write_name(ce->name);
308 strbuf_release(&name);
311 static void show_ru_info(const struct index_state *istate)
313 struct string_list_item *item;
315 if (!istate->resolve_undo)
318 for_each_string_list_item(item, istate->resolve_undo) {
319 const char *path = item->string;
320 struct resolve_undo_info *ui = item->util;
324 if (len < max_prefix_len)
325 continue; /* outside of the prefix */
326 if (!match_pathspec(&pathspec, path, len,
327 max_prefix_len, ps_matched, 0))
328 continue; /* uninterested */
329 for (i = 0; i < 3; i++) {
332 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
333 find_unique_abbrev(ui->sha1[i], abbrev),
340 static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
341 const struct cache_entry *ce)
343 int dtype = ce_to_dtype(ce);
344 return is_excluded(dir, istate, ce->name, &dtype);
347 static void show_files(struct index_state *istate, struct dir_struct *dir)
351 /* For cached/deleted files we don't need to even do the readdir */
352 if (show_others || show_killed) {
354 dir->flags |= DIR_COLLECT_KILLED_ONLY;
355 fill_directory(dir, istate, &pathspec);
357 show_other_files(istate, dir);
359 show_killed_files(istate, dir);
361 if (show_cached || show_stage) {
362 for (i = 0; i < istate->cache_nr; i++) {
363 const struct cache_entry *ce = istate->cache[i];
364 if ((dir->flags & DIR_SHOW_IGNORED) &&
365 !ce_excluded(dir, istate, ce))
367 if (show_unmerged && !ce_stage(ce))
369 if (ce->ce_flags & CE_UPDATE)
371 show_ce_entry(istate, ce_stage(ce) ? tag_unmerged :
372 (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce);
375 if (show_deleted || show_modified) {
376 for (i = 0; i < istate->cache_nr; i++) {
377 const struct cache_entry *ce = istate->cache[i];
380 if ((dir->flags & DIR_SHOW_IGNORED) &&
381 !ce_excluded(dir, istate, ce))
383 if (ce->ce_flags & CE_UPDATE)
385 if (ce_skip_worktree(ce))
387 err = lstat(ce->name, &st);
388 if (show_deleted && err)
389 show_ce_entry(istate, tag_removed, ce);
390 if (show_modified && ie_modified(istate, ce, &st, 0))
391 show_ce_entry(istate, tag_modified, ce);
397 * Prune the index to only contain stuff starting with "prefix"
399 static void prune_index(struct index_state *istate,
400 const char *prefix, size_t prefixlen)
403 unsigned int first, last;
407 pos = index_name_pos(istate, prefix, prefixlen);
411 last = istate->cache_nr;
412 while (last > first) {
413 int next = (last + first) >> 1;
414 const struct cache_entry *ce = istate->cache[next];
415 if (!strncmp(ce->name, prefix, prefixlen)) {
421 memmove(istate->cache, istate->cache + pos,
422 (last - pos) * sizeof(struct cache_entry *));
423 istate->cache_nr = last - pos;
426 static int get_common_prefix_len(const char *common_prefix)
428 int common_prefix_len;
433 common_prefix_len = strlen(common_prefix);
436 * If the prefix has a trailing slash, strip it so that submodules wont
437 * be pruned from the index.
439 if (common_prefix[common_prefix_len - 1] == '/')
442 return common_prefix_len;
446 * Read the tree specified with --with-tree option
447 * (typically, HEAD) into stage #1 and then
448 * squash them down to stage #0. This is used for
449 * --error-unmatch to list and check the path patterns
450 * that were given from the command line. We are not
451 * going to write this index out.
453 void overlay_tree_on_index(struct index_state *istate,
454 const char *tree_name, const char *prefix)
457 struct object_id oid;
458 struct pathspec pathspec;
459 struct cache_entry *last_stage0 = NULL;
462 if (get_oid(tree_name, &oid))
463 die("tree-ish %s not found.", tree_name);
464 tree = parse_tree_indirect(&oid);
466 die("bad tree-ish %s", tree_name);
468 /* Hoist the unmerged entries up to stage #3 to make room */
469 for (i = 0; i < istate->cache_nr; i++) {
470 struct cache_entry *ce = istate->cache[i];
473 ce->ce_flags |= CE_STAGEMASK;
477 static const char *(matchbuf[1]);
479 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
480 PATHSPEC_PREFER_CWD, prefix, matchbuf);
482 memset(&pathspec, 0, sizeof(pathspec));
483 if (read_tree(tree, 1, &pathspec, istate))
484 die("unable to read tree entries %s", tree_name);
486 for (i = 0; i < istate->cache_nr; i++) {
487 struct cache_entry *ce = istate->cache[i];
488 switch (ce_stage(ce)) {
496 * If there is stage #0 entry for this, we do not
497 * need to show it. We use CE_UPDATE bit to mark
501 !strcmp(last_stage0->name, ce->name))
502 ce->ce_flags |= CE_UPDATE;
507 static const char * const ls_files_usage[] = {
508 N_("git ls-files [<options>] [<file>...]"),
512 static int option_parse_exclude(const struct option *opt,
513 const char *arg, int unset)
515 struct string_list *exclude_list = opt->value;
518 string_list_append(exclude_list, arg);
523 static int option_parse_exclude_from(const struct option *opt,
524 const char *arg, int unset)
526 struct dir_struct *dir = opt->value;
529 add_excludes_from_file(dir, arg);
534 static int option_parse_exclude_standard(const struct option *opt,
535 const char *arg, int unset)
537 struct dir_struct *dir = opt->value;
540 setup_standard_excludes(dir);
545 int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
547 int require_work_tree = 0, show_tag = 0, i;
548 const char *max_prefix;
549 struct dir_struct dir;
550 struct exclude_list *el;
551 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
552 struct option builtin_ls_files_options[] = {
553 /* Think twice before adding "--nul" synonym to this */
554 OPT_SET_INT('z', NULL, &line_terminator,
555 N_("paths are separated with NUL character"), '\0'),
556 OPT_BOOL('t', NULL, &show_tag,
557 N_("identify the file status with tags")),
558 OPT_BOOL('v', NULL, &show_valid_bit,
559 N_("use lowercase letters for 'assume unchanged' files")),
560 OPT_BOOL('c', "cached", &show_cached,
561 N_("show cached files in the output (default)")),
562 OPT_BOOL('d', "deleted", &show_deleted,
563 N_("show deleted files in the output")),
564 OPT_BOOL('m', "modified", &show_modified,
565 N_("show modified files in the output")),
566 OPT_BOOL('o', "others", &show_others,
567 N_("show other files in the output")),
568 OPT_BIT('i', "ignored", &dir.flags,
569 N_("show ignored files in the output"),
571 OPT_BOOL('s', "stage", &show_stage,
572 N_("show staged contents' object name in the output")),
573 OPT_BOOL('k', "killed", &show_killed,
574 N_("show files on the filesystem that need to be removed")),
575 OPT_BIT(0, "directory", &dir.flags,
576 N_("show 'other' directories' names only"),
577 DIR_SHOW_OTHER_DIRECTORIES),
578 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
579 OPT_NEGBIT(0, "empty-directory", &dir.flags,
580 N_("don't show empty directories"),
581 DIR_HIDE_EMPTY_DIRECTORIES),
582 OPT_BOOL('u', "unmerged", &show_unmerged,
583 N_("show unmerged files in the output")),
584 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
585 N_("show resolve-undo information")),
586 { OPTION_CALLBACK, 'x', "exclude", &exclude_list, N_("pattern"),
587 N_("skip files matching pattern"),
588 0, option_parse_exclude },
589 { OPTION_CALLBACK, 'X', "exclude-from", &dir, N_("file"),
590 N_("exclude patterns are read from <file>"),
591 0, option_parse_exclude_from },
592 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
593 N_("read additional per-directory exclude patterns in <file>")),
594 { OPTION_CALLBACK, 0, "exclude-standard", &dir, NULL,
595 N_("add the standard git exclusions"),
596 PARSE_OPT_NOARG, option_parse_exclude_standard },
597 { OPTION_SET_INT, 0, "full-name", &prefix_len, NULL,
598 N_("make the output relative to the project top directory"),
599 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL },
600 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
601 N_("recurse through submodules")),
602 OPT_BOOL(0, "error-unmatch", &error_unmatch,
603 N_("if any <file> is not in the index, treat this as an error")),
604 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
605 N_("pretend that paths removed since <tree-ish> are still present")),
606 OPT__ABBREV(&abbrev),
607 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
611 if (argc == 2 && !strcmp(argv[1], "-h"))
612 usage_with_options(ls_files_usage, builtin_ls_files_options);
614 memset(&dir, 0, sizeof(dir));
617 prefix_len = strlen(prefix);
618 super_prefix = get_super_prefix();
619 git_config(git_default_config, NULL);
621 if (read_cache() < 0)
622 die("index file corrupt");
624 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
626 el = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
627 for (i = 0; i < exclude_list.nr; i++) {
628 add_exclude(exclude_list.items[i].string, "", 0, el, --exclude_args);
630 if (show_tag || show_valid_bit) {
637 tag_skip_worktree = "S ";
638 tag_resolve_undo = "U ";
640 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
641 require_work_tree = 1;
644 * There's no point in showing unmerged unless
645 * you also show the stage information.
648 if (dir.exclude_per_dir)
651 if (require_work_tree && !is_inside_work_tree())
654 if (recurse_submodules)
655 compile_submodule_options(argv, &dir, show_tag);
657 if (recurse_submodules &&
658 (show_stage || show_deleted || show_others || show_unmerged ||
659 show_killed || show_modified || show_resolve_undo || with_tree))
660 die("ls-files --recurse-submodules unsupported mode");
662 if (recurse_submodules && error_unmatch)
663 die("ls-files --recurse-submodules does not support "
666 parse_pathspec(&pathspec, 0,
671 * Find common prefix for all pathspec's
672 * This is used as a performance optimization which unfortunately cannot
673 * be done when recursing into submodules
675 if (recurse_submodules)
678 max_prefix = common_prefix(&pathspec);
679 max_prefix_len = get_common_prefix_len(max_prefix);
681 prune_index(&the_index, max_prefix, max_prefix_len);
683 /* Treat unmatching pathspec elements as errors */
684 if (pathspec.nr && error_unmatch)
685 ps_matched = xcalloc(pathspec.nr, 1);
687 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
688 die("ls-files --ignored needs some exclude pattern");
690 /* With no flags, we default to showing the cached files */
691 if (!(show_stage || show_deleted || show_others || show_unmerged ||
692 show_killed || show_modified || show_resolve_undo))
697 * Basic sanity check; show-stages and show-unmerged
698 * would not make any sense with this option.
700 if (show_stage || show_unmerged)
701 die("ls-files --with-tree is incompatible with -s or -u");
702 overlay_tree_on_index(&the_index, with_tree, max_prefix);
704 show_files(&the_index, &dir);
705 if (show_resolve_undo)
706 show_ru_info(&the_index);
710 bad = report_path_error(ps_matched, &pathspec, prefix);
712 fprintf(stderr, "Did you forget to 'git add'?\n");