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