Code

48dd3f77a4157c6f9f7e52e9e9884faa06568955
[git.git] / builtin-ls-files.c
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  */
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 = "";
40 /*
41  * Match a pathspec against a filename. The first "len" characters
42  * are the common prefix
43  */
44 static int match(const char **spec, char *ps_matched,
45                  const char *filename, int len)
46 {
47         const char *m;
49         while ((m = *spec++) != NULL) {
50                 int matchlen = strlen(m + len);
52                 if (!matchlen)
53                         goto matched;
54                 if (!strncmp(m + len, filename + len, matchlen)) {
55                         if (m[len + matchlen - 1] == '/')
56                                 goto matched;
57                         switch (filename[len + matchlen]) {
58                         case '/': case '\0':
59                                 goto matched;
60                         }
61                 }
62                 if (!fnmatch(m + len, filename + len, 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;
71         }
72         return 0;
73 }
75 static void show_dir_entry(const char *tag, struct dir_entry *ent)
76 {
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 && !match(pathspec, ps_matched, ent->name, len))
84                 return;
86         fputs(tag, stdout);
87         write_name_quoted("", 0, ent->name + offset, line_terminator, stdout);
88         putchar(line_terminator);
89 }
91 static void show_other_files(struct dir_struct *dir)
92 {
93         int i;
96         /*
97          * Skip matching and unmerged entries for the paths,
98          * since we want just "others".
99          *
100          * (Matching entries are normally pruned during
101          * the directory tree walk, but will show up for
102          * gitlinks because we don't necessarily have
103          * dir->show_other_directories set to suppress
104          * them).
105          */
106         for (i = 0; i < dir->nr; i++) {
107                 struct dir_entry *ent = dir->entries[i];
108                 int len, pos;
109                 struct cache_entry *ce;
111                 /*
112                  * Remove the '/' at the end that directory
113                  * walking adds for directory entries.
114                  */
115                 len = ent->len;
116                 if (len && ent->name[len-1] == '/')
117                         len--;
118                 pos = cache_name_pos(ent->name, len);
119                 if (0 <= pos)
120                         continue;       /* exact match */
121                 pos = -pos - 1;
122                 if (pos < active_nr) {
123                         ce = active_cache[pos];
124                         if (ce_namelen(ce) == len &&
125                             !memcmp(ce->name, ent->name, len))
126                                 continue; /* Yup, this one exists unmerged */
127                 }
128                 show_dir_entry(tag_other, ent);
129         }
132 static void show_killed_files(struct dir_struct *dir)
134         int i;
135         for (i = 0; i < dir->nr; i++) {
136                 struct dir_entry *ent = dir->entries[i];
137                 char *cp, *sp;
138                 int pos, len, killed = 0;
140                 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
141                         sp = strchr(cp, '/');
142                         if (!sp) {
143                                 /* If ent->name is prefix of an entry in the
144                                  * cache, it will be killed.
145                                  */
146                                 pos = cache_name_pos(ent->name, ent->len);
147                                 if (0 <= pos)
148                                         die("bug in show-killed-files");
149                                 pos = -pos - 1;
150                                 while (pos < active_nr &&
151                                        ce_stage(active_cache[pos]))
152                                         pos++; /* skip unmerged */
153                                 if (active_nr <= pos)
154                                         break;
155                                 /* pos points at a name immediately after
156                                  * ent->name in the cache.  Does it expect
157                                  * ent->name to be a directory?
158                                  */
159                                 len = ce_namelen(active_cache[pos]);
160                                 if ((ent->len < len) &&
161                                     !strncmp(active_cache[pos]->name,
162                                              ent->name, ent->len) &&
163                                     active_cache[pos]->name[ent->len] == '/')
164                                         killed = 1;
165                                 break;
166                         }
167                         if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
168                                 /* If any of the leading directories in
169                                  * ent->name is registered in the cache,
170                                  * ent->name will be killed.
171                                  */
172                                 killed = 1;
173                                 break;
174                         }
175                 }
176                 if (killed)
177                         show_dir_entry(tag_killed, dir->entries[i]);
178         }
181 static void show_ce_entry(const char *tag, struct cache_entry *ce)
183         int len = prefix_len;
184         int offset = prefix_offset;
186         if (len >= ce_namelen(ce))
187                 die("git-ls-files: internal error - cache entry not superset of prefix");
189         if (pathspec && !match(pathspec, ps_matched, ce->name, len))
190                 return;
192         if (tag && *tag && show_valid_bit &&
193             (ce->ce_flags & htons(CE_VALID))) {
194                 static char alttag[4];
195                 memcpy(alttag, tag, 3);
196                 if (isalpha(tag[0]))
197                         alttag[0] = tolower(tag[0]);
198                 else if (tag[0] == '?')
199                         alttag[0] = '!';
200                 else {
201                         alttag[0] = 'v';
202                         alttag[1] = tag[0];
203                         alttag[2] = ' ';
204                         alttag[3] = 0;
205                 }
206                 tag = alttag;
207         }
209         if (!show_stage) {
210                 fputs(tag, stdout);
211                 write_name_quoted("", 0, ce->name + offset,
212                                   line_terminator, stdout);
213                 putchar(line_terminator);
214         }
215         else {
216                 printf("%s%06o %s %d\t",
217                        tag,
218                        ntohl(ce->ce_mode),
219                        abbrev ? find_unique_abbrev(ce->sha1,abbrev)
220                                 : sha1_to_hex(ce->sha1),
221                        ce_stage(ce));
222                 write_name_quoted("", 0, ce->name + offset,
223                                   line_terminator, stdout);
224                 putchar(line_terminator);
225         }
228 static void show_files(struct dir_struct *dir, const char *prefix)
230         int i;
232         /* For cached/deleted files we don't need to even do the readdir */
233         if (show_others || show_killed) {
234                 const char *path = ".", *base = "";
235                 int baselen = prefix_len;
237                 if (baselen)
238                         path = base = prefix;
239                 read_directory(dir, path, base, baselen, pathspec);
240                 if (show_others)
241                         show_other_files(dir);
242                 if (show_killed)
243                         show_killed_files(dir);
244         }
245         if (show_cached | show_stage) {
246                 for (i = 0; i < active_nr; i++) {
247                         struct cache_entry *ce = active_cache[i];
248                         if (excluded(dir, ce->name) != dir->show_ignored)
249                                 continue;
250                         if (show_unmerged && !ce_stage(ce))
251                                 continue;
252                         if (ce->ce_flags & htons(CE_UPDATE))
253                                 continue;
254                         show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
255                 }
256         }
257         if (show_deleted | show_modified) {
258                 for (i = 0; i < active_nr; i++) {
259                         struct cache_entry *ce = active_cache[i];
260                         struct stat st;
261                         int err;
262                         if (excluded(dir, ce->name) != dir->show_ignored)
263                                 continue;
264                         err = lstat(ce->name, &st);
265                         if (show_deleted && err)
266                                 show_ce_entry(tag_removed, ce);
267                         if (show_modified && ce_modified(ce, &st, 0))
268                                 show_ce_entry(tag_modified, ce);
269                 }
270         }
273 /*
274  * Prune the index to only contain stuff starting with "prefix"
275  */
276 static void prune_cache(const char *prefix)
278         int pos = cache_name_pos(prefix, prefix_len);
279         unsigned int first, last;
281         if (pos < 0)
282                 pos = -pos-1;
283         active_cache += pos;
284         active_nr -= pos;
285         first = 0;
286         last = active_nr;
287         while (last > first) {
288                 int next = (last + first) >> 1;
289                 struct cache_entry *ce = active_cache[next];
290                 if (!strncmp(ce->name, prefix, prefix_len)) {
291                         first = next+1;
292                         continue;
293                 }
294                 last = next;
295         }
296         active_nr = last;
299 static const char *verify_pathspec(const char *prefix)
301         const char **p, *n, *prev;
302         unsigned long max;
304         prev = NULL;
305         max = PATH_MAX;
306         for (p = pathspec; (n = *p) != NULL; p++) {
307                 int i, len = 0;
308                 for (i = 0; i < max; i++) {
309                         char c = n[i];
310                         if (prev && prev[i] != c)
311                                 break;
312                         if (!c || c == '*' || c == '?')
313                                 break;
314                         if (c == '/')
315                                 len = i+1;
316                 }
317                 prev = n;
318                 if (len < max) {
319                         max = len;
320                         if (!max)
321                                 break;
322                 }
323         }
325         if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
326                 die("git-ls-files: cannot generate relative filenames containing '..'");
328         prefix_len = max;
329         return max ? xmemdupz(prev, max) : NULL;
332 /*
333  * Read the tree specified with --with-tree option
334  * (typically, HEAD) into stage #1 and then
335  * squash them down to stage #0.  This is used for
336  * --error-unmatch to list and check the path patterns
337  * that were given from the command line.  We are not
338  * going to write this index out.
339  */
340 static void overlay_tree(const char *tree_name, const char *prefix)
342         struct tree *tree;
343         unsigned char sha1[20];
344         const char **match;
345         struct cache_entry *last_stage0 = NULL;
346         int i;
348         if (get_sha1(tree_name, sha1))
349                 die("tree-ish %s not found.", tree_name);
350         tree = parse_tree_indirect(sha1);
351         if (!tree)
352                 die("bad tree-ish %s", tree_name);
354         /* Hoist the unmerged entries up to stage #3 to make room */
355         for (i = 0; i < active_nr; i++) {
356                 struct cache_entry *ce = active_cache[i];
357                 if (!ce_stage(ce))
358                         continue;
359                 ce->ce_flags |= htons(CE_STAGEMASK);
360         }
362         if (prefix) {
363                 static const char *(matchbuf[2]);
364                 matchbuf[0] = prefix;
365                 matchbuf [1] = NULL;
366                 match = matchbuf;
367         } else
368                 match = NULL;
369         if (read_tree(tree, 1, match))
370                 die("unable to read tree entries %s", tree_name);
372         for (i = 0; i < active_nr; i++) {
373                 struct cache_entry *ce = active_cache[i];
374                 switch (ce_stage(ce)) {
375                 case 0:
376                         last_stage0 = ce;
377                         /* fallthru */
378                 default:
379                         continue;
380                 case 1:
381                         /*
382                          * If there is stage #0 entry for this, we do not
383                          * need to show it.  We use CE_UPDATE bit to mark
384                          * such an entry.
385                          */
386                         if (last_stage0 &&
387                             !strcmp(last_stage0->name, ce->name))
388                                 ce->ce_flags |= htons(CE_UPDATE);
389                 }
390         }
393 static const char ls_files_usage[] =
394         "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
395         "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
396         "[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] "
397         "[--] [<file>]*";
399 int cmd_ls_files(int argc, const char **argv, const char *prefix)
401         int i;
402         int exc_given = 0, require_work_tree = 0;
403         struct dir_struct dir;
405         memset(&dir, 0, sizeof(dir));
406         if (prefix)
407                 prefix_offset = strlen(prefix);
408         git_config(git_default_config);
410         for (i = 1; i < argc; i++) {
411                 const char *arg = argv[i];
413                 if (!strcmp(arg, "--")) {
414                         i++;
415                         break;
416                 }
417                 if (!strcmp(arg, "-z")) {
418                         line_terminator = 0;
419                         continue;
420                 }
421                 if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) {
422                         tag_cached = "H ";
423                         tag_unmerged = "M ";
424                         tag_removed = "R ";
425                         tag_modified = "C ";
426                         tag_other = "? ";
427                         tag_killed = "K ";
428                         if (arg[1] == 'v')
429                                 show_valid_bit = 1;
430                         continue;
431                 }
432                 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
433                         show_cached = 1;
434                         continue;
435                 }
436                 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
437                         show_deleted = 1;
438                         continue;
439                 }
440                 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
441                         show_modified = 1;
442                         require_work_tree = 1;
443                         continue;
444                 }
445                 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
446                         show_others = 1;
447                         require_work_tree = 1;
448                         continue;
449                 }
450                 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
451                         dir.show_ignored = 1;
452                         require_work_tree = 1;
453                         continue;
454                 }
455                 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
456                         show_stage = 1;
457                         continue;
458                 }
459                 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
460                         show_killed = 1;
461                         require_work_tree = 1;
462                         continue;
463                 }
464                 if (!strcmp(arg, "--directory")) {
465                         dir.show_other_directories = 1;
466                         continue;
467                 }
468                 if (!strcmp(arg, "--no-empty-directory")) {
469                         dir.hide_empty_directories = 1;
470                         continue;
471                 }
472                 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
473                         /* There's no point in showing unmerged unless
474                          * you also show the stage information.
475                          */
476                         show_stage = 1;
477                         show_unmerged = 1;
478                         continue;
479                 }
480                 if (!strcmp(arg, "-x") && i+1 < argc) {
481                         exc_given = 1;
482                         add_exclude(argv[++i], "", 0, &dir.exclude_list[EXC_CMDL]);
483                         continue;
484                 }
485                 if (!prefixcmp(arg, "--exclude=")) {
486                         exc_given = 1;
487                         add_exclude(arg+10, "", 0, &dir.exclude_list[EXC_CMDL]);
488                         continue;
489                 }
490                 if (!strcmp(arg, "-X") && i+1 < argc) {
491                         exc_given = 1;
492                         add_excludes_from_file(&dir, argv[++i]);
493                         continue;
494                 }
495                 if (!prefixcmp(arg, "--exclude-from=")) {
496                         exc_given = 1;
497                         add_excludes_from_file(&dir, arg+15);
498                         continue;
499                 }
500                 if (!prefixcmp(arg, "--exclude-per-directory=")) {
501                         exc_given = 1;
502                         dir.exclude_per_dir = arg + 24;
503                         continue;
504                 }
505                 if (!strcmp(arg, "--full-name")) {
506                         prefix_offset = 0;
507                         continue;
508                 }
509                 if (!strcmp(arg, "--error-unmatch")) {
510                         error_unmatch = 1;
511                         continue;
512                 }
513                 if (!prefixcmp(arg, "--with-tree=")) {
514                         with_tree = arg + 12;
515                         continue;
516                 }
517                 if (!prefixcmp(arg, "--abbrev=")) {
518                         abbrev = strtoul(arg+9, NULL, 10);
519                         if (abbrev && abbrev < MINIMUM_ABBREV)
520                                 abbrev = MINIMUM_ABBREV;
521                         else if (abbrev > 40)
522                                 abbrev = 40;
523                         continue;
524                 }
525                 if (!strcmp(arg, "--abbrev")) {
526                         abbrev = DEFAULT_ABBREV;
527                         continue;
528                 }
529                 if (*arg == '-')
530                         usage(ls_files_usage);
531                 break;
532         }
534         if (require_work_tree && !is_inside_work_tree()) {
535                 const char *work_tree = get_git_work_tree();
536                 if (!work_tree || chdir(work_tree))
537                         die("This operation must be run in a work tree");
538         }
540         pathspec = get_pathspec(prefix, argv + i);
542         /* Verify that the pathspec matches the prefix */
543         if (pathspec)
544                 prefix = verify_pathspec(prefix);
546         /* Treat unmatching pathspec elements as errors */
547         if (pathspec && error_unmatch) {
548                 int num;
549                 for (num = 0; pathspec[num]; num++)
550                         ;
551                 ps_matched = xcalloc(1, num);
552         }
554         if (dir.show_ignored && !exc_given) {
555                 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
556                         argv[0]);
557                 exit(1);
558         }
560         /* With no flags, we default to showing the cached files */
561         if (!(show_stage | show_deleted | show_others | show_unmerged |
562               show_killed | show_modified))
563                 show_cached = 1;
565         read_cache();
566         if (prefix)
567                 prune_cache(prefix);
568         if (with_tree) {
569                 /*
570                  * Basic sanity check; show-stages and show-unmerged
571                  * would not make any sense with this option.
572                  */
573                 if (show_stage || show_unmerged)
574                         die("ls-files --with-tree is incompatible with -s or -u");
575                 overlay_tree(with_tree, prefix);
576         }
577         show_files(&dir, prefix);
579         if (ps_matched) {
580                 /* We need to make sure all pathspec matched otherwise
581                  * it is an error.
582                  */
583                 int num, errors = 0;
584                 for (num = 0; pathspec[num]; num++) {
585                         int other, found_dup;
587                         if (ps_matched[num])
588                                 continue;
589                         /*
590                          * The caller might have fed identical pathspec
591                          * twice.  Do not barf on such a mistake.
592                          */
593                         for (found_dup = other = 0;
594                              !found_dup && pathspec[other];
595                              other++) {
596                                 if (other == num || !ps_matched[other])
597                                         continue;
598                                 if (!strcmp(pathspec[other], pathspec[num]))
599                                         /*
600                                          * Ok, we have a match already.
601                                          */
602                                         found_dup = 1;
603                         }
604                         if (found_dup)
605                                 continue;
607                         error("pathspec '%s' did not match any file(s) known to git.",
608                               pathspec[num] + prefix_offset);
609                         errors++;
610                 }
612                 if (errors)
613                         fprintf(stderr, "Did you forget to 'git add'?\n");
615                 return errors ? 1 : 0;
616         }
618         return 0;