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"
17 #include "cache-tree.h"
18 #include "parse-options.h"
19 #include "resolve-undo.h"
20 #include "string-list.h"
22 #include "run-command.h"
23 #include "submodule.h"
24 #include "submodule-config.h"
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
;
40 static int recurse_submodules
;
41 static int skipping_duplicates
;
42 static int show_sparse_dirs
;
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
;
52 static int exclude_args
;
53 static const char *format
;
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
= "";
64 static void write_eolinfo(struct index_state
*istate
,
65 const struct cache_entry
*ce
, const char *path
)
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
,
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
);
81 static void write_name(const char *name
)
84 * With "--full-name", prefix_len=0; this caller needs to pass
85 * an empty string in that case (a NULL is good for "").
87 write_name_quoted_relative(name
, prefix_len
? prefix
: NULL
,
88 stdout
, line_terminator
);
91 static void write_name_to_buf(struct strbuf
*sb
, const char *name
)
93 const char *rel
= relative_path(name
, prefix_len
? prefix
: NULL
, sb
);
96 quote_c_style(rel
, sb
, NULL
, 0);
98 strbuf_addstr(sb
, rel
);
101 static const char *get_tag(const struct cache_entry
*ce
, const char *tag
)
103 static char alttag
[4];
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);
109 if (isalpha(tag
[0])) {
110 alttag
[0] = tolower(tag
[0]);
111 } else if (tag
[0] == '?') {
126 static void print_debug(const struct cache_entry
*ce
)
129 const struct stat_data
*sd
= &ce
->ce_stat_data
;
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
);
139 static void show_dir_entry(struct index_state
*istate
,
140 const char *tag
, struct dir_entry
*ent
)
142 int len
= max_prefix_len
;
145 die("git ls-files: internal error - directory entry not superset of prefix");
147 /* If ps_matches is non-NULL, figure out which pathspec(s) match. */
149 dir_path_match(istate
, ent
, &pathspec
, len
, ps_matched
);
152 write_eolinfo(istate
, NULL
, ent
->name
);
153 write_name(ent
->name
);
156 static void show_other_files(struct index_state
*istate
,
157 const struct dir_struct
*dir
)
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
))
165 show_dir_entry(istate
, tag_other
, ent
);
169 static void show_killed_files(struct index_state
*istate
,
170 const struct dir_struct
*dir
)
173 for (i
= 0; i
< dir
->nr
; i
++) {
174 struct dir_entry
*ent
= dir
->entries
[i
];
176 int pos
, len
, killed
= 0;
178 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
179 sp
= strchr(cp
, '/');
181 /* If ent->name is prefix of an entry in the
182 * cache, it will be killed.
184 pos
= index_name_pos(istate
, ent
->name
, ent
->len
);
186 BUG("killed-file %.*s not found",
187 ent
->len
, ent
->name
);
189 while (pos
< istate
->cache_nr
&&
190 ce_stage(istate
->cache
[pos
]))
191 pos
++; /* skip unmerged */
192 if (istate
->cache_nr
<= pos
)
194 /* pos points at a name immediately after
195 * ent->name in the cache. Does it expect
196 * ent->name to be a directory?
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
] == '/')
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.
216 show_dir_entry(istate
, tag_killed
, dir
->entries
[i
]);
220 static void show_files(struct repository
*repo
, struct dir_struct
*dir
);
222 static void show_submodule(struct repository
*superproject
,
223 struct dir_struct
*dir
, const char *path
)
225 struct repository subrepo
;
227 if (repo_submodule_init(&subrepo
, superproject
, path
, null_oid()))
230 if (repo_read_index(&subrepo
) < 0)
231 die("index file corrupt");
233 show_files(&subrepo
, dir
);
235 repo_clear(&subrepo
);
238 struct show_index_data
{
239 const char *pathname
;
240 struct index_state
*istate
;
241 const struct cache_entry
*ce
;
244 static size_t expand_show_index(struct strbuf
*sb
, const char *start
,
247 struct show_index_data
*data
= context
;
250 size_t len
= strbuf_expand_literal_cb(sb
, start
, NULL
);
256 die(_("bad ls-files format: element '%s' "
257 "does not start with '('"), start
);
259 end
= strchr(start
+ 1, ')');
261 die(_("bad ls-files format: element '%s' "
262 "does not end in ')'"), start
);
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
,
282 else if (skip_prefix(start
, "(path)", &p
))
283 write_name_to_buf(sb
, data
->pathname
);
285 die(_("bad ls-files format: %%%.*s"), (int)len
, start
);
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
,
297 struct strbuf sb
= STRBUF_INIT
;
299 strbuf_expand(&sb
, format
, expand_show_index
, &data
);
300 strbuf_addch(&sb
, line_terminator
);
301 fwrite(sb
.buf
, sb
.len
, 1, stdout
);
305 static void show_ce(struct repository
*repo
, struct dir_struct
*dir
,
306 const struct cache_entry
*ce
, const char *fullname
,
309 if (max_prefix_len
> strlen(fullname
))
310 die("git ls-files: internal error - cache entry not superset of prefix");
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
))) {
320 show_ce_fmt(repo
, ce
, format
, fullname
);
325 tag
= get_tag(ce
, tag
);
330 printf("%s%06o %s %d\t",
333 repo_find_unique_abbrev(repo
, &ce
->oid
, abbrev
),
336 write_eolinfo(repo
->index
, ce
, fullname
);
337 write_name(fullname
);
342 static void show_ru_info(struct index_state
*istate
)
344 struct string_list_item
*item
;
346 if (!istate
->resolve_undo
)
349 for_each_string_list_item(item
, istate
->resolve_undo
) {
350 const char *path
= item
->string
;
351 struct resolve_undo_info
*ui
= item
->util
;
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
++) {
363 printf("%s%06o %s %d\t", tag_resolve_undo
, ui
->mode
[i
],
364 find_unique_abbrev(&ui
->oid
[i
], abbrev
),
371 static int ce_excluded(struct dir_struct
*dir
, struct index_state
*istate
,
372 const char *fullname
, const struct cache_entry
*ce
)
374 int dtype
= ce_to_dtype(ce
);
375 return is_excluded(dir
, istate
, fullname
, &dtype
);
378 static void construct_fullname(struct strbuf
*out
, const struct repository
*repo
,
379 const struct cache_entry
*ce
)
382 if (repo
->submodule_prefix
)
383 strbuf_addstr(out
, repo
->submodule_prefix
);
384 strbuf_addstr(out
, ce
->name
);
387 static void show_files(struct repository
*repo
, struct dir_struct
*dir
)
390 struct strbuf fullname
= STRBUF_INIT
;
392 /* For cached/deleted files we don't need to even do the readdir */
393 if (show_others
|| show_killed
) {
395 dir
->flags
|= DIR_COLLECT_KILLED_ONLY
;
396 fill_directory(dir
, repo
->index
, &pathspec
);
398 show_other_files(repo
->index
, dir
);
400 show_killed_files(repo
->index
, dir
);
403 if (!(show_cached
|| show_stage
|| show_deleted
|| show_modified
))
406 if (!show_sparse_dirs
)
407 ensure_full_index(repo
->index
);
409 for (i
= 0; i
< repo
->index
->cache_nr
; i
++) {
410 const struct cache_entry
*ce
= repo
->index
->cache
[i
];
414 construct_fullname(&fullname
, repo
, ce
);
416 if ((dir
->flags
& DIR_SHOW_IGNORED
) &&
417 !ce_excluded(dir
, repo
->index
, fullname
.buf
, ce
))
419 if (ce
->ce_flags
& CE_UPDATE
)
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
:
427 if (skipping_duplicates
)
428 goto skip_to_next_name
;
431 if (!(show_deleted
|| show_modified
))
433 if (ce_skip_worktree(ce
))
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
;
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
;
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
))
458 i
= j
- 1; /* compensate for the for loop */
462 strbuf_release(&fullname
);
466 * Prune the index to only contain stuff starting with "prefix"
468 static void prune_index(struct index_state
*istate
,
469 const char *prefix
, size_t prefixlen
)
472 unsigned int first
, last
;
474 if (!prefix
|| !istate
->cache_nr
)
476 pos
= index_name_pos(istate
, prefix
, prefixlen
);
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
)) {
490 MOVE_ARRAY(istate
->cache
, istate
->cache
+ pos
, last
- pos
);
491 istate
->cache_nr
= last
- pos
;
494 static int get_common_prefix_len(const char *common_prefix
)
496 int common_prefix_len
;
501 common_prefix_len
= strlen(common_prefix
);
504 * If the prefix has a trailing slash, strip it so that submodules wont
505 * be pruned from the index.
507 if (common_prefix
[common_prefix_len
- 1] == '/')
510 return common_prefix_len
;
513 static int read_one_entry_opt(struct index_state
*istate
,
514 const struct object_id
*oid
,
516 const char *pathname
,
517 unsigned mode
, int opt
)
520 struct cache_entry
*ce
;
523 return READ_TREE_RECURSIVE
;
525 len
= strlen(pathname
);
526 ce
= make_empty_cache_entry(istate
, base
->len
+ len
);
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
);
537 static int read_one_entry(const struct object_id
*oid
, struct strbuf
*base
,
538 const char *pathname
, unsigned mode
,
541 struct index_state
*istate
= context
;
542 return read_one_entry_opt(istate
, oid
, base
, pathname
,
544 ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
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.
551 static int read_one_entry_quick(const struct object_id
*oid
, struct strbuf
*base
,
552 const char *pathname
, unsigned mode
,
555 struct index_state
*istate
= context
;
556 return read_one_entry_opt(istate
, oid
, base
, pathname
,
557 mode
, ADD_CACHE_JUST_APPEND
);
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.
568 void overlay_tree_on_index(struct index_state
*istate
,
569 const char *tree_name
, const char *prefix
)
572 struct object_id oid
;
573 struct pathspec pathspec
;
574 struct cache_entry
*last_stage0
= NULL
;
576 read_tree_fn_t fn
= NULL
;
579 if (get_oid(tree_name
, &oid
))
580 die("tree-ish %s not found.", tree_name
);
581 tree
= parse_tree_indirect(&oid
);
583 die("bad tree-ish %s", tree_name
);
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
];
592 ce
->ce_flags
|= CE_STAGEMASK
;
596 static const char *(matchbuf
[1]);
598 parse_pathspec(&pathspec
, PATHSPEC_ALL_MAGIC
,
599 PATHSPEC_PREFER_CWD
, prefix
, matchbuf
);
601 memset(&pathspec
, 0, sizeof(pathspec
));
604 * See if we have cache entry at the stage. If so,
605 * do it the original slow way, otherwise, append and then
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)
615 fn
= read_one_entry_quick
;
616 err
= read_tree(the_repository
, tree
, &pathspec
, fn
, istate
);
617 clear_pathspec(&pathspec
);
619 die("unable to read tree entries %s", tree_name
);
622 * Sort the cache entry -- we need to nuke the cache tree, though.
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
);
629 for (i
= 0; i
< istate
->cache_nr
; i
++) {
630 struct cache_entry
*ce
= istate
->cache
[i
];
631 switch (ce_stage(ce
)) {
639 * If there is stage #0 entry for this, we do not
640 * need to show it. We use CE_UPDATE bit to mark
644 !strcmp(last_stage0
->name
, ce
->name
))
645 ce
->ce_flags
|= CE_UPDATE
;
650 static const char * const ls_files_usage
[] = {
651 N_("git ls-files [<options>] [<file>...]"),
655 static int option_parse_exclude(const struct option
*opt
,
656 const char *arg
, int unset
)
658 struct string_list
*exclude_list
= opt
->value
;
660 BUG_ON_OPT_NEG(unset
);
663 string_list_append(exclude_list
, arg
);
668 static int option_parse_exclude_from(const struct option
*opt
,
669 const char *arg
, int unset
)
671 struct dir_struct
*dir
= opt
->value
;
673 BUG_ON_OPT_NEG(unset
);
676 add_patterns_from_file(dir
, arg
);
681 static int option_parse_exclude_standard(const struct option
*opt
,
682 const char *arg
, int unset
)
684 struct dir_struct
*dir
= opt
->value
;
686 BUG_ON_OPT_NEG(unset
);
690 setup_standard_excludes(dir
);
695 int cmd_ls_files(int argc
, const char **argv
, const char *cmd_prefix
)
697 int require_work_tree
= 0, show_tag
= 0, i
;
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"),
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"),
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"),
772 if (argc
== 2 && !strcmp(argv
[1], "-h"))
773 usage_with_options(ls_files_usage
, builtin_ls_files_options
);
775 prepare_repo_settings(the_repository
);
776 the_repository
->settings
.command_requires_full_index
= 0;
780 prefix_len
= strlen(prefix
);
781 git_config(git_default_config
, NULL
);
783 if (repo_read_index(the_repository
) < 0)
784 die("index file corrupt");
786 argc
= parse_options(argc
, argv
, prefix
, builtin_ls_files_options
,
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
);
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
);
799 if (show_tag
|| show_valid_bit
|| show_fsmonitor_bit
) {
806 tag_skip_worktree
= "S ";
807 tag_resolve_undo
= "U ";
809 if (show_modified
|| show_others
|| show_deleted
|| (dir
.flags
& DIR_SHOW_IGNORED
) || show_killed
)
810 require_work_tree
= 1;
813 * There's no point in showing unmerged unless
814 * you also show the stage information.
817 if (show_tag
|| show_stage
)
818 skipping_duplicates
= 0;
819 if (dir
.exclude_per_dir
)
822 if (require_work_tree
&& !is_inside_work_tree())
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");
830 if (recurse_submodules
&& error_unmatch
)
831 die("ls-files --recurse-submodules does not support "
834 parse_pathspec(&pathspec
, 0,
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
846 if (recurse_submodules
)
849 max_prefix
= common_prefix(&pathspec
);
850 max_prefix_len
= get_common_prefix_len(max_prefix
);
852 prune_index(the_repository
->index
, max_prefix
, max_prefix_len
);
854 /* Treat unmatching pathspec elements as errors */
855 if (pathspec
.nr
&& error_unmatch
)
856 ps_matched
= xcalloc(pathspec
.nr
, 1);
858 if ((dir
.flags
& DIR_SHOW_IGNORED
) && !show_others
&& !show_cached
)
859 die("ls-files -i must be used with either -o or -c");
861 if ((dir
.flags
& DIR_SHOW_IGNORED
) && !exc_given
)
862 die("ls-files --ignored needs some exclude pattern");
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
))
871 * Basic sanity check; show-stages and show-unmerged
872 * would not make any sense with this option.
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
);
879 show_files(the_repository
, &dir
);
881 if (show_resolve_undo
)
882 show_ru_info(the_repository
->index
);
884 if (ps_matched
&& report_path_error(ps_matched
, &pathspec
)) {
885 fprintf(stderr
, "Did you forget to 'git add'?\n");
889 string_list_clear(&exclude_list
, 0);