]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-ls-files.c
fast-import: introduce "feature notes" command
[thirdparty/git.git] / builtin-ls-files.c
CommitLineData
8695c8bf
LT
1/*
2 * This merges the file listing in the directory cache index
3 * with the actual working directory list, and shows different
4 * combinations of the two.
5 *
6 * Copyright (C) Linus Torvalds, 2005
7 */
8695c8bf 8#include "cache.h"
22ddf719 9#include "quote.h"
453ec4bd 10#include "dir.h"
0864f264 11#include "builtin.h"
64586e75 12#include "tree.h"
ce8e8804 13#include "parse-options.h"
9d9a2f4a
JH
14#include "resolve-undo.h"
15#include "string-list.h"
8695c8bf 16
96f1e58f
DR
17static int abbrev;
18static int show_deleted;
19static int show_cached;
20static int show_others;
21static int show_stage;
22static int show_unmerged;
9d9a2f4a 23static int show_resolve_undo;
96f1e58f
DR
24static int show_modified;
25static int show_killed;
26static int show_valid_bit;
b83c8345 27static int line_terminator = '\n';
8695c8bf 28
96f1e58f
DR
29static int prefix_len;
30static int prefix_offset;
31static const char **pathspec;
32static int error_unmatch;
33static char *ps_matched;
64586e75 34static const char *with_tree;
ce8e8804 35static int exc_given;
5be4efbe 36
20d37ef6
PB
37static const char *tag_cached = "";
38static const char *tag_unmerged = "";
39static const char *tag_removed = "";
40static const char *tag_other = "";
6ca45943 41static const char *tag_killed = "";
b0391890 42static const char *tag_modified = "";
44a36913 43static const char *tag_skip_worktree = "";
9d9a2f4a 44static const char *tag_resolve_undo = "";
20d37ef6 45
453ec4bd 46static void show_dir_entry(const char *tag, struct dir_entry *ent)
5be4efbe
LT
47{
48 int len = prefix_len;
49 int offset = prefix_offset;
50
51 if (len >= ent->len)
7e44c935 52 die("git ls-files: internal error - directory entry not superset of prefix");
5be4efbe 53
0b50922a 54 if (!match_pathspec(pathspec, ent->name, ent->len, len, ps_matched))
5be4efbe
LT
55 return;
56
22ddf719 57 fputs(tag, stdout);
663af342 58 write_name_quoted(ent->name + offset, stdout, line_terminator);
5be4efbe
LT
59}
60
453ec4bd 61static void show_other_files(struct dir_struct *dir)
fcbc3083
JH
62{
63 int i;
5698454e 64
453ec4bd 65 for (i = 0; i < dir->nr; i++) {
453ec4bd 66 struct dir_entry *ent = dir->entries[i];
98fa4738
JK
67 if (!cache_name_is_other(ent->name, ent->len))
68 continue;
fcbc3083
JH
69 show_dir_entry(tag_other, ent);
70 }
71}
72
453ec4bd 73static void show_killed_files(struct dir_struct *dir)
6ca45943
JH
74{
75 int i;
453ec4bd
LT
76 for (i = 0; i < dir->nr; i++) {
77 struct dir_entry *ent = dir->entries[i];
6ca45943
JH
78 char *cp, *sp;
79 int pos, len, killed = 0;
80
81 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
82 sp = strchr(cp, '/');
83 if (!sp) {
84 /* If ent->name is prefix of an entry in the
85 * cache, it will be killed.
86 */
87 pos = cache_name_pos(ent->name, ent->len);
88 if (0 <= pos)
89 die("bug in show-killed-files");
90 pos = -pos - 1;
91 while (pos < active_nr &&
92 ce_stage(active_cache[pos]))
93 pos++; /* skip unmerged */
94 if (active_nr <= pos)
95 break;
96 /* pos points at a name immediately after
97 * ent->name in the cache. Does it expect
98 * ent->name to be a directory?
99 */
100 len = ce_namelen(active_cache[pos]);
101 if ((ent->len < len) &&
102 !strncmp(active_cache[pos]->name,
103 ent->name, ent->len) &&
104 active_cache[pos]->name[ent->len] == '/')
105 killed = 1;
106 break;
107 }
108 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
109 /* If any of the leading directories in
110 * ent->name is registered in the cache,
111 * ent->name will be killed.
112 */
113 killed = 1;
114 break;
115 }
116 }
117 if (killed)
453ec4bd 118 show_dir_entry(tag_killed, dir->entries[i]);
6ca45943 119 }
8695c8bf
LT
120}
121
5be4efbe
LT
122static void show_ce_entry(const char *tag, struct cache_entry *ce)
123{
124 int len = prefix_len;
125 int offset = prefix_offset;
126
127 if (len >= ce_namelen(ce))
7e44c935 128 die("git ls-files: internal error - cache entry not superset of prefix");
5be4efbe 129
0b50922a 130 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), len, ps_matched))
5be4efbe
LT
131 return;
132
8bb2e03b 133 if (tag && *tag && show_valid_bit &&
7a51ed66 134 (ce->ce_flags & CE_VALID)) {
2bcab240
JH
135 static char alttag[4];
136 memcpy(alttag, tag, 3);
137 if (isalpha(tag[0]))
138 alttag[0] = tolower(tag[0]);
139 else if (tag[0] == '?')
140 alttag[0] = '!';
141 else {
142 alttag[0] = 'v';
143 alttag[1] = tag[0];
144 alttag[2] = ' ';
145 alttag[3] = 0;
146 }
147 tag = alttag;
148 }
149
22ddf719
JH
150 if (!show_stage) {
151 fputs(tag, stdout);
663af342 152 } else {
22ddf719 153 printf("%s%06o %s %d\t",
5be4efbe 154 tag,
7a51ed66 155 ce->ce_mode,
ad0cae4c
EW
156 abbrev ? find_unique_abbrev(ce->sha1,abbrev)
157 : sha1_to_hex(ce->sha1),
22ddf719 158 ce_stage(ce));
22ddf719 159 }
663af342 160 write_name_quoted(ce->name + offset, stdout, line_terminator);
5be4efbe
LT
161}
162
9d9a2f4a
JH
163static int show_one_ru(struct string_list_item *item, void *cbdata)
164{
165 int offset = prefix_offset;
166 const char *path = item->string;
167 struct resolve_undo_info *ui = item->util;
168 int i, len;
169
170 len = strlen(path);
171 if (len < prefix_len)
172 return 0; /* outside of the prefix */
173 if (!match_pathspec(pathspec, path, len, prefix_len, ps_matched))
174 return 0; /* uninterested */
175 for (i = 0; i < 3; i++) {
176 if (!ui->mode[i])
177 continue;
178 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
179 abbrev
180 ? find_unique_abbrev(ui->sha1[i], abbrev)
181 : sha1_to_hex(ui->sha1[i]),
182 i + 1);
183 write_name_quoted(path + offset, stdout, line_terminator);
184 }
185 return 0;
186}
187
188static void show_ru_info(const char *prefix)
189{
190 if (!the_index.resolve_undo)
191 return;
192 for_each_string_list(show_one_ru, the_index.resolve_undo, NULL);
193}
194
3e04228b 195static void show_files(struct dir_struct *dir, const char *prefix)
8695c8bf
LT
196{
197 int i;
198
199 /* For cached/deleted files we don't need to even do the readdir */
6ca45943 200 if (show_others || show_killed) {
1d8842d9 201 fill_directory(dir, pathspec);
6ca45943 202 if (show_others)
453ec4bd 203 show_other_files(dir);
6ca45943 204 if (show_killed)
453ec4bd 205 show_killed_files(dir);
8695c8bf 206 }
aee46198 207 if (show_cached | show_stage) {
8695c8bf
LT
208 for (i = 0; i < active_nr; i++) {
209 struct cache_entry *ce = active_cache[i];
500348aa
JK
210 int dtype = ce_to_dtype(ce);
211 if (dir->flags & DIR_SHOW_IGNORED &&
212 !excluded(dir, ce->name, &dtype))
213 continue;
eec8c633
LT
214 if (show_unmerged && !ce_stage(ce))
215 continue;
7a51ed66 216 if (ce->ce_flags & CE_UPDATE)
64586e75 217 continue;
44a36913
NTND
218 show_ce_entry(ce_stage(ce) ? tag_unmerged :
219 (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce);
8695c8bf
LT
220 }
221 }
b0391890 222 if (show_deleted | show_modified) {
8695c8bf
LT
223 for (i = 0; i < active_nr; i++) {
224 struct cache_entry *ce = active_cache[i];
225 struct stat st;
b0391890 226 int err;
500348aa
JK
227 int dtype = ce_to_dtype(ce);
228 if (dir->flags & DIR_SHOW_IGNORED &&
229 !excluded(dir, ce->name, &dtype))
230 continue;
4b4e26d2
JH
231 if (ce->ce_flags & CE_UPDATE)
232 continue;
b4d1690d
NTND
233 if (ce_skip_worktree(ce))
234 continue;
b0391890
JH
235 err = lstat(ce->name, &st);
236 if (show_deleted && err)
237 show_ce_entry(tag_removed, ce);
2bcab240 238 if (show_modified && ce_modified(ce, &st, 0))
b0391890 239 show_ce_entry(tag_modified, ce);
5be4efbe
LT
240 }
241 }
242}
243
244/*
245 * Prune the index to only contain stuff starting with "prefix"
246 */
3e04228b 247static void prune_cache(const char *prefix)
5be4efbe
LT
248{
249 int pos = cache_name_pos(prefix, prefix_len);
250 unsigned int first, last;
251
252 if (pos < 0)
253 pos = -pos-1;
95af39fc
KP
254 memmove(active_cache, active_cache + pos,
255 (active_nr - pos) * sizeof(struct cache_entry *));
5be4efbe
LT
256 active_nr -= pos;
257 first = 0;
258 last = active_nr;
259 while (last > first) {
260 int next = (last + first) >> 1;
261 struct cache_entry *ce = active_cache[next];
262 if (!strncmp(ce->name, prefix, prefix_len)) {
263 first = next+1;
264 continue;
8695c8bf 265 }
5be4efbe
LT
266 last = next;
267 }
268 active_nr = last;
269}
270
3e04228b 271static const char *verify_pathspec(const char *prefix)
5be4efbe 272{
56fc5108 273 const char **p, *n, *prev;
56fc5108
LT
274 unsigned long max;
275
276 prev = NULL;
277 max = PATH_MAX;
278 for (p = pathspec; (n = *p) != NULL; p++) {
279 int i, len = 0;
280 for (i = 0; i < max; i++) {
281 char c = n[i];
282 if (prev && prev[i] != c)
283 break;
56906143 284 if (!c || c == '*' || c == '?')
56fc5108
LT
285 break;
286 if (c == '/')
287 len = i+1;
288 }
289 prev = n;
290 if (len < max) {
291 max = len;
292 if (!max)
293 break;
294 }
5be4efbe 295 }
56fc5108
LT
296
297 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
7e44c935 298 die("git ls-files: cannot generate relative filenames containing '..'");
56fc5108 299
56fc5108 300 prefix_len = max;
182af834 301 return max ? xmemdupz(prev, max) : NULL;
8695c8bf
LT
302}
303
f3670a57
JS
304static void strip_trailing_slash_from_submodules(void)
305{
306 const char **p;
307
308 for (p = pathspec; *p != NULL; p++) {
309 int len = strlen(*p), pos;
310
311 if (len < 1 || (*p)[len - 1] != '/')
312 continue;
313 pos = cache_name_pos(*p, len - 1);
314 if (pos >= 0 && S_ISGITLINK(active_cache[pos]->ce_mode))
315 *p = xstrndup(*p, len - 1);
316 }
317}
318
64586e75
JH
319/*
320 * Read the tree specified with --with-tree option
321 * (typically, HEAD) into stage #1 and then
322 * squash them down to stage #0. This is used for
323 * --error-unmatch to list and check the path patterns
324 * that were given from the command line. We are not
325 * going to write this index out.
326 */
ee425e46 327void overlay_tree_on_cache(const char *tree_name, const char *prefix)
64586e75
JH
328{
329 struct tree *tree;
330 unsigned char sha1[20];
331 const char **match;
332 struct cache_entry *last_stage0 = NULL;
333 int i;
334
335 if (get_sha1(tree_name, sha1))
336 die("tree-ish %s not found.", tree_name);
337 tree = parse_tree_indirect(sha1);
338 if (!tree)
339 die("bad tree-ish %s", tree_name);
340
341 /* Hoist the unmerged entries up to stage #3 to make room */
342 for (i = 0; i < active_nr; i++) {
343 struct cache_entry *ce = active_cache[i];
344 if (!ce_stage(ce))
345 continue;
7a51ed66 346 ce->ce_flags |= CE_STAGEMASK;
64586e75
JH
347 }
348
349 if (prefix) {
350 static const char *(matchbuf[2]);
351 matchbuf[0] = prefix;
07e77e40 352 matchbuf[1] = NULL;
64586e75
JH
353 match = matchbuf;
354 } else
355 match = NULL;
356 if (read_tree(tree, 1, match))
357 die("unable to read tree entries %s", tree_name);
358
359 for (i = 0; i < active_nr; i++) {
360 struct cache_entry *ce = active_cache[i];
361 switch (ce_stage(ce)) {
362 case 0:
363 last_stage0 = ce;
364 /* fallthru */
365 default:
366 continue;
367 case 1:
368 /*
369 * If there is stage #0 entry for this, we do not
370 * need to show it. We use CE_UPDATE bit to mark
371 * such an entry.
372 */
373 if (last_stage0 &&
374 !strcmp(last_stage0->name, ce->name))
7a51ed66 375 ce->ce_flags |= CE_UPDATE;
64586e75
JH
376 }
377 }
378}
379
ee425e46
JH
380int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset)
381{
382 /*
383 * Make sure all pathspec matched; otherwise it is an error.
384 */
385 int num, errors = 0;
386 for (num = 0; pathspec[num]; num++) {
387 int other, found_dup;
388
389 if (ps_matched[num])
390 continue;
391 /*
392 * The caller might have fed identical pathspec
393 * twice. Do not barf on such a mistake.
394 */
395 for (found_dup = other = 0;
396 !found_dup && pathspec[other];
397 other++) {
398 if (other == num || !ps_matched[other])
399 continue;
400 if (!strcmp(pathspec[other], pathspec[num]))
401 /*
402 * Ok, we have a match already.
403 */
404 found_dup = 1;
405 }
406 if (found_dup)
407 continue;
408
409 error("pathspec '%s' did not match any file(s) known to git.",
410 pathspec[num] + prefix_offset);
411 errors++;
412 }
413 return errors;
414}
415
ce8e8804
MV
416static const char * const ls_files_usage[] = {
417 "git ls-files [options] [<file>]*",
418 NULL
419};
420
421static int option_parse_z(const struct option *opt,
422 const char *arg, int unset)
423{
424 line_terminator = unset ? '\n' : '\0';
425
426 return 0;
427}
428
429static int option_parse_exclude(const struct option *opt,
430 const char *arg, int unset)
431{
432 struct exclude_list *list = opt->value;
433
434 exc_given = 1;
435 add_exclude(arg, "", 0, list);
436
437 return 0;
438}
439
440static int option_parse_exclude_from(const struct option *opt,
441 const char *arg, int unset)
442{
443 struct dir_struct *dir = opt->value;
444
445 exc_given = 1;
446 add_excludes_from_file(dir, arg);
447
448 return 0;
449}
450
451static int option_parse_exclude_standard(const struct option *opt,
452 const char *arg, int unset)
453{
454 struct dir_struct *dir = opt->value;
455
456 exc_given = 1;
457 setup_standard_excludes(dir);
458
459 return 0;
460}
cf9a113d 461
a633fca0 462int cmd_ls_files(int argc, const char **argv, const char *prefix)
8695c8bf 463{
ce8e8804 464 int require_work_tree = 0, show_tag = 0;
453ec4bd 465 struct dir_struct dir;
ce8e8804
MV
466 struct option builtin_ls_files_options[] = {
467 { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
468 "paths are separated with NUL character",
469 PARSE_OPT_NOARG, option_parse_z },
470 OPT_BOOLEAN('t', NULL, &show_tag,
471 "identify the file status with tags"),
472 OPT_BOOLEAN('v', NULL, &show_valid_bit,
473 "use lowercase letters for 'assume unchanged' files"),
474 OPT_BOOLEAN('c', "cached", &show_cached,
475 "show cached files in the output (default)"),
476 OPT_BOOLEAN('d', "deleted", &show_deleted,
477 "show deleted files in the output"),
478 OPT_BOOLEAN('m', "modified", &show_modified,
479 "show modified files in the output"),
480 OPT_BOOLEAN('o', "others", &show_others,
481 "show other files in the output"),
482 OPT_BIT('i', "ignored", &dir.flags,
483 "show ignored files in the output",
484 DIR_SHOW_IGNORED),
485 OPT_BOOLEAN('s', "stage", &show_stage,
486 "show staged contents' object name in the output"),
487 OPT_BOOLEAN('k', "killed", &show_killed,
488 "show files on the filesystem that need to be removed"),
489 OPT_BIT(0, "directory", &dir.flags,
490 "show 'other' directories' name only",
491 DIR_SHOW_OTHER_DIRECTORIES),
e9008b9a 492 OPT_NEGBIT(0, "empty-directory", &dir.flags,
2fb6d6d6 493 "don't show empty directories",
ce8e8804
MV
494 DIR_HIDE_EMPTY_DIRECTORIES),
495 OPT_BOOLEAN('u', "unmerged", &show_unmerged,
496 "show unmerged files in the output"),
9d9a2f4a
JH
497 OPT_BOOLEAN(0, "resolve-undo", &show_resolve_undo,
498 "show resolve-undo information"),
ce8e8804
MV
499 { OPTION_CALLBACK, 'x', "exclude", &dir.exclude_list[EXC_CMDL], "pattern",
500 "skip files matching pattern",
501 0, option_parse_exclude },
502 { OPTION_CALLBACK, 'X', "exclude-from", &dir, "file",
503 "exclude patterns are read from <file>",
504 0, option_parse_exclude_from },
505 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, "file",
506 "read additional per-directory exclude patterns in <file>"),
507 { OPTION_CALLBACK, 0, "exclude-standard", &dir, NULL,
508 "add the standard git exclusions",
509 PARSE_OPT_NOARG, option_parse_exclude_standard },
510 { OPTION_SET_INT, 0, "full-name", &prefix_offset, NULL,
511 "make the output relative to the project top directory",
512 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL },
513 OPT_BOOLEAN(0, "error-unmatch", &error_unmatch,
514 "if any <file> is not in the index, treat this as an error"),
515 OPT_STRING(0, "with-tree", &with_tree, "tree-ish",
516 "pretend that paths removed since <tree-ish> are still present"),
517 OPT__ABBREV(&abbrev),
518 OPT_END()
519 };
8695c8bf 520
453ec4bd 521 memset(&dir, 0, sizeof(dir));
5be4efbe 522 if (prefix)
56fc5108 523 prefix_offset = strlen(prefix);
ef90d6d4 524 git_config(git_default_config, NULL);
5be4efbe 525
c28b3d6e
NTND
526 if (read_cache() < 0)
527 die("index file corrupt");
528
37782920 529 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
ce8e8804
MV
530 ls_files_usage, 0);
531 if (show_tag || show_valid_bit) {
532 tag_cached = "H ";
533 tag_unmerged = "M ";
534 tag_removed = "R ";
535 tag_modified = "C ";
536 tag_other = "? ";
537 tag_killed = "K ";
44a36913 538 tag_skip_worktree = "S ";
9d9a2f4a 539 tag_resolve_undo = "U ";
9ff768e9 540 }
de2e3b04 541 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
ce8e8804
MV
542 require_work_tree = 1;
543 if (show_unmerged)
544 /*
545 * There's no point in showing unmerged unless
546 * you also show the stage information.
547 */
548 show_stage = 1;
549 if (dir.exclude_per_dir)
550 exc_given = 1;
9ff768e9 551
7d8ae932
MH
552 if (require_work_tree && !is_inside_work_tree())
553 setup_work_tree();
6d9ba67b 554
ce8e8804 555 pathspec = get_pathspec(prefix, argv);
56fc5108 556
3ea3c215 557 /* be nice with submodule paths ending in a slash */
f3670a57
JS
558 if (pathspec)
559 strip_trailing_slash_from_submodules();
560
56fc5108 561 /* Verify that the pathspec matches the prefix */
79418599 562 if (pathspec)
3e04228b 563 prefix = verify_pathspec(prefix);
5be4efbe 564
bba319b5
JH
565 /* Treat unmatching pathspec elements as errors */
566 if (pathspec && error_unmatch) {
567 int num;
568 for (num = 0; pathspec[num]; num++)
569 ;
570 ps_matched = xcalloc(1, num);
571 }
572
ac78b009
BW
573 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
574 die("ls-files --ignored needs some exclude pattern");
8695c8bf
LT
575
576 /* With no flags, we default to showing the cached files */
b0391890 577 if (!(show_stage | show_deleted | show_others | show_unmerged |
9d9a2f4a 578 show_killed | show_modified | show_resolve_undo))
8695c8bf
LT
579 show_cached = 1;
580
5be4efbe 581 if (prefix)
3e04228b 582 prune_cache(prefix);
64586e75
JH
583 if (with_tree) {
584 /*
585 * Basic sanity check; show-stages and show-unmerged
586 * would not make any sense with this option.
587 */
588 if (show_stage || show_unmerged)
589 die("ls-files --with-tree is incompatible with -s or -u");
ee425e46 590 overlay_tree_on_cache(with_tree, prefix);
64586e75 591 }
3e04228b 592 show_files(&dir, prefix);
9d9a2f4a
JH
593 if (show_resolve_undo)
594 show_ru_info(prefix);
bba319b5
JH
595
596 if (ps_matched) {
ee425e46
JH
597 int bad;
598 bad = report_path_error(ps_matched, pathspec, prefix_offset);
599 if (bad)
ced7b828
AE
600 fprintf(stderr, "Did you forget to 'git add'?\n");
601
ee425e46 602 return bad ? 1 : 0;
bba319b5
JH
603 }
604
8695c8bf
LT
605 return 0;
606}