Code

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