]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-ls-files.c
completion: __git_config_get_set_variables() handle values with spaces
[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) {
5be4efbe
LT
164 const char *path = ".", *base = "";
165 int baselen = prefix_len;
166
b4189aa8 167 if (baselen)
5be4efbe 168 path = base = prefix;
9fc42d60 169 read_directory(dir, path, base, baselen, pathspec);
6ca45943 170 if (show_others)
453ec4bd 171 show_other_files(dir);
6ca45943 172 if (show_killed)
453ec4bd 173 show_killed_files(dir);
8695c8bf 174 }
aee46198 175 if (show_cached | show_stage) {
8695c8bf
LT
176 for (i = 0; i < active_nr; i++) {
177 struct cache_entry *ce = active_cache[i];
6831a88a 178 int dtype = ce_to_dtype(ce);
7c4c97c0
JS
179 if (excluded(dir, ce->name, &dtype) !=
180 !!(dir->flags & DIR_SHOW_IGNORED))
9ff768e9 181 continue;
eec8c633
LT
182 if (show_unmerged && !ce_stage(ce))
183 continue;
7a51ed66 184 if (ce->ce_flags & CE_UPDATE)
64586e75 185 continue;
5be4efbe 186 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
8695c8bf
LT
187 }
188 }
b0391890 189 if (show_deleted | show_modified) {
8695c8bf
LT
190 for (i = 0; i < active_nr; i++) {
191 struct cache_entry *ce = active_cache[i];
192 struct stat st;
b0391890 193 int err;
6831a88a 194 int dtype = ce_to_dtype(ce);
7c4c97c0
JS
195 if (excluded(dir, ce->name, &dtype) !=
196 !!(dir->flags & DIR_SHOW_IGNORED))
9ff768e9 197 continue;
4b4e26d2
JH
198 if (ce->ce_flags & CE_UPDATE)
199 continue;
b0391890
JH
200 err = lstat(ce->name, &st);
201 if (show_deleted && err)
202 show_ce_entry(tag_removed, ce);
2bcab240 203 if (show_modified && ce_modified(ce, &st, 0))
b0391890 204 show_ce_entry(tag_modified, ce);
5be4efbe
LT
205 }
206 }
207}
208
209/*
210 * Prune the index to only contain stuff starting with "prefix"
211 */
3e04228b 212static void prune_cache(const char *prefix)
5be4efbe
LT
213{
214 int pos = cache_name_pos(prefix, prefix_len);
215 unsigned int first, last;
216
217 if (pos < 0)
218 pos = -pos-1;
95af39fc
KP
219 memmove(active_cache, active_cache + pos,
220 (active_nr - pos) * sizeof(struct cache_entry *));
5be4efbe
LT
221 active_nr -= pos;
222 first = 0;
223 last = active_nr;
224 while (last > first) {
225 int next = (last + first) >> 1;
226 struct cache_entry *ce = active_cache[next];
227 if (!strncmp(ce->name, prefix, prefix_len)) {
228 first = next+1;
229 continue;
8695c8bf 230 }
5be4efbe
LT
231 last = next;
232 }
233 active_nr = last;
234}
235
3e04228b 236static const char *verify_pathspec(const char *prefix)
5be4efbe 237{
56fc5108 238 const char **p, *n, *prev;
56fc5108
LT
239 unsigned long max;
240
241 prev = NULL;
242 max = PATH_MAX;
243 for (p = pathspec; (n = *p) != NULL; p++) {
244 int i, len = 0;
245 for (i = 0; i < max; i++) {
246 char c = n[i];
247 if (prev && prev[i] != c)
248 break;
56906143 249 if (!c || c == '*' || c == '?')
56fc5108
LT
250 break;
251 if (c == '/')
252 len = i+1;
253 }
254 prev = n;
255 if (len < max) {
256 max = len;
257 if (!max)
258 break;
259 }
5be4efbe 260 }
56fc5108
LT
261
262 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
7e44c935 263 die("git ls-files: cannot generate relative filenames containing '..'");
56fc5108 264
56fc5108 265 prefix_len = max;
182af834 266 return max ? xmemdupz(prev, max) : NULL;
8695c8bf
LT
267}
268
f3670a57
JS
269static void strip_trailing_slash_from_submodules(void)
270{
271 const char **p;
272
273 for (p = pathspec; *p != NULL; p++) {
274 int len = strlen(*p), pos;
275
276 if (len < 1 || (*p)[len - 1] != '/')
277 continue;
278 pos = cache_name_pos(*p, len - 1);
279 if (pos >= 0 && S_ISGITLINK(active_cache[pos]->ce_mode))
280 *p = xstrndup(*p, len - 1);
281 }
282}
283
64586e75
JH
284/*
285 * Read the tree specified with --with-tree option
286 * (typically, HEAD) into stage #1 and then
287 * squash them down to stage #0. This is used for
288 * --error-unmatch to list and check the path patterns
289 * that were given from the command line. We are not
290 * going to write this index out.
291 */
ee425e46 292void overlay_tree_on_cache(const char *tree_name, const char *prefix)
64586e75
JH
293{
294 struct tree *tree;
295 unsigned char sha1[20];
296 const char **match;
297 struct cache_entry *last_stage0 = NULL;
298 int i;
299
300 if (get_sha1(tree_name, sha1))
301 die("tree-ish %s not found.", tree_name);
302 tree = parse_tree_indirect(sha1);
303 if (!tree)
304 die("bad tree-ish %s", tree_name);
305
306 /* Hoist the unmerged entries up to stage #3 to make room */
307 for (i = 0; i < active_nr; i++) {
308 struct cache_entry *ce = active_cache[i];
309 if (!ce_stage(ce))
310 continue;
7a51ed66 311 ce->ce_flags |= CE_STAGEMASK;
64586e75
JH
312 }
313
314 if (prefix) {
315 static const char *(matchbuf[2]);
316 matchbuf[0] = prefix;
07e77e40 317 matchbuf[1] = NULL;
64586e75
JH
318 match = matchbuf;
319 } else
320 match = NULL;
321 if (read_tree(tree, 1, match))
322 die("unable to read tree entries %s", tree_name);
323
324 for (i = 0; i < active_nr; i++) {
325 struct cache_entry *ce = active_cache[i];
326 switch (ce_stage(ce)) {
327 case 0:
328 last_stage0 = ce;
329 /* fallthru */
330 default:
331 continue;
332 case 1:
333 /*
334 * If there is stage #0 entry for this, we do not
335 * need to show it. We use CE_UPDATE bit to mark
336 * such an entry.
337 */
338 if (last_stage0 &&
339 !strcmp(last_stage0->name, ce->name))
7a51ed66 340 ce->ce_flags |= CE_UPDATE;
64586e75
JH
341 }
342 }
343}
344
ee425e46
JH
345int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset)
346{
347 /*
348 * Make sure all pathspec matched; otherwise it is an error.
349 */
350 int num, errors = 0;
351 for (num = 0; pathspec[num]; num++) {
352 int other, found_dup;
353
354 if (ps_matched[num])
355 continue;
356 /*
357 * The caller might have fed identical pathspec
358 * twice. Do not barf on such a mistake.
359 */
360 for (found_dup = other = 0;
361 !found_dup && pathspec[other];
362 other++) {
363 if (other == num || !ps_matched[other])
364 continue;
365 if (!strcmp(pathspec[other], pathspec[num]))
366 /*
367 * Ok, we have a match already.
368 */
369 found_dup = 1;
370 }
371 if (found_dup)
372 continue;
373
374 error("pathspec '%s' did not match any file(s) known to git.",
375 pathspec[num] + prefix_offset);
376 errors++;
377 }
378 return errors;
379}
380
ce8e8804
MV
381static const char * const ls_files_usage[] = {
382 "git ls-files [options] [<file>]*",
383 NULL
384};
385
386static int option_parse_z(const struct option *opt,
387 const char *arg, int unset)
388{
389 line_terminator = unset ? '\n' : '\0';
390
391 return 0;
392}
393
394static int option_parse_exclude(const struct option *opt,
395 const char *arg, int unset)
396{
397 struct exclude_list *list = opt->value;
398
399 exc_given = 1;
400 add_exclude(arg, "", 0, list);
401
402 return 0;
403}
404
405static int option_parse_exclude_from(const struct option *opt,
406 const char *arg, int unset)
407{
408 struct dir_struct *dir = opt->value;
409
410 exc_given = 1;
411 add_excludes_from_file(dir, arg);
412
413 return 0;
414}
415
416static int option_parse_exclude_standard(const struct option *opt,
417 const char *arg, int unset)
418{
419 struct dir_struct *dir = opt->value;
420
421 exc_given = 1;
422 setup_standard_excludes(dir);
423
424 return 0;
425}
cf9a113d 426
a633fca0 427int cmd_ls_files(int argc, const char **argv, const char *prefix)
8695c8bf 428{
ce8e8804 429 int require_work_tree = 0, show_tag = 0;
453ec4bd 430 struct dir_struct dir;
ce8e8804
MV
431 struct option builtin_ls_files_options[] = {
432 { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
433 "paths are separated with NUL character",
434 PARSE_OPT_NOARG, option_parse_z },
435 OPT_BOOLEAN('t', NULL, &show_tag,
436 "identify the file status with tags"),
437 OPT_BOOLEAN('v', NULL, &show_valid_bit,
438 "use lowercase letters for 'assume unchanged' files"),
439 OPT_BOOLEAN('c', "cached", &show_cached,
440 "show cached files in the output (default)"),
441 OPT_BOOLEAN('d', "deleted", &show_deleted,
442 "show deleted files in the output"),
443 OPT_BOOLEAN('m', "modified", &show_modified,
444 "show modified files in the output"),
445 OPT_BOOLEAN('o', "others", &show_others,
446 "show other files in the output"),
447 OPT_BIT('i', "ignored", &dir.flags,
448 "show ignored files in the output",
449 DIR_SHOW_IGNORED),
450 OPT_BOOLEAN('s', "stage", &show_stage,
451 "show staged contents' object name in the output"),
452 OPT_BOOLEAN('k', "killed", &show_killed,
453 "show files on the filesystem that need to be removed"),
454 OPT_BIT(0, "directory", &dir.flags,
455 "show 'other' directories' name only",
456 DIR_SHOW_OTHER_DIRECTORIES),
e9008b9a 457 OPT_NEGBIT(0, "empty-directory", &dir.flags,
2fb6d6d6 458 "don't show empty directories",
ce8e8804
MV
459 DIR_HIDE_EMPTY_DIRECTORIES),
460 OPT_BOOLEAN('u', "unmerged", &show_unmerged,
461 "show unmerged files in the output"),
462 { OPTION_CALLBACK, 'x', "exclude", &dir.exclude_list[EXC_CMDL], "pattern",
463 "skip files matching pattern",
464 0, option_parse_exclude },
465 { OPTION_CALLBACK, 'X', "exclude-from", &dir, "file",
466 "exclude patterns are read from <file>",
467 0, option_parse_exclude_from },
468 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, "file",
469 "read additional per-directory exclude patterns in <file>"),
470 { OPTION_CALLBACK, 0, "exclude-standard", &dir, NULL,
471 "add the standard git exclusions",
472 PARSE_OPT_NOARG, option_parse_exclude_standard },
473 { OPTION_SET_INT, 0, "full-name", &prefix_offset, NULL,
474 "make the output relative to the project top directory",
475 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL },
476 OPT_BOOLEAN(0, "error-unmatch", &error_unmatch,
477 "if any <file> is not in the index, treat this as an error"),
478 OPT_STRING(0, "with-tree", &with_tree, "tree-ish",
479 "pretend that paths removed since <tree-ish> are still present"),
480 OPT__ABBREV(&abbrev),
481 OPT_END()
482 };
8695c8bf 483
453ec4bd 484 memset(&dir, 0, sizeof(dir));
5be4efbe 485 if (prefix)
56fc5108 486 prefix_offset = strlen(prefix);
ef90d6d4 487 git_config(git_default_config, NULL);
5be4efbe 488
37782920 489 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
ce8e8804
MV
490 ls_files_usage, 0);
491 if (show_tag || show_valid_bit) {
492 tag_cached = "H ";
493 tag_unmerged = "M ";
494 tag_removed = "R ";
495 tag_modified = "C ";
496 tag_other = "? ";
497 tag_killed = "K ";
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}