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