Merge branch 'jk/maint-ls-files-other' into jk/fix-ls-files-other
[git/msvc.git] / builtin-ls-files.c
blobb48327db950e5d8a54045f4956fa92b7879227bc
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.
6 * Copyright (C) Linus Torvalds, 2005
7 */
8 #include "cache.h"
9 #include "quote.h"
10 #include "dir.h"
11 #include "builtin.h"
12 #include "tree.h"
14 static int abbrev;
15 static int show_deleted;
16 static int show_cached;
17 static int show_others;
18 static int show_stage;
19 static int show_unmerged;
20 static int show_modified;
21 static int show_killed;
22 static int show_valid_bit;
23 static int line_terminator = '\n';
25 static int prefix_len;
26 static int prefix_offset;
27 static const char **pathspec;
28 static int error_unmatch;
29 static char *ps_matched;
30 static const char *with_tree;
32 static const char *tag_cached = "";
33 static const char *tag_unmerged = "";
34 static const char *tag_removed = "";
35 static const char *tag_other = "";
36 static const char *tag_killed = "";
37 static const char *tag_modified = "";
41 * Match a pathspec against a filename. The first "skiplen" characters
42 * are the common prefix
44 int pathspec_match(const char **spec, char *ps_matched,
45 const char *filename, int skiplen)
47 const char *m;
49 while ((m = *spec++) != NULL) {
50 int matchlen = strlen(m + skiplen);
52 if (!matchlen)
53 goto matched;
54 if (!strncmp(m + skiplen, filename + skiplen, matchlen)) {
55 if (m[skiplen + matchlen - 1] == '/')
56 goto matched;
57 switch (filename[skiplen + matchlen]) {
58 case '/': case '\0':
59 goto matched;
62 if (!fnmatch(m + skiplen, filename + skiplen, 0))
63 goto matched;
64 if (ps_matched)
65 ps_matched++;
66 continue;
67 matched:
68 if (ps_matched)
69 *ps_matched = 1;
70 return 1;
72 return 0;
75 static void show_dir_entry(const char *tag, struct dir_entry *ent)
77 int len = prefix_len;
78 int offset = prefix_offset;
80 if (len >= ent->len)
81 die("git ls-files: internal error - directory entry not superset of prefix");
83 if (pathspec && !pathspec_match(pathspec, ps_matched, ent->name, len))
84 return;
86 fputs(tag, stdout);
87 write_name_quoted(ent->name + offset, stdout, line_terminator);
90 static void show_other_files(struct dir_struct *dir)
92 int i;
94 for (i = 0; i < dir->nr; i++) {
95 struct dir_entry *ent = dir->entries[i];
96 if (!cache_name_is_other(ent->name, ent->len))
97 continue;
98 show_dir_entry(tag_other, ent);
102 static void show_killed_files(struct dir_struct *dir)
104 int i;
105 for (i = 0; i < dir->nr; i++) {
106 struct dir_entry *ent = dir->entries[i];
107 char *cp, *sp;
108 int pos, len, killed = 0;
110 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
111 sp = strchr(cp, '/');
112 if (!sp) {
113 /* If ent->name is prefix of an entry in the
114 * cache, it will be killed.
116 pos = cache_name_pos(ent->name, ent->len);
117 if (0 <= pos)
118 die("bug in show-killed-files");
119 pos = -pos - 1;
120 while (pos < active_nr &&
121 ce_stage(active_cache[pos]))
122 pos++; /* skip unmerged */
123 if (active_nr <= pos)
124 break;
125 /* pos points at a name immediately after
126 * ent->name in the cache. Does it expect
127 * ent->name to be a directory?
129 len = ce_namelen(active_cache[pos]);
130 if ((ent->len < len) &&
131 !strncmp(active_cache[pos]->name,
132 ent->name, ent->len) &&
133 active_cache[pos]->name[ent->len] == '/')
134 killed = 1;
135 break;
137 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
138 /* If any of the leading directories in
139 * ent->name is registered in the cache,
140 * ent->name will be killed.
142 killed = 1;
143 break;
146 if (killed)
147 show_dir_entry(tag_killed, dir->entries[i]);
151 static void show_ce_entry(const char *tag, struct cache_entry *ce)
153 int len = prefix_len;
154 int offset = prefix_offset;
156 if (len >= ce_namelen(ce))
157 die("git ls-files: internal error - cache entry not superset of prefix");
159 if (pathspec && !pathspec_match(pathspec, ps_matched, ce->name, len))
160 return;
162 if (tag && *tag && show_valid_bit &&
163 (ce->ce_flags & CE_VALID)) {
164 static char alttag[4];
165 memcpy(alttag, tag, 3);
166 if (isalpha(tag[0]))
167 alttag[0] = tolower(tag[0]);
168 else if (tag[0] == '?')
169 alttag[0] = '!';
170 else {
171 alttag[0] = 'v';
172 alttag[1] = tag[0];
173 alttag[2] = ' ';
174 alttag[3] = 0;
176 tag = alttag;
179 if (!show_stage) {
180 fputs(tag, stdout);
181 } else {
182 printf("%s%06o %s %d\t",
183 tag,
184 ce->ce_mode,
185 abbrev ? find_unique_abbrev(ce->sha1,abbrev)
186 : sha1_to_hex(ce->sha1),
187 ce_stage(ce));
189 write_name_quoted(ce->name + offset, stdout, line_terminator);
192 static void show_files(struct dir_struct *dir, const char *prefix)
194 int i;
196 /* For cached/deleted files we don't need to even do the readdir */
197 if (show_others || show_killed) {
198 const char *path = ".", *base = "";
199 int baselen = prefix_len;
201 if (baselen)
202 path = base = prefix;
203 read_directory(dir, path, base, baselen, pathspec);
204 if (show_others)
205 show_other_files(dir);
206 if (show_killed)
207 show_killed_files(dir);
209 if (show_cached | show_stage) {
210 for (i = 0; i < active_nr; i++) {
211 struct cache_entry *ce = active_cache[i];
212 int dtype = ce_to_dtype(ce);
213 if (excluded(dir, ce->name, &dtype) != dir->show_ignored)
214 continue;
215 if (show_unmerged && !ce_stage(ce))
216 continue;
217 if (ce->ce_flags & CE_UPDATE)
218 continue;
219 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
222 if (show_deleted | show_modified) {
223 for (i = 0; i < active_nr; i++) {
224 struct cache_entry *ce = active_cache[i];
225 struct stat st;
226 int err;
227 int dtype = ce_to_dtype(ce);
228 if (excluded(dir, ce->name, &dtype) != dir->show_ignored)
229 continue;
230 err = lstat(ce->name, &st);
231 if (show_deleted && err)
232 show_ce_entry(tag_removed, ce);
233 if (show_modified && ce_modified(ce, &st, 0))
234 show_ce_entry(tag_modified, ce);
240 * Prune the index to only contain stuff starting with "prefix"
242 static void prune_cache(const char *prefix)
244 int pos = cache_name_pos(prefix, prefix_len);
245 unsigned int first, last;
247 if (pos < 0)
248 pos = -pos-1;
249 memmove(active_cache, active_cache + pos,
250 (active_nr - pos) * sizeof(struct cache_entry *));
251 active_nr -= pos;
252 first = 0;
253 last = active_nr;
254 while (last > first) {
255 int next = (last + first) >> 1;
256 struct cache_entry *ce = active_cache[next];
257 if (!strncmp(ce->name, prefix, prefix_len)) {
258 first = next+1;
259 continue;
261 last = next;
263 active_nr = last;
266 static const char *verify_pathspec(const char *prefix)
268 const char **p, *n, *prev;
269 unsigned long max;
271 prev = NULL;
272 max = PATH_MAX;
273 for (p = pathspec; (n = *p) != NULL; p++) {
274 int i, len = 0;
275 for (i = 0; i < max; i++) {
276 char c = n[i];
277 if (prev && prev[i] != c)
278 break;
279 if (!c || c == '*' || c == '?')
280 break;
281 if (c == '/')
282 len = i+1;
284 prev = n;
285 if (len < max) {
286 max = len;
287 if (!max)
288 break;
292 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
293 die("git ls-files: cannot generate relative filenames containing '..'");
295 prefix_len = max;
296 return max ? xmemdupz(prev, max) : NULL;
300 * Read the tree specified with --with-tree option
301 * (typically, HEAD) into stage #1 and then
302 * squash them down to stage #0. This is used for
303 * --error-unmatch to list and check the path patterns
304 * that were given from the command line. We are not
305 * going to write this index out.
307 void overlay_tree_on_cache(const char *tree_name, const char *prefix)
309 struct tree *tree;
310 unsigned char sha1[20];
311 const char **match;
312 struct cache_entry *last_stage0 = NULL;
313 int i;
315 if (get_sha1(tree_name, sha1))
316 die("tree-ish %s not found.", tree_name);
317 tree = parse_tree_indirect(sha1);
318 if (!tree)
319 die("bad tree-ish %s", tree_name);
321 /* Hoist the unmerged entries up to stage #3 to make room */
322 for (i = 0; i < active_nr; i++) {
323 struct cache_entry *ce = active_cache[i];
324 if (!ce_stage(ce))
325 continue;
326 ce->ce_flags |= CE_STAGEMASK;
329 if (prefix) {
330 static const char *(matchbuf[2]);
331 matchbuf[0] = prefix;
332 matchbuf [1] = NULL;
333 match = matchbuf;
334 } else
335 match = NULL;
336 if (read_tree(tree, 1, match))
337 die("unable to read tree entries %s", tree_name);
339 for (i = 0; i < active_nr; i++) {
340 struct cache_entry *ce = active_cache[i];
341 switch (ce_stage(ce)) {
342 case 0:
343 last_stage0 = ce;
344 /* fallthru */
345 default:
346 continue;
347 case 1:
349 * If there is stage #0 entry for this, we do not
350 * need to show it. We use CE_UPDATE bit to mark
351 * such an entry.
353 if (last_stage0 &&
354 !strcmp(last_stage0->name, ce->name))
355 ce->ce_flags |= CE_UPDATE;
360 int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset)
363 * Make sure all pathspec matched; otherwise it is an error.
365 int num, errors = 0;
366 for (num = 0; pathspec[num]; num++) {
367 int other, found_dup;
369 if (ps_matched[num])
370 continue;
372 * The caller might have fed identical pathspec
373 * twice. Do not barf on such a mistake.
375 for (found_dup = other = 0;
376 !found_dup && pathspec[other];
377 other++) {
378 if (other == num || !ps_matched[other])
379 continue;
380 if (!strcmp(pathspec[other], pathspec[num]))
382 * Ok, we have a match already.
384 found_dup = 1;
386 if (found_dup)
387 continue;
389 error("pathspec '%s' did not match any file(s) known to git.",
390 pathspec[num] + prefix_offset);
391 errors++;
393 return errors;
396 static const char ls_files_usage[] =
397 "git ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
398 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
399 "[ --exclude-per-directory=<filename> ] [--exclude-standard] "
400 "[--full-name] [--abbrev] [--] [<file>]*";
402 int cmd_ls_files(int argc, const char **argv, const char *prefix)
404 int i;
405 int exc_given = 0, require_work_tree = 0;
406 struct dir_struct dir;
408 memset(&dir, 0, sizeof(dir));
409 if (prefix)
410 prefix_offset = strlen(prefix);
411 git_config(git_default_config, NULL);
413 for (i = 1; i < argc; i++) {
414 const char *arg = argv[i];
416 if (!strcmp(arg, "--")) {
417 i++;
418 break;
420 if (!strcmp(arg, "-z")) {
421 line_terminator = 0;
422 continue;
424 if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) {
425 tag_cached = "H ";
426 tag_unmerged = "M ";
427 tag_removed = "R ";
428 tag_modified = "C ";
429 tag_other = "? ";
430 tag_killed = "K ";
431 if (arg[1] == 'v')
432 show_valid_bit = 1;
433 continue;
435 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
436 show_cached = 1;
437 continue;
439 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
440 show_deleted = 1;
441 continue;
443 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
444 show_modified = 1;
445 require_work_tree = 1;
446 continue;
448 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
449 show_others = 1;
450 require_work_tree = 1;
451 continue;
453 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
454 dir.show_ignored = 1;
455 require_work_tree = 1;
456 continue;
458 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
459 show_stage = 1;
460 continue;
462 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
463 show_killed = 1;
464 require_work_tree = 1;
465 continue;
467 if (!strcmp(arg, "--directory")) {
468 dir.show_other_directories = 1;
469 continue;
471 if (!strcmp(arg, "--no-empty-directory")) {
472 dir.hide_empty_directories = 1;
473 continue;
475 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
476 /* There's no point in showing unmerged unless
477 * you also show the stage information.
479 show_stage = 1;
480 show_unmerged = 1;
481 continue;
483 if (!strcmp(arg, "-x") && i+1 < argc) {
484 exc_given = 1;
485 add_exclude(argv[++i], "", 0, &dir.exclude_list[EXC_CMDL]);
486 continue;
488 if (!prefixcmp(arg, "--exclude=")) {
489 exc_given = 1;
490 add_exclude(arg+10, "", 0, &dir.exclude_list[EXC_CMDL]);
491 continue;
493 if (!strcmp(arg, "-X") && i+1 < argc) {
494 exc_given = 1;
495 add_excludes_from_file(&dir, argv[++i]);
496 continue;
498 if (!prefixcmp(arg, "--exclude-from=")) {
499 exc_given = 1;
500 add_excludes_from_file(&dir, arg+15);
501 continue;
503 if (!prefixcmp(arg, "--exclude-per-directory=")) {
504 exc_given = 1;
505 dir.exclude_per_dir = arg + 24;
506 continue;
508 if (!strcmp(arg, "--exclude-standard")) {
509 exc_given = 1;
510 setup_standard_excludes(&dir);
511 continue;
513 if (!strcmp(arg, "--full-name")) {
514 prefix_offset = 0;
515 continue;
517 if (!strcmp(arg, "--error-unmatch")) {
518 error_unmatch = 1;
519 continue;
521 if (!prefixcmp(arg, "--with-tree=")) {
522 with_tree = arg + 12;
523 continue;
525 if (!prefixcmp(arg, "--abbrev=")) {
526 abbrev = strtoul(arg+9, NULL, 10);
527 if (abbrev && abbrev < MINIMUM_ABBREV)
528 abbrev = MINIMUM_ABBREV;
529 else if (abbrev > 40)
530 abbrev = 40;
531 continue;
533 if (!strcmp(arg, "--abbrev")) {
534 abbrev = DEFAULT_ABBREV;
535 continue;
537 if (*arg == '-')
538 usage(ls_files_usage);
539 break;
542 if (require_work_tree && !is_inside_work_tree())
543 setup_work_tree();
545 pathspec = get_pathspec(prefix, argv + i);
547 /* Verify that the pathspec matches the prefix */
548 if (pathspec)
549 prefix = verify_pathspec(prefix);
551 /* Treat unmatching pathspec elements as errors */
552 if (pathspec && error_unmatch) {
553 int num;
554 for (num = 0; pathspec[num]; num++)
556 ps_matched = xcalloc(1, num);
559 if (dir.show_ignored && !exc_given) {
560 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
561 argv[0]);
562 exit(1);
565 /* With no flags, we default to showing the cached files */
566 if (!(show_stage | show_deleted | show_others | show_unmerged |
567 show_killed | show_modified))
568 show_cached = 1;
570 read_cache();
571 if (prefix)
572 prune_cache(prefix);
573 if (with_tree) {
575 * Basic sanity check; show-stages and show-unmerged
576 * would not make any sense with this option.
578 if (show_stage || show_unmerged)
579 die("ls-files --with-tree is incompatible with -s or -u");
580 overlay_tree_on_cache(with_tree, prefix);
582 show_files(&dir, prefix);
584 if (ps_matched) {
585 int bad;
586 bad = report_path_error(ps_matched, pathspec, prefix_offset);
587 if (bad)
588 fprintf(stderr, "Did you forget to 'git add'?\n");
590 return bad ? 1 : 0;
593 return 0;