blob: 4cf8a2364835c9fc1ae39b8d52fd09a8aeeb368e [file] [log] [blame]
Linus Torvalds8695c8b2005-04-11 18:55:38 -07001/*
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 */
Linus Torvalds8695c8b2005-04-11 18:55:38 -07008#include "cache.h"
Brandon Williams188dce12017-06-22 11:43:48 -07009#include "repository.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070010#include "config.h"
Junio C Hamano22ddf712005-10-14 21:56:46 -070011#include "quote.h"
Linus Torvalds453ec4b2006-05-16 19:02:14 -070012#include "dir.h"
Peter Eriksen0864f262006-05-23 14:15:29 +020013#include "builtin.h"
ZheNing Huce74de92022-07-23 06:44:45 +000014#include "strbuf.h"
Junio C Hamano64586e72007-09-12 16:04:22 -070015#include "tree.h"
Ævar Arnfjörð Bjarmasoneefadd12021-03-20 23:37:46 +010016#include "cache-tree.h"
Miklos Vajnace8e8802009-02-17 15:27:11 +010017#include "parse-options.h"
Junio C Hamano9d9a2f42009-12-25 10:08:04 -080018#include "resolve-undo.h"
19#include "string-list.h"
Nguyễn Thái Ngọc Duy64acde92013-07-14 15:35:25 +070020#include "pathspec.h"
Brandon Williamse77aa332016-10-07 11:18:49 -070021#include "run-command.h"
Jacob Keller2e5d6502017-04-13 10:12:23 -070022#include "submodule.h"
Brandon Williams1b796ac2017-08-03 11:19:57 -070023#include "submodule-config.h"
Linus Torvalds8695c8b2005-04-11 18:55:38 -070024
David Rientjes96f1e582006-08-15 10:23:48 -070025static int abbrev;
26static int show_deleted;
27static int show_cached;
28static int show_others;
29static int show_stage;
30static int show_unmerged;
Junio C Hamano9d9a2f42009-12-25 10:08:04 -080031static int show_resolve_undo;
David Rientjes96f1e582006-08-15 10:23:48 -070032static int show_modified;
33static int show_killed;
34static int show_valid_bit;
Ben Peartd8c71db2017-09-22 12:35:42 -040035static int show_fsmonitor_bit;
Junio C Hamanob83c8342005-04-15 11:11:01 -070036static int line_terminator = '\n';
Thomas Rast84974212010-07-31 00:35:59 +020037static int debug_mode;
Torsten Bögershausena7630bd2016-01-16 07:50:02 +010038static int show_eol;
Brandon Williamse77aa332016-10-07 11:18:49 -070039static int recurse_submodules;
ZheNing Hu93a7d982021-01-23 10:20:10 +000040static int skipping_duplicates;
Derrick Stolee78087092021-12-22 14:20:53 +000041static int show_sparse_dirs;
Linus Torvalds8695c8b2005-04-11 18:55:38 -070042
Clemens Buchacherefad1a52010-06-03 15:39:18 +020043static const char *prefix;
44static int max_prefix_len;
David Rientjes96f1e582006-08-15 10:23:48 -070045static int prefix_len;
Nguyễn Thái Ngọc Duy9e06d6e2013-07-14 15:35:43 +070046static struct pathspec pathspec;
David Rientjes96f1e582006-08-15 10:23:48 -070047static int error_unmatch;
48static char *ps_matched;
Junio C Hamano64586e72007-09-12 16:04:22 -070049static const char *with_tree;
Miklos Vajnace8e8802009-02-17 15:27:11 +010050static int exc_given;
Adam Spiersc04318e2013-01-06 16:58:04 +000051static int exclude_args;
ZheNing Huce74de92022-07-23 06:44:45 +000052static const char *format;
Linus Torvalds5be4efb2005-08-21 12:55:33 -070053
Petr Baudis20d37ef2005-04-21 19:47:08 -070054static const char *tag_cached = "";
55static const char *tag_unmerged = "";
56static const char *tag_removed = "";
57static const char *tag_other = "";
Junio C Hamano6ca45942005-05-12 17:17:54 -070058static const char *tag_killed = "";
Junio C Hamanob0391892005-09-19 15:11:15 -070059static const char *tag_modified = "";
Nguyễn Thái Ngọc Duy44a36912009-08-20 20:46:57 +070060static const char *tag_skip_worktree = "";
Junio C Hamano9d9a2f42009-12-25 10:08:04 -080061static const char *tag_resolve_undo = "";
Petr Baudis20d37ef2005-04-21 19:47:08 -070062
Derrick Stolee847a9e52021-04-01 01:49:39 +000063static void write_eolinfo(struct index_state *istate,
Brandon Williams1985fd62017-06-12 15:13:59 -070064 const struct cache_entry *ce, const char *path)
Torsten Bögershausena7630bd2016-01-16 07:50:02 +010065{
Brandon Williams1985fd62017-06-12 15:13:59 -070066 if (show_eol) {
Torsten Bögershausena7630bd2016-01-16 07:50:02 +010067 struct stat st;
68 const char *i_txt = "";
69 const char *w_txt = "";
Nguyễn Thái Ngọc Duya52b3212018-08-13 18:14:24 +020070 const char *a_txt = get_convert_attr_ascii(istate, path);
Torsten Bögershausena7630bd2016-01-16 07:50:02 +010071 if (ce && S_ISREG(ce->ce_mode))
Brandon Williams1985fd62017-06-12 15:13:59 -070072 i_txt = get_cached_convert_stats_ascii(istate,
Brandon Williamsa7609c52017-06-12 15:13:52 -070073 ce->name);
Torsten Bögershausena7630bd2016-01-16 07:50:02 +010074 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
Jiang Xine9a820c2013-06-25 23:53:46 +080080static void write_name(const char *name)
Clemens Buchacherefad1a52010-06-03 15:39:18 +020081{
Jiang Xinad66df22013-06-25 23:53:44 +080082 /*
Jiang Xine9a820c2013-06-25 23:53:46 +080083 * With "--full-name", prefix_len=0; this caller needs to pass
84 * an empty string in that case (a NULL is good for "").
Jiang Xinad66df22013-06-25 23:53:44 +080085 */
Jiang Xine9a820c2013-06-25 23:53:46 +080086 write_name_quoted_relative(name, prefix_len ? prefix : NULL,
87 stdout, line_terminator);
Clemens Buchacherefad1a52010-06-03 15:39:18 +020088}
89
ZheNing Huce74de92022-07-23 06:44:45 +000090static 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
Brandon Williamsa84f3e52017-06-12 15:14:08 -0700100static const char *get_tag(const struct cache_entry *ce, const char *tag)
101{
102 static char alttag[4];
103
Ben Peartd8c71db2017-09-22 12:35:42 -0400104 if (tag && *tag && ((show_valid_bit && (ce->ce_flags & CE_VALID)) ||
105 (show_fsmonitor_bit && (ce->ce_flags & CE_FSMONITOR_VALID)))) {
Brandon Williamsa84f3e52017-06-12 15:14:08 -0700106 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
Brandon Williams5306ccf2017-06-12 15:14:07 -0700125static void print_debug(const struct cache_entry *ce)
126{
127 if (debug_mode) {
128 const struct stat_data *sd = &ce->ce_stat_data;
129
Thomas Gummerer7cb72832019-04-07 19:47:51 +0100130 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);
Brandon Williams5306ccf2017-06-12 15:14:07 -0700135 }
136}
137
Derrick Stolee847a9e52021-04-01 01:49:39 +0000138static void show_dir_entry(struct index_state *istate,
Nguyễn Thái Ngọc Duya52b3212018-08-13 18:14:24 +0200139 const char *tag, struct dir_entry *ent)
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700140{
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200141 int len = max_prefix_len;
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700142
Brandon Williamscbca0602017-05-11 15:04:25 -0700143 if (len > ent->len)
Junio C Hamano7e44c932008-08-31 09:39:19 -0700144 die("git ls-files: internal error - directory entry not superset of prefix");
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700145
Elijah Newren95c11ec2020-04-01 04:17:45 +0000146 /* 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);
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700149
Junio C Hamano22ddf712005-10-14 21:56:46 -0700150 fputs(tag, stdout);
Nguyễn Thái Ngọc Duya52b3212018-08-13 18:14:24 +0200151 write_eolinfo(istate, NULL, ent->name);
Jiang Xine9a820c2013-06-25 23:53:46 +0800152 write_name(ent->name);
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700153}
154
Derrick Stolee847a9e52021-04-01 01:49:39 +0000155static void show_other_files(struct index_state *istate,
Brandon Williams23d68462017-06-12 15:14:01 -0700156 const struct dir_struct *dir)
Junio C Hamanofcbc3082005-11-06 17:26:31 -0800157{
158 int i;
Linus Torvalds56984542007-04-14 16:22:08 -0700159
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700160 for (i = 0; i < dir->nr; i++) {
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700161 struct dir_entry *ent = dir->entries[i];
Brandon Williams23d68462017-06-12 15:14:01 -0700162 if (!index_name_is_other(istate, ent->name, ent->len))
Jeff King98fa4732008-10-16 11:07:26 -0400163 continue;
Nguyễn Thái Ngọc Duya52b3212018-08-13 18:14:24 +0200164 show_dir_entry(istate, tag_other, ent);
Junio C Hamanofcbc3082005-11-06 17:26:31 -0800165 }
166}
167
Derrick Stolee847a9e52021-04-01 01:49:39 +0000168static void show_killed_files(struct index_state *istate,
Brandon Williams23d62362017-06-12 15:14:00 -0700169 const struct dir_struct *dir)
Junio C Hamano6ca45942005-05-12 17:17:54 -0700170{
171 int i;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700172 for (i = 0; i < dir->nr; i++) {
173 struct dir_entry *ent = dir->entries[i];
Junio C Hamano6ca45942005-05-12 17:17:54 -0700174 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 */
Brandon Williams23d62362017-06-12 15:14:00 -0700183 pos = index_name_pos(istate, ent->name, ent->len);
Junio C Hamano6ca45942005-05-12 17:17:54 -0700184 if (0 <= pos)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200185 BUG("killed-file %.*s not found",
Johannes Schindelinef1177d12016-07-26 18:05:50 +0200186 ent->len, ent->name);
Junio C Hamano6ca45942005-05-12 17:17:54 -0700187 pos = -pos - 1;
Brandon Williams23d62362017-06-12 15:14:00 -0700188 while (pos < istate->cache_nr &&
189 ce_stage(istate->cache[pos]))
Junio C Hamano6ca45942005-05-12 17:17:54 -0700190 pos++; /* skip unmerged */
Brandon Williams23d62362017-06-12 15:14:00 -0700191 if (istate->cache_nr <= pos)
Junio C Hamano6ca45942005-05-12 17:17:54 -0700192 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 */
Brandon Williams23d62362017-06-12 15:14:00 -0700197 len = ce_namelen(istate->cache[pos]);
Junio C Hamano6ca45942005-05-12 17:17:54 -0700198 if ((ent->len < len) &&
Brandon Williams23d62362017-06-12 15:14:00 -0700199 !strncmp(istate->cache[pos]->name,
Junio C Hamano6ca45942005-05-12 17:17:54 -0700200 ent->name, ent->len) &&
Brandon Williams23d62362017-06-12 15:14:00 -0700201 istate->cache[pos]->name[ent->len] == '/')
Junio C Hamano6ca45942005-05-12 17:17:54 -0700202 killed = 1;
203 break;
204 }
Brandon Williams23d62362017-06-12 15:14:00 -0700205 if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
Junio C Hamano6ca45942005-05-12 17:17:54 -0700206 /* 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)
Nguyễn Thái Ngọc Duya52b3212018-08-13 18:14:24 +0200215 show_dir_entry(istate, tag_killed, dir->entries[i]);
Junio C Hamano6ca45942005-05-12 17:17:54 -0700216 }
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700217}
218
Brandon Williams188dce12017-06-22 11:43:48 -0700219static void show_files(struct repository *repo, struct dir_struct *dir);
Brandon Williamsb2dfeb72017-03-17 10:22:57 -0700220
Brandon Williams188dce12017-06-22 11:43:48 -0700221static void show_submodule(struct repository *superproject,
222 struct dir_struct *dir, const char *path)
223{
Stefan Bellerd5498e02018-11-28 16:27:53 -0800224 struct repository subrepo;
Brandon Williams188dce12017-06-22 11:43:48 -0700225
Jonathan Tan8eb8dcf2021-09-09 11:47:28 -0700226 if (repo_submodule_init(&subrepo, superproject, path, null_oid()))
Brandon Williams188dce12017-06-22 11:43:48 -0700227 return;
228
Stefan Bellerd5498e02018-11-28 16:27:53 -0800229 if (repo_read_index(&subrepo) < 0)
Brandon Williams188dce12017-06-22 11:43:48 -0700230 die("index file corrupt");
231
Stefan Bellerd5498e02018-11-28 16:27:53 -0800232 show_files(&subrepo, dir);
Brandon Williams188dce12017-06-22 11:43:48 -0700233
Stefan Bellerd5498e02018-11-28 16:27:53 -0800234 repo_clear(&subrepo);
Brandon Williams07c01b92016-10-07 11:18:50 -0700235}
236
ZheNing Huce74de92022-07-23 06:44:45 +0000237struct 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)
ZheNing Hu746aae32022-09-11 14:03:17 +0000260 die(_("bad ls-files format: element '%s' "
ZheNing Huce74de92022-07-23 06:44:45 +0000261 "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
Brandon Williams188dce12017-06-22 11:43:48 -0700304static void show_ce(struct repository *repo, struct dir_struct *dir,
305 const struct cache_entry *ce, const char *fullname,
306 const char *tag)
Brandon Williamse77aa332016-10-07 11:18:49 -0700307{
Brandon Williams188dce12017-06-22 11:43:48 -0700308 if (max_prefix_len > strlen(fullname))
Junio C Hamano7e44c932008-08-31 09:39:19 -0700309 die("git ls-files: internal error - cache entry not superset of prefix");
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700310
Brandon Williams75a63152016-10-07 11:18:51 -0700311 if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
Brandon Williams188dce12017-06-22 11:43:48 -0700312 is_submodule_active(repo, ce->name)) {
313 show_submodule(repo, dir, ce->name);
Nguyễn Thái Ngọc Duya52b3212018-08-13 18:14:24 +0200314 } else if (match_pathspec(repo->index, &pathspec, fullname, strlen(fullname),
Brandon Williams188dce12017-06-22 11:43:48 -0700315 max_prefix_len, ps_matched,
Brandon Williamse77aa332016-10-07 11:18:49 -0700316 S_ISDIR(ce->ce_mode) ||
317 S_ISGITLINK(ce->ce_mode))) {
ZheNing Huce74de92022-07-23 06:44:45 +0000318 if (format) {
319 show_ce_fmt(repo, ce, format, fullname);
320 print_debug(ce);
321 return;
322 }
323
Brandon Williamsa84f3e52017-06-12 15:14:08 -0700324 tag = get_tag(ce, tag);
Brandon Williamse77aa332016-10-07 11:18:49 -0700325
326 if (!show_stage) {
327 fputs(tag, stdout);
328 } else {
329 printf("%s%06o %s %d\t",
330 tag,
331 ce->ce_mode,
Jonathan Tan290eada2022-02-23 16:23:03 -0800332 repo_find_unique_abbrev(repo, &ce->oid, abbrev),
Brandon Williamse77aa332016-10-07 11:18:49 -0700333 ce_stage(ce));
334 }
Brandon Williams188dce12017-06-22 11:43:48 -0700335 write_eolinfo(repo->index, ce, fullname);
336 write_name(fullname);
Brandon Williams5306ccf2017-06-12 15:14:07 -0700337 print_debug(ce);
Junio C Hamano2bcab242006-02-08 21:50:18 -0800338 }
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700339}
340
Derrick Stolee847a9e52021-04-01 01:49:39 +0000341static void show_ru_info(struct index_state *istate)
Junio C Hamano9d9a2f42009-12-25 10:08:04 -0800342{
Alex Riesen8a57c6e2010-07-03 14:41:54 +0200343 struct string_list_item *item;
344
Brandon Williams2d407e22017-06-12 15:14:02 -0700345 if (!istate->resolve_undo)
Junio C Hamano9d9a2f42009-12-25 10:08:04 -0800346 return;
Alex Riesen8a57c6e2010-07-03 14:41:54 +0200347
Brandon Williams2d407e22017-06-12 15:14:02 -0700348 for_each_string_list_item(item, istate->resolve_undo) {
Alex Riesen8a57c6e2010-07-03 14:41:54 +0200349 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 */
Nguyễn Thái Ngọc Duya52b3212018-08-13 18:14:24 +0200356 if (!match_pathspec(istate, &pathspec, path, len,
Nguyễn Thái Ngọc Duyae8d0822014-01-24 20:40:33 +0700357 max_prefix_len, ps_matched, 0))
Alex Riesen8a57c6e2010-07-03 14:41:54 +0200358 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],
brian m. carlsonaab95832018-03-12 02:27:30 +0000363 find_unique_abbrev(&ui->oid[i], abbrev),
Alex Riesen8a57c6e2010-07-03 14:41:54 +0200364 i + 1);
Jiang Xine9a820c2013-06-25 23:53:46 +0800365 write_name(path);
Alex Riesen8a57c6e2010-07-03 14:41:54 +0200366 }
367 }
Junio C Hamano9d9a2f42009-12-25 10:08:04 -0800368}
369
Brandon Williams1d35e3b2017-06-12 15:14:03 -0700370static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
Brandon Williams188dce12017-06-22 11:43:48 -0700371 const char *fullname, const struct cache_entry *ce)
Junio C Hamano782cd4c2012-06-05 21:17:52 -0700372{
373 int dtype = ce_to_dtype(ce);
Brandon Williams188dce12017-06-22 11:43:48 -0700374 return is_excluded(dir, istate, fullname, &dtype);
Junio C Hamano782cd4c2012-06-05 21:17:52 -0700375}
376
Brandon Williams188dce12017-06-22 11:43:48 -0700377static 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);
384}
385
386static void show_files(struct repository *repo, struct dir_struct *dir)
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700387{
388 int i;
Brandon Williams188dce12017-06-22 11:43:48 -0700389 struct strbuf fullname = STRBUF_INIT;
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700390
391 /* For cached/deleted files we don't need to even do the readdir */
Junio C Hamano6ca45942005-05-12 17:17:54 -0700392 if (show_others || show_killed) {
Junio C Hamano2eac2a42013-08-15 12:13:46 -0700393 if (!show_others)
394 dir->flags |= DIR_COLLECT_KILLED_ONLY;
Brandon Williams188dce12017-06-22 11:43:48 -0700395 fill_directory(dir, repo->index, &pathspec);
Junio C Hamano6ca45942005-05-12 17:17:54 -0700396 if (show_others)
Brandon Williams188dce12017-06-22 11:43:48 -0700397 show_other_files(repo->index, dir);
Junio C Hamano6ca45942005-05-12 17:17:54 -0700398 if (show_killed)
Brandon Williams188dce12017-06-22 11:43:48 -0700399 show_killed_files(repo->index, dir);
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700400 }
Brandon Williams188dce12017-06-22 11:43:48 -0700401
ZheNing Hued644d12021-01-23 10:20:09 +0000402 if (!(show_cached || show_stage || show_deleted || show_modified))
403 return;
Derrick Stolee78087092021-12-22 14:20:53 +0000404
405 if (!show_sparse_dirs)
406 ensure_full_index(repo->index);
407
ZheNing Hued644d12021-01-23 10:20:09 +0000408 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;
Brandon Williams188dce12017-06-22 11:43:48 -0700412
ZheNing Hued644d12021-01-23 10:20:09 +0000413 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) &&
ZheNing Hu93a7d982021-01-23 10:20:10 +0000421 (!show_unmerged || ce_stage(ce))) {
Brandon Williams188dce12017-06-22 11:43:48 -0700422 show_ce(repo, dir, ce, fullname.buf,
423 ce_stage(ce) ? tag_unmerged :
424 (ce_skip_worktree(ce) ? tag_skip_worktree :
425 tag_cached));
ZheNing Hu93a7d982021-01-23 10:20:10 +0000426 if (skipping_duplicates)
427 goto skip_to_next_name;
428 }
Brandon Williams188dce12017-06-22 11:43:48 -0700429
ZheNing Hued644d12021-01-23 10:20:09 +0000430 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);
ZheNing Hu93a7d982021-01-23 10:20:10 +0000437 if (stat_err && show_deleted) {
ZheNing Hued644d12021-01-23 10:20:09 +0000438 show_ce(repo, dir, ce, fullname.buf, tag_removed);
ZheNing Hu93a7d982021-01-23 10:20:10 +0000439 if (skipping_duplicates)
440 goto skip_to_next_name;
441 }
ZheNing Hued644d12021-01-23 10:20:09 +0000442 if (show_modified &&
ZheNing Hu93a7d982021-01-23 10:20:10 +0000443 (stat_err || ie_modified(repo->index, ce, &st, 0))) {
ZheNing Hued644d12021-01-23 10:20:09 +0000444 show_ce(repo, dir, ce, fullname.buf, tag_modified);
ZheNing Hu93a7d982021-01-23 10:20:10 +0000445 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 }
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700459 }
Brandon Williams188dce12017-06-22 11:43:48 -0700460
461 strbuf_release(&fullname);
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700462}
463
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700464/*
465 * Prune the index to only contain stuff starting with "prefix"
466 */
Brandon Williams6510ae12017-06-12 15:14:04 -0700467static void prune_index(struct index_state *istate,
468 const char *prefix, size_t prefixlen)
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700469{
René Scharfe7b4158a2017-02-10 20:42:28 +0100470 int pos;
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700471 unsigned int first, last;
472
René Scharfe168e6352017-07-16 13:16:01 +0200473 if (!prefix || !istate->cache_nr)
René Scharfe7b4158a2017-02-10 20:42:28 +0100474 return;
Brandon Williams6510ae12017-06-12 15:14:04 -0700475 pos = index_name_pos(istate, prefix, prefixlen);
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700476 if (pos < 0)
477 pos = -pos-1;
René Scharfe96f6d3f2017-02-10 21:03:30 +0100478 first = pos;
Brandon Williams6510ae12017-06-12 15:14:04 -0700479 last = istate->cache_nr;
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700480 while (last > first) {
René Scharfe568a05c2019-06-13 19:51:56 +0200481 int next = first + ((last - first) >> 1);
Brandon Williams6510ae12017-06-12 15:14:04 -0700482 const struct cache_entry *ce = istate->cache[next];
René Scharfe7b4158a2017-02-10 20:42:28 +0100483 if (!strncmp(ce->name, prefix, prefixlen)) {
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700484 first = next+1;
485 continue;
486 }
487 last = next;
488 }
René Scharfef331ab92017-07-15 22:00:45 +0200489 MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos);
Brandon Williams6510ae12017-06-12 15:14:04 -0700490 istate->cache_nr = last - pos;
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700491}
492
Brandon Williamscbca0602017-05-11 15:04:25 -0700493static 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
Ævar Arnfjörð Bjarmasoneefadd12021-03-20 23:37:46 +0100512static int read_one_entry_opt(struct index_state *istate,
513 const struct object_id *oid,
Ævar Arnfjörð Bjarmason9614ad32021-03-20 23:37:48 +0100514 struct strbuf *base,
Ævar Arnfjörð Bjarmasoneefadd12021-03-20 23:37:46 +0100515 const char *pathname,
Ævar Arnfjörð Bjarmasonfcc7c122021-03-20 23:37:47 +0100516 unsigned mode, int opt)
Ævar Arnfjörð Bjarmasoneefadd12021-03-20 23:37:46 +0100517{
518 int len;
519 struct cache_entry *ce;
520
521 if (S_ISDIR(mode))
522 return READ_TREE_RECURSIVE;
523
524 len = strlen(pathname);
Ævar Arnfjörð Bjarmason9614ad32021-03-20 23:37:48 +0100525 ce = make_empty_cache_entry(istate, base->len + len);
Ævar Arnfjörð Bjarmasoneefadd12021-03-20 23:37:46 +0100526
527 ce->ce_mode = create_ce_mode(mode);
Ævar Arnfjörð Bjarmasonfcc7c122021-03-20 23:37:47 +0100528 ce->ce_flags = create_ce_flags(1);
Ævar Arnfjörð Bjarmason9614ad32021-03-20 23:37:48 +0100529 ce->ce_namelen = base->len + len;
530 memcpy(ce->name, base->buf, base->len);
531 memcpy(ce->name + base->len, pathname, len+1);
Ævar Arnfjörð Bjarmasoneefadd12021-03-20 23:37:46 +0100532 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,
Ævar Arnfjörð Bjarmason47957482021-03-20 23:37:51 +0100537 const char *pathname, unsigned mode,
Ævar Arnfjörð Bjarmasoneefadd12021-03-20 23:37:46 +0100538 void *context)
539{
540 struct index_state *istate = context;
Ævar Arnfjörð Bjarmason9614ad32021-03-20 23:37:48 +0100541 return read_one_entry_opt(istate, oid, base, pathname,
Ævar Arnfjörð Bjarmasonfcc7c122021-03-20 23:37:47 +0100542 mode,
Ævar Arnfjörð Bjarmasoneefadd12021-03-20 23:37:46 +0100543 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,
Ævar Arnfjörð Bjarmason47957482021-03-20 23:37:51 +0100551 const char *pathname, unsigned mode,
Ævar Arnfjörð Bjarmasoneefadd12021-03-20 23:37:46 +0100552 void *context)
553{
554 struct index_state *istate = context;
Ævar Arnfjörð Bjarmason9614ad32021-03-20 23:37:48 +0100555 return read_one_entry_opt(istate, oid, base, pathname,
556 mode, ADD_CACHE_JUST_APPEND);
Ævar Arnfjörð Bjarmasoneefadd12021-03-20 23:37:46 +0100557}
558
Junio C Hamano64586e72007-09-12 16:04:22 -0700559/*
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 */
Brandon Williams312c9842017-06-12 15:13:58 -0700567void overlay_tree_on_index(struct index_state *istate,
568 const char *tree_name, const char *prefix)
Junio C Hamano64586e72007-09-12 16:04:22 -0700569{
570 struct tree *tree;
brian m. carlson6f37eb72017-05-06 22:10:31 +0000571 struct object_id oid;
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700572 struct pathspec pathspec;
Junio C Hamano64586e72007-09-12 16:04:22 -0700573 struct cache_entry *last_stage0 = NULL;
574 int i;
Ævar Arnfjörð Bjarmason9614ad32021-03-20 23:37:48 +0100575 read_tree_fn_t fn = NULL;
576 int err;
Junio C Hamano64586e72007-09-12 16:04:22 -0700577
brian m. carlson6f37eb72017-05-06 22:10:31 +0000578 if (get_oid(tree_name, &oid))
Junio C Hamano64586e72007-09-12 16:04:22 -0700579 die("tree-ish %s not found.", tree_name);
brian m. carlsona9dbc172017-05-06 22:10:37 +0000580 tree = parse_tree_indirect(&oid);
Junio C Hamano64586e72007-09-12 16:04:22 -0700581 if (!tree)
582 die("bad tree-ish %s", tree_name);
583
584 /* Hoist the unmerged entries up to stage #3 to make room */
Derrick Stolee42f44e82021-04-01 01:49:49 +0000585 /* TODO: audit for interaction with sparse-index. */
586 ensure_full_index(istate);
Brandon Williams312c9842017-06-12 15:13:58 -0700587 for (i = 0; i < istate->cache_nr; i++) {
588 struct cache_entry *ce = istate->cache[i];
Junio C Hamano64586e72007-09-12 16:04:22 -0700589 if (!ce_stage(ce))
590 continue;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800591 ce->ce_flags |= CE_STAGEMASK;
Junio C Hamano64586e72007-09-12 16:04:22 -0700592 }
593
594 if (prefix) {
Nguyễn Thái Ngọc Duy9a087272013-07-14 15:35:59 +0700595 static const char *(matchbuf[1]);
596 matchbuf[0] = NULL;
597 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
598 PATHSPEC_PREFER_CWD, prefix, matchbuf);
Junio C Hamano64586e72007-09-12 16:04:22 -0700599 } else
Nguyễn Thái Ngọc Duy9a087272013-07-14 15:35:59 +0700600 memset(&pathspec, 0, sizeof(pathspec));
Ævar Arnfjörð Bjarmason9614ad32021-03-20 23:37:48 +0100601
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;
Ævar Arnfjörð Bjarmason47957482021-03-20 23:37:51 +0100615 err = read_tree(the_repository, tree, &pathspec, fn, istate);
Ævar Arnfjörð Bjarmason9614ad32021-03-20 23:37:48 +0100616 if (err)
Junio C Hamano64586e72007-09-12 16:04:22 -0700617 die("unable to read tree entries %s", tree_name);
618
Ævar Arnfjörð Bjarmason9614ad32021-03-20 23:37:48 +0100619 /*
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
Brandon Williams312c9842017-06-12 15:13:58 -0700627 for (i = 0; i < istate->cache_nr; i++) {
628 struct cache_entry *ce = istate->cache[i];
Junio C Hamano64586e72007-09-12 16:04:22 -0700629 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))
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800643 ce->ce_flags |= CE_UPDATE;
Junio C Hamano64586e72007-09-12 16:04:22 -0700644 }
645 }
646}
647
Miklos Vajnace8e8802009-02-17 15:27:11 +0100648static const char * const ls_files_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -0700649 N_("git ls-files [<options>] [<file>...]"),
Miklos Vajnace8e8802009-02-17 15:27:11 +0100650 NULL
651};
652
Miklos Vajnace8e8802009-02-17 15:27:11 +0100653static int option_parse_exclude(const struct option *opt,
654 const char *arg, int unset)
655{
Adam Spiers72aeb182013-01-16 13:25:58 +0000656 struct string_list *exclude_list = opt->value;
Miklos Vajnace8e8802009-02-17 15:27:11 +0100657
Jeff King517fe802018-11-05 01:45:42 -0500658 BUG_ON_OPT_NEG(unset);
659
Miklos Vajnace8e8802009-02-17 15:27:11 +0100660 exc_given = 1;
Adam Spiers72aeb182013-01-16 13:25:58 +0000661 string_list_append(exclude_list, arg);
Miklos Vajnace8e8802009-02-17 15:27:11 +0100662
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
Jeff King517fe802018-11-05 01:45:42 -0500671 BUG_ON_OPT_NEG(unset);
672
Miklos Vajnace8e8802009-02-17 15:27:11 +0100673 exc_given = 1;
Derrick Stolee65edd962019-09-03 11:04:57 -0700674 add_patterns_from_file(dir, arg);
Miklos Vajnace8e8802009-02-17 15:27:11 +0100675
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
Jeff King517fe802018-11-05 01:45:42 -0500684 BUG_ON_OPT_NEG(unset);
685 BUG_ON_OPT_ARG(arg);
686
Miklos Vajnace8e8802009-02-17 15:27:11 +0100687 exc_given = 1;
688 setup_standard_excludes(dir);
689
690 return 0;
691}
Nicolas Pitrecf9a1132005-04-28 15:06:25 -0700692
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200693int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700694{
Adam Spiers72aeb182013-01-16 13:25:58 +0000695 int require_work_tree = 0, show_tag = 0, i;
Andrzej Hunt4c217a42021-04-25 14:16:10 +0000696 char *max_prefix;
Ævar Arnfjörð Bjarmasonce93a4c2021-07-01 12:51:27 +0200697 struct dir_struct dir = DIR_INIT;
Derrick Stoleecaa3d552019-09-03 11:04:56 -0700698 struct pattern_list *pl;
Adam Spiers72aeb182013-01-16 13:25:58 +0000699 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
Miklos Vajnace8e8802009-02-17 15:27:11 +0100700 struct option builtin_ls_files_options[] = {
Jeff King1f3c79a2016-01-31 06:35:46 -0500701 /* Think twice before adding "--nul" synonym to this */
702 OPT_SET_INT('z', NULL, &line_terminator,
Bagas Sanjaya54b4d122021-09-21 18:13:05 +0700703 N_("separate paths with the NUL character"), '\0'),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200704 OPT_BOOL('t', NULL, &show_tag,
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700705 N_("identify the file status with tags")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200706 OPT_BOOL('v', NULL, &show_valid_bit,
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700707 N_("use lowercase letters for 'assume unchanged' files")),
Ben Peartd8c71db2017-09-22 12:35:42 -0400708 OPT_BOOL('f', NULL, &show_fsmonitor_bit,
709 N_("use lowercase letters for 'fsmonitor clean' files")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200710 OPT_BOOL('c', "cached", &show_cached,
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700711 N_("show cached files in the output (default)")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200712 OPT_BOOL('d', "deleted", &show_deleted,
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700713 N_("show deleted files in the output")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200714 OPT_BOOL('m', "modified", &show_modified,
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700715 N_("show modified files in the output")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200716 OPT_BOOL('o', "others", &show_others,
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700717 N_("show other files in the output")),
Miklos Vajnace8e8802009-02-17 15:27:11 +0100718 OPT_BIT('i', "ignored", &dir.flags,
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700719 N_("show ignored files in the output"),
Miklos Vajnace8e8802009-02-17 15:27:11 +0100720 DIR_SHOW_IGNORED),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200721 OPT_BOOL('s', "stage", &show_stage,
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700722 N_("show staged contents' object name in the output")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200723 OPT_BOOL('k', "killed", &show_killed,
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700724 N_("show files on the filesystem that need to be removed")),
Miklos Vajnace8e8802009-02-17 15:27:11 +0100725 OPT_BIT(0, "directory", &dir.flags,
Alex Henriead5fe372014-08-30 13:56:01 -0600726 N_("show 'other' directories' names only"),
Miklos Vajnace8e8802009-02-17 15:27:11 +0100727 DIR_SHOW_OTHER_DIRECTORIES),
Torsten Bögershausena7630bd2016-01-16 07:50:02 +0100728 OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
Jeff Kinge9008b92009-05-08 01:01:17 -0400729 OPT_NEGBIT(0, "empty-directory", &dir.flags,
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700730 N_("don't show empty directories"),
Miklos Vajnace8e8802009-02-17 15:27:11 +0100731 DIR_HIDE_EMPTY_DIRECTORIES),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200732 OPT_BOOL('u', "unmerged", &show_unmerged,
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700733 N_("show unmerged files in the output")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200734 OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700735 N_("show resolve-undo information")),
Denton Liu203c8532020-04-28 04:36:28 -0400736 OPT_CALLBACK_F('x', "exclude", &exclude_list, N_("pattern"),
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700737 N_("skip files matching pattern"),
Denton Liu203c8532020-04-28 04:36:28 -0400738 PARSE_OPT_NONEG, option_parse_exclude),
739 OPT_CALLBACK_F('X', "exclude-from", &dir, N_("file"),
Bagas Sanjaya54b4d122021-09-21 18:13:05 +0700740 N_("read exclude patterns from <file>"),
Denton Liu203c8532020-04-28 04:36:28 -0400741 PARSE_OPT_NONEG, option_parse_exclude_from),
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700742 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
743 N_("read additional per-directory exclude patterns in <file>")),
Denton Liu203c8532020-04-28 04:36:28 -0400744 OPT_CALLBACK_F(0, "exclude-standard", &dir, NULL,
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700745 N_("add the standard git exclusions"),
Jeff Kingccf659e2018-11-05 01:39:20 -0500746 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
Denton Liu203c8532020-04-28 04:36:28 -0400747 option_parse_exclude_standard),
Nguyễn Thái Ngọc Duy3e4a67b2018-05-20 17:42:58 +0200748 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),
Brandon Williamse77aa332016-10-07 11:18:49 -0700751 OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
752 N_("recurse through submodules")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200753 OPT_BOOL(0, "error-unmatch", &error_unmatch,
Nguyễn Thái Ngọc Duy377adc32012-08-20 19:32:20 +0700754 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")),
Miklos Vajnace8e8802009-02-17 15:27:11 +0100757 OPT__ABBREV(&abbrev),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200758 OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
ZheNing Hu93a7d982021-01-23 10:20:10 +0000759 OPT_BOOL(0, "deduplicate", &skipping_duplicates,
760 N_("suppress duplicate entries")),
Derrick Stolee78087092021-12-22 14:20:53 +0000761 OPT_BOOL(0, "sparse", &show_sparse_dirs,
762 N_("show sparse directories in the presence of a sparse index")),
ZheNing Huce74de92022-07-23 06:44:45 +0000763 OPT_STRING_F(0, "format", &format, N_("format"),
764 N_("format to use for the output"),
765 PARSE_OPT_NONEG),
Miklos Vajnace8e8802009-02-17 15:27:11 +0100766 OPT_END()
767 };
Ævar Arnfjörð Bjarmasoneab4ac62021-10-07 12:01:35 +0200768 int ret = 0;
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700769
Nguyễn Thái Ngọc Duycbb31672010-10-22 01:48:14 -0500770 if (argc == 2 && !strcmp(argv[1], "-h"))
771 usage_with_options(ls_files_usage, builtin_ls_files_options);
772
Derrick Stolee78087092021-12-22 14:20:53 +0000773 prepare_repo_settings(the_repository);
774 the_repository->settings.command_requires_full_index = 0;
775
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200776 prefix = cmd_prefix;
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700777 if (prefix)
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200778 prefix_len = strlen(prefix);
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100779 git_config(git_default_config, NULL);
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700780
Brandon Williams188dce12017-06-22 11:43:48 -0700781 if (repo_read_index(the_repository) < 0)
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700782 die("index file corrupt");
783
Stephen Boyd37782922009-05-23 11:53:12 -0700784 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
Miklos Vajnace8e8802009-02-17 15:27:11 +0100785 ls_files_usage, 0);
Derrick Stolee65edd962019-09-03 11:04:57 -0700786 pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
Adam Spiers72aeb182013-01-16 13:25:58 +0000787 for (i = 0; i < exclude_list.nr; i++) {
Derrick Stolee65edd962019-09-03 11:04:57 -0700788 add_pattern(exclude_list.items[i].string, "", 0, pl, --exclude_args);
Adam Spiers72aeb182013-01-16 13:25:58 +0000789 }
ZheNing Huce74de92022-07-23 06:44:45 +0000790
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
Ben Peartd8c71db2017-09-22 12:35:42 -0400797 if (show_tag || show_valid_bit || show_fsmonitor_bit) {
Miklos Vajnace8e8802009-02-17 15:27:11 +0100798 tag_cached = "H ";
799 tag_unmerged = "M ";
800 tag_removed = "R ";
801 tag_modified = "C ";
802 tag_other = "? ";
803 tag_killed = "K ";
Nguyễn Thái Ngọc Duy44a36912009-08-20 20:46:57 +0700804 tag_skip_worktree = "S ";
Junio C Hamano9d9a2f42009-12-25 10:08:04 -0800805 tag_resolve_undo = "U ";
Nicolas Pitre9ff768e2005-04-28 11:44:04 -0700806 }
Junio C Hamanode2e3b02009-03-20 14:30:51 -0700807 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
Miklos Vajnace8e8802009-02-17 15:27:11 +0100808 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;
ZheNing Hu93a7d982021-01-23 10:20:10 +0000815 if (show_tag || show_stage)
816 skipping_duplicates = 0;
Miklos Vajnace8e8802009-02-17 15:27:11 +0100817 if (dir.exclude_per_dir)
818 exc_given = 1;
Nicolas Pitre9ff768e2005-04-28 11:44:04 -0700819
Mike Hommey7d8ae932007-11-03 12:23:12 +0100820 if (require_work_tree && !is_inside_work_tree())
821 setup_work_tree();
Johannes Schindelin6d9ba672007-01-23 13:30:20 +0100822
Brandon Williamse77aa332016-10-07 11:18:49 -0700823 if (recurse_submodules &&
Jonathan Tan290eada2022-02-23 16:23:03 -0800824 (show_deleted || show_others || show_unmerged ||
Brandon Williams07c01b92016-10-07 11:18:50 -0700825 show_killed || show_modified || show_resolve_undo || with_tree))
Brandon Williamse77aa332016-10-07 11:18:49 -0700826 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
Nguyễn Thái Ngọc Duy9e06d6e2013-07-14 15:35:43 +0700832 parse_pathspec(&pathspec, 0,
Brandon Williamscbca0602017-05-11 15:04:25 -0700833 PATHSPEC_PREFER_CWD,
Nguyễn Thái Ngọc Duy9e06d6e2013-07-14 15:35:43 +0700834 prefix, argv);
Johannes Schindelinf3670a52009-02-07 14:43:03 +0100835
Brandon Williams75a63152016-10-07 11:18:51 -0700836 /*
837 * Find common prefix for all pathspec's
838 * This is used as a performance optimization which unfortunately cannot
Brandon Williams188dce12017-06-22 11:43:48 -0700839 * 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.
Brandon Williams75a63152016-10-07 11:18:51 -0700843 */
844 if (recurse_submodules)
845 max_prefix = NULL;
846 else
847 max_prefix = common_prefix(&pathspec);
Brandon Williamscbca0602017-05-11 15:04:25 -0700848 max_prefix_len = get_common_prefix_len(max_prefix);
849
Brandon Williams188dce12017-06-22 11:43:48 -0700850 prune_index(the_repository->index, max_prefix, max_prefix_len);
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700851
Junio C Hamanobba319b2006-02-14 12:40:20 -0800852 /* Treat unmatching pathspec elements as errors */
Nguyễn Thái Ngọc Duy9e06d6e2013-07-14 15:35:43 +0700853 if (pathspec.nr && error_unmatch)
Junio C Hamano8b54c232015-08-20 09:57:32 -0700854 ps_matched = xcalloc(pathspec.nr, 1);
Junio C Hamanobba319b2006-02-14 12:40:20 -0800855
Elijah Newrenb338e9f2021-05-12 17:28:16 +0000856 if ((dir.flags & DIR_SHOW_IGNORED) && !show_others && !show_cached)
857 die("ls-files -i must be used with either -o or -c");
858
Ben Waltonac78b002009-10-08 21:53:35 -0400859 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
860 die("ls-files --ignored needs some exclude pattern");
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700861
862 /* With no flags, we default to showing the cached files */
René Scharfe0b437a12013-06-13 20:19:44 +0200863 if (!(show_stage || show_deleted || show_others || show_unmerged ||
864 show_killed || show_modified || show_resolve_undo))
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700865 show_cached = 1;
866
Junio C Hamano64586e72007-09-12 16:04:22 -0700867 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)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000873 die(_("options '%s' and '%s' cannot be used together"), "ls-files --with-tree", "-s/-u");
Brandon Williams188dce12017-06-22 11:43:48 -0700874 overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
Junio C Hamano64586e72007-09-12 16:04:22 -0700875 }
Brandon Williams188dce12017-06-22 11:43:48 -0700876
877 show_files(the_repository, &dir);
878
Junio C Hamano9d9a2f42009-12-25 10:08:04 -0800879 if (show_resolve_undo)
Brandon Williams188dce12017-06-22 11:43:48 -0700880 show_ru_info(the_repository->index);
Junio C Hamanobba319b2006-02-14 12:40:20 -0800881
Ævar Arnfjörð Bjarmasoneab4ac62021-10-07 12:01:35 +0200882 if (ps_matched && report_path_error(ps_matched, &pathspec)) {
883 fprintf(stderr, "Did you forget to 'git add'?\n");
884 ret = 1;
Junio C Hamanobba319b2006-02-14 12:40:20 -0800885 }
886
Ævar Arnfjörð Bjarmason272f0a52021-10-07 12:01:36 +0200887 string_list_clear(&exclude_list, 0);
Elijah Newreneceba532020-08-18 22:58:26 +0000888 dir_clear(&dir);
Andrzej Hunt4c217a42021-04-25 14:16:10 +0000889 free(max_prefix);
Ævar Arnfjörð Bjarmasoneab4ac62021-10-07 12:01:35 +0200890 return ret;
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700891}