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