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