]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/ls-files.c
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / builtin / ls-files.c
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.
5 *
6 * Copyright (C) Linus Torvalds, 2005
7 */
8 #include "cache.h"
9 #include "repository.h"
10 #include "config.h"
11 #include "convert.h"
12 #include "quote.h"
13 #include "dir.h"
14 #include "builtin.h"
15 #include "gettext.h"
16 #include "object-name.h"
17 #include "strbuf.h"
18 #include "tree.h"
19 #include "cache-tree.h"
20 #include "parse-options.h"
21 #include "resolve-undo.h"
22 #include "string-list.h"
23 #include "pathspec.h"
24 #include "run-command.h"
25 #include "setup.h"
26 #include "submodule.h"
27 #include "submodule-config.h"
28
29 static int abbrev;
30 static int show_deleted;
31 static int show_cached;
32 static int show_others;
33 static int show_stage;
34 static int show_unmerged;
35 static int show_resolve_undo;
36 static int show_modified;
37 static int show_killed;
38 static int show_valid_bit;
39 static int show_fsmonitor_bit;
40 static int line_terminator = '\n';
41 static int debug_mode;
42 static int show_eol;
43 static int recurse_submodules;
44 static int skipping_duplicates;
45 static int show_sparse_dirs;
46
47 static const char *prefix;
48 static int max_prefix_len;
49 static int prefix_len;
50 static struct pathspec pathspec;
51 static int error_unmatch;
52 static char *ps_matched;
53 static const char *with_tree;
54 static int exc_given;
55 static int exclude_args;
56 static const char *format;
57
58 static const char *tag_cached = "";
59 static const char *tag_unmerged = "";
60 static const char *tag_removed = "";
61 static const char *tag_other = "";
62 static const char *tag_killed = "";
63 static const char *tag_modified = "";
64 static const char *tag_skip_worktree = "";
65 static const char *tag_resolve_undo = "";
66
67 static void write_eolinfo(struct index_state *istate,
68 const struct cache_entry *ce, const char *path)
69 {
70 if (show_eol) {
71 struct stat st;
72 const char *i_txt = "";
73 const char *w_txt = "";
74 const char *a_txt = get_convert_attr_ascii(istate, path);
75 if (ce && S_ISREG(ce->ce_mode))
76 i_txt = get_cached_convert_stats_ascii(istate,
77 ce->name);
78 if (!lstat(path, &st) && S_ISREG(st.st_mode))
79 w_txt = get_wt_convert_stats_ascii(path);
80 printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
81 }
82 }
83
84 static void write_name(const char *name)
85 {
86 /*
87 * With "--full-name", prefix_len=0; this caller needs to pass
88 * an empty string in that case (a NULL is good for "").
89 */
90 write_name_quoted_relative(name, prefix_len ? prefix : NULL,
91 stdout, line_terminator);
92 }
93
94 static void write_name_to_buf(struct strbuf *sb, const char *name)
95 {
96 const char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);
97
98 if (line_terminator)
99 quote_c_style(rel, sb, NULL, 0);
100 else
101 strbuf_addstr(sb, rel);
102 }
103
104 static const char *get_tag(const struct cache_entry *ce, const char *tag)
105 {
106 static char alttag[4];
107
108 if (tag && *tag && ((show_valid_bit && (ce->ce_flags & CE_VALID)) ||
109 (show_fsmonitor_bit && (ce->ce_flags & CE_FSMONITOR_VALID)))) {
110 memcpy(alttag, tag, 3);
111
112 if (isalpha(tag[0])) {
113 alttag[0] = tolower(tag[0]);
114 } else if (tag[0] == '?') {
115 alttag[0] = '!';
116 } else {
117 alttag[0] = 'v';
118 alttag[1] = tag[0];
119 alttag[2] = ' ';
120 alttag[3] = 0;
121 }
122
123 tag = alttag;
124 }
125
126 return tag;
127 }
128
129 static void print_debug(const struct cache_entry *ce)
130 {
131 if (debug_mode) {
132 const struct stat_data *sd = &ce->ce_stat_data;
133
134 printf(" ctime: %u:%u\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
135 printf(" mtime: %u:%u\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
136 printf(" dev: %u\tino: %u\n", sd->sd_dev, sd->sd_ino);
137 printf(" uid: %u\tgid: %u\n", sd->sd_uid, sd->sd_gid);
138 printf(" size: %u\tflags: %x\n", sd->sd_size, ce->ce_flags);
139 }
140 }
141
142 static void show_dir_entry(struct index_state *istate,
143 const char *tag, struct dir_entry *ent)
144 {
145 int len = max_prefix_len;
146
147 if (len > ent->len)
148 die("git ls-files: internal error - directory entry not superset of prefix");
149
150 /* If ps_matches is non-NULL, figure out which pathspec(s) match. */
151 if (ps_matched)
152 dir_path_match(istate, ent, &pathspec, len, ps_matched);
153
154 fputs(tag, stdout);
155 write_eolinfo(istate, NULL, ent->name);
156 write_name(ent->name);
157 }
158
159 static void show_other_files(struct index_state *istate,
160 const struct dir_struct *dir)
161 {
162 int i;
163
164 for (i = 0; i < dir->nr; i++) {
165 struct dir_entry *ent = dir->entries[i];
166 if (!index_name_is_other(istate, ent->name, ent->len))
167 continue;
168 show_dir_entry(istate, tag_other, ent);
169 }
170 }
171
172 static void show_killed_files(struct index_state *istate,
173 const struct dir_struct *dir)
174 {
175 int i;
176 for (i = 0; i < dir->nr; i++) {
177 struct dir_entry *ent = dir->entries[i];
178 char *cp, *sp;
179 int pos, len, killed = 0;
180
181 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
182 sp = strchr(cp, '/');
183 if (!sp) {
184 /* If ent->name is prefix of an entry in the
185 * cache, it will be killed.
186 */
187 pos = index_name_pos(istate, ent->name, ent->len);
188 if (0 <= pos)
189 BUG("killed-file %.*s not found",
190 ent->len, ent->name);
191 pos = -pos - 1;
192 while (pos < istate->cache_nr &&
193 ce_stage(istate->cache[pos]))
194 pos++; /* skip unmerged */
195 if (istate->cache_nr <= pos)
196 break;
197 /* pos points at a name immediately after
198 * ent->name in the cache. Does it expect
199 * ent->name to be a directory?
200 */
201 len = ce_namelen(istate->cache[pos]);
202 if ((ent->len < len) &&
203 !strncmp(istate->cache[pos]->name,
204 ent->name, ent->len) &&
205 istate->cache[pos]->name[ent->len] == '/')
206 killed = 1;
207 break;
208 }
209 if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
210 /* If any of the leading directories in
211 * ent->name is registered in the cache,
212 * ent->name will be killed.
213 */
214 killed = 1;
215 break;
216 }
217 }
218 if (killed)
219 show_dir_entry(istate, tag_killed, dir->entries[i]);
220 }
221 }
222
223 static void show_files(struct repository *repo, struct dir_struct *dir);
224
225 static void show_submodule(struct repository *superproject,
226 struct dir_struct *dir, const char *path)
227 {
228 struct repository subrepo;
229
230 if (repo_submodule_init(&subrepo, superproject, path, null_oid()))
231 return;
232
233 if (repo_read_index(&subrepo) < 0)
234 die("index file corrupt");
235
236 show_files(&subrepo, dir);
237
238 repo_clear(&subrepo);
239 }
240
241 struct show_index_data {
242 const char *pathname;
243 struct index_state *istate;
244 const struct cache_entry *ce;
245 };
246
247 static size_t expand_show_index(struct strbuf *sb, const char *start,
248 void *context)
249 {
250 struct show_index_data *data = context;
251 const char *end;
252 const char *p;
253 size_t len = strbuf_expand_literal_cb(sb, start, NULL);
254 struct stat st;
255
256 if (len)
257 return len;
258 if (*start != '(')
259 die(_("bad ls-files format: element '%s' "
260 "does not start with '('"), start);
261
262 end = strchr(start + 1, ')');
263 if (!end)
264 die(_("bad ls-files format: element '%s' "
265 "does not end in ')'"), start);
266
267 len = end - start + 1;
268 if (skip_prefix(start, "(objectmode)", &p))
269 strbuf_addf(sb, "%06o", data->ce->ce_mode);
270 else if (skip_prefix(start, "(objectname)", &p))
271 strbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);
272 else if (skip_prefix(start, "(stage)", &p))
273 strbuf_addf(sb, "%d", ce_stage(data->ce));
274 else if (skip_prefix(start, "(eolinfo:index)", &p))
275 strbuf_addstr(sb, S_ISREG(data->ce->ce_mode) ?
276 get_cached_convert_stats_ascii(data->istate,
277 data->ce->name) : "");
278 else if (skip_prefix(start, "(eolinfo:worktree)", &p))
279 strbuf_addstr(sb, !lstat(data->pathname, &st) &&
280 S_ISREG(st.st_mode) ?
281 get_wt_convert_stats_ascii(data->pathname) : "");
282 else if (skip_prefix(start, "(eolattr)", &p))
283 strbuf_addstr(sb, get_convert_attr_ascii(data->istate,
284 data->pathname));
285 else if (skip_prefix(start, "(path)", &p))
286 write_name_to_buf(sb, data->pathname);
287 else
288 die(_("bad ls-files format: %%%.*s"), (int)len, start);
289
290 return len;
291 }
292
293 static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
294 const char *format, const char *fullname) {
295 struct show_index_data data = {
296 .pathname = fullname,
297 .istate = repo->index,
298 .ce = ce,
299 };
300 struct strbuf sb = STRBUF_INIT;
301
302 strbuf_expand(&sb, format, expand_show_index, &data);
303 strbuf_addch(&sb, line_terminator);
304 fwrite(sb.buf, sb.len, 1, stdout);
305 strbuf_release(&sb);
306 }
307
308 static void show_ce(struct repository *repo, struct dir_struct *dir,
309 const struct cache_entry *ce, const char *fullname,
310 const char *tag)
311 {
312 if (max_prefix_len > strlen(fullname))
313 die("git ls-files: internal error - cache entry not superset of prefix");
314
315 if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
316 is_submodule_active(repo, ce->name)) {
317 show_submodule(repo, dir, ce->name);
318 } else if (match_pathspec(repo->index, &pathspec, fullname, strlen(fullname),
319 max_prefix_len, ps_matched,
320 S_ISDIR(ce->ce_mode) ||
321 S_ISGITLINK(ce->ce_mode))) {
322 if (format) {
323 show_ce_fmt(repo, ce, format, fullname);
324 print_debug(ce);
325 return;
326 }
327
328 tag = get_tag(ce, tag);
329
330 if (!show_stage) {
331 fputs(tag, stdout);
332 } else {
333 printf("%s%06o %s %d\t",
334 tag,
335 ce->ce_mode,
336 repo_find_unique_abbrev(repo, &ce->oid, abbrev),
337 ce_stage(ce));
338 }
339 write_eolinfo(repo->index, ce, fullname);
340 write_name(fullname);
341 print_debug(ce);
342 }
343 }
344
345 static void show_ru_info(struct index_state *istate)
346 {
347 struct string_list_item *item;
348
349 if (!istate->resolve_undo)
350 return;
351
352 for_each_string_list_item(item, istate->resolve_undo) {
353 const char *path = item->string;
354 struct resolve_undo_info *ui = item->util;
355 int i, len;
356
357 len = strlen(path);
358 if (len < max_prefix_len)
359 continue; /* outside of the prefix */
360 if (!match_pathspec(istate, &pathspec, path, len,
361 max_prefix_len, ps_matched, 0))
362 continue; /* uninterested */
363 for (i = 0; i < 3; i++) {
364 if (!ui->mode[i])
365 continue;
366 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
367 repo_find_unique_abbrev(the_repository, &ui->oid[i], abbrev),
368 i + 1);
369 write_name(path);
370 }
371 }
372 }
373
374 static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
375 const char *fullname, const struct cache_entry *ce)
376 {
377 int dtype = ce_to_dtype(ce);
378 return is_excluded(dir, istate, fullname, &dtype);
379 }
380
381 static void construct_fullname(struct strbuf *out, const struct repository *repo,
382 const struct cache_entry *ce)
383 {
384 strbuf_reset(out);
385 if (repo->submodule_prefix)
386 strbuf_addstr(out, repo->submodule_prefix);
387 strbuf_addstr(out, ce->name);
388 }
389
390 static void show_files(struct repository *repo, struct dir_struct *dir)
391 {
392 int i;
393 struct strbuf fullname = STRBUF_INIT;
394
395 /* For cached/deleted files we don't need to even do the readdir */
396 if (show_others || show_killed) {
397 if (!show_others)
398 dir->flags |= DIR_COLLECT_KILLED_ONLY;
399 fill_directory(dir, repo->index, &pathspec);
400 if (show_others)
401 show_other_files(repo->index, dir);
402 if (show_killed)
403 show_killed_files(repo->index, dir);
404 }
405
406 if (!(show_cached || show_stage || show_deleted || show_modified))
407 return;
408
409 if (!show_sparse_dirs)
410 ensure_full_index(repo->index);
411
412 for (i = 0; i < repo->index->cache_nr; i++) {
413 const struct cache_entry *ce = repo->index->cache[i];
414 struct stat st;
415 int stat_err;
416
417 construct_fullname(&fullname, repo, ce);
418
419 if ((dir->flags & DIR_SHOW_IGNORED) &&
420 !ce_excluded(dir, repo->index, fullname.buf, ce))
421 continue;
422 if (ce->ce_flags & CE_UPDATE)
423 continue;
424 if ((show_cached || show_stage) &&
425 (!show_unmerged || ce_stage(ce))) {
426 show_ce(repo, dir, ce, fullname.buf,
427 ce_stage(ce) ? tag_unmerged :
428 (ce_skip_worktree(ce) ? tag_skip_worktree :
429 tag_cached));
430 if (skipping_duplicates)
431 goto skip_to_next_name;
432 }
433
434 if (!(show_deleted || show_modified))
435 continue;
436 if (ce_skip_worktree(ce))
437 continue;
438 stat_err = lstat(fullname.buf, &st);
439 if (stat_err && (errno != ENOENT && errno != ENOTDIR))
440 error_errno("cannot lstat '%s'", fullname.buf);
441 if (stat_err && show_deleted) {
442 show_ce(repo, dir, ce, fullname.buf, tag_removed);
443 if (skipping_duplicates)
444 goto skip_to_next_name;
445 }
446 if (show_modified &&
447 (stat_err || ie_modified(repo->index, ce, &st, 0))) {
448 show_ce(repo, dir, ce, fullname.buf, tag_modified);
449 if (skipping_duplicates)
450 goto skip_to_next_name;
451 }
452 continue;
453
454 skip_to_next_name:
455 {
456 int j;
457 struct cache_entry **cache = repo->index->cache;
458 for (j = i + 1; j < repo->index->cache_nr; j++)
459 if (strcmp(ce->name, cache[j]->name))
460 break;
461 i = j - 1; /* compensate for the for loop */
462 }
463 }
464
465 strbuf_release(&fullname);
466 }
467
468 /*
469 * Prune the index to only contain stuff starting with "prefix"
470 */
471 static void prune_index(struct index_state *istate,
472 const char *prefix, size_t prefixlen)
473 {
474 int pos;
475 unsigned int first, last;
476
477 if (!prefix || !istate->cache_nr)
478 return;
479 pos = index_name_pos(istate, prefix, prefixlen);
480 if (pos < 0)
481 pos = -pos-1;
482 first = pos;
483 last = istate->cache_nr;
484 while (last > first) {
485 int next = first + ((last - first) >> 1);
486 const struct cache_entry *ce = istate->cache[next];
487 if (!strncmp(ce->name, prefix, prefixlen)) {
488 first = next+1;
489 continue;
490 }
491 last = next;
492 }
493 MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos);
494 istate->cache_nr = last - pos;
495 }
496
497 static int get_common_prefix_len(const char *common_prefix)
498 {
499 int common_prefix_len;
500
501 if (!common_prefix)
502 return 0;
503
504 common_prefix_len = strlen(common_prefix);
505
506 /*
507 * If the prefix has a trailing slash, strip it so that submodules wont
508 * be pruned from the index.
509 */
510 if (common_prefix[common_prefix_len - 1] == '/')
511 common_prefix_len--;
512
513 return common_prefix_len;
514 }
515
516 static int read_one_entry_opt(struct index_state *istate,
517 const struct object_id *oid,
518 struct strbuf *base,
519 const char *pathname,
520 unsigned mode, int opt)
521 {
522 int len;
523 struct cache_entry *ce;
524
525 if (S_ISDIR(mode))
526 return READ_TREE_RECURSIVE;
527
528 len = strlen(pathname);
529 ce = make_empty_cache_entry(istate, base->len + len);
530
531 ce->ce_mode = create_ce_mode(mode);
532 ce->ce_flags = create_ce_flags(1);
533 ce->ce_namelen = base->len + len;
534 memcpy(ce->name, base->buf, base->len);
535 memcpy(ce->name + base->len, pathname, len+1);
536 oidcpy(&ce->oid, oid);
537 return add_index_entry(istate, ce, opt);
538 }
539
540 static int read_one_entry(const struct object_id *oid, struct strbuf *base,
541 const char *pathname, unsigned mode,
542 void *context)
543 {
544 struct index_state *istate = context;
545 return read_one_entry_opt(istate, oid, base, pathname,
546 mode,
547 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
548 }
549
550 /*
551 * This is used when the caller knows there is no existing entries at
552 * the stage that will conflict with the entry being added.
553 */
554 static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
555 const char *pathname, unsigned mode,
556 void *context)
557 {
558 struct index_state *istate = context;
559 return read_one_entry_opt(istate, oid, base, pathname,
560 mode, ADD_CACHE_JUST_APPEND);
561 }
562
563 /*
564 * Read the tree specified with --with-tree option
565 * (typically, HEAD) into stage #1 and then
566 * squash them down to stage #0. This is used for
567 * --error-unmatch to list and check the path patterns
568 * that were given from the command line. We are not
569 * going to write this index out.
570 */
571 void overlay_tree_on_index(struct index_state *istate,
572 const char *tree_name, const char *prefix)
573 {
574 struct tree *tree;
575 struct object_id oid;
576 struct pathspec pathspec;
577 struct cache_entry *last_stage0 = NULL;
578 int i;
579 read_tree_fn_t fn = NULL;
580 int err;
581
582 if (repo_get_oid(the_repository, tree_name, &oid))
583 die("tree-ish %s not found.", tree_name);
584 tree = parse_tree_indirect(&oid);
585 if (!tree)
586 die("bad tree-ish %s", tree_name);
587
588 /* Hoist the unmerged entries up to stage #3 to make room */
589 /* TODO: audit for interaction with sparse-index. */
590 ensure_full_index(istate);
591 for (i = 0; i < istate->cache_nr; i++) {
592 struct cache_entry *ce = istate->cache[i];
593 if (!ce_stage(ce))
594 continue;
595 ce->ce_flags |= CE_STAGEMASK;
596 }
597
598 if (prefix) {
599 static const char *(matchbuf[1]);
600 matchbuf[0] = NULL;
601 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
602 PATHSPEC_PREFER_CWD, prefix, matchbuf);
603 } else
604 memset(&pathspec, 0, sizeof(pathspec));
605
606 /*
607 * See if we have cache entry at the stage. If so,
608 * do it the original slow way, otherwise, append and then
609 * sort at the end.
610 */
611 for (i = 0; !fn && i < istate->cache_nr; i++) {
612 const struct cache_entry *ce = istate->cache[i];
613 if (ce_stage(ce) == 1)
614 fn = read_one_entry;
615 }
616
617 if (!fn)
618 fn = read_one_entry_quick;
619 err = read_tree(the_repository, tree, &pathspec, fn, istate);
620 clear_pathspec(&pathspec);
621 if (err)
622 die("unable to read tree entries %s", tree_name);
623
624 /*
625 * Sort the cache entry -- we need to nuke the cache tree, though.
626 */
627 if (fn == read_one_entry_quick) {
628 cache_tree_free(&istate->cache_tree);
629 QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
630 }
631
632 for (i = 0; i < istate->cache_nr; i++) {
633 struct cache_entry *ce = istate->cache[i];
634 switch (ce_stage(ce)) {
635 case 0:
636 last_stage0 = ce;
637 /* fallthru */
638 default:
639 continue;
640 case 1:
641 /*
642 * If there is stage #0 entry for this, we do not
643 * need to show it. We use CE_UPDATE bit to mark
644 * such an entry.
645 */
646 if (last_stage0 &&
647 !strcmp(last_stage0->name, ce->name))
648 ce->ce_flags |= CE_UPDATE;
649 }
650 }
651 }
652
653 static const char * const ls_files_usage[] = {
654 N_("git ls-files [<options>] [<file>...]"),
655 NULL
656 };
657
658 static int option_parse_exclude(const struct option *opt,
659 const char *arg, int unset)
660 {
661 struct string_list *exclude_list = opt->value;
662
663 BUG_ON_OPT_NEG(unset);
664
665 exc_given = 1;
666 string_list_append(exclude_list, arg);
667
668 return 0;
669 }
670
671 static int option_parse_exclude_from(const struct option *opt,
672 const char *arg, int unset)
673 {
674 struct dir_struct *dir = opt->value;
675
676 BUG_ON_OPT_NEG(unset);
677
678 exc_given = 1;
679 add_patterns_from_file(dir, arg);
680
681 return 0;
682 }
683
684 static int option_parse_exclude_standard(const struct option *opt,
685 const char *arg, int unset)
686 {
687 struct dir_struct *dir = opt->value;
688
689 BUG_ON_OPT_NEG(unset);
690 BUG_ON_OPT_ARG(arg);
691
692 exc_given = 1;
693 setup_standard_excludes(dir);
694
695 return 0;
696 }
697
698 int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
699 {
700 int require_work_tree = 0, show_tag = 0, i;
701 char *max_prefix;
702 struct dir_struct dir = DIR_INIT;
703 struct pattern_list *pl;
704 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
705 struct option builtin_ls_files_options[] = {
706 /* Think twice before adding "--nul" synonym to this */
707 OPT_SET_INT('z', NULL, &line_terminator,
708 N_("separate paths with the NUL character"), '\0'),
709 OPT_BOOL('t', NULL, &show_tag,
710 N_("identify the file status with tags")),
711 OPT_BOOL('v', NULL, &show_valid_bit,
712 N_("use lowercase letters for 'assume unchanged' files")),
713 OPT_BOOL('f', NULL, &show_fsmonitor_bit,
714 N_("use lowercase letters for 'fsmonitor clean' files")),
715 OPT_BOOL('c', "cached", &show_cached,
716 N_("show cached files in the output (default)")),
717 OPT_BOOL('d', "deleted", &show_deleted,
718 N_("show deleted files in the output")),
719 OPT_BOOL('m', "modified", &show_modified,
720 N_("show modified files in the output")),
721 OPT_BOOL('o', "others", &show_others,
722 N_("show other files in the output")),
723 OPT_BIT('i', "ignored", &dir.flags,
724 N_("show ignored files in the output"),
725 DIR_SHOW_IGNORED),
726 OPT_BOOL('s', "stage", &show_stage,
727 N_("show staged contents' object name in the output")),
728 OPT_BOOL('k', "killed", &show_killed,
729 N_("show files on the filesystem that need to be removed")),
730 OPT_BIT(0, "directory", &dir.flags,
731 N_("show 'other' directories' names only"),
732 DIR_SHOW_OTHER_DIRECTORIES),
733 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
734 OPT_NEGBIT(0, "empty-directory", &dir.flags,
735 N_("don't show empty directories"),
736 DIR_HIDE_EMPTY_DIRECTORIES),
737 OPT_BOOL('u', "unmerged", &show_unmerged,
738 N_("show unmerged files in the output")),
739 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
740 N_("show resolve-undo information")),
741 OPT_CALLBACK_F('x', "exclude", &exclude_list, N_("pattern"),
742 N_("skip files matching pattern"),
743 PARSE_OPT_NONEG, option_parse_exclude),
744 OPT_CALLBACK_F('X', "exclude-from", &dir, N_("file"),
745 N_("read exclude patterns from <file>"),
746 PARSE_OPT_NONEG, option_parse_exclude_from),
747 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
748 N_("read additional per-directory exclude patterns in <file>")),
749 OPT_CALLBACK_F(0, "exclude-standard", &dir, NULL,
750 N_("add the standard git exclusions"),
751 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
752 option_parse_exclude_standard),
753 OPT_SET_INT_F(0, "full-name", &prefix_len,
754 N_("make the output relative to the project top directory"),
755 0, PARSE_OPT_NONEG),
756 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
757 N_("recurse through submodules")),
758 OPT_BOOL(0, "error-unmatch", &error_unmatch,
759 N_("if any <file> is not in the index, treat this as an error")),
760 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
761 N_("pretend that paths removed since <tree-ish> are still present")),
762 OPT__ABBREV(&abbrev),
763 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
764 OPT_BOOL(0, "deduplicate", &skipping_duplicates,
765 N_("suppress duplicate entries")),
766 OPT_BOOL(0, "sparse", &show_sparse_dirs,
767 N_("show sparse directories in the presence of a sparse index")),
768 OPT_STRING_F(0, "format", &format, N_("format"),
769 N_("format to use for the output"),
770 PARSE_OPT_NONEG),
771 OPT_END()
772 };
773 int ret = 0;
774
775 if (argc == 2 && !strcmp(argv[1], "-h"))
776 usage_with_options(ls_files_usage, builtin_ls_files_options);
777
778 prepare_repo_settings(the_repository);
779 the_repository->settings.command_requires_full_index = 0;
780
781 prefix = cmd_prefix;
782 if (prefix)
783 prefix_len = strlen(prefix);
784 git_config(git_default_config, NULL);
785
786 if (repo_read_index(the_repository) < 0)
787 die("index file corrupt");
788
789 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
790 ls_files_usage, 0);
791 pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
792 for (i = 0; i < exclude_list.nr; i++) {
793 add_pattern(exclude_list.items[i].string, "", 0, pl, --exclude_args);
794 }
795
796 if (format && (show_stage || show_others || show_killed ||
797 show_resolve_undo || skipping_duplicates || show_eol || show_tag))
798 usage_msg_opt(_("--format cannot be used with -s, -o, -k, -t, "
799 "--resolve-undo, --deduplicate, --eol"),
800 ls_files_usage, builtin_ls_files_options);
801
802 if (show_tag || show_valid_bit || show_fsmonitor_bit) {
803 tag_cached = "H ";
804 tag_unmerged = "M ";
805 tag_removed = "R ";
806 tag_modified = "C ";
807 tag_other = "? ";
808 tag_killed = "K ";
809 tag_skip_worktree = "S ";
810 tag_resolve_undo = "U ";
811 }
812 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
813 require_work_tree = 1;
814 if (show_unmerged)
815 /*
816 * There's no point in showing unmerged unless
817 * you also show the stage information.
818 */
819 show_stage = 1;
820 if (show_tag || show_stage)
821 skipping_duplicates = 0;
822 if (dir.exclude_per_dir)
823 exc_given = 1;
824
825 if (require_work_tree && !is_inside_work_tree())
826 setup_work_tree();
827
828 if (recurse_submodules &&
829 (show_deleted || show_others || show_unmerged ||
830 show_killed || show_modified || show_resolve_undo || with_tree))
831 die("ls-files --recurse-submodules unsupported mode");
832
833 if (recurse_submodules && error_unmatch)
834 die("ls-files --recurse-submodules does not support "
835 "--error-unmatch");
836
837 parse_pathspec(&pathspec, 0,
838 PATHSPEC_PREFER_CWD,
839 prefix, argv);
840
841 /*
842 * Find common prefix for all pathspec's
843 * This is used as a performance optimization which unfortunately cannot
844 * be done when recursing into submodules because when a pathspec is
845 * given which spans repository boundaries you can't simply remove the
846 * submodule entry because the pathspec may match something inside the
847 * submodule.
848 */
849 if (recurse_submodules)
850 max_prefix = NULL;
851 else
852 max_prefix = common_prefix(&pathspec);
853 max_prefix_len = get_common_prefix_len(max_prefix);
854
855 prune_index(the_repository->index, max_prefix, max_prefix_len);
856
857 /* Treat unmatching pathspec elements as errors */
858 if (pathspec.nr && error_unmatch)
859 ps_matched = xcalloc(pathspec.nr, 1);
860
861 if ((dir.flags & DIR_SHOW_IGNORED) && !show_others && !show_cached)
862 die("ls-files -i must be used with either -o or -c");
863
864 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
865 die("ls-files --ignored needs some exclude pattern");
866
867 /* With no flags, we default to showing the cached files */
868 if (!(show_stage || show_deleted || show_others || show_unmerged ||
869 show_killed || show_modified || show_resolve_undo))
870 show_cached = 1;
871
872 if (with_tree) {
873 /*
874 * Basic sanity check; show-stages and show-unmerged
875 * would not make any sense with this option.
876 */
877 if (show_stage || show_unmerged)
878 die(_("options '%s' and '%s' cannot be used together"), "ls-files --with-tree", "-s/-u");
879 overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
880 }
881
882 show_files(the_repository, &dir);
883
884 if (show_resolve_undo)
885 show_ru_info(the_repository->index);
886
887 if (ps_matched && report_path_error(ps_matched, &pathspec)) {
888 fprintf(stderr, "Did you forget to 'git add'?\n");
889 ret = 1;
890 }
891
892 string_list_clear(&exclude_list, 0);
893 dir_clear(&dir);
894 free(max_prefix);
895 return ret;
896 }