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.
6 * Copyright (C) Linus Torvalds, 2005
13 #include "parse-options.h"
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;
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)
45 int offset = prefix_offset;
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))
54 write_name_quoted(ent->name + offset, stdout, line_terminator);
57 static void show_other_files(struct dir_struct *dir)
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))
65 show_dir_entry(tag_other, ent);
69 static void show_killed_files(struct dir_struct *dir)
72 for (i = 0; i < dir->nr; i++) {
73 struct dir_entry *ent = dir->entries[i];
75 int pos, len, killed = 0;
77 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
80 /* If ent->name is prefix of an entry in the
81 * cache, it will be killed.
83 pos = cache_name_pos(ent->name, ent->len);
85 die("bug in show-killed-files");
87 while (pos < active_nr &&
88 ce_stage(active_cache[pos]))
89 pos++; /* skip unmerged */
92 /* pos points at a name immediately after
93 * ent->name in the cache. Does it expect
94 * ent->name to be a directory?
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] == '/')
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.
114 show_dir_entry(tag_killed, dir->entries[i]);
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))
129 if (tag && *tag && show_valid_bit &&
130 (ce->ce_flags & CE_VALID)) {
131 static char alttag[4];
132 memcpy(alttag, tag, 3);
134 alttag[0] = tolower(tag[0]);
135 else if (tag[0] == '?')
149 printf("%s%06o %s %d\t",
152 abbrev ? find_unique_abbrev(ce->sha1,abbrev)
153 : sha1_to_hex(ce->sha1),
156 write_name_quoted(ce->name + offset, stdout, line_terminator);
159 static void show_files(struct dir_struct *dir, const char *prefix)
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);
167 show_other_files(dir);
169 show_killed_files(dir);
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))
178 if (show_unmerged && !ce_stage(ce))
180 if (ce->ce_flags & CE_UPDATE)
182 show_ce_entry(ce_stage(ce) ? tag_unmerged :
183 (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce);
186 if (show_deleted | show_modified) {
187 for (i = 0; i < active_nr; i++) {
188 struct cache_entry *ce = active_cache[i];
191 int dtype = ce_to_dtype(ce);
192 if (dir->flags & DIR_SHOW_IGNORED &&
193 !excluded(dir, ce->name, &dtype))
195 if (ce->ce_flags & CE_UPDATE)
197 if (ce_skip_worktree(ce))
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);
209 * Prune the index to only contain stuff starting with "prefix"
211 static void prune_cache(const char *prefix)
213 int pos = cache_name_pos(prefix, prefix_len);
214 unsigned int first, last;
218 memmove(active_cache, active_cache + pos,
219 (active_nr - pos) * sizeof(struct cache_entry *));
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)) {
235 static const char *verify_pathspec(const char *prefix)
237 const char **p, *n, *prev;
242 for (p = pathspec; (n = *p) != NULL; p++) {
244 for (i = 0; i < max; i++) {
246 if (prev && prev[i] != c)
248 if (!c || c == '*' || c == '?')
261 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
262 die("git ls-files: cannot generate relative filenames containing '..'");
265 return max ? xmemdupz(prev, max) : NULL;
268 static void strip_trailing_slash_from_submodules(void)
272 for (p = pathspec; *p != NULL; p++) {
273 int len = strlen(*p), pos;
275 if (len < 1 || (*p)[len - 1] != '/')
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);
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.
291 void overlay_tree_on_cache(const char *tree_name, const char *prefix)
294 unsigned char sha1[20];
296 struct cache_entry *last_stage0 = NULL;
299 if (get_sha1(tree_name, sha1))
300 die("tree-ish %s not found.", tree_name);
301 tree = parse_tree_indirect(sha1);
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];
310 ce->ce_flags |= CE_STAGEMASK;
314 static const char *(matchbuf[2]);
315 matchbuf[0] = prefix;
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)) {
333 * If there is stage #0 entry for this, we do not
334 * need to show it. We use CE_UPDATE bit to mark
338 !strcmp(last_stage0->name, ce->name))
339 ce->ce_flags |= CE_UPDATE;
344 int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset)
347 * Make sure all pathspec matched; otherwise it is an error.
350 for (num = 0; pathspec[num]; num++) {
351 int other, found_dup;
356 * The caller might have fed identical pathspec
357 * twice. Do not barf on such a mistake.
359 for (found_dup = other = 0;
360 !found_dup && pathspec[other];
362 if (other == num || !ps_matched[other])
364 if (!strcmp(pathspec[other], pathspec[num]))
366 * Ok, we have a match already.
373 error("pathspec '%s' did not match any file(s) known to git.",
374 pathspec[num] + prefix_offset);
380 static const char * const ls_files_usage[] = {
381 "git ls-files [options] [<file>]*",
385 static int option_parse_z(const struct option *opt,
386 const char *arg, int unset)
388 line_terminator = unset ? '\n' : '\0';
393 static int option_parse_exclude(const struct option *opt,
394 const char *arg, int unset)
396 struct exclude_list *list = opt->value;
399 add_exclude(arg, "", 0, list);
404 static int option_parse_exclude_from(const struct option *opt,
405 const char *arg, int unset)
407 struct dir_struct *dir = opt->value;
410 add_excludes_from_file(dir, arg);
415 static int option_parse_exclude_standard(const struct option *opt,
416 const char *arg, int unset)
418 struct dir_struct *dir = opt->value;
421 setup_standard_excludes(dir);
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",
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),
483 memset(&dir, 0, sizeof(dir));
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,
493 if (show_tag || show_valid_bit) {
500 tag_skip_worktree = "S ";
502 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
503 require_work_tree = 1;
506 * There's no point in showing unmerged unless
507 * you also show the stage information.
510 if (dir.exclude_per_dir)
513 if (require_work_tree && !is_inside_work_tree())
516 pathspec = get_pathspec(prefix, argv);
518 /* be nice with submodule paths ending in a slash */
520 strip_trailing_slash_from_submodules();
522 /* Verify that the pathspec matches the prefix */
524 prefix = verify_pathspec(prefix);
526 /* Treat unmatching pathspec elements as errors */
527 if (pathspec && error_unmatch) {
529 for (num = 0; pathspec[num]; num++)
531 ps_matched = xcalloc(1, num);
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))
546 * Basic sanity check; show-stages and show-unmerged
547 * would not make any sense with this option.
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);
553 show_files(&dir, prefix);
557 bad = report_path_error(ps_matched, pathspec, prefix_offset);
559 fprintf(stderr, "Did you forget to 'git add'?\n");