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