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