quote.c: substitute path_relative with relative_path
[git/mingw.git] / builtin / ls-files.c
blob67e3713cd331dd878f579400c17964ce4c31e9ce
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';
28 static int debug_mode;
30 static const char *prefix;
31 static int max_prefix_len;
32 static int prefix_len;
33 static const char **pathspec;
34 static int error_unmatch;
35 static char *ps_matched;
36 static const char *with_tree;
37 static int exc_given;
38 static int exclude_args;
40 static const char *tag_cached = "";
41 static const char *tag_unmerged = "";
42 static const char *tag_removed = "";
43 static const char *tag_other = "";
44 static const char *tag_killed = "";
45 static const char *tag_modified = "";
46 static const char *tag_skip_worktree = "";
47 static const char *tag_resolve_undo = "";
49 static void write_name(const char* name, size_t len)
52 * With "--full-name", prefix_len=0; write_name_quoted_relative()
53 * ignores prefix_len, so this caller needs to pass empty string
54 * in that case (a NULL is good for "").
56 write_name_quoted_relative(name, len, prefix_len ? prefix : NULL,
57 prefix_len, stdout, line_terminator);
60 static void show_dir_entry(const char *tag, struct dir_entry *ent)
62 int len = max_prefix_len;
64 if (len >= ent->len)
65 die("git ls-files: internal error - directory entry not superset of prefix");
67 if (!match_pathspec(pathspec, ent->name, ent->len, len, ps_matched))
68 return;
70 fputs(tag, stdout);
71 write_name(ent->name, ent->len);
74 static void show_other_files(struct dir_struct *dir)
76 int i;
78 for (i = 0; i < dir->nr; i++) {
79 struct dir_entry *ent = dir->entries[i];
80 if (!cache_name_is_other(ent->name, ent->len))
81 continue;
82 show_dir_entry(tag_other, ent);
86 static void show_killed_files(struct dir_struct *dir)
88 int i;
89 for (i = 0; i < dir->nr; i++) {
90 struct dir_entry *ent = dir->entries[i];
91 char *cp, *sp;
92 int pos, len, killed = 0;
94 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
95 sp = strchr(cp, '/');
96 if (!sp) {
97 /* If ent->name is prefix of an entry in the
98 * cache, it will be killed.
100 pos = cache_name_pos(ent->name, ent->len);
101 if (0 <= pos)
102 die("bug in show-killed-files");
103 pos = -pos - 1;
104 while (pos < active_nr &&
105 ce_stage(active_cache[pos]))
106 pos++; /* skip unmerged */
107 if (active_nr <= pos)
108 break;
109 /* pos points at a name immediately after
110 * ent->name in the cache. Does it expect
111 * ent->name to be a directory?
113 len = ce_namelen(active_cache[pos]);
114 if ((ent->len < len) &&
115 !strncmp(active_cache[pos]->name,
116 ent->name, ent->len) &&
117 active_cache[pos]->name[ent->len] == '/')
118 killed = 1;
119 break;
121 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
122 /* If any of the leading directories in
123 * ent->name is registered in the cache,
124 * ent->name will be killed.
126 killed = 1;
127 break;
130 if (killed)
131 show_dir_entry(tag_killed, dir->entries[i]);
135 static void show_ce_entry(const char *tag, struct cache_entry *ce)
137 int len = max_prefix_len;
139 if (len >= ce_namelen(ce))
140 die("git ls-files: internal error - cache entry not superset of prefix");
142 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), len, ps_matched))
143 return;
145 if (tag && *tag && show_valid_bit &&
146 (ce->ce_flags & CE_VALID)) {
147 static char alttag[4];
148 memcpy(alttag, tag, 3);
149 if (isalpha(tag[0]))
150 alttag[0] = tolower(tag[0]);
151 else if (tag[0] == '?')
152 alttag[0] = '!';
153 else {
154 alttag[0] = 'v';
155 alttag[1] = tag[0];
156 alttag[2] = ' ';
157 alttag[3] = 0;
159 tag = alttag;
162 if (!show_stage) {
163 fputs(tag, stdout);
164 } else {
165 printf("%s%06o %s %d\t",
166 tag,
167 ce->ce_mode,
168 find_unique_abbrev(ce->sha1,abbrev),
169 ce_stage(ce));
171 write_name(ce->name, ce_namelen(ce));
172 if (debug_mode) {
173 printf(" ctime: %d:%d\n", ce->ce_ctime.sec, ce->ce_ctime.nsec);
174 printf(" mtime: %d:%d\n", ce->ce_mtime.sec, ce->ce_mtime.nsec);
175 printf(" dev: %d\tino: %d\n", ce->ce_dev, ce->ce_ino);
176 printf(" uid: %d\tgid: %d\n", ce->ce_uid, ce->ce_gid);
177 printf(" size: %d\tflags: %x\n", ce->ce_size, ce->ce_flags);
181 static void show_ru_info(void)
183 struct string_list_item *item;
185 if (!the_index.resolve_undo)
186 return;
188 for_each_string_list_item(item, the_index.resolve_undo) {
189 const char *path = item->string;
190 struct resolve_undo_info *ui = item->util;
191 int i, len;
193 len = strlen(path);
194 if (len < max_prefix_len)
195 continue; /* outside of the prefix */
196 if (!match_pathspec(pathspec, path, len, max_prefix_len, ps_matched))
197 continue; /* uninterested */
198 for (i = 0; i < 3; i++) {
199 if (!ui->mode[i])
200 continue;
201 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
202 find_unique_abbrev(ui->sha1[i], abbrev),
203 i + 1);
204 write_name(path, len);
209 static int ce_excluded(struct dir_struct *dir, struct cache_entry *ce)
211 int dtype = ce_to_dtype(ce);
212 return is_excluded(dir, ce->name, &dtype);
215 static void show_files(struct dir_struct *dir)
217 int i;
219 /* For cached/deleted files we don't need to even do the readdir */
220 if (show_others || show_killed) {
221 fill_directory(dir, pathspec);
222 if (show_others)
223 show_other_files(dir);
224 if (show_killed)
225 show_killed_files(dir);
227 if (show_cached | show_stage) {
228 for (i = 0; i < active_nr; i++) {
229 struct cache_entry *ce = active_cache[i];
230 if ((dir->flags & DIR_SHOW_IGNORED) &&
231 !ce_excluded(dir, ce))
232 continue;
233 if (show_unmerged && !ce_stage(ce))
234 continue;
235 if (ce->ce_flags & CE_UPDATE)
236 continue;
237 show_ce_entry(ce_stage(ce) ? tag_unmerged :
238 (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce);
241 if (show_deleted | show_modified) {
242 for (i = 0; i < active_nr; i++) {
243 struct cache_entry *ce = active_cache[i];
244 struct stat st;
245 int err;
246 if ((dir->flags & DIR_SHOW_IGNORED) &&
247 !ce_excluded(dir, ce))
248 continue;
249 if (ce->ce_flags & CE_UPDATE)
250 continue;
251 if (ce_skip_worktree(ce))
252 continue;
253 err = lstat(ce->name, &st);
254 if (show_deleted && err)
255 show_ce_entry(tag_removed, ce);
256 if (show_modified && ce_modified(ce, &st, 0))
257 show_ce_entry(tag_modified, ce);
263 * Prune the index to only contain stuff starting with "prefix"
265 static void prune_cache(const char *prefix)
267 int pos = cache_name_pos(prefix, max_prefix_len);
268 unsigned int first, last;
270 if (pos < 0)
271 pos = -pos-1;
272 memmove(active_cache, active_cache + pos,
273 (active_nr - pos) * sizeof(struct cache_entry *));
274 active_nr -= pos;
275 first = 0;
276 last = active_nr;
277 while (last > first) {
278 int next = (last + first) >> 1;
279 struct cache_entry *ce = active_cache[next];
280 if (!strncmp(ce->name, prefix, max_prefix_len)) {
281 first = next+1;
282 continue;
284 last = next;
286 active_nr = last;
289 static void strip_trailing_slash_from_submodules(void)
291 const char **p;
293 for (p = pathspec; *p != NULL; p++) {
294 int len = strlen(*p), pos;
296 if (len < 1 || (*p)[len - 1] != '/')
297 continue;
298 pos = cache_name_pos(*p, len - 1);
299 if (pos >= 0 && S_ISGITLINK(active_cache[pos]->ce_mode))
300 *p = xstrndup(*p, len - 1);
305 * Read the tree specified with --with-tree option
306 * (typically, HEAD) into stage #1 and then
307 * squash them down to stage #0. This is used for
308 * --error-unmatch to list and check the path patterns
309 * that were given from the command line. We are not
310 * going to write this index out.
312 void overlay_tree_on_cache(const char *tree_name, const char *prefix)
314 struct tree *tree;
315 unsigned char sha1[20];
316 struct pathspec pathspec;
317 struct cache_entry *last_stage0 = NULL;
318 int i;
320 if (get_sha1(tree_name, sha1))
321 die("tree-ish %s not found.", tree_name);
322 tree = parse_tree_indirect(sha1);
323 if (!tree)
324 die("bad tree-ish %s", tree_name);
326 /* Hoist the unmerged entries up to stage #3 to make room */
327 for (i = 0; i < active_nr; i++) {
328 struct cache_entry *ce = active_cache[i];
329 if (!ce_stage(ce))
330 continue;
331 ce->ce_flags |= CE_STAGEMASK;
334 if (prefix) {
335 static const char *(matchbuf[2]);
336 matchbuf[0] = prefix;
337 matchbuf[1] = NULL;
338 init_pathspec(&pathspec, matchbuf);
339 pathspec.items[0].nowildcard_len = pathspec.items[0].len;
340 } else
341 init_pathspec(&pathspec, NULL);
342 if (read_tree(tree, 1, &pathspec))
343 die("unable to read tree entries %s", tree_name);
345 for (i = 0; i < active_nr; i++) {
346 struct cache_entry *ce = active_cache[i];
347 switch (ce_stage(ce)) {
348 case 0:
349 last_stage0 = ce;
350 /* fallthru */
351 default:
352 continue;
353 case 1:
355 * If there is stage #0 entry for this, we do not
356 * need to show it. We use CE_UPDATE bit to mark
357 * such an entry.
359 if (last_stage0 &&
360 !strcmp(last_stage0->name, ce->name))
361 ce->ce_flags |= CE_UPDATE;
366 int report_path_error(const char *ps_matched, const char **pathspec, const char *prefix)
369 * Make sure all pathspec matched; otherwise it is an error.
371 struct strbuf sb = STRBUF_INIT;
372 const char *name;
373 int num, errors = 0;
374 for (num = 0; pathspec[num]; num++) {
375 int other, found_dup;
377 if (ps_matched[num])
378 continue;
380 * The caller might have fed identical pathspec
381 * twice. Do not barf on such a mistake.
383 for (found_dup = other = 0;
384 !found_dup && pathspec[other];
385 other++) {
386 if (other == num || !ps_matched[other])
387 continue;
388 if (!strcmp(pathspec[other], pathspec[num]))
390 * Ok, we have a match already.
392 found_dup = 1;
394 if (found_dup)
395 continue;
397 name = quote_path_relative(pathspec[num], -1, &sb, prefix);
398 error("pathspec '%s' did not match any file(s) known to git.",
399 name);
400 errors++;
402 strbuf_release(&sb);
403 return errors;
406 static const char * const ls_files_usage[] = {
407 N_("git ls-files [options] [<file>...]"),
408 NULL
411 static int option_parse_z(const struct option *opt,
412 const char *arg, int unset)
414 line_terminator = unset ? '\n' : '\0';
416 return 0;
419 static int option_parse_exclude(const struct option *opt,
420 const char *arg, int unset)
422 struct string_list *exclude_list = opt->value;
424 exc_given = 1;
425 string_list_append(exclude_list, arg);
427 return 0;
430 static int option_parse_exclude_from(const struct option *opt,
431 const char *arg, int unset)
433 struct dir_struct *dir = opt->value;
435 exc_given = 1;
436 add_excludes_from_file(dir, arg);
438 return 0;
441 static int option_parse_exclude_standard(const struct option *opt,
442 const char *arg, int unset)
444 struct dir_struct *dir = opt->value;
446 exc_given = 1;
447 setup_standard_excludes(dir);
449 return 0;
452 int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
454 int require_work_tree = 0, show_tag = 0, i;
455 const char *max_prefix;
456 struct dir_struct dir;
457 struct exclude_list *el;
458 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
459 struct option builtin_ls_files_options[] = {
460 { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
461 N_("paths are separated with NUL character"),
462 PARSE_OPT_NOARG, option_parse_z },
463 OPT_BOOLEAN('t', NULL, &show_tag,
464 N_("identify the file status with tags")),
465 OPT_BOOLEAN('v', NULL, &show_valid_bit,
466 N_("use lowercase letters for 'assume unchanged' files")),
467 OPT_BOOLEAN('c', "cached", &show_cached,
468 N_("show cached files in the output (default)")),
469 OPT_BOOLEAN('d', "deleted", &show_deleted,
470 N_("show deleted files in the output")),
471 OPT_BOOLEAN('m', "modified", &show_modified,
472 N_("show modified files in the output")),
473 OPT_BOOLEAN('o', "others", &show_others,
474 N_("show other files in the output")),
475 OPT_BIT('i', "ignored", &dir.flags,
476 N_("show ignored files in the output"),
477 DIR_SHOW_IGNORED),
478 OPT_BOOLEAN('s', "stage", &show_stage,
479 N_("show staged contents' object name in the output")),
480 OPT_BOOLEAN('k', "killed", &show_killed,
481 N_("show files on the filesystem that need to be removed")),
482 OPT_BIT(0, "directory", &dir.flags,
483 N_("show 'other' directories' name only"),
484 DIR_SHOW_OTHER_DIRECTORIES),
485 OPT_NEGBIT(0, "empty-directory", &dir.flags,
486 N_("don't show empty directories"),
487 DIR_HIDE_EMPTY_DIRECTORIES),
488 OPT_BOOLEAN('u', "unmerged", &show_unmerged,
489 N_("show unmerged files in the output")),
490 OPT_BOOLEAN(0, "resolve-undo", &show_resolve_undo,
491 N_("show resolve-undo information")),
492 { OPTION_CALLBACK, 'x', "exclude", &exclude_list, N_("pattern"),
493 N_("skip files matching pattern"),
494 0, option_parse_exclude },
495 { OPTION_CALLBACK, 'X', "exclude-from", &dir, N_("file"),
496 N_("exclude patterns are read from <file>"),
497 0, option_parse_exclude_from },
498 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
499 N_("read additional per-directory exclude patterns in <file>")),
500 { OPTION_CALLBACK, 0, "exclude-standard", &dir, NULL,
501 N_("add the standard git exclusions"),
502 PARSE_OPT_NOARG, option_parse_exclude_standard },
503 { OPTION_SET_INT, 0, "full-name", &prefix_len, NULL,
504 N_("make the output relative to the project top directory"),
505 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL },
506 OPT_BOOLEAN(0, "error-unmatch", &error_unmatch,
507 N_("if any <file> is not in the index, treat this as an error")),
508 OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
509 N_("pretend that paths removed since <tree-ish> are still present")),
510 OPT__ABBREV(&abbrev),
511 OPT_BOOLEAN(0, "debug", &debug_mode, N_("show debugging data")),
512 OPT_END()
515 if (argc == 2 && !strcmp(argv[1], "-h"))
516 usage_with_options(ls_files_usage, builtin_ls_files_options);
518 memset(&dir, 0, sizeof(dir));
519 prefix = cmd_prefix;
520 if (prefix)
521 prefix_len = strlen(prefix);
522 git_config(git_default_config, NULL);
524 if (read_cache() < 0)
525 die("index file corrupt");
527 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
528 ls_files_usage, 0);
529 el = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
530 for (i = 0; i < exclude_list.nr; i++) {
531 add_exclude(exclude_list.items[i].string, "", 0, el, --exclude_args);
533 if (show_tag || show_valid_bit) {
534 tag_cached = "H ";
535 tag_unmerged = "M ";
536 tag_removed = "R ";
537 tag_modified = "C ";
538 tag_other = "? ";
539 tag_killed = "K ";
540 tag_skip_worktree = "S ";
541 tag_resolve_undo = "U ";
543 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
544 require_work_tree = 1;
545 if (show_unmerged)
547 * There's no point in showing unmerged unless
548 * you also show the stage information.
550 show_stage = 1;
551 if (dir.exclude_per_dir)
552 exc_given = 1;
554 if (require_work_tree && !is_inside_work_tree())
555 setup_work_tree();
557 pathspec = get_pathspec(prefix, argv);
559 /* be nice with submodule paths ending in a slash */
560 if (pathspec)
561 strip_trailing_slash_from_submodules();
563 /* Find common prefix for all pathspec's */
564 max_prefix = common_prefix(pathspec);
565 max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
567 /* Treat unmatching pathspec elements as errors */
568 if (pathspec && error_unmatch) {
569 int num;
570 for (num = 0; pathspec[num]; num++)
572 ps_matched = xcalloc(1, num);
575 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
576 die("ls-files --ignored needs some exclude pattern");
578 /* With no flags, we default to showing the cached files */
579 if (!(show_stage | show_deleted | show_others | show_unmerged |
580 show_killed | show_modified | show_resolve_undo))
581 show_cached = 1;
583 if (max_prefix)
584 prune_cache(max_prefix);
585 if (with_tree) {
587 * Basic sanity check; show-stages and show-unmerged
588 * would not make any sense with this option.
590 if (show_stage || show_unmerged)
591 die("ls-files --with-tree is incompatible with -s or -u");
592 overlay_tree_on_cache(with_tree, max_prefix);
594 show_files(&dir);
595 if (show_resolve_undo)
596 show_ru_info();
598 if (ps_matched) {
599 int bad;
600 bad = report_path_error(ps_matched, pathspec, prefix);
601 if (bad)
602 fprintf(stderr, "Did you forget to 'git add'?\n");
604 return bad ? 1 : 0;
607 return 0;