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