gitweb: Add git_merge sub
[git/gsoc2010-gitweb.git] / builtin / ls-files.c
blob7113b95f1f1b4c697ca7efb628d2c092e0d89f31
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 const char *prefix;
30 static int max_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 write_name(const char *name, size_t len)
49 struct strbuf path = STRBUF_INIT;
50 if (prefix_offset > 0)
51 quote_path_relative(name, len, &path, prefix);
52 else
53 strbuf_add(&path, name, len);
54 write_name_quoted(strbuf_detach(&path, NULL), stdout, line_terminator);
57 static void show_dir_entry(const char *tag, struct dir_entry *ent)
59 int len = max_prefix_len;
61 if (len >= ent->len)
62 die("git ls-files: internal error - directory entry not superset of prefix");
64 if (!match_pathspec(pathspec, ent->name, ent->len, len, ps_matched))
65 return;
67 fputs(tag, stdout);
68 write_name(ent->name, ent->len);
71 static void show_other_files(struct dir_struct *dir)
73 int i;
75 for (i = 0; i < dir->nr; i++) {
76 struct dir_entry *ent = dir->entries[i];
77 if (!cache_name_is_other(ent->name, ent->len))
78 continue;
79 show_dir_entry(tag_other, ent);
83 static void show_killed_files(struct dir_struct *dir)
85 int i;
86 for (i = 0; i < dir->nr; i++) {
87 struct dir_entry *ent = dir->entries[i];
88 char *cp, *sp;
89 int pos, len, killed = 0;
91 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
92 sp = strchr(cp, '/');
93 if (!sp) {
94 /* If ent->name is prefix of an entry in the
95 * cache, it will be killed.
97 pos = cache_name_pos(ent->name, ent->len);
98 if (0 <= pos)
99 die("bug in show-killed-files");
100 pos = -pos - 1;
101 while (pos < active_nr &&
102 ce_stage(active_cache[pos]))
103 pos++; /* skip unmerged */
104 if (active_nr <= pos)
105 break;
106 /* pos points at a name immediately after
107 * ent->name in the cache. Does it expect
108 * ent->name to be a directory?
110 len = ce_namelen(active_cache[pos]);
111 if ((ent->len < len) &&
112 !strncmp(active_cache[pos]->name,
113 ent->name, ent->len) &&
114 active_cache[pos]->name[ent->len] == '/')
115 killed = 1;
116 break;
118 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
119 /* If any of the leading directories in
120 * ent->name is registered in the cache,
121 * ent->name will be killed.
123 killed = 1;
124 break;
127 if (killed)
128 show_dir_entry(tag_killed, dir->entries[i]);
132 static void show_ce_entry(const char *tag, struct cache_entry *ce)
134 int len = max_prefix_len;
136 if (len >= ce_namelen(ce))
137 die("git ls-files: internal error - cache entry not superset of prefix");
139 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), len, ps_matched))
140 return;
142 if (tag && *tag && show_valid_bit &&
143 (ce->ce_flags & CE_VALID)) {
144 static char alttag[4];
145 memcpy(alttag, tag, 3);
146 if (isalpha(tag[0]))
147 alttag[0] = tolower(tag[0]);
148 else if (tag[0] == '?')
149 alttag[0] = '!';
150 else {
151 alttag[0] = 'v';
152 alttag[1] = tag[0];
153 alttag[2] = ' ';
154 alttag[3] = 0;
156 tag = alttag;
159 if (!show_stage) {
160 fputs(tag, stdout);
161 } else {
162 printf("%s%06o %s %d\t",
163 tag,
164 ce->ce_mode,
165 find_unique_abbrev(ce->sha1,abbrev),
166 ce_stage(ce));
168 write_name(ce->name, ce_namelen(ce));
171 static int show_one_ru(struct string_list_item *item, void *cbdata)
173 const char *path = item->string;
174 struct resolve_undo_info *ui = item->util;
175 int i, len;
177 len = strlen(path);
178 if (len < max_prefix_len)
179 return 0; /* outside of the prefix */
180 if (!match_pathspec(pathspec, path, len, max_prefix_len, ps_matched))
181 return 0; /* uninterested */
182 for (i = 0; i < 3; i++) {
183 if (!ui->mode[i])
184 continue;
185 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
186 find_unique_abbrev(ui->sha1[i], abbrev),
187 i + 1);
188 write_name(path, len);
190 return 0;
193 static void show_ru_info(void)
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)
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) {
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, max_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, max_prefix_len)) {
268 first = next+1;
269 continue;
271 last = next;
273 active_nr = last;
276 static const char *pathspec_prefix(const char *prefix)
278 const char **p, *n, *prev;
279 unsigned long max;
281 if (!pathspec) {
282 max_prefix_len = prefix ? strlen(prefix) : 0;
283 return prefix;
286 prev = NULL;
287 max = PATH_MAX;
288 for (p = pathspec; (n = *p) != NULL; p++) {
289 int i, len = 0;
290 for (i = 0; i < max; i++) {
291 char c = n[i];
292 if (prev && prev[i] != c)
293 break;
294 if (!c || c == '*' || c == '?')
295 break;
296 if (c == '/')
297 len = i+1;
299 prev = n;
300 if (len < max) {
301 max = len;
302 if (!max)
303 break;
307 max_prefix_len = max;
308 return max ? xmemdupz(prev, max) : NULL;
311 static void strip_trailing_slash_from_submodules(void)
313 const char **p;
315 for (p = pathspec; *p != NULL; p++) {
316 int len = strlen(*p), pos;
318 if (len < 1 || (*p)[len - 1] != '/')
319 continue;
320 pos = cache_name_pos(*p, len - 1);
321 if (pos >= 0 && S_ISGITLINK(active_cache[pos]->ce_mode))
322 *p = xstrndup(*p, len - 1);
327 * Read the tree specified with --with-tree option
328 * (typically, HEAD) into stage #1 and then
329 * squash them down to stage #0. This is used for
330 * --error-unmatch to list and check the path patterns
331 * that were given from the command line. We are not
332 * going to write this index out.
334 void overlay_tree_on_cache(const char *tree_name, const char *prefix)
336 struct tree *tree;
337 unsigned char sha1[20];
338 const char **match;
339 struct cache_entry *last_stage0 = NULL;
340 int i;
342 if (get_sha1(tree_name, sha1))
343 die("tree-ish %s not found.", tree_name);
344 tree = parse_tree_indirect(sha1);
345 if (!tree)
346 die("bad tree-ish %s", tree_name);
348 /* Hoist the unmerged entries up to stage #3 to make room */
349 for (i = 0; i < active_nr; i++) {
350 struct cache_entry *ce = active_cache[i];
351 if (!ce_stage(ce))
352 continue;
353 ce->ce_flags |= CE_STAGEMASK;
356 if (prefix) {
357 static const char *(matchbuf[2]);
358 matchbuf[0] = prefix;
359 matchbuf[1] = NULL;
360 match = matchbuf;
361 } else
362 match = NULL;
363 if (read_tree(tree, 1, match))
364 die("unable to read tree entries %s", tree_name);
366 for (i = 0; i < active_nr; i++) {
367 struct cache_entry *ce = active_cache[i];
368 switch (ce_stage(ce)) {
369 case 0:
370 last_stage0 = ce;
371 /* fallthru */
372 default:
373 continue;
374 case 1:
376 * If there is stage #0 entry for this, we do not
377 * need to show it. We use CE_UPDATE bit to mark
378 * such an entry.
380 if (last_stage0 &&
381 !strcmp(last_stage0->name, ce->name))
382 ce->ce_flags |= CE_UPDATE;
387 int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset)
390 * Make sure all pathspec matched; otherwise it is an error.
392 int num, errors = 0;
393 for (num = 0; pathspec[num]; num++) {
394 int other, found_dup;
396 if (ps_matched[num])
397 continue;
399 * The caller might have fed identical pathspec
400 * twice. Do not barf on such a mistake.
402 for (found_dup = other = 0;
403 !found_dup && pathspec[other];
404 other++) {
405 if (other == num || !ps_matched[other])
406 continue;
407 if (!strcmp(pathspec[other], pathspec[num]))
409 * Ok, we have a match already.
411 found_dup = 1;
413 if (found_dup)
414 continue;
416 error("pathspec '%s' did not match any file(s) known to git.",
417 pathspec[num] + prefix_offset);
418 errors++;
420 return errors;
423 static const char * const ls_files_usage[] = {
424 "git ls-files [options] [<file>]*",
425 NULL
428 static int option_parse_z(const struct option *opt,
429 const char *arg, int unset)
431 line_terminator = unset ? '\n' : '\0';
433 return 0;
436 static int option_parse_exclude(const struct option *opt,
437 const char *arg, int unset)
439 struct exclude_list *list = opt->value;
441 exc_given = 1;
442 add_exclude(arg, "", 0, list);
444 return 0;
447 static int option_parse_exclude_from(const struct option *opt,
448 const char *arg, int unset)
450 struct dir_struct *dir = opt->value;
452 exc_given = 1;
453 add_excludes_from_file(dir, arg);
455 return 0;
458 static int option_parse_exclude_standard(const struct option *opt,
459 const char *arg, int unset)
461 struct dir_struct *dir = opt->value;
463 exc_given = 1;
464 setup_standard_excludes(dir);
466 return 0;
469 int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
471 int require_work_tree = 0, show_tag = 0;
472 const char *max_prefix;
473 struct dir_struct dir;
474 struct option builtin_ls_files_options[] = {
475 { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
476 "paths are separated with NUL character",
477 PARSE_OPT_NOARG, option_parse_z },
478 OPT_BOOLEAN('t', NULL, &show_tag,
479 "identify the file status with tags"),
480 OPT_BOOLEAN('v', NULL, &show_valid_bit,
481 "use lowercase letters for 'assume unchanged' files"),
482 OPT_BOOLEAN('c', "cached", &show_cached,
483 "show cached files in the output (default)"),
484 OPT_BOOLEAN('d', "deleted", &show_deleted,
485 "show deleted files in the output"),
486 OPT_BOOLEAN('m', "modified", &show_modified,
487 "show modified files in the output"),
488 OPT_BOOLEAN('o', "others", &show_others,
489 "show other files in the output"),
490 OPT_BIT('i', "ignored", &dir.flags,
491 "show ignored files in the output",
492 DIR_SHOW_IGNORED),
493 OPT_BOOLEAN('s', "stage", &show_stage,
494 "show staged contents' object name in the output"),
495 OPT_BOOLEAN('k', "killed", &show_killed,
496 "show files on the filesystem that need to be removed"),
497 OPT_BIT(0, "directory", &dir.flags,
498 "show 'other' directories' name only",
499 DIR_SHOW_OTHER_DIRECTORIES),
500 OPT_NEGBIT(0, "empty-directory", &dir.flags,
501 "don't show empty directories",
502 DIR_HIDE_EMPTY_DIRECTORIES),
503 OPT_BOOLEAN('u', "unmerged", &show_unmerged,
504 "show unmerged files in the output"),
505 OPT_BOOLEAN(0, "resolve-undo", &show_resolve_undo,
506 "show resolve-undo information"),
507 { OPTION_CALLBACK, 'x', "exclude", &dir.exclude_list[EXC_CMDL], "pattern",
508 "skip files matching pattern",
509 0, option_parse_exclude },
510 { OPTION_CALLBACK, 'X', "exclude-from", &dir, "file",
511 "exclude patterns are read from <file>",
512 0, option_parse_exclude_from },
513 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, "file",
514 "read additional per-directory exclude patterns in <file>"),
515 { OPTION_CALLBACK, 0, "exclude-standard", &dir, NULL,
516 "add the standard git exclusions",
517 PARSE_OPT_NOARG, option_parse_exclude_standard },
518 { OPTION_SET_INT, 0, "full-name", &prefix_offset, NULL,
519 "make the output relative to the project top directory",
520 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL },
521 OPT_BOOLEAN(0, "error-unmatch", &error_unmatch,
522 "if any <file> is not in the index, treat this as an error"),
523 OPT_STRING(0, "with-tree", &with_tree, "tree-ish",
524 "pretend that paths removed since <tree-ish> are still present"),
525 OPT__ABBREV(&abbrev),
526 OPT_END()
529 memset(&dir, 0, sizeof(dir));
530 prefix = cmd_prefix;
531 if (prefix)
532 prefix_offset = strlen(prefix);
533 git_config(git_default_config, NULL);
535 if (read_cache() < 0)
536 die("index file corrupt");
538 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
539 ls_files_usage, 0);
540 if (show_tag || show_valid_bit) {
541 tag_cached = "H ";
542 tag_unmerged = "M ";
543 tag_removed = "R ";
544 tag_modified = "C ";
545 tag_other = "? ";
546 tag_killed = "K ";
547 tag_skip_worktree = "S ";
548 tag_resolve_undo = "U ";
550 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
551 require_work_tree = 1;
552 if (show_unmerged)
554 * There's no point in showing unmerged unless
555 * you also show the stage information.
557 show_stage = 1;
558 if (dir.exclude_per_dir)
559 exc_given = 1;
561 if (require_work_tree && !is_inside_work_tree())
562 setup_work_tree();
564 pathspec = get_pathspec(prefix, argv);
566 /* be nice with submodule paths ending in a slash */
567 if (pathspec)
568 strip_trailing_slash_from_submodules();
570 /* Find common prefix for all pathspec's */
571 max_prefix = pathspec_prefix(prefix);
573 /* Treat unmatching pathspec elements as errors */
574 if (pathspec && error_unmatch) {
575 int num;
576 for (num = 0; pathspec[num]; num++)
578 ps_matched = xcalloc(1, num);
581 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
582 die("ls-files --ignored needs some exclude pattern");
584 /* With no flags, we default to showing the cached files */
585 if (!(show_stage | show_deleted | show_others | show_unmerged |
586 show_killed | show_modified | show_resolve_undo))
587 show_cached = 1;
589 if (max_prefix)
590 prune_cache(max_prefix);
591 if (with_tree) {
593 * Basic sanity check; show-stages and show-unmerged
594 * would not make any sense with this option.
596 if (show_stage || show_unmerged)
597 die("ls-files --with-tree is incompatible with -s or -u");
598 overlay_tree_on_cache(with_tree, max_prefix);
600 show_files(&dir);
601 if (show_resolve_undo)
602 show_ru_info();
604 if (ps_matched) {
605 int bad;
606 bad = report_path_error(ps_matched, pathspec, prefix_offset);
607 if (bad)
608 fprintf(stderr, "Did you forget to 'git add'?\n");
610 return bad ? 1 : 0;
613 return 0;