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