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