]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/ls-files.c
ls-files: convert ce_excluded to take an index
[thirdparty/git.git] / builtin / ls-files.c
CommitLineData
8695c8bf
LT
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 */
8695c8bf 8#include "cache.h"
22ddf719 9#include "quote.h"
453ec4bd 10#include "dir.h"
0864f264 11#include "builtin.h"
64586e75 12#include "tree.h"
ce8e8804 13#include "parse-options.h"
9d9a2f4a
JH
14#include "resolve-undo.h"
15#include "string-list.h"
64acde94 16#include "pathspec.h"
e77aa336 17#include "run-command.h"
2e5d6503 18#include "submodule.h"
8695c8bf 19
96f1e58f
DR
20static int abbrev;
21static int show_deleted;
22static int show_cached;
23static int show_others;
24static int show_stage;
25static int show_unmerged;
9d9a2f4a 26static int show_resolve_undo;
96f1e58f
DR
27static int show_modified;
28static int show_killed;
29static int show_valid_bit;
b83c8345 30static int line_terminator = '\n';
84974217 31static int debug_mode;
a7630bd4 32static int show_eol;
e77aa336 33static int recurse_submodules;
e4770f67 34static struct argv_array submodule_options = ARGV_ARRAY_INIT;
8695c8bf 35
efad1a56 36static const char *prefix;
e77aa336 37static const char *super_prefix;
efad1a56 38static int max_prefix_len;
96f1e58f 39static int prefix_len;
9e06d6ed 40static struct pathspec pathspec;
96f1e58f
DR
41static int error_unmatch;
42static char *ps_matched;
64586e75 43static const char *with_tree;
ce8e8804 44static int exc_given;
c04318e4 45static int exclude_args;
5be4efbe 46
20d37ef6
PB
47static const char *tag_cached = "";
48static const char *tag_unmerged = "";
49static const char *tag_removed = "";
50static const char *tag_other = "";
6ca45943 51static const char *tag_killed = "";
b0391890 52static const char *tag_modified = "";
44a36913 53static const char *tag_skip_worktree = "";
9d9a2f4a 54static const char *tag_resolve_undo = "";
20d37ef6 55
1985fd68
BW
56static void write_eolinfo(const struct index_state *istate,
57 const struct cache_entry *ce, const char *path)
a7630bd4 58{
1985fd68 59 if (show_eol) {
a7630bd4
TB
60 struct stat st;
61 const char *i_txt = "";
62 const char *w_txt = "";
63 const char *a_txt = get_convert_attr_ascii(path);
64 if (ce && S_ISREG(ce->ce_mode))
1985fd68 65 i_txt = get_cached_convert_stats_ascii(istate,
a7609c54 66 ce->name);
a7630bd4
TB
67 if (!lstat(path, &st) && S_ISREG(st.st_mode))
68 w_txt = get_wt_convert_stats_ascii(path);
69 printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
70 }
71}
72
e9a820ce 73static void write_name(const char *name)
efad1a56 74{
e77aa336
BW
75 /*
76 * Prepend the super_prefix to name to construct the full_name to be
77 * written.
78 */
79 struct strbuf full_name = STRBUF_INIT;
80 if (super_prefix) {
81 strbuf_addstr(&full_name, super_prefix);
82 strbuf_addstr(&full_name, name);
83 name = full_name.buf;
84 }
85
ad66df2d 86 /*
e9a820ce
JX
87 * With "--full-name", prefix_len=0; this caller needs to pass
88 * an empty string in that case (a NULL is good for "").
ad66df2d 89 */
e9a820ce
JX
90 write_name_quoted_relative(name, prefix_len ? prefix : NULL,
91 stdout, line_terminator);
e77aa336
BW
92
93 strbuf_release(&full_name);
efad1a56
CB
94}
95
453ec4bd 96static void show_dir_entry(const char *tag, struct dir_entry *ent)
5be4efbe 97{
efad1a56 98 int len = max_prefix_len;
5be4efbe 99
cbca060e 100 if (len > ent->len)
7e44c935 101 die("git ls-files: internal error - directory entry not superset of prefix");
5be4efbe 102
ebb32893 103 if (!dir_path_match(ent, &pathspec, len, ps_matched))
5be4efbe
LT
104 return;
105
22ddf719 106 fputs(tag, stdout);
1985fd68 107 write_eolinfo(NULL, NULL, ent->name);
e9a820ce 108 write_name(ent->name);
5be4efbe
LT
109}
110
23d6846b
BW
111static void show_other_files(const struct index_state *istate,
112 const struct dir_struct *dir)
fcbc3083
JH
113{
114 int i;
5698454e 115
453ec4bd 116 for (i = 0; i < dir->nr; i++) {
453ec4bd 117 struct dir_entry *ent = dir->entries[i];
23d6846b 118 if (!index_name_is_other(istate, ent->name, ent->len))
98fa4738 119 continue;
fcbc3083
JH
120 show_dir_entry(tag_other, ent);
121 }
122}
123
23d6236a
BW
124static void show_killed_files(const struct index_state *istate,
125 const struct dir_struct *dir)
6ca45943
JH
126{
127 int i;
453ec4bd
LT
128 for (i = 0; i < dir->nr; i++) {
129 struct dir_entry *ent = dir->entries[i];
6ca45943
JH
130 char *cp, *sp;
131 int pos, len, killed = 0;
132
133 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
134 sp = strchr(cp, '/');
135 if (!sp) {
136 /* If ent->name is prefix of an entry in the
137 * cache, it will be killed.
138 */
23d6236a 139 pos = index_name_pos(istate, ent->name, ent->len);
6ca45943 140 if (0 <= pos)
ef1177d1
JS
141 die("BUG: killed-file %.*s not found",
142 ent->len, ent->name);
6ca45943 143 pos = -pos - 1;
23d6236a
BW
144 while (pos < istate->cache_nr &&
145 ce_stage(istate->cache[pos]))
6ca45943 146 pos++; /* skip unmerged */
23d6236a 147 if (istate->cache_nr <= pos)
6ca45943
JH
148 break;
149 /* pos points at a name immediately after
150 * ent->name in the cache. Does it expect
151 * ent->name to be a directory?
152 */
23d6236a 153 len = ce_namelen(istate->cache[pos]);
6ca45943 154 if ((ent->len < len) &&
23d6236a 155 !strncmp(istate->cache[pos]->name,
6ca45943 156 ent->name, ent->len) &&
23d6236a 157 istate->cache[pos]->name[ent->len] == '/')
6ca45943
JH
158 killed = 1;
159 break;
160 }
23d6236a 161 if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
6ca45943
JH
162 /* If any of the leading directories in
163 * ent->name is registered in the cache,
164 * ent->name will be killed.
165 */
166 killed = 1;
167 break;
168 }
169 }
170 if (killed)
453ec4bd 171 show_dir_entry(tag_killed, dir->entries[i]);
6ca45943 172 }
8695c8bf
LT
173}
174
07c01b9f
BW
175/*
176 * Compile an argv_array with all of the options supported by --recurse_submodules
177 */
b2dfeb7c
BW
178static void compile_submodule_options(const char **argv,
179 const struct dir_struct *dir,
180 int show_tag)
07c01b9f
BW
181{
182 if (line_terminator == '\0')
e4770f67 183 argv_array_push(&submodule_options, "-z");
07c01b9f 184 if (show_tag)
e4770f67 185 argv_array_push(&submodule_options, "-t");
07c01b9f 186 if (show_valid_bit)
e4770f67 187 argv_array_push(&submodule_options, "-v");
07c01b9f 188 if (show_cached)
e4770f67 189 argv_array_push(&submodule_options, "--cached");
07c01b9f 190 if (show_eol)
e4770f67 191 argv_array_push(&submodule_options, "--eol");
07c01b9f 192 if (debug_mode)
e4770f67 193 argv_array_push(&submodule_options, "--debug");
b2dfeb7c
BW
194
195 /* Add Pathspecs */
196 argv_array_push(&submodule_options, "--");
197 for (; *argv; argv++)
198 argv_array_push(&submodule_options, *argv);
07c01b9f
BW
199}
200
e77aa336
BW
201/**
202 * Recursively call ls-files on a submodule
203 */
204static void show_gitlink(const struct cache_entry *ce)
205{
206 struct child_process cp = CHILD_PROCESS_INIT;
207 int status;
2cfe66a8 208 char *dir;
e77aa336 209
2e5d6503
JK
210 prepare_submodule_repo_env(&cp.env_array);
211 argv_array_push(&cp.env_array, GIT_DIR_ENVIRONMENT);
212
b2dfeb7c
BW
213 if (prefix_len)
214 argv_array_pushf(&cp.env_array, "%s=%s",
215 GIT_TOPLEVEL_PREFIX_ENVIRONMENT,
216 prefix);
e77aa336
BW
217 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
218 super_prefix ? super_prefix : "",
219 ce->name);
220 argv_array_push(&cp.args, "ls-files");
221 argv_array_push(&cp.args, "--recurse-submodules");
222
07c01b9f 223 /* add supported options */
e4770f67 224 argv_array_pushv(&cp.args, submodule_options.argv);
07c01b9f 225
e77aa336 226 cp.git_cmd = 1;
2cfe66a8
JK
227 dir = mkpathdup("%s/%s", get_git_work_tree(), ce->name);
228 cp.dir = dir;
e77aa336 229 status = run_command(&cp);
2cfe66a8 230 free(dir);
e77aa336
BW
231 if (status)
232 exit(status);
233}
234
9c5e6c80 235static void show_ce_entry(const char *tag, const struct cache_entry *ce)
5be4efbe 236{
e77aa336 237 struct strbuf name = STRBUF_INIT;
efad1a56 238 int len = max_prefix_len;
e77aa336
BW
239 if (super_prefix)
240 strbuf_addstr(&name, super_prefix);
241 strbuf_addstr(&name, ce->name);
5be4efbe 242
cbca060e 243 if (len > ce_namelen(ce))
7e44c935 244 die("git ls-files: internal error - cache entry not superset of prefix");
5be4efbe 245
75a6315f
BW
246 if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
247 submodule_path_match(&pathspec, name.buf, ps_matched)) {
e77aa336
BW
248 show_gitlink(ce);
249 } else if (match_pathspec(&pathspec, name.buf, name.len,
250 len, ps_matched,
251 S_ISDIR(ce->ce_mode) ||
252 S_ISGITLINK(ce->ce_mode))) {
253 if (tag && *tag && show_valid_bit &&
254 (ce->ce_flags & CE_VALID)) {
255 static char alttag[4];
256 memcpy(alttag, tag, 3);
257 if (isalpha(tag[0]))
258 alttag[0] = tolower(tag[0]);
259 else if (tag[0] == '?')
260 alttag[0] = '!';
261 else {
262 alttag[0] = 'v';
263 alttag[1] = tag[0];
264 alttag[2] = ' ';
265 alttag[3] = 0;
266 }
267 tag = alttag;
268 }
5be4efbe 269
e77aa336
BW
270 if (!show_stage) {
271 fputs(tag, stdout);
272 } else {
273 printf("%s%06o %s %d\t",
274 tag,
275 ce->ce_mode,
1c2b1f70 276 find_unique_abbrev(ce->oid.hash, abbrev),
e77aa336
BW
277 ce_stage(ce));
278 }
1985fd68 279 write_eolinfo(&the_index, ce, ce->name);
e77aa336
BW
280 write_name(ce->name);
281 if (debug_mode) {
282 const struct stat_data *sd = &ce->ce_stat_data;
283
284 printf(" ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
285 printf(" mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
286 printf(" dev: %d\tino: %d\n", sd->sd_dev, sd->sd_ino);
287 printf(" uid: %d\tgid: %d\n", sd->sd_uid, sd->sd_gid);
288 printf(" size: %d\tflags: %x\n", sd->sd_size, ce->ce_flags);
2bcab240 289 }
2bcab240
JH
290 }
291
e77aa336 292 strbuf_release(&name);
5be4efbe
LT
293}
294
2d407e2d 295static void show_ru_info(const struct index_state *istate)
9d9a2f4a 296{
8a57c6e9
AR
297 struct string_list_item *item;
298
2d407e2d 299 if (!istate->resolve_undo)
9d9a2f4a 300 return;
8a57c6e9 301
2d407e2d 302 for_each_string_list_item(item, istate->resolve_undo) {
8a57c6e9
AR
303 const char *path = item->string;
304 struct resolve_undo_info *ui = item->util;
305 int i, len;
306
307 len = strlen(path);
308 if (len < max_prefix_len)
309 continue; /* outside of the prefix */
854b0959 310 if (!match_pathspec(&pathspec, path, len,
ae8d0824 311 max_prefix_len, ps_matched, 0))
8a57c6e9
AR
312 continue; /* uninterested */
313 for (i = 0; i < 3; i++) {
314 if (!ui->mode[i])
315 continue;
316 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
317 find_unique_abbrev(ui->sha1[i], abbrev),
318 i + 1);
e9a820ce 319 write_name(path);
8a57c6e9
AR
320 }
321 }
9d9a2f4a
JH
322}
323
1d35e3bf
BW
324static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
325 const struct cache_entry *ce)
782cd4c0
JH
326{
327 int dtype = ce_to_dtype(ce);
1d35e3bf 328 return is_excluded(dir, istate, ce->name, &dtype);
782cd4c0
JH
329}
330
efad1a56 331static void show_files(struct dir_struct *dir)
8695c8bf
LT
332{
333 int i;
334
335 /* For cached/deleted files we don't need to even do the readdir */
6ca45943 336 if (show_others || show_killed) {
2eac2a4c
JH
337 if (!show_others)
338 dir->flags |= DIR_COLLECT_KILLED_ONLY;
0d32c183 339 fill_directory(dir, &the_index, &pathspec);
6ca45943 340 if (show_others)
23d6846b 341 show_other_files(&the_index, dir);
6ca45943 342 if (show_killed)
23d6236a 343 show_killed_files(&the_index, dir);
8695c8bf 344 }
0b437a18 345 if (show_cached || show_stage) {
8695c8bf 346 for (i = 0; i < active_nr; i++) {
9c5e6c80 347 const struct cache_entry *ce = active_cache[i];
eb41775e 348 if ((dir->flags & DIR_SHOW_IGNORED) &&
1d35e3bf 349 !ce_excluded(dir, &the_index, ce))
500348aa 350 continue;
eec8c633
LT
351 if (show_unmerged && !ce_stage(ce))
352 continue;
7a51ed66 353 if (ce->ce_flags & CE_UPDATE)
64586e75 354 continue;
44a36913
NTND
355 show_ce_entry(ce_stage(ce) ? tag_unmerged :
356 (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce);
8695c8bf
LT
357 }
358 }
0b437a18 359 if (show_deleted || show_modified) {
8695c8bf 360 for (i = 0; i < active_nr; i++) {
9c5e6c80 361 const struct cache_entry *ce = active_cache[i];
8695c8bf 362 struct stat st;
b0391890 363 int err;
eb41775e 364 if ((dir->flags & DIR_SHOW_IGNORED) &&
1d35e3bf 365 !ce_excluded(dir, &the_index, ce))
500348aa 366 continue;
4b4e26d2
JH
367 if (ce->ce_flags & CE_UPDATE)
368 continue;
b4d1690d
NTND
369 if (ce_skip_worktree(ce))
370 continue;
b0391890
JH
371 err = lstat(ce->name, &st);
372 if (show_deleted && err)
373 show_ce_entry(tag_removed, ce);
2bcab240 374 if (show_modified && ce_modified(ce, &st, 0))
b0391890 375 show_ce_entry(tag_modified, ce);
5be4efbe
LT
376 }
377 }
378}
379
380/*
381 * Prune the index to only contain stuff starting with "prefix"
382 */
7b4158a8 383static void prune_cache(const char *prefix, size_t prefixlen)
5be4efbe 384{
7b4158a8 385 int pos;
5be4efbe
LT
386 unsigned int first, last;
387
7b4158a8
RS
388 if (!prefix)
389 return;
390 pos = cache_name_pos(prefix, prefixlen);
5be4efbe
LT
391 if (pos < 0)
392 pos = -pos-1;
96f6d3f6 393 first = pos;
5be4efbe
LT
394 last = active_nr;
395 while (last > first) {
396 int next = (last + first) >> 1;
9c5e6c80 397 const struct cache_entry *ce = active_cache[next];
7b4158a8 398 if (!strncmp(ce->name, prefix, prefixlen)) {
5be4efbe
LT
399 first = next+1;
400 continue;
8695c8bf 401 }
5be4efbe
LT
402 last = next;
403 }
96f6d3f6
RS
404 memmove(active_cache, active_cache + pos,
405 (last - pos) * sizeof(struct cache_entry *));
406 active_nr = last - pos;
5be4efbe
LT
407}
408
cbca060e
BW
409static int get_common_prefix_len(const char *common_prefix)
410{
411 int common_prefix_len;
412
413 if (!common_prefix)
414 return 0;
415
416 common_prefix_len = strlen(common_prefix);
417
418 /*
419 * If the prefix has a trailing slash, strip it so that submodules wont
420 * be pruned from the index.
421 */
422 if (common_prefix[common_prefix_len - 1] == '/')
423 common_prefix_len--;
424
425 return common_prefix_len;
426}
427
64586e75
JH
428/*
429 * Read the tree specified with --with-tree option
430 * (typically, HEAD) into stage #1 and then
431 * squash them down to stage #0. This is used for
432 * --error-unmatch to list and check the path patterns
433 * that were given from the command line. We are not
434 * going to write this index out.
435 */
312c984a
BW
436void overlay_tree_on_index(struct index_state *istate,
437 const char *tree_name, const char *prefix)
64586e75
JH
438{
439 struct tree *tree;
6f37eb7d 440 struct object_id oid;
f0096c06 441 struct pathspec pathspec;
64586e75
JH
442 struct cache_entry *last_stage0 = NULL;
443 int i;
444
6f37eb7d 445 if (get_oid(tree_name, &oid))
64586e75 446 die("tree-ish %s not found.", tree_name);
a9dbc179 447 tree = parse_tree_indirect(&oid);
64586e75
JH
448 if (!tree)
449 die("bad tree-ish %s", tree_name);
450
451 /* Hoist the unmerged entries up to stage #3 to make room */
312c984a
BW
452 for (i = 0; i < istate->cache_nr; i++) {
453 struct cache_entry *ce = istate->cache[i];
64586e75
JH
454 if (!ce_stage(ce))
455 continue;
7a51ed66 456 ce->ce_flags |= CE_STAGEMASK;
64586e75
JH
457 }
458
459 if (prefix) {
9a087274
NTND
460 static const char *(matchbuf[1]);
461 matchbuf[0] = NULL;
462 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
463 PATHSPEC_PREFER_CWD, prefix, matchbuf);
64586e75 464 } else
9a087274 465 memset(&pathspec, 0, sizeof(pathspec));
312c984a 466 if (read_tree(tree, 1, &pathspec, istate))
64586e75
JH
467 die("unable to read tree entries %s", tree_name);
468
312c984a
BW
469 for (i = 0; i < istate->cache_nr; i++) {
470 struct cache_entry *ce = istate->cache[i];
64586e75
JH
471 switch (ce_stage(ce)) {
472 case 0:
473 last_stage0 = ce;
474 /* fallthru */
475 default:
476 continue;
477 case 1:
478 /*
479 * If there is stage #0 entry for this, we do not
480 * need to show it. We use CE_UPDATE bit to mark
481 * such an entry.
482 */
483 if (last_stage0 &&
484 !strcmp(last_stage0->name, ce->name))
7a51ed66 485 ce->ce_flags |= CE_UPDATE;
64586e75
JH
486 }
487 }
488}
489
ce8e8804 490static const char * const ls_files_usage[] = {
9c9b4f2f 491 N_("git ls-files [<options>] [<file>...]"),
ce8e8804
MV
492 NULL
493};
494
ce8e8804
MV
495static int option_parse_exclude(const struct option *opt,
496 const char *arg, int unset)
497{
72aeb187 498 struct string_list *exclude_list = opt->value;
ce8e8804
MV
499
500 exc_given = 1;
72aeb187 501 string_list_append(exclude_list, arg);
ce8e8804
MV
502
503 return 0;
504}
505
506static int option_parse_exclude_from(const struct option *opt,
507 const char *arg, int unset)
508{
509 struct dir_struct *dir = opt->value;
510
511 exc_given = 1;
512 add_excludes_from_file(dir, arg);
513
514 return 0;
515}
516
517static int option_parse_exclude_standard(const struct option *opt,
518 const char *arg, int unset)
519{
520 struct dir_struct *dir = opt->value;
521
522 exc_given = 1;
523 setup_standard_excludes(dir);
524
525 return 0;
526}
cf9a113d 527
efad1a56 528int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
8695c8bf 529{
72aeb187 530 int require_work_tree = 0, show_tag = 0, i;
efad1a56 531 const char *max_prefix;
453ec4bd 532 struct dir_struct dir;
72aeb187
AS
533 struct exclude_list *el;
534 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
ce8e8804 535 struct option builtin_ls_files_options[] = {
1f3c79a9
JK
536 /* Think twice before adding "--nul" synonym to this */
537 OPT_SET_INT('z', NULL, &line_terminator,
538 N_("paths are separated with NUL character"), '\0'),
d5d09d47 539 OPT_BOOL('t', NULL, &show_tag,
377adc3a 540 N_("identify the file status with tags")),
d5d09d47 541 OPT_BOOL('v', NULL, &show_valid_bit,
377adc3a 542 N_("use lowercase letters for 'assume unchanged' files")),
d5d09d47 543 OPT_BOOL('c', "cached", &show_cached,
377adc3a 544 N_("show cached files in the output (default)")),
d5d09d47 545 OPT_BOOL('d', "deleted", &show_deleted,
377adc3a 546 N_("show deleted files in the output")),
d5d09d47 547 OPT_BOOL('m', "modified", &show_modified,
377adc3a 548 N_("show modified files in the output")),
d5d09d47 549 OPT_BOOL('o', "others", &show_others,
377adc3a 550 N_("show other files in the output")),
ce8e8804 551 OPT_BIT('i', "ignored", &dir.flags,
377adc3a 552 N_("show ignored files in the output"),
ce8e8804 553 DIR_SHOW_IGNORED),
d5d09d47 554 OPT_BOOL('s', "stage", &show_stage,
377adc3a 555 N_("show staged contents' object name in the output")),
d5d09d47 556 OPT_BOOL('k', "killed", &show_killed,
377adc3a 557 N_("show files on the filesystem that need to be removed")),
ce8e8804 558 OPT_BIT(0, "directory", &dir.flags,
ad5fe377 559 N_("show 'other' directories' names only"),
ce8e8804 560 DIR_SHOW_OTHER_DIRECTORIES),
a7630bd4 561 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
e9008b9a 562 OPT_NEGBIT(0, "empty-directory", &dir.flags,
377adc3a 563 N_("don't show empty directories"),
ce8e8804 564 DIR_HIDE_EMPTY_DIRECTORIES),
d5d09d47 565 OPT_BOOL('u', "unmerged", &show_unmerged,
377adc3a 566 N_("show unmerged files in the output")),
d5d09d47 567 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
377adc3a 568 N_("show resolve-undo information")),
a39b15b4 569 { OPTION_CALLBACK, 'x', "exclude", &exclude_list, N_("pattern"),
377adc3a 570 N_("skip files matching pattern"),
ce8e8804 571 0, option_parse_exclude },
377adc3a
NTND
572 { OPTION_CALLBACK, 'X', "exclude-from", &dir, N_("file"),
573 N_("exclude patterns are read from <file>"),
ce8e8804 574 0, option_parse_exclude_from },
377adc3a
NTND
575 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
576 N_("read additional per-directory exclude patterns in <file>")),
ce8e8804 577 { OPTION_CALLBACK, 0, "exclude-standard", &dir, NULL,
377adc3a 578 N_("add the standard git exclusions"),
ce8e8804 579 PARSE_OPT_NOARG, option_parse_exclude_standard },
efad1a56 580 { OPTION_SET_INT, 0, "full-name", &prefix_len, NULL,
377adc3a 581 N_("make the output relative to the project top directory"),
ce8e8804 582 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL },
e77aa336
BW
583 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
584 N_("recurse through submodules")),
d5d09d47 585 OPT_BOOL(0, "error-unmatch", &error_unmatch,
377adc3a
NTND
586 N_("if any <file> is not in the index, treat this as an error")),
587 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
588 N_("pretend that paths removed since <tree-ish> are still present")),
ce8e8804 589 OPT__ABBREV(&abbrev),
d5d09d47 590 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
ce8e8804
MV
591 OPT_END()
592 };
8695c8bf 593
cbb3167e
NTND
594 if (argc == 2 && !strcmp(argv[1], "-h"))
595 usage_with_options(ls_files_usage, builtin_ls_files_options);
596
453ec4bd 597 memset(&dir, 0, sizeof(dir));
efad1a56 598 prefix = cmd_prefix;
5be4efbe 599 if (prefix)
efad1a56 600 prefix_len = strlen(prefix);
e77aa336 601 super_prefix = get_super_prefix();
ef90d6d4 602 git_config(git_default_config, NULL);
5be4efbe 603
c28b3d6e
NTND
604 if (read_cache() < 0)
605 die("index file corrupt");
606
37782920 607 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
ce8e8804 608 ls_files_usage, 0);
72aeb187
AS
609 el = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
610 for (i = 0; i < exclude_list.nr; i++) {
611 add_exclude(exclude_list.items[i].string, "", 0, el, --exclude_args);
612 }
ce8e8804
MV
613 if (show_tag || show_valid_bit) {
614 tag_cached = "H ";
615 tag_unmerged = "M ";
616 tag_removed = "R ";
617 tag_modified = "C ";
618 tag_other = "? ";
619 tag_killed = "K ";
44a36913 620 tag_skip_worktree = "S ";
9d9a2f4a 621 tag_resolve_undo = "U ";
9ff768e9 622 }
de2e3b04 623 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
ce8e8804
MV
624 require_work_tree = 1;
625 if (show_unmerged)
626 /*
627 * There's no point in showing unmerged unless
628 * you also show the stage information.
629 */
630 show_stage = 1;
631 if (dir.exclude_per_dir)
632 exc_given = 1;
9ff768e9 633
7d8ae932
MH
634 if (require_work_tree && !is_inside_work_tree())
635 setup_work_tree();
6d9ba67b 636
07c01b9f 637 if (recurse_submodules)
b2dfeb7c 638 compile_submodule_options(argv, &dir, show_tag);
07c01b9f 639
e77aa336
BW
640 if (recurse_submodules &&
641 (show_stage || show_deleted || show_others || show_unmerged ||
07c01b9f 642 show_killed || show_modified || show_resolve_undo || with_tree))
e77aa336
BW
643 die("ls-files --recurse-submodules unsupported mode");
644
645 if (recurse_submodules && error_unmatch)
646 die("ls-files --recurse-submodules does not support "
647 "--error-unmatch");
648
9e06d6ed 649 parse_pathspec(&pathspec, 0,
cbca060e 650 PATHSPEC_PREFER_CWD,
9e06d6ed 651 prefix, argv);
f3670a57 652
75a6315f
BW
653 /*
654 * Find common prefix for all pathspec's
655 * This is used as a performance optimization which unfortunately cannot
656 * be done when recursing into submodules
657 */
658 if (recurse_submodules)
659 max_prefix = NULL;
660 else
661 max_prefix = common_prefix(&pathspec);
cbca060e
BW
662 max_prefix_len = get_common_prefix_len(max_prefix);
663
664 prune_cache(max_prefix, max_prefix_len);
5be4efbe 665
bba319b5 666 /* Treat unmatching pathspec elements as errors */
9e06d6ed 667 if (pathspec.nr && error_unmatch)
8b54c234 668 ps_matched = xcalloc(pathspec.nr, 1);
bba319b5 669
ac78b009
BW
670 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
671 die("ls-files --ignored needs some exclude pattern");
8695c8bf
LT
672
673 /* With no flags, we default to showing the cached files */
0b437a18
RS
674 if (!(show_stage || show_deleted || show_others || show_unmerged ||
675 show_killed || show_modified || show_resolve_undo))
8695c8bf
LT
676 show_cached = 1;
677
64586e75
JH
678 if (with_tree) {
679 /*
680 * Basic sanity check; show-stages and show-unmerged
681 * would not make any sense with this option.
682 */
683 if (show_stage || show_unmerged)
684 die("ls-files --with-tree is incompatible with -s or -u");
312c984a 685 overlay_tree_on_index(&the_index, with_tree, max_prefix);
64586e75 686 }
efad1a56 687 show_files(&dir);
9d9a2f4a 688 if (show_resolve_undo)
2d407e2d 689 show_ru_info(&the_index);
bba319b5
JH
690
691 if (ps_matched) {
ee425e46 692 int bad;
17ddc66e 693 bad = report_path_error(ps_matched, &pathspec, prefix);
ee425e46 694 if (bad)
ced7b828
AE
695 fprintf(stderr, "Did you forget to 'git add'?\n");
696
ee425e46 697 return bad ? 1 : 0;
bba319b5
JH
698 }
699
8695c8bf
LT
700 return 0;
701}