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
13 #include "parse-options.h"
14 #include "resolve-undo.h"
15 #include "string-list.h"
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
;
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
);
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
;
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
))
68 write_name(ent
->name
, ent
->len
);
71 static void show_other_files(struct dir_struct
*dir
)
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
))
79 show_dir_entry(tag_other
, ent
);
83 static void show_killed_files(struct dir_struct
*dir
)
86 for (i
= 0; i
< dir
->nr
; i
++) {
87 struct dir_entry
*ent
= dir
->entries
[i
];
89 int pos
, len
, killed
= 0;
91 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
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
);
99 die("bug in show-killed-files");
101 while (pos
< active_nr
&&
102 ce_stage(active_cache
[pos
]))
103 pos
++; /* skip unmerged */
104 if (active_nr
<= pos
)
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
] == '/')
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.
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
))
142 if (tag
&& *tag
&& show_valid_bit
&&
143 (ce
->ce_flags
& CE_VALID
)) {
144 static char alttag
[4];
145 memcpy(alttag
, tag
, 3);
147 alttag
[0] = tolower(tag
[0]);
148 else if (tag
[0] == '?')
162 printf("%s%06o %s %d\t",
165 find_unique_abbrev(ce
->sha1
,abbrev
),
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
;
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
++) {
185 printf("%s%06o %s %d\t", tag_resolve_undo
, ui
->mode
[i
],
186 find_unique_abbrev(ui
->sha1
[i
], abbrev
),
188 write_name(path
, len
);
193 static void show_ru_info(void)
195 if (!the_index
.resolve_undo
)
197 for_each_string_list(show_one_ru
, the_index
.resolve_undo
, NULL
);
200 static void show_files(struct dir_struct
*dir
)
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
);
208 show_other_files(dir
);
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
))
219 if (show_unmerged
&& !ce_stage(ce
))
221 if (ce
->ce_flags
& CE_UPDATE
)
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
];
232 int dtype
= ce_to_dtype(ce
);
233 if (dir
->flags
& DIR_SHOW_IGNORED
&&
234 !excluded(dir
, ce
->name
, &dtype
))
236 if (ce
->ce_flags
& CE_UPDATE
)
238 if (ce_skip_worktree(ce
))
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
;
259 memmove(active_cache
, active_cache
+ pos
,
260 (active_nr
- pos
) * sizeof(struct cache_entry
*));
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
)) {
276 static const char *pathspec_prefix(const char *prefix
)
278 const char **p
, *n
, *prev
;
282 max_prefix_len
= prefix
? strlen(prefix
) : 0;
288 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
290 for (i
= 0; i
< max
; i
++) {
292 if (prev
&& prev
[i
] != c
)
294 if (!c
|| c
== '*' || c
== '?')
307 max_prefix_len
= max
;
308 return max
? xmemdupz(prev
, max
) : NULL
;
311 static void strip_trailing_slash_from_submodules(void)
315 for (p
= pathspec
; *p
!= NULL
; p
++) {
316 int len
= strlen(*p
), pos
;
318 if (len
< 1 || (*p
)[len
- 1] != '/')
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
)
337 unsigned char sha1
[20];
339 struct cache_entry
*last_stage0
= NULL
;
342 if (get_sha1(tree_name
, sha1
))
343 die("tree-ish %s not found.", tree_name
);
344 tree
= parse_tree_indirect(sha1
);
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
];
353 ce
->ce_flags
|= CE_STAGEMASK
;
357 static const char *(matchbuf
[2]);
358 matchbuf
[0] = prefix
;
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
)) {
376 * If there is stage #0 entry for this, we do not
377 * need to show it. We use CE_UPDATE bit to mark
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.
393 for (num
= 0; pathspec
[num
]; num
++) {
394 int other
, found_dup
;
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
];
405 if (other
== num
|| !ps_matched
[other
])
407 if (!strcmp(pathspec
[other
], pathspec
[num
]))
409 * Ok, we have a match already.
416 error("pathspec '%s' did not match any file(s) known to git.",
417 pathspec
[num
] + prefix_offset
);
423 static const char * const ls_files_usage
[] = {
424 "git ls-files [options] [<file>]*",
428 static int option_parse_z(const struct option
*opt
,
429 const char *arg
, int unset
)
431 line_terminator
= unset
? '\n' : '\0';
436 static int option_parse_exclude(const struct option
*opt
,
437 const char *arg
, int unset
)
439 struct exclude_list
*list
= opt
->value
;
442 add_exclude(arg
, "", 0, list
);
447 static int option_parse_exclude_from(const struct option
*opt
,
448 const char *arg
, int unset
)
450 struct dir_struct
*dir
= opt
->value
;
453 add_excludes_from_file(dir
, arg
);
458 static int option_parse_exclude_standard(const struct option
*opt
,
459 const char *arg
, int unset
)
461 struct dir_struct
*dir
= opt
->value
;
464 setup_standard_excludes(dir
);
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",
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
),
529 memset(&dir
, 0, sizeof(dir
));
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
,
540 if (show_tag
|| show_valid_bit
) {
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;
554 * There's no point in showing unmerged unless
555 * you also show the stage information.
558 if (dir
.exclude_per_dir
)
561 if (require_work_tree
&& !is_inside_work_tree())
564 pathspec
= get_pathspec(prefix
, argv
);
566 /* be nice with submodule paths ending in a slash */
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
) {
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
))
590 prune_cache(max_prefix
);
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
);
601 if (show_resolve_undo
)
606 bad
= report_path_error(ps_matched
, pathspec
, prefix_offset
);
608 fprintf(stderr
, "Did you forget to 'git add'?\n");