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