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