grep: remove redundant REG_NEWLINE when compiling fixed regex
[git.git] / builtin / ls-files.c
blobb12d0bb61240890b3145baa8f051c83005b97dbe
1 /*
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
7 */
8 #include "cache.h"
9 #include "config.h"
10 #include "quote.h"
11 #include "dir.h"
12 #include "builtin.h"
13 #include "tree.h"
14 #include "parse-options.h"
15 #include "resolve-undo.h"
16 #include "string-list.h"
17 #include "pathspec.h"
18 #include "run-command.h"
19 #include "submodule.h"
21 static int abbrev;
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;
33 static int show_eol;
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;
45 static int exc_given;
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)
60 if (show_eol) {
61 struct stat st;
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,
67 ce->name);
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
78 * written.
80 struct strbuf full_name = STRBUF_INIT;
81 if (super_prefix) {
82 strbuf_addstr(&full_name, super_prefix);
83 strbuf_addstr(&full_name, name);
84 name = full_name.buf;
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] == '?') {
107 alttag[0] = '!';
108 } else {
109 alttag[0] = 'v';
110 alttag[1] = tag[0];
111 alttag[2] = ' ';
112 alttag[3] = 0;
115 tag = alttag;
118 return tag;
121 static void print_debug(const struct cache_entry *ce)
123 if (debug_mode) {
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;
138 if (len > ent->len)
139 die("git ls-files: internal error - directory entry not superset of prefix");
141 if (!dir_path_match(ent, &pathspec, len, ps_matched))
142 return;
144 fputs(tag, stdout);
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)
152 int i;
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))
157 continue;
158 show_dir_entry(tag_other, ent);
162 static void show_killed_files(const struct index_state *istate,
163 const struct dir_struct *dir)
165 int i;
166 for (i = 0; i < dir->nr; i++) {
167 struct dir_entry *ent = dir->entries[i];
168 char *cp, *sp;
169 int pos, len, killed = 0;
171 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
172 sp = strchr(cp, '/');
173 if (!sp) {
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);
178 if (0 <= pos)
179 die("BUG: killed-file %.*s not found",
180 ent->len, ent->name);
181 pos = -pos - 1;
182 while (pos < istate->cache_nr &&
183 ce_stage(istate->cache[pos]))
184 pos++; /* skip unmerged */
185 if (istate->cache_nr <= pos)
186 break;
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] == '/')
196 killed = 1;
197 break;
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.
204 killed = 1;
205 break;
208 if (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,
218 int show_tag)
220 if (line_terminator == '\0')
221 argv_array_push(&submodule_options, "-z");
222 if (show_tag)
223 argv_array_push(&submodule_options, "-t");
224 if (show_valid_bit)
225 argv_array_push(&submodule_options, "-v");
226 if (show_cached)
227 argv_array_push(&submodule_options, "--cached");
228 if (show_eol)
229 argv_array_push(&submodule_options, "--eol");
230 if (debug_mode)
231 argv_array_push(&submodule_options, "--debug");
233 /* Add Pathspecs */
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;
245 int status;
246 char *dir;
248 prepare_submodule_repo_env(&cp.env_array);
249 argv_array_push(&cp.env_array, GIT_DIR_ENVIRONMENT);
251 if (prefix_len)
252 argv_array_pushf(&cp.env_array, "%s=%s",
253 GIT_TOPLEVEL_PREFIX_ENVIRONMENT,
254 prefix);
255 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
256 super_prefix ? super_prefix : "",
257 ce->name);
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);
264 cp.git_cmd = 1;
265 dir = mkpathdup("%s/%s", get_git_work_tree(), ce->name);
266 cp.dir = dir;
267 status = run_command(&cp);
268 free(dir);
269 if (status)
270 exit(status);
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;
278 if (super_prefix)
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)) {
287 show_gitlink(ce);
288 } else if (match_pathspec(&pathspec, name.buf, name.len,
289 len, ps_matched,
290 S_ISDIR(ce->ce_mode) ||
291 S_ISGITLINK(ce->ce_mode))) {
292 tag = get_tag(ce, tag);
294 if (!show_stage) {
295 fputs(tag, stdout);
296 } else {
297 printf("%s%06o %s %d\t",
298 tag,
299 ce->ce_mode,
300 find_unique_abbrev(ce->oid.hash, abbrev),
301 ce_stage(ce));
303 write_eolinfo(istate, ce, ce->name);
304 write_name(ce->name);
305 print_debug(ce);
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)
316 return;
318 for_each_string_list_item(item, istate->resolve_undo) {
319 const char *path = item->string;
320 struct resolve_undo_info *ui = item->util;
321 int i, len;
323 len = strlen(path);
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++) {
330 if (!ui->mode[i])
331 continue;
332 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
333 find_unique_abbrev(ui->sha1[i], abbrev),
334 i + 1);
335 write_name(path);
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)
349 int i;
351 /* For cached/deleted files we don't need to even do the readdir */
352 if (show_others || show_killed) {
353 if (!show_others)
354 dir->flags |= DIR_COLLECT_KILLED_ONLY;
355 fill_directory(dir, istate, &pathspec);
356 if (show_others)
357 show_other_files(istate, dir);
358 if (show_killed)
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))
366 continue;
367 if (show_unmerged && !ce_stage(ce))
368 continue;
369 if (ce->ce_flags & CE_UPDATE)
370 continue;
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];
378 struct stat st;
379 int err;
380 if ((dir->flags & DIR_SHOW_IGNORED) &&
381 !ce_excluded(dir, istate, ce))
382 continue;
383 if (ce->ce_flags & CE_UPDATE)
384 continue;
385 if (ce_skip_worktree(ce))
386 continue;
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)
402 int pos;
403 unsigned int first, last;
405 if (!prefix)
406 return;
407 pos = index_name_pos(istate, prefix, prefixlen);
408 if (pos < 0)
409 pos = -pos-1;
410 first = pos;
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)) {
416 first = next+1;
417 continue;
419 last = next;
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;
430 if (!common_prefix)
431 return 0;
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] == '/')
440 common_prefix_len--;
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)
456 struct tree *tree;
457 struct object_id oid;
458 struct pathspec pathspec;
459 struct cache_entry *last_stage0 = NULL;
460 int i;
462 if (get_oid(tree_name, &oid))
463 die("tree-ish %s not found.", tree_name);
464 tree = parse_tree_indirect(&oid);
465 if (!tree)
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];
471 if (!ce_stage(ce))
472 continue;
473 ce->ce_flags |= CE_STAGEMASK;
476 if (prefix) {
477 static const char *(matchbuf[1]);
478 matchbuf[0] = NULL;
479 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
480 PATHSPEC_PREFER_CWD, prefix, matchbuf);
481 } else
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)) {
489 case 0:
490 last_stage0 = ce;
491 /* fallthru */
492 default:
493 continue;
494 case 1:
496 * If there is stage #0 entry for this, we do not
497 * need to show it. We use CE_UPDATE bit to mark
498 * such an entry.
500 if (last_stage0 &&
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>...]"),
509 NULL
512 static int option_parse_exclude(const struct option *opt,
513 const char *arg, int unset)
515 struct string_list *exclude_list = opt->value;
517 exc_given = 1;
518 string_list_append(exclude_list, arg);
520 return 0;
523 static int option_parse_exclude_from(const struct option *opt,
524 const char *arg, int unset)
526 struct dir_struct *dir = opt->value;
528 exc_given = 1;
529 add_excludes_from_file(dir, arg);
531 return 0;
534 static int option_parse_exclude_standard(const struct option *opt,
535 const char *arg, int unset)
537 struct dir_struct *dir = opt->value;
539 exc_given = 1;
540 setup_standard_excludes(dir);
542 return 0;
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"),
570 DIR_SHOW_IGNORED),
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")),
608 OPT_END()
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));
615 prefix = cmd_prefix;
616 if (prefix)
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,
625 ls_files_usage, 0);
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) {
631 tag_cached = "H ";
632 tag_unmerged = "M ";
633 tag_removed = "R ";
634 tag_modified = "C ";
635 tag_other = "? ";
636 tag_killed = "K ";
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;
642 if (show_unmerged)
644 * There's no point in showing unmerged unless
645 * you also show the stage information.
647 show_stage = 1;
648 if (dir.exclude_per_dir)
649 exc_given = 1;
651 if (require_work_tree && !is_inside_work_tree())
652 setup_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 "
664 "--error-unmatch");
666 parse_pathspec(&pathspec, 0,
667 PATHSPEC_PREFER_CWD,
668 prefix, argv);
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)
676 max_prefix = NULL;
677 else
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))
693 show_cached = 1;
695 if (with_tree) {
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);
708 if (ps_matched) {
709 int bad;
710 bad = report_path_error(ps_matched, &pathspec, prefix);
711 if (bad)
712 fprintf(stderr, "Did you forget to 'git add'?\n");
714 return bad ? 1 : 0;
717 return 0;