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