Code

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