bash: complete more options for 'git rebase'
[git.git] / builtin-ls-files.c
blobc5c0407b0b824327b0bbc6ae50ae1a030761b2b9
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"
15 static int abbrev;
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;
32 static int exc_given;
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 = "";
41 static void show_dir_entry(const char *tag, struct dir_entry *ent)
43 int len = prefix_len;
44 int offset = prefix_offset;
46 if (len >= ent->len)
47 die("git ls-files: internal error - directory entry not superset of prefix");
49 if (!match_pathspec(pathspec, ent->name, ent->len, len, ps_matched))
50 return;
52 fputs(tag, stdout);
53 write_name_quoted(ent->name + offset, stdout, line_terminator);
56 static void show_other_files(struct dir_struct *dir)
58 int i;
60 for (i = 0; i < dir->nr; i++) {
61 struct dir_entry *ent = dir->entries[i];
62 if (!cache_name_is_other(ent->name, ent->len))
63 continue;
64 show_dir_entry(tag_other, ent);
68 static void show_killed_files(struct dir_struct *dir)
70 int i;
71 for (i = 0; i < dir->nr; i++) {
72 struct dir_entry *ent = dir->entries[i];
73 char *cp, *sp;
74 int pos, len, killed = 0;
76 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
77 sp = strchr(cp, '/');
78 if (!sp) {
79 /* If ent->name is prefix of an entry in the
80 * cache, it will be killed.
82 pos = cache_name_pos(ent->name, ent->len);
83 if (0 <= pos)
84 die("bug in show-killed-files");
85 pos = -pos - 1;
86 while (pos < active_nr &&
87 ce_stage(active_cache[pos]))
88 pos++; /* skip unmerged */
89 if (active_nr <= pos)
90 break;
91 /* pos points at a name immediately after
92 * ent->name in the cache. Does it expect
93 * ent->name to be a directory?
95 len = ce_namelen(active_cache[pos]);
96 if ((ent->len < len) &&
97 !strncmp(active_cache[pos]->name,
98 ent->name, ent->len) &&
99 active_cache[pos]->name[ent->len] == '/')
100 killed = 1;
101 break;
103 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
104 /* If any of the leading directories in
105 * ent->name is registered in the cache,
106 * ent->name will be killed.
108 killed = 1;
109 break;
112 if (killed)
113 show_dir_entry(tag_killed, dir->entries[i]);
117 static void show_ce_entry(const char *tag, struct cache_entry *ce)
119 int len = prefix_len;
120 int offset = prefix_offset;
122 if (len >= ce_namelen(ce))
123 die("git ls-files: internal error - cache entry not superset of prefix");
125 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), len, ps_matched))
126 return;
128 if (tag && *tag && show_valid_bit &&
129 (ce->ce_flags & CE_VALID)) {
130 static char alttag[4];
131 memcpy(alttag, tag, 3);
132 if (isalpha(tag[0]))
133 alttag[0] = tolower(tag[0]);
134 else if (tag[0] == '?')
135 alttag[0] = '!';
136 else {
137 alttag[0] = 'v';
138 alttag[1] = tag[0];
139 alttag[2] = ' ';
140 alttag[3] = 0;
142 tag = alttag;
145 if (!show_stage) {
146 fputs(tag, stdout);
147 } else {
148 printf("%s%06o %s %d\t",
149 tag,
150 ce->ce_mode,
151 abbrev ? find_unique_abbrev(ce->sha1,abbrev)
152 : sha1_to_hex(ce->sha1),
153 ce_stage(ce));
155 write_name_quoted(ce->name + offset, stdout, line_terminator);
158 static void show_files(struct dir_struct *dir, const char *prefix)
160 int i;
162 /* For cached/deleted files we don't need to even do the readdir */
163 if (show_others || show_killed) {
164 fill_directory(dir, pathspec);
165 if (show_others)
166 show_other_files(dir);
167 if (show_killed)
168 show_killed_files(dir);
170 if (show_cached | show_stage) {
171 for (i = 0; i < active_nr; i++) {
172 struct cache_entry *ce = active_cache[i];
173 if (show_unmerged && !ce_stage(ce))
174 continue;
175 if (ce->ce_flags & CE_UPDATE)
176 continue;
177 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
180 if (show_deleted | show_modified) {
181 for (i = 0; i < active_nr; i++) {
182 struct cache_entry *ce = active_cache[i];
183 struct stat st;
184 int err;
185 if (ce->ce_flags & CE_UPDATE)
186 continue;
187 err = lstat(ce->name, &st);
188 if (show_deleted && err)
189 show_ce_entry(tag_removed, ce);
190 if (show_modified && ce_modified(ce, &st, 0))
191 show_ce_entry(tag_modified, ce);
197 * Prune the index to only contain stuff starting with "prefix"
199 static void prune_cache(const char *prefix)
201 int pos = cache_name_pos(prefix, prefix_len);
202 unsigned int first, last;
204 if (pos < 0)
205 pos = -pos-1;
206 memmove(active_cache, active_cache + pos,
207 (active_nr - pos) * sizeof(struct cache_entry *));
208 active_nr -= pos;
209 first = 0;
210 last = active_nr;
211 while (last > first) {
212 int next = (last + first) >> 1;
213 struct cache_entry *ce = active_cache[next];
214 if (!strncmp(ce->name, prefix, prefix_len)) {
215 first = next+1;
216 continue;
218 last = next;
220 active_nr = last;
223 static const char *verify_pathspec(const char *prefix)
225 const char **p, *n, *prev;
226 unsigned long max;
228 prev = NULL;
229 max = PATH_MAX;
230 for (p = pathspec; (n = *p) != NULL; p++) {
231 int i, len = 0;
232 for (i = 0; i < max; i++) {
233 char c = n[i];
234 if (prev && prev[i] != c)
235 break;
236 if (!c || c == '*' || c == '?')
237 break;
238 if (c == '/')
239 len = i+1;
241 prev = n;
242 if (len < max) {
243 max = len;
244 if (!max)
245 break;
249 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
250 die("git ls-files: cannot generate relative filenames containing '..'");
252 prefix_len = max;
253 return max ? xmemdupz(prev, max) : NULL;
256 static void strip_trailing_slash_from_submodules(void)
258 const char **p;
260 for (p = pathspec; *p != NULL; p++) {
261 int len = strlen(*p), pos;
263 if (len < 1 || (*p)[len - 1] != '/')
264 continue;
265 pos = cache_name_pos(*p, len - 1);
266 if (pos >= 0 && S_ISGITLINK(active_cache[pos]->ce_mode))
267 *p = xstrndup(*p, len - 1);
272 * Read the tree specified with --with-tree option
273 * (typically, HEAD) into stage #1 and then
274 * squash them down to stage #0. This is used for
275 * --error-unmatch to list and check the path patterns
276 * that were given from the command line. We are not
277 * going to write this index out.
279 void overlay_tree_on_cache(const char *tree_name, const char *prefix)
281 struct tree *tree;
282 unsigned char sha1[20];
283 const char **match;
284 struct cache_entry *last_stage0 = NULL;
285 int i;
287 if (get_sha1(tree_name, sha1))
288 die("tree-ish %s not found.", tree_name);
289 tree = parse_tree_indirect(sha1);
290 if (!tree)
291 die("bad tree-ish %s", tree_name);
293 /* Hoist the unmerged entries up to stage #3 to make room */
294 for (i = 0; i < active_nr; i++) {
295 struct cache_entry *ce = active_cache[i];
296 if (!ce_stage(ce))
297 continue;
298 ce->ce_flags |= CE_STAGEMASK;
301 if (prefix) {
302 static const char *(matchbuf[2]);
303 matchbuf[0] = prefix;
304 matchbuf[1] = NULL;
305 match = matchbuf;
306 } else
307 match = NULL;
308 if (read_tree(tree, 1, match))
309 die("unable to read tree entries %s", tree_name);
311 for (i = 0; i < active_nr; i++) {
312 struct cache_entry *ce = active_cache[i];
313 switch (ce_stage(ce)) {
314 case 0:
315 last_stage0 = ce;
316 /* fallthru */
317 default:
318 continue;
319 case 1:
321 * If there is stage #0 entry for this, we do not
322 * need to show it. We use CE_UPDATE bit to mark
323 * such an entry.
325 if (last_stage0 &&
326 !strcmp(last_stage0->name, ce->name))
327 ce->ce_flags |= CE_UPDATE;
332 int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset)
335 * Make sure all pathspec matched; otherwise it is an error.
337 int num, errors = 0;
338 for (num = 0; pathspec[num]; num++) {
339 int other, found_dup;
341 if (ps_matched[num])
342 continue;
344 * The caller might have fed identical pathspec
345 * twice. Do not barf on such a mistake.
347 for (found_dup = other = 0;
348 !found_dup && pathspec[other];
349 other++) {
350 if (other == num || !ps_matched[other])
351 continue;
352 if (!strcmp(pathspec[other], pathspec[num]))
354 * Ok, we have a match already.
356 found_dup = 1;
358 if (found_dup)
359 continue;
361 error("pathspec '%s' did not match any file(s) known to git.",
362 pathspec[num] + prefix_offset);
363 errors++;
365 return errors;
368 static const char * const ls_files_usage[] = {
369 "git ls-files [options] [<file>]*",
370 NULL
373 static int option_parse_z(const struct option *opt,
374 const char *arg, int unset)
376 line_terminator = unset ? '\n' : '\0';
378 return 0;
381 static int option_parse_exclude(const struct option *opt,
382 const char *arg, int unset)
384 struct exclude_list *list = opt->value;
386 exc_given = 1;
387 add_exclude(arg, "", 0, list);
389 return 0;
392 static int option_parse_exclude_from(const struct option *opt,
393 const char *arg, int unset)
395 struct dir_struct *dir = opt->value;
397 exc_given = 1;
398 add_excludes_from_file(dir, arg);
400 return 0;
403 static int option_parse_exclude_standard(const struct option *opt,
404 const char *arg, int unset)
406 struct dir_struct *dir = opt->value;
408 exc_given = 1;
409 setup_standard_excludes(dir);
411 return 0;
414 int cmd_ls_files(int argc, const char **argv, const char *prefix)
416 int require_work_tree = 0, show_tag = 0;
417 struct dir_struct dir;
418 struct option builtin_ls_files_options[] = {
419 { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
420 "paths are separated with NUL character",
421 PARSE_OPT_NOARG, option_parse_z },
422 OPT_BOOLEAN('t', NULL, &show_tag,
423 "identify the file status with tags"),
424 OPT_BOOLEAN('v', NULL, &show_valid_bit,
425 "use lowercase letters for 'assume unchanged' files"),
426 OPT_BOOLEAN('c', "cached", &show_cached,
427 "show cached files in the output (default)"),
428 OPT_BOOLEAN('d', "deleted", &show_deleted,
429 "show deleted files in the output"),
430 OPT_BOOLEAN('m', "modified", &show_modified,
431 "show modified files in the output"),
432 OPT_BOOLEAN('o', "others", &show_others,
433 "show other files in the output"),
434 OPT_BIT('i', "ignored", &dir.flags,
435 "show ignored files in the output",
436 DIR_SHOW_IGNORED),
437 OPT_BOOLEAN('s', "stage", &show_stage,
438 "show staged contents' object name in the output"),
439 OPT_BOOLEAN('k', "killed", &show_killed,
440 "show files on the filesystem that need to be removed"),
441 OPT_BIT(0, "directory", &dir.flags,
442 "show 'other' directories' name only",
443 DIR_SHOW_OTHER_DIRECTORIES),
444 OPT_NEGBIT(0, "empty-directory", &dir.flags,
445 "don't show empty directories",
446 DIR_HIDE_EMPTY_DIRECTORIES),
447 OPT_BOOLEAN('u', "unmerged", &show_unmerged,
448 "show unmerged files in the output"),
449 { OPTION_CALLBACK, 'x', "exclude", &dir.exclude_list[EXC_CMDL], "pattern",
450 "skip files matching pattern",
451 0, option_parse_exclude },
452 { OPTION_CALLBACK, 'X', "exclude-from", &dir, "file",
453 "exclude patterns are read from <file>",
454 0, option_parse_exclude_from },
455 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, "file",
456 "read additional per-directory exclude patterns in <file>"),
457 { OPTION_CALLBACK, 0, "exclude-standard", &dir, NULL,
458 "add the standard git exclusions",
459 PARSE_OPT_NOARG, option_parse_exclude_standard },
460 { OPTION_SET_INT, 0, "full-name", &prefix_offset, NULL,
461 "make the output relative to the project top directory",
462 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL },
463 OPT_BOOLEAN(0, "error-unmatch", &error_unmatch,
464 "if any <file> is not in the index, treat this as an error"),
465 OPT_STRING(0, "with-tree", &with_tree, "tree-ish",
466 "pretend that paths removed since <tree-ish> are still present"),
467 OPT__ABBREV(&abbrev),
468 OPT_END()
471 memset(&dir, 0, sizeof(dir));
472 if (prefix)
473 prefix_offset = strlen(prefix);
474 git_config(git_default_config, NULL);
476 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
477 ls_files_usage, 0);
478 if (show_tag || show_valid_bit) {
479 tag_cached = "H ";
480 tag_unmerged = "M ";
481 tag_removed = "R ";
482 tag_modified = "C ";
483 tag_other = "? ";
484 tag_killed = "K ";
486 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
487 require_work_tree = 1;
488 if (show_unmerged)
490 * There's no point in showing unmerged unless
491 * you also show the stage information.
493 show_stage = 1;
494 if (dir.exclude_per_dir)
495 exc_given = 1;
497 if (require_work_tree && !is_inside_work_tree())
498 setup_work_tree();
500 pathspec = get_pathspec(prefix, argv);
502 /* be nice with submodule paths ending in a slash */
503 read_cache();
504 if (pathspec)
505 strip_trailing_slash_from_submodules();
507 /* Verify that the pathspec matches the prefix */
508 if (pathspec)
509 prefix = verify_pathspec(prefix);
511 /* Treat unmatching pathspec elements as errors */
512 if (pathspec && error_unmatch) {
513 int num;
514 for (num = 0; pathspec[num]; num++)
516 ps_matched = xcalloc(1, num);
519 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
520 die("ls-files --ignored needs some exclude pattern");
522 /* With no flags, we default to showing the cached files */
523 if (!(show_stage | show_deleted | show_others | show_unmerged |
524 show_killed | show_modified))
525 show_cached = 1;
527 if (prefix)
528 prune_cache(prefix);
529 if (with_tree) {
531 * Basic sanity check; show-stages and show-unmerged
532 * would not make any sense with this option.
534 if (show_stage || show_unmerged)
535 die("ls-files --with-tree is incompatible with -s or -u");
536 overlay_tree_on_cache(with_tree, prefix);
538 show_files(&dir, prefix);
540 if (ps_matched) {
541 int bad;
542 bad = report_path_error(ps_matched, pathspec, prefix_offset);
543 if (bad)
544 fprintf(stderr, "Did you forget to 'git add'?\n");
546 return bad ? 1 : 0;
549 return 0;