]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/ls-files.c
treewide: remove unnecessary includes in source files
[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 */
bc5c5ec0 8#include "builtin.h"
188dce13 9#include "repository.h"
b2141fc1 10#include "config.h"
73359a9b 11#include "convert.h"
22ddf719 12#include "quote.h"
453ec4bd 13#include "dir.h"
f394e093 14#include "gettext.h"
dabab1d6 15#include "object-name.h"
ce74de93 16#include "strbuf.h"
ce8e8804 17#include "parse-options.h"
9d9a2f4a
JH
18#include "resolve-undo.h"
19#include "string-list.h"
c339932b 20#include "path.h"
64acde94 21#include "pathspec.h"
08c46a49 22#include "read-cache.h"
e38da487 23#include "setup.h"
baf889c2 24#include "sparse-index.h"
2e5d6503 25#include "submodule.h"
4d28c4f7
ZH
26#include "object-store.h"
27#include "hex.h"
28
8695c8bf 29
96f1e58f
DR
30static int abbrev;
31static int show_deleted;
32static int show_cached;
33static int show_others;
34static int show_stage;
35static int show_unmerged;
9d9a2f4a 36static int show_resolve_undo;
96f1e58f
DR
37static int show_modified;
38static int show_killed;
39static int show_valid_bit;
d8c71db8 40static int show_fsmonitor_bit;
b83c8345 41static int line_terminator = '\n';
84974217 42static int debug_mode;
a7630bd4 43static int show_eol;
e77aa336 44static int recurse_submodules;
93a7d983 45static int skipping_duplicates;
78087097 46static int show_sparse_dirs;
8695c8bf 47
efad1a56
CB
48static const char *prefix;
49static int max_prefix_len;
96f1e58f 50static int prefix_len;
9e06d6ed 51static struct pathspec pathspec;
96f1e58f
DR
52static int error_unmatch;
53static char *ps_matched;
64586e75 54static const char *with_tree;
ce8e8804 55static int exc_given;
c04318e4 56static int exclude_args;
ce74de93 57static const char *format;
5be4efbe 58
20d37ef6
PB
59static const char *tag_cached = "";
60static const char *tag_unmerged = "";
61static const char *tag_removed = "";
62static const char *tag_other = "";
6ca45943 63static const char *tag_killed = "";
b0391890 64static const char *tag_modified = "";
44a36913 65static const char *tag_skip_worktree = "";
9d9a2f4a 66static const char *tag_resolve_undo = "";
20d37ef6 67
847a9e5d 68static void write_eolinfo(struct index_state *istate,
1985fd68 69 const struct cache_entry *ce, const char *path)
a7630bd4 70{
1985fd68 71 if (show_eol) {
a7630bd4
TB
72 struct stat st;
73 const char *i_txt = "";
74 const char *w_txt = "";
a52b321d 75 const char *a_txt = get_convert_attr_ascii(istate, path);
a7630bd4 76 if (ce && S_ISREG(ce->ce_mode))
1985fd68 77 i_txt = get_cached_convert_stats_ascii(istate,
a7609c54 78 ce->name);
a7630bd4
TB
79 if (!lstat(path, &st) && S_ISREG(st.st_mode))
80 w_txt = get_wt_convert_stats_ascii(path);
81 printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
82 }
83}
84
e9a820ce 85static void write_name(const char *name)
efad1a56 86{
ad66df2d 87 /*
e9a820ce
JX
88 * With "--full-name", prefix_len=0; this caller needs to pass
89 * an empty string in that case (a NULL is good for "").
ad66df2d 90 */
e9a820ce
JX
91 write_name_quoted_relative(name, prefix_len ? prefix : NULL,
92 stdout, line_terminator);
efad1a56
CB
93}
94
ce74de93
ZH
95static void write_name_to_buf(struct strbuf *sb, const char *name)
96{
cfb62dd0
AJ
97 struct strbuf buf = STRBUF_INIT;
98 const char *rel = relative_path(name, prefix_len ? prefix : NULL, &buf);
ce74de93
ZH
99
100 if (line_terminator)
101 quote_c_style(rel, sb, NULL, 0);
102 else
103 strbuf_addstr(sb, rel);
cfb62dd0
AJ
104
105 strbuf_release(&buf);
ce74de93
ZH
106}
107
a84f3e59
BW
108static const char *get_tag(const struct cache_entry *ce, const char *tag)
109{
110 static char alttag[4];
111
d8c71db8
BP
112 if (tag && *tag && ((show_valid_bit && (ce->ce_flags & CE_VALID)) ||
113 (show_fsmonitor_bit && (ce->ce_flags & CE_FSMONITOR_VALID)))) {
a84f3e59
BW
114 memcpy(alttag, tag, 3);
115
116 if (isalpha(tag[0])) {
117 alttag[0] = tolower(tag[0]);
118 } else if (tag[0] == '?') {
119 alttag[0] = '!';
120 } else {
121 alttag[0] = 'v';
122 alttag[1] = tag[0];
123 alttag[2] = ' ';
124 alttag[3] = 0;
125 }
126
127 tag = alttag;
128 }
129
130 return tag;
131}
132
5306ccf9
BW
133static void print_debug(const struct cache_entry *ce)
134{
135 if (debug_mode) {
136 const struct stat_data *sd = &ce->ce_stat_data;
137
7cb7283a
TG
138 printf(" ctime: %u:%u\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
139 printf(" mtime: %u:%u\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
140 printf(" dev: %u\tino: %u\n", sd->sd_dev, sd->sd_ino);
141 printf(" uid: %u\tgid: %u\n", sd->sd_uid, sd->sd_gid);
142 printf(" size: %u\tflags: %x\n", sd->sd_size, ce->ce_flags);
5306ccf9
BW
143 }
144}
145
847a9e5d 146static void show_dir_entry(struct index_state *istate,
a52b321d 147 const char *tag, struct dir_entry *ent)
5be4efbe 148{
efad1a56 149 int len = max_prefix_len;
5be4efbe 150
cbca060e 151 if (len > ent->len)
7e44c935 152 die("git ls-files: internal error - directory entry not superset of prefix");
5be4efbe 153
95c11ecc
EN
154 /* If ps_matches is non-NULL, figure out which pathspec(s) match. */
155 if (ps_matched)
156 dir_path_match(istate, ent, &pathspec, len, ps_matched);
5be4efbe 157
22ddf719 158 fputs(tag, stdout);
a52b321d 159 write_eolinfo(istate, NULL, ent->name);
e9a820ce 160 write_name(ent->name);
5be4efbe
LT
161}
162
847a9e5d 163static void show_other_files(struct index_state *istate,
23d6846b 164 const struct dir_struct *dir)
fcbc3083
JH
165{
166 int i;
5698454e 167
453ec4bd 168 for (i = 0; i < dir->nr; i++) {
453ec4bd 169 struct dir_entry *ent = dir->entries[i];
23d6846b 170 if (!index_name_is_other(istate, ent->name, ent->len))
98fa4738 171 continue;
a52b321d 172 show_dir_entry(istate, tag_other, ent);
fcbc3083
JH
173 }
174}
175
847a9e5d 176static void show_killed_files(struct index_state *istate,
23d6236a 177 const struct dir_struct *dir)
6ca45943
JH
178{
179 int i;
453ec4bd
LT
180 for (i = 0; i < dir->nr; i++) {
181 struct dir_entry *ent = dir->entries[i];
6ca45943
JH
182 char *cp, *sp;
183 int pos, len, killed = 0;
184
185 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
186 sp = strchr(cp, '/');
187 if (!sp) {
188 /* If ent->name is prefix of an entry in the
189 * cache, it will be killed.
190 */
23d6236a 191 pos = index_name_pos(istate, ent->name, ent->len);
6ca45943 192 if (0 <= pos)
033abf97 193 BUG("killed-file %.*s not found",
ef1177d1 194 ent->len, ent->name);
6ca45943 195 pos = -pos - 1;
23d6236a
BW
196 while (pos < istate->cache_nr &&
197 ce_stage(istate->cache[pos]))
6ca45943 198 pos++; /* skip unmerged */
23d6236a 199 if (istate->cache_nr <= pos)
6ca45943
JH
200 break;
201 /* pos points at a name immediately after
202 * ent->name in the cache. Does it expect
203 * ent->name to be a directory?
204 */
23d6236a 205 len = ce_namelen(istate->cache[pos]);
6ca45943 206 if ((ent->len < len) &&
23d6236a 207 !strncmp(istate->cache[pos]->name,
6ca45943 208 ent->name, ent->len) &&
23d6236a 209 istate->cache[pos]->name[ent->len] == '/')
6ca45943
JH
210 killed = 1;
211 break;
212 }
23d6236a 213 if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
6ca45943
JH
214 /* If any of the leading directories in
215 * ent->name is registered in the cache,
216 * ent->name will be killed.
217 */
218 killed = 1;
219 break;
220 }
221 }
222 if (killed)
a52b321d 223 show_dir_entry(istate, tag_killed, dir->entries[i]);
6ca45943 224 }
8695c8bf
LT
225}
226
188dce13 227static void show_files(struct repository *repo, struct dir_struct *dir);
07c01b9f 228
188dce13
BW
229static void show_submodule(struct repository *superproject,
230 struct dir_struct *dir, const char *path)
e77aa336 231{
d5498e08 232 struct repository subrepo;
188dce13 233
8eb8dcf9 234 if (repo_submodule_init(&subrepo, superproject, path, null_oid()))
188dce13
BW
235 return;
236
d5498e08 237 if (repo_read_index(&subrepo) < 0)
188dce13
BW
238 die("index file corrupt");
239
d5498e08 240 show_files(&subrepo, dir);
188dce13 241
d5498e08 242 repo_clear(&subrepo);
e77aa336
BW
243}
244
4d28c4f7
ZH
245static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
246 const enum object_type type, unsigned int padded)
247{
248 if (type == OBJ_BLOB) {
249 unsigned long size;
250 if (oid_object_info(the_repository, oid, &size) < 0)
251 die(_("could not get object info about '%s'"),
252 oid_to_hex(oid));
253 if (padded)
254 strbuf_addf(line, "%7"PRIuMAX, (uintmax_t)size);
255 else
256 strbuf_addf(line, "%"PRIuMAX, (uintmax_t)size);
257 } else if (padded) {
258 strbuf_addf(line, "%7s", "-");
259 } else {
260 strbuf_addstr(line, "-");
261 }
262}
ce74de93
ZH
263
264static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
265 const char *format, const char *fullname) {
ce74de93
ZH
266 struct strbuf sb = STRBUF_INIT;
267
6f1e2d52
RS
268 while (strbuf_expand_step(&sb, &format)) {
269 const char *end;
270 size_t len;
271 struct stat st;
272
273 if (skip_prefix(format, "%", &format))
274 strbuf_addch(&sb, '%');
4416b86c 275 else if ((len = strbuf_expand_literal(&sb, format)))
6f1e2d52
RS
276 format += len;
277 else if (*format != '(')
278 die(_("bad ls-files format: element '%s' "
279 "does not start with '('"), format);
280 else if (!(end = strchr(format + 1, ')')))
281 die(_("bad ls-files format: element '%s' "
282 "does not end in ')'"), format);
283 else if (skip_prefix(format, "(objectmode)", &format))
284 strbuf_addf(&sb, "%06o", ce->ce_mode);
285 else if (skip_prefix(format, "(objectname)", &format))
286 strbuf_add_unique_abbrev(&sb, &ce->oid, abbrev);
287 else if (skip_prefix(format, "(objecttype)", &format))
288 strbuf_addstr(&sb, type_name(object_type(ce->ce_mode)));
289 else if (skip_prefix(format, "(objectsize:padded)", &format))
290 expand_objectsize(&sb, &ce->oid,
291 object_type(ce->ce_mode), 1);
292 else if (skip_prefix(format, "(objectsize)", &format))
293 expand_objectsize(&sb, &ce->oid,
294 object_type(ce->ce_mode), 0);
295 else if (skip_prefix(format, "(stage)", &format))
296 strbuf_addf(&sb, "%d", ce_stage(ce));
297 else if (skip_prefix(format, "(eolinfo:index)", &format))
298 strbuf_addstr(&sb, S_ISREG(ce->ce_mode) ?
299 get_cached_convert_stats_ascii(repo->index,
300 ce->name) : "");
301 else if (skip_prefix(format, "(eolinfo:worktree)", &format))
302 strbuf_addstr(&sb, !lstat(fullname, &st) &&
303 S_ISREG(st.st_mode) ?
304 get_wt_convert_stats_ascii(fullname) : "");
305 else if (skip_prefix(format, "(eolattr)", &format))
306 strbuf_addstr(&sb, get_convert_attr_ascii(repo->index,
307 fullname));
308 else if (skip_prefix(format, "(path)", &format))
309 write_name_to_buf(&sb, fullname);
310 else
311 die(_("bad ls-files format: %%%.*s"),
312 (int)(end - format + 1), format);
313 }
ce74de93
ZH
314 strbuf_addch(&sb, line_terminator);
315 fwrite(sb.buf, sb.len, 1, stdout);
316 strbuf_release(&sb);
317}
318
188dce13
BW
319static void show_ce(struct repository *repo, struct dir_struct *dir,
320 const struct cache_entry *ce, const char *fullname,
321 const char *tag)
5be4efbe 322{
188dce13 323 if (max_prefix_len > strlen(fullname))
7e44c935 324 die("git ls-files: internal error - cache entry not superset of prefix");
5be4efbe 325
75a6315f 326 if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
188dce13
BW
327 is_submodule_active(repo, ce->name)) {
328 show_submodule(repo, dir, ce->name);
a52b321d 329 } else if (match_pathspec(repo->index, &pathspec, fullname, strlen(fullname),
188dce13 330 max_prefix_len, ps_matched,
e77aa336
BW
331 S_ISDIR(ce->ce_mode) ||
332 S_ISGITLINK(ce->ce_mode))) {
ce74de93
ZH
333 if (format) {
334 show_ce_fmt(repo, ce, format, fullname);
335 print_debug(ce);
336 return;
337 }
338
a84f3e59 339 tag = get_tag(ce, tag);
5be4efbe 340
e77aa336
BW
341 if (!show_stage) {
342 fputs(tag, stdout);
343 } else {
344 printf("%s%06o %s %d\t",
345 tag,
346 ce->ce_mode,
290eada0 347 repo_find_unique_abbrev(repo, &ce->oid, abbrev),
e77aa336
BW
348 ce_stage(ce));
349 }
188dce13
BW
350 write_eolinfo(repo->index, ce, fullname);
351 write_name(fullname);
5306ccf9 352 print_debug(ce);
2bcab240 353 }
5be4efbe
LT
354}
355
847a9e5d 356static void show_ru_info(struct index_state *istate)
9d9a2f4a 357{
8a57c6e9
AR
358 struct string_list_item *item;
359
2d407e2d 360 if (!istate->resolve_undo)
9d9a2f4a 361 return;
8a57c6e9 362
2d407e2d 363 for_each_string_list_item(item, istate->resolve_undo) {
8a57c6e9
AR
364 const char *path = item->string;
365 struct resolve_undo_info *ui = item->util;
366 int i, len;
367
368 len = strlen(path);
369 if (len < max_prefix_len)
370 continue; /* outside of the prefix */
a52b321d 371 if (!match_pathspec(istate, &pathspec, path, len,
ae8d0824 372 max_prefix_len, ps_matched, 0))
8a57c6e9
AR
373 continue; /* uninterested */
374 for (i = 0; i < 3; i++) {
375 if (!ui->mode[i])
376 continue;
377 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
d850b7a5 378 repo_find_unique_abbrev(the_repository, &ui->oid[i], abbrev),
8a57c6e9 379 i + 1);
e9a820ce 380 write_name(path);
8a57c6e9
AR
381 }
382 }
9d9a2f4a
JH
383}
384
1d35e3bf 385static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
188dce13 386 const char *fullname, const struct cache_entry *ce)
782cd4c0
JH
387{
388 int dtype = ce_to_dtype(ce);
188dce13
BW
389 return is_excluded(dir, istate, fullname, &dtype);
390}
391
392static void construct_fullname(struct strbuf *out, const struct repository *repo,
393 const struct cache_entry *ce)
394{
395 strbuf_reset(out);
396 if (repo->submodule_prefix)
397 strbuf_addstr(out, repo->submodule_prefix);
398 strbuf_addstr(out, ce->name);
782cd4c0
JH
399}
400
188dce13 401static void show_files(struct repository *repo, struct dir_struct *dir)
8695c8bf
LT
402{
403 int i;
188dce13 404 struct strbuf fullname = STRBUF_INIT;
8695c8bf
LT
405
406 /* For cached/deleted files we don't need to even do the readdir */
6ca45943 407 if (show_others || show_killed) {
2eac2a4c
JH
408 if (!show_others)
409 dir->flags |= DIR_COLLECT_KILLED_ONLY;
188dce13 410 fill_directory(dir, repo->index, &pathspec);
6ca45943 411 if (show_others)
188dce13 412 show_other_files(repo->index, dir);
6ca45943 413 if (show_killed)
188dce13 414 show_killed_files(repo->index, dir);
8695c8bf 415 }
188dce13 416
ed644d16
ZH
417 if (!(show_cached || show_stage || show_deleted || show_modified))
418 return;
78087097
DS
419
420 if (!show_sparse_dirs)
421 ensure_full_index(repo->index);
422
ed644d16
ZH
423 for (i = 0; i < repo->index->cache_nr; i++) {
424 const struct cache_entry *ce = repo->index->cache[i];
425 struct stat st;
426 int stat_err;
188dce13 427
ed644d16
ZH
428 construct_fullname(&fullname, repo, ce);
429
430 if ((dir->flags & DIR_SHOW_IGNORED) &&
431 !ce_excluded(dir, repo->index, fullname.buf, ce))
432 continue;
433 if (ce->ce_flags & CE_UPDATE)
434 continue;
435 if ((show_cached || show_stage) &&
93a7d983 436 (!show_unmerged || ce_stage(ce))) {
188dce13
BW
437 show_ce(repo, dir, ce, fullname.buf,
438 ce_stage(ce) ? tag_unmerged :
439 (ce_skip_worktree(ce) ? tag_skip_worktree :
440 tag_cached));
93a7d983
ZH
441 if (skipping_duplicates)
442 goto skip_to_next_name;
443 }
188dce13 444
ed644d16
ZH
445 if (!(show_deleted || show_modified))
446 continue;
447 if (ce_skip_worktree(ce))
448 continue;
449 stat_err = lstat(fullname.buf, &st);
450 if (stat_err && (errno != ENOENT && errno != ENOTDIR))
451 error_errno("cannot lstat '%s'", fullname.buf);
93a7d983 452 if (stat_err && show_deleted) {
ed644d16 453 show_ce(repo, dir, ce, fullname.buf, tag_removed);
93a7d983
ZH
454 if (skipping_duplicates)
455 goto skip_to_next_name;
456 }
ed644d16 457 if (show_modified &&
93a7d983 458 (stat_err || ie_modified(repo->index, ce, &st, 0))) {
ed644d16 459 show_ce(repo, dir, ce, fullname.buf, tag_modified);
93a7d983
ZH
460 if (skipping_duplicates)
461 goto skip_to_next_name;
462 }
463 continue;
464
465skip_to_next_name:
466 {
467 int j;
468 struct cache_entry **cache = repo->index->cache;
469 for (j = i + 1; j < repo->index->cache_nr; j++)
470 if (strcmp(ce->name, cache[j]->name))
471 break;
472 i = j - 1; /* compensate for the for loop */
473 }
5be4efbe 474 }
188dce13
BW
475
476 strbuf_release(&fullname);
5be4efbe
LT
477}
478
479/*
480 * Prune the index to only contain stuff starting with "prefix"
481 */
6510ae17
BW
482static void prune_index(struct index_state *istate,
483 const char *prefix, size_t prefixlen)
5be4efbe 484{
7b4158a8 485 int pos;
5be4efbe
LT
486 unsigned int first, last;
487
168e6355 488 if (!prefix || !istate->cache_nr)
7b4158a8 489 return;
6510ae17 490 pos = index_name_pos(istate, prefix, prefixlen);
5be4efbe
LT
491 if (pos < 0)
492 pos = -pos-1;
96f6d3f6 493 first = pos;
6510ae17 494 last = istate->cache_nr;
5be4efbe 495 while (last > first) {
568a05c5 496 int next = first + ((last - first) >> 1);
6510ae17 497 const struct cache_entry *ce = istate->cache[next];
7b4158a8 498 if (!strncmp(ce->name, prefix, prefixlen)) {
5be4efbe
LT
499 first = next+1;
500 continue;
8695c8bf 501 }
5be4efbe
LT
502 last = next;
503 }
f331ab9d 504 MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos);
6510ae17 505 istate->cache_nr = last - pos;
5be4efbe
LT
506}
507
cbca060e
BW
508static int get_common_prefix_len(const char *common_prefix)
509{
510 int common_prefix_len;
511
512 if (!common_prefix)
513 return 0;
514
515 common_prefix_len = strlen(common_prefix);
516
517 /*
518 * If the prefix has a trailing slash, strip it so that submodules wont
519 * be pruned from the index.
520 */
521 if (common_prefix[common_prefix_len - 1] == '/')
522 common_prefix_len--;
523
524 return common_prefix_len;
525}
526
ce8e8804 527static const char * const ls_files_usage[] = {
9c9b4f2f 528 N_("git ls-files [<options>] [<file>...]"),
ce8e8804
MV
529 NULL
530};
531
ce8e8804
MV
532static int option_parse_exclude(const struct option *opt,
533 const char *arg, int unset)
534{
72aeb187 535 struct string_list *exclude_list = opt->value;
ce8e8804 536
517fe807
JK
537 BUG_ON_OPT_NEG(unset);
538
ce8e8804 539 exc_given = 1;
72aeb187 540 string_list_append(exclude_list, arg);
ce8e8804
MV
541
542 return 0;
543}
544
545static int option_parse_exclude_from(const struct option *opt,
546 const char *arg, int unset)
547{
548 struct dir_struct *dir = opt->value;
549
517fe807
JK
550 BUG_ON_OPT_NEG(unset);
551
ce8e8804 552 exc_given = 1;
65edd96a 553 add_patterns_from_file(dir, arg);
ce8e8804
MV
554
555 return 0;
556}
557
558static int option_parse_exclude_standard(const struct option *opt,
559 const char *arg, int unset)
560{
561 struct dir_struct *dir = opt->value;
562
517fe807
JK
563 BUG_ON_OPT_NEG(unset);
564 BUG_ON_OPT_ARG(arg);
565
ce8e8804
MV
566 exc_given = 1;
567 setup_standard_excludes(dir);
568
569 return 0;
570}
cf9a113d 571
efad1a56 572int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
8695c8bf 573{
72aeb187 574 int require_work_tree = 0, show_tag = 0, i;
4c217a4c 575 char *max_prefix;
ce93a4c6 576 struct dir_struct dir = DIR_INIT;
caa3d554 577 struct pattern_list *pl;
72aeb187 578 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
ce8e8804 579 struct option builtin_ls_files_options[] = {
1f3c79a9
JK
580 /* Think twice before adding "--nul" synonym to this */
581 OPT_SET_INT('z', NULL, &line_terminator,
54b4d125 582 N_("separate paths with the NUL character"), '\0'),
d5d09d47 583 OPT_BOOL('t', NULL, &show_tag,
377adc3a 584 N_("identify the file status with tags")),
d5d09d47 585 OPT_BOOL('v', NULL, &show_valid_bit,
377adc3a 586 N_("use lowercase letters for 'assume unchanged' files")),
d8c71db8
BP
587 OPT_BOOL('f', NULL, &show_fsmonitor_bit,
588 N_("use lowercase letters for 'fsmonitor clean' files")),
d5d09d47 589 OPT_BOOL('c', "cached", &show_cached,
377adc3a 590 N_("show cached files in the output (default)")),
d5d09d47 591 OPT_BOOL('d', "deleted", &show_deleted,
377adc3a 592 N_("show deleted files in the output")),
d5d09d47 593 OPT_BOOL('m', "modified", &show_modified,
377adc3a 594 N_("show modified files in the output")),
d5d09d47 595 OPT_BOOL('o', "others", &show_others,
377adc3a 596 N_("show other files in the output")),
ce8e8804 597 OPT_BIT('i', "ignored", &dir.flags,
377adc3a 598 N_("show ignored files in the output"),
ce8e8804 599 DIR_SHOW_IGNORED),
d5d09d47 600 OPT_BOOL('s', "stage", &show_stage,
377adc3a 601 N_("show staged contents' object name in the output")),
d5d09d47 602 OPT_BOOL('k', "killed", &show_killed,
377adc3a 603 N_("show files on the filesystem that need to be removed")),
ce8e8804 604 OPT_BIT(0, "directory", &dir.flags,
ad5fe377 605 N_("show 'other' directories' names only"),
ce8e8804 606 DIR_SHOW_OTHER_DIRECTORIES),
a7630bd4 607 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
e9008b9a 608 OPT_NEGBIT(0, "empty-directory", &dir.flags,
377adc3a 609 N_("don't show empty directories"),
ce8e8804 610 DIR_HIDE_EMPTY_DIRECTORIES),
d5d09d47 611 OPT_BOOL('u', "unmerged", &show_unmerged,
377adc3a 612 N_("show unmerged files in the output")),
d5d09d47 613 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
377adc3a 614 N_("show resolve-undo information")),
203c8533 615 OPT_CALLBACK_F('x', "exclude", &exclude_list, N_("pattern"),
377adc3a 616 N_("skip files matching pattern"),
203c8533
DL
617 PARSE_OPT_NONEG, option_parse_exclude),
618 OPT_CALLBACK_F('X', "exclude-from", &dir, N_("file"),
54b4d125 619 N_("read exclude patterns from <file>"),
203c8533 620 PARSE_OPT_NONEG, option_parse_exclude_from),
377adc3a
NTND
621 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
622 N_("read additional per-directory exclude patterns in <file>")),
203c8533 623 OPT_CALLBACK_F(0, "exclude-standard", &dir, NULL,
377adc3a 624 N_("add the standard git exclusions"),
ccf659e8 625 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
203c8533 626 option_parse_exclude_standard),
3e4a67b4
NTND
627 OPT_SET_INT_F(0, "full-name", &prefix_len,
628 N_("make the output relative to the project top directory"),
629 0, PARSE_OPT_NONEG),
e77aa336
BW
630 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
631 N_("recurse through submodules")),
d5d09d47 632 OPT_BOOL(0, "error-unmatch", &error_unmatch,
377adc3a
NTND
633 N_("if any <file> is not in the index, treat this as an error")),
634 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
635 N_("pretend that paths removed since <tree-ish> are still present")),
ce8e8804 636 OPT__ABBREV(&abbrev),
d5d09d47 637 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
93a7d983
ZH
638 OPT_BOOL(0, "deduplicate", &skipping_duplicates,
639 N_("suppress duplicate entries")),
78087097
DS
640 OPT_BOOL(0, "sparse", &show_sparse_dirs,
641 N_("show sparse directories in the presence of a sparse index")),
ce74de93
ZH
642 OPT_STRING_F(0, "format", &format, N_("format"),
643 N_("format to use for the output"),
644 PARSE_OPT_NONEG),
ce8e8804
MV
645 OPT_END()
646 };
eab4ac6a 647 int ret = 0;
8695c8bf 648
cbb3167e
NTND
649 if (argc == 2 && !strcmp(argv[1], "-h"))
650 usage_with_options(ls_files_usage, builtin_ls_files_options);
651
78087097
DS
652 prepare_repo_settings(the_repository);
653 the_repository->settings.command_requires_full_index = 0;
654
efad1a56 655 prefix = cmd_prefix;
5be4efbe 656 if (prefix)
efad1a56 657 prefix_len = strlen(prefix);
ef90d6d4 658 git_config(git_default_config, NULL);
5be4efbe 659
188dce13 660 if (repo_read_index(the_repository) < 0)
c28b3d6e
NTND
661 die("index file corrupt");
662
37782920 663 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
ce8e8804 664 ls_files_usage, 0);
65edd96a 665 pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
72aeb187 666 for (i = 0; i < exclude_list.nr; i++) {
65edd96a 667 add_pattern(exclude_list.items[i].string, "", 0, pl, --exclude_args);
72aeb187 668 }
ce74de93
ZH
669
670 if (format && (show_stage || show_others || show_killed ||
671 show_resolve_undo || skipping_duplicates || show_eol || show_tag))
672 usage_msg_opt(_("--format cannot be used with -s, -o, -k, -t, "
673 "--resolve-undo, --deduplicate, --eol"),
674 ls_files_usage, builtin_ls_files_options);
675
d8c71db8 676 if (show_tag || show_valid_bit || show_fsmonitor_bit) {
ce8e8804
MV
677 tag_cached = "H ";
678 tag_unmerged = "M ";
679 tag_removed = "R ";
680 tag_modified = "C ";
681 tag_other = "? ";
682 tag_killed = "K ";
44a36913 683 tag_skip_worktree = "S ";
9d9a2f4a 684 tag_resolve_undo = "U ";
9ff768e9 685 }
de2e3b04 686 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
ce8e8804
MV
687 require_work_tree = 1;
688 if (show_unmerged)
689 /*
690 * There's no point in showing unmerged unless
691 * you also show the stage information.
692 */
693 show_stage = 1;
93a7d983
ZH
694 if (show_tag || show_stage)
695 skipping_duplicates = 0;
ce8e8804
MV
696 if (dir.exclude_per_dir)
697 exc_given = 1;
9ff768e9 698
7d8ae932
MH
699 if (require_work_tree && !is_inside_work_tree())
700 setup_work_tree();
6d9ba67b 701
e77aa336 702 if (recurse_submodules &&
290eada0 703 (show_deleted || show_others || show_unmerged ||
07c01b9f 704 show_killed || show_modified || show_resolve_undo || with_tree))
e77aa336
BW
705 die("ls-files --recurse-submodules unsupported mode");
706
707 if (recurse_submodules && error_unmatch)
708 die("ls-files --recurse-submodules does not support "
709 "--error-unmatch");
710
9e06d6ed 711 parse_pathspec(&pathspec, 0,
cbca060e 712 PATHSPEC_PREFER_CWD,
9e06d6ed 713 prefix, argv);
f3670a57 714
75a6315f
BW
715 /*
716 * Find common prefix for all pathspec's
717 * This is used as a performance optimization which unfortunately cannot
188dce13
BW
718 * be done when recursing into submodules because when a pathspec is
719 * given which spans repository boundaries you can't simply remove the
720 * submodule entry because the pathspec may match something inside the
721 * submodule.
75a6315f
BW
722 */
723 if (recurse_submodules)
724 max_prefix = NULL;
725 else
726 max_prefix = common_prefix(&pathspec);
cbca060e
BW
727 max_prefix_len = get_common_prefix_len(max_prefix);
728
188dce13 729 prune_index(the_repository->index, max_prefix, max_prefix_len);
5be4efbe 730
bba319b5 731 /* Treat unmatching pathspec elements as errors */
9e06d6ed 732 if (pathspec.nr && error_unmatch)
8b54c234 733 ps_matched = xcalloc(pathspec.nr, 1);
bba319b5 734
b338e9f6
EN
735 if ((dir.flags & DIR_SHOW_IGNORED) && !show_others && !show_cached)
736 die("ls-files -i must be used with either -o or -c");
737
ac78b009
BW
738 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
739 die("ls-files --ignored needs some exclude pattern");
8695c8bf
LT
740
741 /* With no flags, we default to showing the cached files */
0b437a18
RS
742 if (!(show_stage || show_deleted || show_others || show_unmerged ||
743 show_killed || show_modified || show_resolve_undo))
8695c8bf
LT
744 show_cached = 1;
745
64586e75
JH
746 if (with_tree) {
747 /*
748 * Basic sanity check; show-stages and show-unmerged
749 * would not make any sense with this option.
750 */
751 if (show_stage || show_unmerged)
12909b6b 752 die(_("options '%s' and '%s' cannot be used together"), "ls-files --with-tree", "-s/-u");
188dce13 753 overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
64586e75 754 }
188dce13
BW
755
756 show_files(the_repository, &dir);
757
9d9a2f4a 758 if (show_resolve_undo)
188dce13 759 show_ru_info(the_repository->index);
bba319b5 760
eab4ac6a
ÆAB
761 if (ps_matched && report_path_error(ps_matched, &pathspec)) {
762 fprintf(stderr, "Did you forget to 'git add'?\n");
763 ret = 1;
bba319b5
JH
764 }
765
272f0a57 766 string_list_clear(&exclude_list, 0);
eceba532 767 dir_clear(&dir);
4c217a4c 768 free(max_prefix);
eab4ac6a 769 return ret;
8695c8bf 770}