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
9 #include "repository.h"
16 #include "object-name.h"
19 #include "cache-tree.h"
20 #include "parse-options.h"
21 #include "resolve-undo.h"
22 #include "string-list.h"
24 #include "run-command.h"
26 #include "submodule.h"
27 #include "submodule-config.h"
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
;
43 static int recurse_submodules
;
44 static int skipping_duplicates
;
45 static int show_sparse_dirs
;
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
;
55 static int exclude_args
;
56 static const char *format
;
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
= "";
67 static void write_eolinfo(struct index_state
*istate
,
68 const struct cache_entry
*ce
, const char *path
)
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
,
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
);
84 static void write_name(const char *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
);
94 static void write_name_to_buf(struct strbuf
*sb
, const char *name
)
96 const char *rel
= relative_path(name
, prefix_len
? prefix
: NULL
, sb
);
99 quote_c_style(rel
, sb
, NULL
, 0);
101 strbuf_addstr(sb
, rel
);
104 static const char *get_tag(const struct cache_entry
*ce
, const char *tag
)
106 static char alttag
[4];
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);
112 if (isalpha(tag
[0])) {
113 alttag
[0] = tolower(tag
[0]);
114 } else if (tag
[0] == '?') {
129 static void print_debug(const struct cache_entry
*ce
)
132 const struct stat_data
*sd
= &ce
->ce_stat_data
;
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
);
142 static void show_dir_entry(struct index_state
*istate
,
143 const char *tag
, struct dir_entry
*ent
)
145 int len
= max_prefix_len
;
148 die("git ls-files: internal error - directory entry not superset of prefix");
150 /* If ps_matches is non-NULL, figure out which pathspec(s) match. */
152 dir_path_match(istate
, ent
, &pathspec
, len
, ps_matched
);
155 write_eolinfo(istate
, NULL
, ent
->name
);
156 write_name(ent
->name
);
159 static void show_other_files(struct index_state
*istate
,
160 const struct dir_struct
*dir
)
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
))
168 show_dir_entry(istate
, tag_other
, ent
);
172 static void show_killed_files(struct index_state
*istate
,
173 const struct dir_struct
*dir
)
176 for (i
= 0; i
< dir
->nr
; i
++) {
177 struct dir_entry
*ent
= dir
->entries
[i
];
179 int pos
, len
, killed
= 0;
181 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
182 sp
= strchr(cp
, '/');
184 /* If ent->name is prefix of an entry in the
185 * cache, it will be killed.
187 pos
= index_name_pos(istate
, ent
->name
, ent
->len
);
189 BUG("killed-file %.*s not found",
190 ent
->len
, ent
->name
);
192 while (pos
< istate
->cache_nr
&&
193 ce_stage(istate
->cache
[pos
]))
194 pos
++; /* skip unmerged */
195 if (istate
->cache_nr
<= pos
)
197 /* pos points at a name immediately after
198 * ent->name in the cache. Does it expect
199 * ent->name to be a directory?
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
] == '/')
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.
219 show_dir_entry(istate
, tag_killed
, dir
->entries
[i
]);
223 static void show_files(struct repository
*repo
, struct dir_struct
*dir
);
225 static void show_submodule(struct repository
*superproject
,
226 struct dir_struct
*dir
, const char *path
)
228 struct repository subrepo
;
230 if (repo_submodule_init(&subrepo
, superproject
, path
, null_oid()))
233 if (repo_read_index(&subrepo
) < 0)
234 die("index file corrupt");
236 show_files(&subrepo
, dir
);
238 repo_clear(&subrepo
);
241 struct show_index_data
{
242 const char *pathname
;
243 struct index_state
*istate
;
244 const struct cache_entry
*ce
;
247 static size_t expand_show_index(struct strbuf
*sb
, const char *start
,
250 struct show_index_data
*data
= context
;
253 size_t len
= strbuf_expand_literal_cb(sb
, start
, NULL
);
259 die(_("bad ls-files format: element '%s' "
260 "does not start with '('"), start
);
262 end
= strchr(start
+ 1, ')');
264 die(_("bad ls-files format: element '%s' "
265 "does not end in ')'"), start
);
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
,
285 else if (skip_prefix(start
, "(path)", &p
))
286 write_name_to_buf(sb
, data
->pathname
);
288 die(_("bad ls-files format: %%%.*s"), (int)len
, start
);
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
,
300 struct strbuf sb
= STRBUF_INIT
;
302 strbuf_expand(&sb
, format
, expand_show_index
, &data
);
303 strbuf_addch(&sb
, line_terminator
);
304 fwrite(sb
.buf
, sb
.len
, 1, stdout
);
308 static void show_ce(struct repository
*repo
, struct dir_struct
*dir
,
309 const struct cache_entry
*ce
, const char *fullname
,
312 if (max_prefix_len
> strlen(fullname
))
313 die("git ls-files: internal error - cache entry not superset of prefix");
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
))) {
323 show_ce_fmt(repo
, ce
, format
, fullname
);
328 tag
= get_tag(ce
, tag
);
333 printf("%s%06o %s %d\t",
336 repo_find_unique_abbrev(repo
, &ce
->oid
, abbrev
),
339 write_eolinfo(repo
->index
, ce
, fullname
);
340 write_name(fullname
);
345 static void show_ru_info(struct index_state
*istate
)
347 struct string_list_item
*item
;
349 if (!istate
->resolve_undo
)
352 for_each_string_list_item(item
, istate
->resolve_undo
) {
353 const char *path
= item
->string
;
354 struct resolve_undo_info
*ui
= item
->util
;
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
++) {
366 printf("%s%06o %s %d\t", tag_resolve_undo
, ui
->mode
[i
],
367 repo_find_unique_abbrev(the_repository
, &ui
->oid
[i
], abbrev
),
374 static int ce_excluded(struct dir_struct
*dir
, struct index_state
*istate
,
375 const char *fullname
, const struct cache_entry
*ce
)
377 int dtype
= ce_to_dtype(ce
);
378 return is_excluded(dir
, istate
, fullname
, &dtype
);
381 static void construct_fullname(struct strbuf
*out
, const struct repository
*repo
,
382 const struct cache_entry
*ce
)
385 if (repo
->submodule_prefix
)
386 strbuf_addstr(out
, repo
->submodule_prefix
);
387 strbuf_addstr(out
, ce
->name
);
390 static void show_files(struct repository
*repo
, struct dir_struct
*dir
)
393 struct strbuf fullname
= STRBUF_INIT
;
395 /* For cached/deleted files we don't need to even do the readdir */
396 if (show_others
|| show_killed
) {
398 dir
->flags
|= DIR_COLLECT_KILLED_ONLY
;
399 fill_directory(dir
, repo
->index
, &pathspec
);
401 show_other_files(repo
->index
, dir
);
403 show_killed_files(repo
->index
, dir
);
406 if (!(show_cached
|| show_stage
|| show_deleted
|| show_modified
))
409 if (!show_sparse_dirs
)
410 ensure_full_index(repo
->index
);
412 for (i
= 0; i
< repo
->index
->cache_nr
; i
++) {
413 const struct cache_entry
*ce
= repo
->index
->cache
[i
];
417 construct_fullname(&fullname
, repo
, ce
);
419 if ((dir
->flags
& DIR_SHOW_IGNORED
) &&
420 !ce_excluded(dir
, repo
->index
, fullname
.buf
, ce
))
422 if (ce
->ce_flags
& CE_UPDATE
)
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
:
430 if (skipping_duplicates
)
431 goto skip_to_next_name
;
434 if (!(show_deleted
|| show_modified
))
436 if (ce_skip_worktree(ce
))
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
;
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
;
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
))
461 i
= j
- 1; /* compensate for the for loop */
465 strbuf_release(&fullname
);
469 * Prune the index to only contain stuff starting with "prefix"
471 static void prune_index(struct index_state
*istate
,
472 const char *prefix
, size_t prefixlen
)
475 unsigned int first
, last
;
477 if (!prefix
|| !istate
->cache_nr
)
479 pos
= index_name_pos(istate
, prefix
, prefixlen
);
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
)) {
493 MOVE_ARRAY(istate
->cache
, istate
->cache
+ pos
, last
- pos
);
494 istate
->cache_nr
= last
- pos
;
497 static int get_common_prefix_len(const char *common_prefix
)
499 int common_prefix_len
;
504 common_prefix_len
= strlen(common_prefix
);
507 * If the prefix has a trailing slash, strip it so that submodules wont
508 * be pruned from the index.
510 if (common_prefix
[common_prefix_len
- 1] == '/')
513 return common_prefix_len
;
516 static int read_one_entry_opt(struct index_state
*istate
,
517 const struct object_id
*oid
,
519 const char *pathname
,
520 unsigned mode
, int opt
)
523 struct cache_entry
*ce
;
526 return READ_TREE_RECURSIVE
;
528 len
= strlen(pathname
);
529 ce
= make_empty_cache_entry(istate
, base
->len
+ len
);
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
);
540 static int read_one_entry(const struct object_id
*oid
, struct strbuf
*base
,
541 const char *pathname
, unsigned mode
,
544 struct index_state
*istate
= context
;
545 return read_one_entry_opt(istate
, oid
, base
, pathname
,
547 ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
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.
554 static int read_one_entry_quick(const struct object_id
*oid
, struct strbuf
*base
,
555 const char *pathname
, unsigned mode
,
558 struct index_state
*istate
= context
;
559 return read_one_entry_opt(istate
, oid
, base
, pathname
,
560 mode
, ADD_CACHE_JUST_APPEND
);
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.
571 void overlay_tree_on_index(struct index_state
*istate
,
572 const char *tree_name
, const char *prefix
)
575 struct object_id oid
;
576 struct pathspec pathspec
;
577 struct cache_entry
*last_stage0
= NULL
;
579 read_tree_fn_t fn
= NULL
;
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
);
586 die("bad tree-ish %s", tree_name
);
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
];
595 ce
->ce_flags
|= CE_STAGEMASK
;
599 static const char *(matchbuf
[1]);
601 parse_pathspec(&pathspec
, PATHSPEC_ALL_MAGIC
,
602 PATHSPEC_PREFER_CWD
, prefix
, matchbuf
);
604 memset(&pathspec
, 0, sizeof(pathspec
));
607 * See if we have cache entry at the stage. If so,
608 * do it the original slow way, otherwise, append and then
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)
618 fn
= read_one_entry_quick
;
619 err
= read_tree(the_repository
, tree
, &pathspec
, fn
, istate
);
620 clear_pathspec(&pathspec
);
622 die("unable to read tree entries %s", tree_name
);
625 * Sort the cache entry -- we need to nuke the cache tree, though.
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
);
632 for (i
= 0; i
< istate
->cache_nr
; i
++) {
633 struct cache_entry
*ce
= istate
->cache
[i
];
634 switch (ce_stage(ce
)) {
642 * If there is stage #0 entry for this, we do not
643 * need to show it. We use CE_UPDATE bit to mark
647 !strcmp(last_stage0
->name
, ce
->name
))
648 ce
->ce_flags
|= CE_UPDATE
;
653 static const char * const ls_files_usage
[] = {
654 N_("git ls-files [<options>] [<file>...]"),
658 static int option_parse_exclude(const struct option
*opt
,
659 const char *arg
, int unset
)
661 struct string_list
*exclude_list
= opt
->value
;
663 BUG_ON_OPT_NEG(unset
);
666 string_list_append(exclude_list
, arg
);
671 static int option_parse_exclude_from(const struct option
*opt
,
672 const char *arg
, int unset
)
674 struct dir_struct
*dir
= opt
->value
;
676 BUG_ON_OPT_NEG(unset
);
679 add_patterns_from_file(dir
, arg
);
684 static int option_parse_exclude_standard(const struct option
*opt
,
685 const char *arg
, int unset
)
687 struct dir_struct
*dir
= opt
->value
;
689 BUG_ON_OPT_NEG(unset
);
693 setup_standard_excludes(dir
);
698 int cmd_ls_files(int argc
, const char **argv
, const char *cmd_prefix
)
700 int require_work_tree
= 0, show_tag
= 0, i
;
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"),
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"),
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"),
775 if (argc
== 2 && !strcmp(argv
[1], "-h"))
776 usage_with_options(ls_files_usage
, builtin_ls_files_options
);
778 prepare_repo_settings(the_repository
);
779 the_repository
->settings
.command_requires_full_index
= 0;
783 prefix_len
= strlen(prefix
);
784 git_config(git_default_config
, NULL
);
786 if (repo_read_index(the_repository
) < 0)
787 die("index file corrupt");
789 argc
= parse_options(argc
, argv
, prefix
, builtin_ls_files_options
,
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
);
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
);
802 if (show_tag
|| show_valid_bit
|| show_fsmonitor_bit
) {
809 tag_skip_worktree
= "S ";
810 tag_resolve_undo
= "U ";
812 if (show_modified
|| show_others
|| show_deleted
|| (dir
.flags
& DIR_SHOW_IGNORED
) || show_killed
)
813 require_work_tree
= 1;
816 * There's no point in showing unmerged unless
817 * you also show the stage information.
820 if (show_tag
|| show_stage
)
821 skipping_duplicates
= 0;
822 if (dir
.exclude_per_dir
)
825 if (require_work_tree
&& !is_inside_work_tree())
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");
833 if (recurse_submodules
&& error_unmatch
)
834 die("ls-files --recurse-submodules does not support "
837 parse_pathspec(&pathspec
, 0,
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
849 if (recurse_submodules
)
852 max_prefix
= common_prefix(&pathspec
);
853 max_prefix_len
= get_common_prefix_len(max_prefix
);
855 prune_index(the_repository
->index
, max_prefix
, max_prefix_len
);
857 /* Treat unmatching pathspec elements as errors */
858 if (pathspec
.nr
&& error_unmatch
)
859 ps_matched
= xcalloc(pathspec
.nr
, 1);
861 if ((dir
.flags
& DIR_SHOW_IGNORED
) && !show_others
&& !show_cached
)
862 die("ls-files -i must be used with either -o or -c");
864 if ((dir
.flags
& DIR_SHOW_IGNORED
) && !exc_given
)
865 die("ls-files --ignored needs some exclude pattern");
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
))
874 * Basic sanity check; show-stages and show-unmerged
875 * would not make any sense with this option.
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
);
882 show_files(the_repository
, &dir
);
884 if (show_resolve_undo
)
885 show_ru_info(the_repository
->index
);
887 if (ps_matched
&& report_path_error(ps_matched
, &pathspec
)) {
888 fprintf(stderr
, "Did you forget to 'git add'?\n");
892 string_list_clear(&exclude_list
, 0);