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 const char *get_tag(const struct cache_entry *ce, const char *tag)
98 static char alttag[4];
100 if (tag && *tag && show_valid_bit && (ce->ce_flags & CE_VALID)) {
101 memcpy(alttag, tag, 3);
103 if (isalpha(tag[0])) {
104 alttag[0] = tolower(tag[0]);
105 } else if (tag[0] == '?') {
120 static void print_debug(const struct cache_entry *ce)
123 const struct stat_data *sd = &ce->ce_stat_data;
125 printf(" ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
126 printf(" mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
127 printf(" dev: %d\tino: %d\n", sd->sd_dev, sd->sd_ino);
128 printf(" uid: %d\tgid: %d\n", sd->sd_uid, sd->sd_gid);
129 printf(" size: %d\tflags: %x\n", sd->sd_size, ce->ce_flags);
133 static void show_dir_entry(const char *tag, struct dir_entry *ent)
135 int len = max_prefix_len;
138 die("git ls-files: internal error - directory entry not superset of prefix");
140 if (!dir_path_match(ent, &pathspec, len, ps_matched))
144 write_eolinfo(NULL, NULL, ent->name);
145 write_name(ent->name);
148 static void show_other_files(const struct index_state *istate,
149 const struct dir_struct *dir)
153 for (i = 0; i < dir->nr; i++) {
154 struct dir_entry *ent = dir->entries[i];
155 if (!index_name_is_other(istate, ent->name, ent->len))
157 show_dir_entry(tag_other, ent);
161 static void show_killed_files(const struct index_state *istate,
162 const struct dir_struct *dir)
165 for (i = 0; i < dir->nr; i++) {
166 struct dir_entry *ent = dir->entries[i];
168 int pos, len, killed = 0;
170 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
171 sp = strchr(cp, '/');
173 /* If ent->name is prefix of an entry in the
174 * cache, it will be killed.
176 pos = index_name_pos(istate, ent->name, ent->len);
178 die("BUG: killed-file %.*s not found",
179 ent->len, ent->name);
181 while (pos < istate->cache_nr &&
182 ce_stage(istate->cache[pos]))
183 pos++; /* skip unmerged */
184 if (istate->cache_nr <= pos)
186 /* pos points at a name immediately after
187 * ent->name in the cache. Does it expect
188 * ent->name to be a directory?
190 len = ce_namelen(istate->cache[pos]);
191 if ((ent->len < len) &&
192 !strncmp(istate->cache[pos]->name,
193 ent->name, ent->len) &&
194 istate->cache[pos]->name[ent->len] == '/')
198 if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
199 /* If any of the leading directories in
200 * ent->name is registered in the cache,
201 * ent->name will be killed.
208 show_dir_entry(tag_killed, dir->entries[i]);
213 * Compile an argv_array with all of the options supported by --recurse_submodules
215 static void compile_submodule_options(const char **argv,
216 const struct dir_struct *dir,
219 if (line_terminator == '\0')
220 argv_array_push(&submodule_options, "-z");
222 argv_array_push(&submodule_options, "-t");
224 argv_array_push(&submodule_options, "-v");
226 argv_array_push(&submodule_options, "--cached");
228 argv_array_push(&submodule_options, "--eol");
230 argv_array_push(&submodule_options, "--debug");
233 argv_array_push(&submodule_options, "--");
234 for (; *argv; argv++)
235 argv_array_push(&submodule_options, *argv);
239 * Recursively call ls-files on a submodule
241 static void show_gitlink(const struct cache_entry *ce)
243 struct child_process cp = CHILD_PROCESS_INIT;
247 prepare_submodule_repo_env(&cp.env_array);
248 argv_array_push(&cp.env_array, GIT_DIR_ENVIRONMENT);
251 argv_array_pushf(&cp.env_array, "%s=%s",
252 GIT_TOPLEVEL_PREFIX_ENVIRONMENT,
254 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
255 super_prefix ? super_prefix : "",
257 argv_array_push(&cp.args, "ls-files");
258 argv_array_push(&cp.args, "--recurse-submodules");
260 /* add supported options */
261 argv_array_pushv(&cp.args, submodule_options.argv);
264 dir = mkpathdup("%s/%s", get_git_work_tree(), ce->name);
266 status = run_command(&cp);
272 static void show_ce_entry(const struct index_state *istate,
273 const char *tag, const struct cache_entry *ce)
275 struct strbuf name = STRBUF_INIT;
276 int len = max_prefix_len;
278 strbuf_addstr(&name, super_prefix);
279 strbuf_addstr(&name, ce->name);
281 if (len > ce_namelen(ce))
282 die("git ls-files: internal error - cache entry not superset of prefix");
284 if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
285 submodule_path_match(&pathspec, name.buf, ps_matched)) {
287 } else if (match_pathspec(&pathspec, name.buf, name.len,
289 S_ISDIR(ce->ce_mode) ||
290 S_ISGITLINK(ce->ce_mode))) {
291 tag = get_tag(ce, tag);
296 printf("%s%06o %s %d\t",
299 find_unique_abbrev(ce->oid.hash, abbrev),
302 write_eolinfo(istate, ce, ce->name);
303 write_name(ce->name);
307 strbuf_release(&name);
310 static void show_ru_info(const struct index_state *istate)
312 struct string_list_item *item;
314 if (!istate->resolve_undo)
317 for_each_string_list_item(item, istate->resolve_undo) {
318 const char *path = item->string;
319 struct resolve_undo_info *ui = item->util;
323 if (len < max_prefix_len)
324 continue; /* outside of the prefix */
325 if (!match_pathspec(&pathspec, path, len,
326 max_prefix_len, ps_matched, 0))
327 continue; /* uninterested */
328 for (i = 0; i < 3; i++) {
331 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
332 find_unique_abbrev(ui->sha1[i], abbrev),
339 static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
340 const struct cache_entry *ce)
342 int dtype = ce_to_dtype(ce);
343 return is_excluded(dir, istate, ce->name, &dtype);
346 static void show_files(struct index_state *istate, struct dir_struct *dir)
350 /* For cached/deleted files we don't need to even do the readdir */
351 if (show_others || show_killed) {
353 dir->flags |= DIR_COLLECT_KILLED_ONLY;
354 fill_directory(dir, istate, &pathspec);
356 show_other_files(istate, dir);
358 show_killed_files(istate, dir);
360 if (show_cached || show_stage) {
361 for (i = 0; i < istate->cache_nr; i++) {
362 const struct cache_entry *ce = istate->cache[i];
363 if ((dir->flags & DIR_SHOW_IGNORED) &&
364 !ce_excluded(dir, istate, ce))
366 if (show_unmerged && !ce_stage(ce))
368 if (ce->ce_flags & CE_UPDATE)
370 show_ce_entry(istate, ce_stage(ce) ? tag_unmerged :
371 (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce);
374 if (show_deleted || show_modified) {
375 for (i = 0; i < istate->cache_nr; i++) {
376 const struct cache_entry *ce = istate->cache[i];
379 if ((dir->flags & DIR_SHOW_IGNORED) &&
380 !ce_excluded(dir, istate, ce))
382 if (ce->ce_flags & CE_UPDATE)
384 if (ce_skip_worktree(ce))
386 err = lstat(ce->name, &st);
387 if (show_deleted && err)
388 show_ce_entry(istate, tag_removed, ce);
389 if (show_modified && ie_modified(istate, ce, &st, 0))
390 show_ce_entry(istate, tag_modified, ce);
396 * Prune the index to only contain stuff starting with "prefix"
398 static void prune_index(struct index_state *istate,
399 const char *prefix, size_t prefixlen)
402 unsigned int first, last;
406 pos = index_name_pos(istate, prefix, prefixlen);
410 last = istate->cache_nr;
411 while (last > first) {
412 int next = (last + first) >> 1;
413 const struct cache_entry *ce = istate->cache[next];
414 if (!strncmp(ce->name, prefix, prefixlen)) {
420 memmove(istate->cache, istate->cache + pos,
421 (last - pos) * sizeof(struct cache_entry *));
422 istate->cache_nr = last - pos;
425 static int get_common_prefix_len(const char *common_prefix)
427 int common_prefix_len;
432 common_prefix_len = strlen(common_prefix);
435 * If the prefix has a trailing slash, strip it so that submodules wont
436 * be pruned from the index.
438 if (common_prefix[common_prefix_len - 1] == '/')
441 return common_prefix_len;
445 * Read the tree specified with --with-tree option
446 * (typically, HEAD) into stage #1 and then
447 * squash them down to stage #0. This is used for
448 * --error-unmatch to list and check the path patterns
449 * that were given from the command line. We are not
450 * going to write this index out.
452 void overlay_tree_on_index(struct index_state *istate,
453 const char *tree_name, const char *prefix)
456 struct object_id oid;
457 struct pathspec pathspec;
458 struct cache_entry *last_stage0 = NULL;
461 if (get_oid(tree_name, &oid))
462 die("tree-ish %s not found.", tree_name);
463 tree = parse_tree_indirect(&oid);
465 die("bad tree-ish %s", tree_name);
467 /* Hoist the unmerged entries up to stage #3 to make room */
468 for (i = 0; i < istate->cache_nr; i++) {
469 struct cache_entry *ce = istate->cache[i];
472 ce->ce_flags |= CE_STAGEMASK;
476 static const char *(matchbuf[1]);
478 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
479 PATHSPEC_PREFER_CWD, prefix, matchbuf);
481 memset(&pathspec, 0, sizeof(pathspec));
482 if (read_tree(tree, 1, &pathspec, istate))
483 die("unable to read tree entries %s", tree_name);
485 for (i = 0; i < istate->cache_nr; i++) {
486 struct cache_entry *ce = istate->cache[i];
487 switch (ce_stage(ce)) {
495 * If there is stage #0 entry for this, we do not
496 * need to show it. We use CE_UPDATE bit to mark
500 !strcmp(last_stage0->name, ce->name))
501 ce->ce_flags |= CE_UPDATE;
506 static const char * const ls_files_usage[] = {
507 N_("git ls-files [<options>] [<file>...]"),
511 static int option_parse_exclude(const struct option *opt,
512 const char *arg, int unset)
514 struct string_list *exclude_list = opt->value;
517 string_list_append(exclude_list, arg);
522 static int option_parse_exclude_from(const struct option *opt,
523 const char *arg, int unset)
525 struct dir_struct *dir = opt->value;
528 add_excludes_from_file(dir, arg);
533 static int option_parse_exclude_standard(const struct option *opt,
534 const char *arg, int unset)
536 struct dir_struct *dir = opt->value;
539 setup_standard_excludes(dir);
544 int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
546 int require_work_tree = 0, show_tag = 0, i;
547 const char *max_prefix;
548 struct dir_struct dir;
549 struct exclude_list *el;
550 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
551 struct option builtin_ls_files_options[] = {
552 /* Think twice before adding "--nul" synonym to this */
553 OPT_SET_INT('z', NULL, &line_terminator,
554 N_("paths are separated with NUL character"), '\0'),
555 OPT_BOOL('t', NULL, &show_tag,
556 N_("identify the file status with tags")),
557 OPT_BOOL('v', NULL, &show_valid_bit,
558 N_("use lowercase letters for 'assume unchanged' files")),
559 OPT_BOOL('c', "cached", &show_cached,
560 N_("show cached files in the output (default)")),
561 OPT_BOOL('d', "deleted", &show_deleted,
562 N_("show deleted files in the output")),
563 OPT_BOOL('m', "modified", &show_modified,
564 N_("show modified files in the output")),
565 OPT_BOOL('o', "others", &show_others,
566 N_("show other files in the output")),
567 OPT_BIT('i', "ignored", &dir.flags,
568 N_("show ignored files in the output"),
570 OPT_BOOL('s', "stage", &show_stage,
571 N_("show staged contents' object name in the output")),
572 OPT_BOOL('k', "killed", &show_killed,
573 N_("show files on the filesystem that need to be removed")),
574 OPT_BIT(0, "directory", &dir.flags,
575 N_("show 'other' directories' names only"),
576 DIR_SHOW_OTHER_DIRECTORIES),
577 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
578 OPT_NEGBIT(0, "empty-directory", &dir.flags,
579 N_("don't show empty directories"),
580 DIR_HIDE_EMPTY_DIRECTORIES),
581 OPT_BOOL('u', "unmerged", &show_unmerged,
582 N_("show unmerged files in the output")),
583 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
584 N_("show resolve-undo information")),
585 { OPTION_CALLBACK, 'x', "exclude", &exclude_list, N_("pattern"),
586 N_("skip files matching pattern"),
587 0, option_parse_exclude },
588 { OPTION_CALLBACK, 'X', "exclude-from", &dir, N_("file"),
589 N_("exclude patterns are read from <file>"),
590 0, option_parse_exclude_from },
591 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
592 N_("read additional per-directory exclude patterns in <file>")),
593 { OPTION_CALLBACK, 0, "exclude-standard", &dir, NULL,
594 N_("add the standard git exclusions"),
595 PARSE_OPT_NOARG, option_parse_exclude_standard },
596 { OPTION_SET_INT, 0, "full-name", &prefix_len, NULL,
597 N_("make the output relative to the project top directory"),
598 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL },
599 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
600 N_("recurse through submodules")),
601 OPT_BOOL(0, "error-unmatch", &error_unmatch,
602 N_("if any <file> is not in the index, treat this as an error")),
603 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
604 N_("pretend that paths removed since <tree-ish> are still present")),
605 OPT__ABBREV(&abbrev),
606 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
610 if (argc == 2 && !strcmp(argv[1], "-h"))
611 usage_with_options(ls_files_usage, builtin_ls_files_options);
613 memset(&dir, 0, sizeof(dir));
616 prefix_len = strlen(prefix);
617 super_prefix = get_super_prefix();
618 git_config(git_default_config, NULL);
620 if (read_cache() < 0)
621 die("index file corrupt");
623 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
625 el = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
626 for (i = 0; i < exclude_list.nr; i++) {
627 add_exclude(exclude_list.items[i].string, "", 0, el, --exclude_args);
629 if (show_tag || show_valid_bit) {
636 tag_skip_worktree = "S ";
637 tag_resolve_undo = "U ";
639 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
640 require_work_tree = 1;
643 * There's no point in showing unmerged unless
644 * you also show the stage information.
647 if (dir.exclude_per_dir)
650 if (require_work_tree && !is_inside_work_tree())
653 if (recurse_submodules)
654 compile_submodule_options(argv, &dir, show_tag);
656 if (recurse_submodules &&
657 (show_stage || show_deleted || show_others || show_unmerged ||
658 show_killed || show_modified || show_resolve_undo || with_tree))
659 die("ls-files --recurse-submodules unsupported mode");
661 if (recurse_submodules && error_unmatch)
662 die("ls-files --recurse-submodules does not support "
665 parse_pathspec(&pathspec, 0,
670 * Find common prefix for all pathspec's
671 * This is used as a performance optimization which unfortunately cannot
672 * be done when recursing into submodules
674 if (recurse_submodules)
677 max_prefix = common_prefix(&pathspec);
678 max_prefix_len = get_common_prefix_len(max_prefix);
680 prune_index(&the_index, max_prefix, max_prefix_len);
682 /* Treat unmatching pathspec elements as errors */
683 if (pathspec.nr && error_unmatch)
684 ps_matched = xcalloc(pathspec.nr, 1);
686 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
687 die("ls-files --ignored needs some exclude pattern");
689 /* With no flags, we default to showing the cached files */
690 if (!(show_stage || show_deleted || show_others || show_unmerged ||
691 show_killed || show_modified || show_resolve_undo))
696 * Basic sanity check; show-stages and show-unmerged
697 * would not make any sense with this option.
699 if (show_stage || show_unmerged)
700 die("ls-files --with-tree is incompatible with -s or -u");
701 overlay_tree_on_index(&the_index, with_tree, max_prefix);
703 show_files(&the_index, &dir);
704 if (show_resolve_undo)
705 show_ru_info(&the_index);
709 bad = report_path_error(ps_matched, &pathspec, prefix);
711 fprintf(stderr, "Did you forget to 'git add'?\n");