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
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 = "";
39 static void show_dir_entry(const char *tag, struct dir_entry *ent)
42 int offset = prefix_offset;
45 die("git ls-files: internal error - directory entry not superset of prefix");
47 if (!match_pathspec(pathspec, ent->name, ent->len, len, ps_matched))
51 write_name_quoted(ent->name + offset, stdout, line_terminator);
54 static void show_other_files(struct dir_struct *dir)
58 for (i = 0; i < dir->nr; i++) {
59 struct dir_entry *ent = dir->entries[i];
60 if (!cache_name_is_other(ent->name, ent->len))
62 show_dir_entry(tag_other, ent);
66 static void show_killed_files(struct dir_struct *dir)
69 for (i = 0; i < dir->nr; i++) {
70 struct dir_entry *ent = dir->entries[i];
72 int pos, len, killed = 0;
74 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
77 /* If ent->name is prefix of an entry in the
78 * cache, it will be killed.
80 pos = cache_name_pos(ent->name, ent->len);
82 die("bug in show-killed-files");
84 while (pos < active_nr &&
85 ce_stage(active_cache[pos]))
86 pos++; /* skip unmerged */
89 /* pos points at a name immediately after
90 * ent->name in the cache. Does it expect
91 * ent->name to be a directory?
93 len = ce_namelen(active_cache[pos]);
94 if ((ent->len < len) &&
95 !strncmp(active_cache[pos]->name,
96 ent->name, ent->len) &&
97 active_cache[pos]->name[ent->len] == '/')
101 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
102 /* If any of the leading directories in
103 * ent->name is registered in the cache,
104 * ent->name will be killed.
111 show_dir_entry(tag_killed, dir->entries[i]);
115 static void show_ce_entry(const char *tag, struct cache_entry *ce)
117 int len = prefix_len;
118 int offset = prefix_offset;
120 if (len >= ce_namelen(ce))
121 die("git ls-files: internal error - cache entry not superset of prefix");
123 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), len, ps_matched))
126 if (tag && *tag && show_valid_bit &&
127 (ce->ce_flags & CE_VALID)) {
128 static char alttag[4];
129 memcpy(alttag, tag, 3);
131 alttag[0] = tolower(tag[0]);
132 else if (tag[0] == '?')
146 printf("%s%06o %s %d\t",
149 abbrev ? find_unique_abbrev(ce->sha1,abbrev)
150 : sha1_to_hex(ce->sha1),
153 write_name_quoted(ce->name + offset, stdout, line_terminator);
156 static void show_files(struct dir_struct *dir, const char *prefix)
160 /* For cached/deleted files we don't need to even do the readdir */
161 if (show_others || show_killed) {
162 const char *path = ".", *base = "";
163 int baselen = prefix_len;
166 path = base = prefix;
167 read_directory(dir, path, base, baselen, pathspec);
169 show_other_files(dir);
171 show_killed_files(dir);
173 if (show_cached | show_stage) {
174 for (i = 0; i < active_nr; i++) {
175 struct cache_entry *ce = active_cache[i];
176 int dtype = ce_to_dtype(ce);
177 if (excluded(dir, ce->name, &dtype) !=
178 !!(dir->flags & DIR_SHOW_IGNORED))
180 if (show_unmerged && !ce_stage(ce))
182 if (ce->ce_flags & CE_UPDATE)
184 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
187 if (show_deleted | show_modified) {
188 for (i = 0; i < active_nr; i++) {
189 struct cache_entry *ce = active_cache[i];
192 int dtype = ce_to_dtype(ce);
193 if (excluded(dir, ce->name, &dtype) !=
194 !!(dir->flags & DIR_SHOW_IGNORED))
196 if (ce->ce_flags & CE_UPDATE)
198 err = lstat(ce->name, &st);
199 if (show_deleted && err)
200 show_ce_entry(tag_removed, ce);
201 if (show_modified && ce_modified(ce, &st, 0))
202 show_ce_entry(tag_modified, ce);
208 * Prune the index to only contain stuff starting with "prefix"
210 static void prune_cache(const char *prefix)
212 int pos = cache_name_pos(prefix, prefix_len);
213 unsigned int first, last;
217 memmove(active_cache, active_cache + pos,
218 (active_nr - pos) * sizeof(struct cache_entry *));
222 while (last > first) {
223 int next = (last + first) >> 1;
224 struct cache_entry *ce = active_cache[next];
225 if (!strncmp(ce->name, prefix, prefix_len)) {
234 static const char *verify_pathspec(const char *prefix)
236 const char **p, *n, *prev;
241 for (p = pathspec; (n = *p) != NULL; p++) {
243 for (i = 0; i < max; i++) {
245 if (prev && prev[i] != c)
247 if (!c || c == '*' || c == '?')
260 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
261 die("git ls-files: cannot generate relative filenames containing '..'");
264 return max ? xmemdupz(prev, max) : NULL;
267 static void strip_trailing_slash_from_submodules(void)
271 for (p = pathspec; *p != NULL; p++) {
272 int len = strlen(*p), pos;
274 if (len < 1 || (*p)[len - 1] != '/')
276 pos = cache_name_pos(*p, len - 1);
277 if (pos >= 0 && S_ISGITLINK(active_cache[pos]->ce_mode))
278 *p = xstrndup(*p, len - 1);
283 * Read the tree specified with --with-tree option
284 * (typically, HEAD) into stage #1 and then
285 * squash them down to stage #0. This is used for
286 * --error-unmatch to list and check the path patterns
287 * that were given from the command line. We are not
288 * going to write this index out.
290 void overlay_tree_on_cache(const char *tree_name, const char *prefix)
293 unsigned char sha1[20];
295 struct cache_entry *last_stage0 = NULL;
298 if (get_sha1(tree_name, sha1))
299 die("tree-ish %s not found.", tree_name);
300 tree = parse_tree_indirect(sha1);
302 die("bad tree-ish %s", tree_name);
304 /* Hoist the unmerged entries up to stage #3 to make room */
305 for (i = 0; i < active_nr; i++) {
306 struct cache_entry *ce = active_cache[i];
309 ce->ce_flags |= CE_STAGEMASK;
313 static const char *(matchbuf[2]);
314 matchbuf[0] = prefix;
319 if (read_tree(tree, 1, match))
320 die("unable to read tree entries %s", tree_name);
322 for (i = 0; i < active_nr; i++) {
323 struct cache_entry *ce = active_cache[i];
324 switch (ce_stage(ce)) {
332 * If there is stage #0 entry for this, we do not
333 * need to show it. We use CE_UPDATE bit to mark
337 !strcmp(last_stage0->name, ce->name))
338 ce->ce_flags |= CE_UPDATE;
343 int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset)
346 * Make sure all pathspec matched; otherwise it is an error.
349 for (num = 0; pathspec[num]; num++) {
350 int other, found_dup;
355 * The caller might have fed identical pathspec
356 * twice. Do not barf on such a mistake.
358 for (found_dup = other = 0;
359 !found_dup && pathspec[other];
361 if (other == num || !ps_matched[other])
363 if (!strcmp(pathspec[other], pathspec[num]))
365 * Ok, we have a match already.
372 error("pathspec '%s' did not match any file(s) known to git.",
373 pathspec[num] + prefix_offset);
379 static const char ls_files_usage[] =
380 "git ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
381 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
382 "[ --exclude-per-directory=<filename> ] [--exclude-standard] "
383 "[--full-name] [--abbrev] [--] [<file>]*";
385 int cmd_ls_files(int argc, const char **argv, const char *prefix)
388 int exc_given = 0, require_work_tree = 0;
389 struct dir_struct dir;
391 memset(&dir, 0, sizeof(dir));
393 prefix_offset = strlen(prefix);
394 git_config(git_default_config, NULL);
396 for (i = 1; i < argc; i++) {
397 const char *arg = argv[i];
399 if (!strcmp(arg, "--")) {
403 if (!strcmp(arg, "-z")) {
407 if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) {
418 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
422 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
426 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
428 require_work_tree = 1;
431 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
433 require_work_tree = 1;
436 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
437 dir.flags |= DIR_SHOW_IGNORED;
438 require_work_tree = 1;
441 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
445 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
447 require_work_tree = 1;
450 if (!strcmp(arg, "--directory")) {
451 dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
454 if (!strcmp(arg, "--no-empty-directory")) {
455 dir.flags |= DIR_HIDE_EMPTY_DIRECTORIES;
458 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
459 /* There's no point in showing unmerged unless
460 * you also show the stage information.
466 if (!strcmp(arg, "-x") && i+1 < argc) {
468 add_exclude(argv[++i], "", 0, &dir.exclude_list[EXC_CMDL]);
471 if (!prefixcmp(arg, "--exclude=")) {
473 add_exclude(arg+10, "", 0, &dir.exclude_list[EXC_CMDL]);
476 if (!strcmp(arg, "-X") && i+1 < argc) {
478 add_excludes_from_file(&dir, argv[++i]);
481 if (!prefixcmp(arg, "--exclude-from=")) {
483 add_excludes_from_file(&dir, arg+15);
486 if (!prefixcmp(arg, "--exclude-per-directory=")) {
488 dir.exclude_per_dir = arg + 24;
491 if (!strcmp(arg, "--exclude-standard")) {
493 setup_standard_excludes(&dir);
496 if (!strcmp(arg, "--full-name")) {
500 if (!strcmp(arg, "--error-unmatch")) {
504 if (!prefixcmp(arg, "--with-tree=")) {
505 with_tree = arg + 12;
508 if (!prefixcmp(arg, "--abbrev=")) {
509 abbrev = strtoul(arg+9, NULL, 10);
510 if (abbrev && abbrev < MINIMUM_ABBREV)
511 abbrev = MINIMUM_ABBREV;
512 else if (abbrev > 40)
516 if (!strcmp(arg, "--abbrev")) {
517 abbrev = DEFAULT_ABBREV;
521 usage(ls_files_usage);
525 if (require_work_tree && !is_inside_work_tree())
528 pathspec = get_pathspec(prefix, argv + i);
530 /* be nice with submodule patsh ending in a slash */
533 strip_trailing_slash_from_submodules();
535 /* Verify that the pathspec matches the prefix */
537 prefix = verify_pathspec(prefix);
539 /* Treat unmatching pathspec elements as errors */
540 if (pathspec && error_unmatch) {
542 for (num = 0; pathspec[num]; num++)
544 ps_matched = xcalloc(1, num);
547 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given) {
548 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
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))
562 * Basic sanity check; show-stages and show-unmerged
563 * would not make any sense with this option.
565 if (show_stage || show_unmerged)
566 die("ls-files --with-tree is incompatible with -s or -u");
567 overlay_tree_on_cache(with_tree, prefix);
569 show_files(&dir, prefix);
573 bad = report_path_error(ps_matched, pathspec, prefix_offset);
575 fprintf(stderr, "Did you forget to 'git add'?\n");