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"
20 static int show_deleted;
21 static int show_cached;
22 static int show_others;
23 static int show_stage;
24 static int show_unmerged;
25 static int show_resolve_undo;
26 static int show_modified;
27 static int show_killed;
28 static int show_valid_bit;
29 static int line_terminator = '\n';
30 static int debug_mode;
32 static int recurse_submodules;
33 static struct argv_array submodules_options = ARGV_ARRAY_INIT;
35 static const char *prefix;
36 static const char *super_prefix;
37 static int max_prefix_len;
38 static int prefix_len;
39 static struct pathspec pathspec;
40 static int error_unmatch;
41 static char *ps_matched;
42 static const char *with_tree;
44 static int exclude_args;
46 static const char *tag_cached = "";
47 static const char *tag_unmerged = "";
48 static const char *tag_removed = "";
49 static const char *tag_other = "";
50 static const char *tag_killed = "";
51 static const char *tag_modified = "";
52 static const char *tag_skip_worktree = "";
53 static const char *tag_resolve_undo = "";
55 static void write_eolinfo(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(ce->name);
66 if (!lstat(path, &st) && S_ISREG(st.st_mode))
67 w_txt = get_wt_convert_stats_ascii(path);
68 printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
72 static void write_name(const char *name)
75 * Prepend the super_prefix to name to construct the full_name to be
78 struct strbuf full_name = STRBUF_INIT;
80 strbuf_addstr(&full_name, super_prefix);
81 strbuf_addstr(&full_name, name);
86 * With "--full-name", prefix_len=0; this caller needs to pass
87 * an empty string in that case (a NULL is good for "").
89 write_name_quoted_relative(name, prefix_len ? prefix : NULL,
90 stdout, line_terminator);
92 strbuf_release(&full_name);
95 static void show_dir_entry(const char *tag, struct dir_entry *ent)
97 int len = max_prefix_len;
100 die("git ls-files: internal error - directory entry not superset of prefix");
102 if (!dir_path_match(ent, &pathspec, len, ps_matched))
106 write_eolinfo(NULL, ent->name);
107 write_name(ent->name);
110 static void show_other_files(struct dir_struct *dir)
114 for (i = 0; i < dir->nr; i++) {
115 struct dir_entry *ent = dir->entries[i];
116 if (!cache_name_is_other(ent->name, ent->len))
118 show_dir_entry(tag_other, ent);
122 static void show_killed_files(struct dir_struct *dir)
125 for (i = 0; i < dir->nr; i++) {
126 struct dir_entry *ent = dir->entries[i];
128 int pos, len, killed = 0;
130 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
131 sp = strchr(cp, '/');
133 /* If ent->name is prefix of an entry in the
134 * cache, it will be killed.
136 pos = cache_name_pos(ent->name, ent->len);
138 die("BUG: killed-file %.*s not found",
139 ent->len, ent->name);
141 while (pos < active_nr &&
142 ce_stage(active_cache[pos]))
143 pos++; /* skip unmerged */
144 if (active_nr <= pos)
146 /* pos points at a name immediately after
147 * ent->name in the cache. Does it expect
148 * ent->name to be a directory?
150 len = ce_namelen(active_cache[pos]);
151 if ((ent->len < len) &&
152 !strncmp(active_cache[pos]->name,
153 ent->name, ent->len) &&
154 active_cache[pos]->name[ent->len] == '/')
158 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
159 /* If any of the leading directories in
160 * ent->name is registered in the cache,
161 * ent->name will be killed.
168 show_dir_entry(tag_killed, dir->entries[i]);
173 * Compile an argv_array with all of the options supported by --recurse_submodules
175 static void compile_submodule_options(const struct dir_struct *dir, int show_tag)
177 if (line_terminator == '\0')
178 argv_array_push(&submodules_options, "-z");
180 argv_array_push(&submodules_options, "-t");
182 argv_array_push(&submodules_options, "-v");
184 argv_array_push(&submodules_options, "--cached");
186 argv_array_push(&submodules_options, "--eol");
188 argv_array_push(&submodules_options, "--debug");
192 * Recursively call ls-files on a submodule
194 static void show_gitlink(const struct cache_entry *ce)
196 struct child_process cp = CHILD_PROCESS_INIT;
199 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
200 super_prefix ? super_prefix : "",
202 argv_array_push(&cp.args, "ls-files");
203 argv_array_push(&cp.args, "--recurse-submodules");
205 /* add supported options */
206 argv_array_pushv(&cp.args, submodules_options.argv);
210 status = run_command(&cp);
215 static void show_ce_entry(const char *tag, const struct cache_entry *ce)
217 struct strbuf name = STRBUF_INIT;
218 int len = max_prefix_len;
220 strbuf_addstr(&name, super_prefix);
221 strbuf_addstr(&name, ce->name);
223 if (len >= ce_namelen(ce))
224 die("git ls-files: internal error - cache entry not superset of prefix");
226 if (recurse_submodules && S_ISGITLINK(ce->ce_mode)) {
228 } else if (match_pathspec(&pathspec, name.buf, name.len,
230 S_ISDIR(ce->ce_mode) ||
231 S_ISGITLINK(ce->ce_mode))) {
232 if (tag && *tag && show_valid_bit &&
233 (ce->ce_flags & CE_VALID)) {
234 static char alttag[4];
235 memcpy(alttag, tag, 3);
237 alttag[0] = tolower(tag[0]);
238 else if (tag[0] == '?')
252 printf("%s%06o %s %d\t",
255 find_unique_abbrev(ce->sha1,abbrev),
258 write_eolinfo(ce, ce->name);
259 write_name(ce->name);
261 const struct stat_data *sd = &ce->ce_stat_data;
263 printf(" ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
264 printf(" mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
265 printf(" dev: %d\tino: %d\n", sd->sd_dev, sd->sd_ino);
266 printf(" uid: %d\tgid: %d\n", sd->sd_uid, sd->sd_gid);
267 printf(" size: %d\tflags: %x\n", sd->sd_size, ce->ce_flags);
271 strbuf_release(&name);
274 static void show_ru_info(void)
276 struct string_list_item *item;
278 if (!the_index.resolve_undo)
281 for_each_string_list_item(item, the_index.resolve_undo) {
282 const char *path = item->string;
283 struct resolve_undo_info *ui = item->util;
287 if (len < max_prefix_len)
288 continue; /* outside of the prefix */
289 if (!match_pathspec(&pathspec, path, len,
290 max_prefix_len, ps_matched, 0))
291 continue; /* uninterested */
292 for (i = 0; i < 3; i++) {
295 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
296 find_unique_abbrev(ui->sha1[i], abbrev),
303 static int ce_excluded(struct dir_struct *dir, const struct cache_entry *ce)
305 int dtype = ce_to_dtype(ce);
306 return is_excluded(dir, ce->name, &dtype);
309 static void show_files(struct dir_struct *dir)
313 /* For cached/deleted files we don't need to even do the readdir */
314 if (show_others || show_killed) {
316 dir->flags |= DIR_COLLECT_KILLED_ONLY;
317 fill_directory(dir, &pathspec);
319 show_other_files(dir);
321 show_killed_files(dir);
323 if (show_cached || show_stage) {
324 for (i = 0; i < active_nr; i++) {
325 const struct cache_entry *ce = active_cache[i];
326 if ((dir->flags & DIR_SHOW_IGNORED) &&
327 !ce_excluded(dir, ce))
329 if (show_unmerged && !ce_stage(ce))
331 if (ce->ce_flags & CE_UPDATE)
333 show_ce_entry(ce_stage(ce) ? tag_unmerged :
334 (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce);
337 if (show_deleted || show_modified) {
338 for (i = 0; i < active_nr; i++) {
339 const struct cache_entry *ce = active_cache[i];
342 if ((dir->flags & DIR_SHOW_IGNORED) &&
343 !ce_excluded(dir, ce))
345 if (ce->ce_flags & CE_UPDATE)
347 if (ce_skip_worktree(ce))
349 err = lstat(ce->name, &st);
350 if (show_deleted && err)
351 show_ce_entry(tag_removed, ce);
352 if (show_modified && ce_modified(ce, &st, 0))
353 show_ce_entry(tag_modified, ce);
359 * Prune the index to only contain stuff starting with "prefix"
361 static void prune_cache(const char *prefix)
363 int pos = cache_name_pos(prefix, max_prefix_len);
364 unsigned int first, last;
368 memmove(active_cache, active_cache + pos,
369 (active_nr - pos) * sizeof(struct cache_entry *));
373 while (last > first) {
374 int next = (last + first) >> 1;
375 const struct cache_entry *ce = active_cache[next];
376 if (!strncmp(ce->name, prefix, max_prefix_len)) {
386 * Read the tree specified with --with-tree option
387 * (typically, HEAD) into stage #1 and then
388 * squash them down to stage #0. This is used for
389 * --error-unmatch to list and check the path patterns
390 * that were given from the command line. We are not
391 * going to write this index out.
393 void overlay_tree_on_cache(const char *tree_name, const char *prefix)
396 unsigned char sha1[20];
397 struct pathspec pathspec;
398 struct cache_entry *last_stage0 = NULL;
401 if (get_sha1(tree_name, sha1))
402 die("tree-ish %s not found.", tree_name);
403 tree = parse_tree_indirect(sha1);
405 die("bad tree-ish %s", tree_name);
407 /* Hoist the unmerged entries up to stage #3 to make room */
408 for (i = 0; i < active_nr; i++) {
409 struct cache_entry *ce = active_cache[i];
412 ce->ce_flags |= CE_STAGEMASK;
416 static const char *(matchbuf[1]);
418 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
419 PATHSPEC_PREFER_CWD, prefix, matchbuf);
421 memset(&pathspec, 0, sizeof(pathspec));
422 if (read_tree(tree, 1, &pathspec))
423 die("unable to read tree entries %s", tree_name);
425 for (i = 0; i < active_nr; i++) {
426 struct cache_entry *ce = active_cache[i];
427 switch (ce_stage(ce)) {
435 * If there is stage #0 entry for this, we do not
436 * need to show it. We use CE_UPDATE bit to mark
440 !strcmp(last_stage0->name, ce->name))
441 ce->ce_flags |= CE_UPDATE;
446 static const char * const ls_files_usage[] = {
447 N_("git ls-files [<options>] [<file>...]"),
451 static int option_parse_exclude(const struct option *opt,
452 const char *arg, int unset)
454 struct string_list *exclude_list = opt->value;
457 string_list_append(exclude_list, arg);
462 static int option_parse_exclude_from(const struct option *opt,
463 const char *arg, int unset)
465 struct dir_struct *dir = opt->value;
468 add_excludes_from_file(dir, arg);
473 static int option_parse_exclude_standard(const struct option *opt,
474 const char *arg, int unset)
476 struct dir_struct *dir = opt->value;
479 setup_standard_excludes(dir);
484 int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
486 int require_work_tree = 0, show_tag = 0, i;
487 const char *max_prefix;
488 struct dir_struct dir;
489 struct exclude_list *el;
490 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
491 struct option builtin_ls_files_options[] = {
492 /* Think twice before adding "--nul" synonym to this */
493 OPT_SET_INT('z', NULL, &line_terminator,
494 N_("paths are separated with NUL character"), '\0'),
495 OPT_BOOL('t', NULL, &show_tag,
496 N_("identify the file status with tags")),
497 OPT_BOOL('v', NULL, &show_valid_bit,
498 N_("use lowercase letters for 'assume unchanged' files")),
499 OPT_BOOL('c', "cached", &show_cached,
500 N_("show cached files in the output (default)")),
501 OPT_BOOL('d', "deleted", &show_deleted,
502 N_("show deleted files in the output")),
503 OPT_BOOL('m', "modified", &show_modified,
504 N_("show modified files in the output")),
505 OPT_BOOL('o', "others", &show_others,
506 N_("show other files in the output")),
507 OPT_BIT('i', "ignored", &dir.flags,
508 N_("show ignored files in the output"),
510 OPT_BOOL('s', "stage", &show_stage,
511 N_("show staged contents' object name in the output")),
512 OPT_BOOL('k', "killed", &show_killed,
513 N_("show files on the filesystem that need to be removed")),
514 OPT_BIT(0, "directory", &dir.flags,
515 N_("show 'other' directories' names only"),
516 DIR_SHOW_OTHER_DIRECTORIES),
517 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
518 OPT_NEGBIT(0, "empty-directory", &dir.flags,
519 N_("don't show empty directories"),
520 DIR_HIDE_EMPTY_DIRECTORIES),
521 OPT_BOOL('u', "unmerged", &show_unmerged,
522 N_("show unmerged files in the output")),
523 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
524 N_("show resolve-undo information")),
525 { OPTION_CALLBACK, 'x', "exclude", &exclude_list, N_("pattern"),
526 N_("skip files matching pattern"),
527 0, option_parse_exclude },
528 { OPTION_CALLBACK, 'X', "exclude-from", &dir, N_("file"),
529 N_("exclude patterns are read from <file>"),
530 0, option_parse_exclude_from },
531 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
532 N_("read additional per-directory exclude patterns in <file>")),
533 { OPTION_CALLBACK, 0, "exclude-standard", &dir, NULL,
534 N_("add the standard git exclusions"),
535 PARSE_OPT_NOARG, option_parse_exclude_standard },
536 { OPTION_SET_INT, 0, "full-name", &prefix_len, NULL,
537 N_("make the output relative to the project top directory"),
538 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL },
539 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
540 N_("recurse through submodules")),
541 OPT_BOOL(0, "error-unmatch", &error_unmatch,
542 N_("if any <file> is not in the index, treat this as an error")),
543 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
544 N_("pretend that paths removed since <tree-ish> are still present")),
545 OPT__ABBREV(&abbrev),
546 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
550 if (argc == 2 && !strcmp(argv[1], "-h"))
551 usage_with_options(ls_files_usage, builtin_ls_files_options);
553 memset(&dir, 0, sizeof(dir));
556 prefix_len = strlen(prefix);
557 super_prefix = get_super_prefix();
558 git_config(git_default_config, NULL);
560 if (read_cache() < 0)
561 die("index file corrupt");
563 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
565 el = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
566 for (i = 0; i < exclude_list.nr; i++) {
567 add_exclude(exclude_list.items[i].string, "", 0, el, --exclude_args);
569 if (show_tag || show_valid_bit) {
576 tag_skip_worktree = "S ";
577 tag_resolve_undo = "U ";
579 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
580 require_work_tree = 1;
583 * There's no point in showing unmerged unless
584 * you also show the stage information.
587 if (dir.exclude_per_dir)
590 if (require_work_tree && !is_inside_work_tree())
593 if (recurse_submodules)
594 compile_submodule_options(&dir, show_tag);
596 if (recurse_submodules &&
597 (show_stage || show_deleted || show_others || show_unmerged ||
598 show_killed || show_modified || show_resolve_undo || with_tree))
599 die("ls-files --recurse-submodules unsupported mode");
601 if (recurse_submodules && error_unmatch)
602 die("ls-files --recurse-submodules does not support "
605 if (recurse_submodules && argc)
606 die("ls-files --recurse-submodules does not support pathspec");
608 parse_pathspec(&pathspec, 0,
609 PATHSPEC_PREFER_CWD |
610 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
613 /* Find common prefix for all pathspec's */
614 max_prefix = common_prefix(&pathspec);
615 max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
617 /* Treat unmatching pathspec elements as errors */
618 if (pathspec.nr && error_unmatch)
619 ps_matched = xcalloc(pathspec.nr, 1);
621 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
622 die("ls-files --ignored needs some exclude pattern");
624 /* With no flags, we default to showing the cached files */
625 if (!(show_stage || show_deleted || show_others || show_unmerged ||
626 show_killed || show_modified || show_resolve_undo))
630 prune_cache(max_prefix);
633 * Basic sanity check; show-stages and show-unmerged
634 * would not make any sense with this option.
636 if (show_stage || show_unmerged)
637 die("ls-files --with-tree is incompatible with -s or -u");
638 overlay_tree_on_cache(with_tree, max_prefix);
641 if (show_resolve_undo)
646 bad = report_path_error(ps_matched, &pathspec, prefix);
648 fprintf(stderr, "Did you forget to 'git add'?\n");