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 int prefix_len
;
30 static int prefix_offset
;
31 static const char **pathspec
;
32 static int error_unmatch
;
33 static char *ps_matched
;
34 static const char *with_tree
;
37 static const char *tag_cached
= "";
38 static const char *tag_unmerged
= "";
39 static const char *tag_removed
= "";
40 static const char *tag_other
= "";
41 static const char *tag_killed
= "";
42 static const char *tag_modified
= "";
43 static const char *tag_resolve_undo
= "";
45 static void show_dir_entry(const char *tag
, struct dir_entry
*ent
)
48 int offset
= prefix_offset
;
51 die("git ls-files: internal error - directory entry not superset of prefix");
53 if (!match_pathspec(pathspec
, ent
->name
, ent
->len
, len
, ps_matched
))
57 write_name_quoted(ent
->name
+ offset
, stdout
, line_terminator
);
60 static void show_other_files(struct dir_struct
*dir
)
64 for (i
= 0; i
< dir
->nr
; i
++) {
65 struct dir_entry
*ent
= dir
->entries
[i
];
66 if (!cache_name_is_other(ent
->name
, ent
->len
))
68 show_dir_entry(tag_other
, ent
);
72 static void show_killed_files(struct dir_struct
*dir
)
75 for (i
= 0; i
< dir
->nr
; i
++) {
76 struct dir_entry
*ent
= dir
->entries
[i
];
78 int pos
, len
, killed
= 0;
80 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
83 /* If ent->name is prefix of an entry in the
84 * cache, it will be killed.
86 pos
= cache_name_pos(ent
->name
, ent
->len
);
88 die("bug in show-killed-files");
90 while (pos
< active_nr
&&
91 ce_stage(active_cache
[pos
]))
92 pos
++; /* skip unmerged */
95 /* pos points at a name immediately after
96 * ent->name in the cache. Does it expect
97 * ent->name to be a directory?
99 len
= ce_namelen(active_cache
[pos
]);
100 if ((ent
->len
< len
) &&
101 !strncmp(active_cache
[pos
]->name
,
102 ent
->name
, ent
->len
) &&
103 active_cache
[pos
]->name
[ent
->len
] == '/')
107 if (0 <= cache_name_pos(ent
->name
, sp
- ent
->name
)) {
108 /* If any of the leading directories in
109 * ent->name is registered in the cache,
110 * ent->name will be killed.
117 show_dir_entry(tag_killed
, dir
->entries
[i
]);
121 static void show_ce_entry(const char *tag
, struct cache_entry
*ce
)
123 int len
= prefix_len
;
124 int offset
= prefix_offset
;
126 if (len
>= ce_namelen(ce
))
127 die("git ls-files: internal error - cache entry not superset of prefix");
129 if (!match_pathspec(pathspec
, ce
->name
, ce_namelen(ce
), len
, ps_matched
))
132 if (tag
&& *tag
&& show_valid_bit
&&
133 (ce
->ce_flags
& CE_VALID
)) {
134 static char alttag
[4];
135 memcpy(alttag
, tag
, 3);
137 alttag
[0] = tolower(tag
[0]);
138 else if (tag
[0] == '?')
152 printf("%s%06o %s %d\t",
155 abbrev
? find_unique_abbrev(ce
->sha1
,abbrev
)
156 : sha1_to_hex(ce
->sha1
),
159 write_name_quoted(ce
->name
+ offset
, stdout
, line_terminator
);
162 static int show_one_ru(struct string_list_item
*item
, void *cbdata
)
164 int offset
= prefix_offset
;
165 const char *path
= item
->string
;
166 struct resolve_undo_info
*ui
= item
->util
;
170 if (len
< prefix_len
)
171 return 0; /* outside of the prefix */
172 if (!match_pathspec(pathspec
, path
, len
, prefix_len
, ps_matched
))
173 return 0; /* uninterested */
174 for (i
= 0; i
< 3; i
++) {
177 printf("%s%06o %s %d\t", tag_resolve_undo
, ui
->mode
[i
],
179 ? find_unique_abbrev(ui
->sha1
[i
], abbrev
)
180 : sha1_to_hex(ui
->sha1
[i
]),
182 write_name_quoted(path
+ offset
, stdout
, line_terminator
);
187 static void show_ru_info(const char *prefix
)
189 if (!the_index
.resolve_undo
)
191 for_each_string_list(show_one_ru
, the_index
.resolve_undo
, NULL
);
194 static void show_files(struct dir_struct
*dir
, const char *prefix
)
198 /* For cached/deleted files we don't need to even do the readdir */
199 if (show_others
|| show_killed
) {
200 fill_directory(dir
, pathspec
);
202 show_other_files(dir
);
204 show_killed_files(dir
);
206 if (show_cached
| show_stage
) {
207 for (i
= 0; i
< active_nr
; i
++) {
208 struct cache_entry
*ce
= active_cache
[i
];
209 int dtype
= ce_to_dtype(ce
);
210 if (dir
->flags
& DIR_SHOW_IGNORED
&&
211 !excluded(dir
, ce
->name
, &dtype
))
213 if (show_unmerged
&& !ce_stage(ce
))
215 if (ce
->ce_flags
& CE_UPDATE
)
217 show_ce_entry(ce_stage(ce
) ? tag_unmerged
: tag_cached
, ce
);
220 if (show_deleted
| show_modified
) {
221 for (i
= 0; i
< active_nr
; i
++) {
222 struct cache_entry
*ce
= active_cache
[i
];
225 int dtype
= ce_to_dtype(ce
);
226 if (dir
->flags
& DIR_SHOW_IGNORED
&&
227 !excluded(dir
, ce
->name
, &dtype
))
229 if (ce
->ce_flags
& CE_UPDATE
)
231 err
= lstat(ce
->name
, &st
);
232 if (show_deleted
&& err
)
233 show_ce_entry(tag_removed
, ce
);
234 if (show_modified
&& ce_modified(ce
, &st
, 0))
235 show_ce_entry(tag_modified
, ce
);
241 * Prune the index to only contain stuff starting with "prefix"
243 static void prune_cache(const char *prefix
)
245 int pos
= cache_name_pos(prefix
, prefix_len
);
246 unsigned int first
, last
;
250 memmove(active_cache
, active_cache
+ pos
,
251 (active_nr
- pos
) * sizeof(struct cache_entry
*));
255 while (last
> first
) {
256 int next
= (last
+ first
) >> 1;
257 struct cache_entry
*ce
= active_cache
[next
];
258 if (!strncmp(ce
->name
, prefix
, prefix_len
)) {
267 static const char *verify_pathspec(const char *prefix
)
269 const char **p
, *n
, *prev
;
274 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
276 for (i
= 0; i
< max
; i
++) {
278 if (prev
&& prev
[i
] != c
)
280 if (!c
|| c
== '*' || c
== '?')
293 if (prefix_offset
> max
|| memcmp(prev
, prefix
, prefix_offset
))
294 die("git ls-files: cannot generate relative filenames containing '..'");
297 return max
? xmemdupz(prev
, max
) : NULL
;
300 static void strip_trailing_slash_from_submodules(void)
304 for (p
= pathspec
; *p
!= NULL
; p
++) {
305 int len
= strlen(*p
), pos
;
307 if (len
< 1 || (*p
)[len
- 1] != '/')
309 pos
= cache_name_pos(*p
, len
- 1);
310 if (pos
>= 0 && S_ISGITLINK(active_cache
[pos
]->ce_mode
))
311 *p
= xstrndup(*p
, len
- 1);
316 * Read the tree specified with --with-tree option
317 * (typically, HEAD) into stage #1 and then
318 * squash them down to stage #0. This is used for
319 * --error-unmatch to list and check the path patterns
320 * that were given from the command line. We are not
321 * going to write this index out.
323 void overlay_tree_on_cache(const char *tree_name
, const char *prefix
)
326 unsigned char sha1
[20];
328 struct cache_entry
*last_stage0
= NULL
;
331 if (get_sha1(tree_name
, sha1
))
332 die("tree-ish %s not found.", tree_name
);
333 tree
= parse_tree_indirect(sha1
);
335 die("bad tree-ish %s", tree_name
);
337 /* Hoist the unmerged entries up to stage #3 to make room */
338 for (i
= 0; i
< active_nr
; i
++) {
339 struct cache_entry
*ce
= active_cache
[i
];
342 ce
->ce_flags
|= CE_STAGEMASK
;
346 static const char *(matchbuf
[2]);
347 matchbuf
[0] = prefix
;
352 if (read_tree(tree
, 1, match
))
353 die("unable to read tree entries %s", tree_name
);
355 for (i
= 0; i
< active_nr
; i
++) {
356 struct cache_entry
*ce
= active_cache
[i
];
357 switch (ce_stage(ce
)) {
365 * If there is stage #0 entry for this, we do not
366 * need to show it. We use CE_UPDATE bit to mark
370 !strcmp(last_stage0
->name
, ce
->name
))
371 ce
->ce_flags
|= CE_UPDATE
;
376 int report_path_error(const char *ps_matched
, const char **pathspec
, int prefix_offset
)
379 * Make sure all pathspec matched; otherwise it is an error.
382 for (num
= 0; pathspec
[num
]; num
++) {
383 int other
, found_dup
;
388 * The caller might have fed identical pathspec
389 * twice. Do not barf on such a mistake.
391 for (found_dup
= other
= 0;
392 !found_dup
&& pathspec
[other
];
394 if (other
== num
|| !ps_matched
[other
])
396 if (!strcmp(pathspec
[other
], pathspec
[num
]))
398 * Ok, we have a match already.
405 error("pathspec '%s' did not match any file(s) known to git.",
406 pathspec
[num
] + prefix_offset
);
412 static const char * const ls_files_usage
[] = {
413 "git ls-files [options] [<file>]*",
417 static int option_parse_z(const struct option
*opt
,
418 const char *arg
, int unset
)
420 line_terminator
= unset
? '\n' : '\0';
425 static int option_parse_exclude(const struct option
*opt
,
426 const char *arg
, int unset
)
428 struct exclude_list
*list
= opt
->value
;
431 add_exclude(arg
, "", 0, list
);
436 static int option_parse_exclude_from(const struct option
*opt
,
437 const char *arg
, int unset
)
439 struct dir_struct
*dir
= opt
->value
;
442 add_excludes_from_file(dir
, arg
);
447 static int option_parse_exclude_standard(const struct option
*opt
,
448 const char *arg
, int unset
)
450 struct dir_struct
*dir
= opt
->value
;
453 setup_standard_excludes(dir
);
458 int cmd_ls_files(int argc
, const char **argv
, const char *prefix
)
460 int require_work_tree
= 0, show_tag
= 0;
461 struct dir_struct dir
;
462 struct option builtin_ls_files_options
[] = {
463 { OPTION_CALLBACK
, 'z', NULL
, NULL
, NULL
,
464 "paths are separated with NUL character",
465 PARSE_OPT_NOARG
, option_parse_z
},
466 OPT_BOOLEAN('t', NULL
, &show_tag
,
467 "identify the file status with tags"),
468 OPT_BOOLEAN('v', NULL
, &show_valid_bit
,
469 "use lowercase letters for 'assume unchanged' files"),
470 OPT_BOOLEAN('c', "cached", &show_cached
,
471 "show cached files in the output (default)"),
472 OPT_BOOLEAN('d', "deleted", &show_deleted
,
473 "show deleted files in the output"),
474 OPT_BOOLEAN('m', "modified", &show_modified
,
475 "show modified files in the output"),
476 OPT_BOOLEAN('o', "others", &show_others
,
477 "show other files in the output"),
478 OPT_BIT('i', "ignored", &dir
.flags
,
479 "show ignored files in the output",
481 OPT_BOOLEAN('s', "stage", &show_stage
,
482 "show staged contents' object name in the output"),
483 OPT_BOOLEAN('k', "killed", &show_killed
,
484 "show files on the filesystem that need to be removed"),
485 OPT_BIT(0, "directory", &dir
.flags
,
486 "show 'other' directories' name only",
487 DIR_SHOW_OTHER_DIRECTORIES
),
488 OPT_NEGBIT(0, "empty-directory", &dir
.flags
,
489 "don't show empty directories",
490 DIR_HIDE_EMPTY_DIRECTORIES
),
491 OPT_BOOLEAN('u', "unmerged", &show_unmerged
,
492 "show unmerged files in the output"),
493 OPT_BOOLEAN(0, "resolve-undo", &show_resolve_undo
,
494 "show resolve-undo information"),
495 { OPTION_CALLBACK
, 'x', "exclude", &dir
.exclude_list
[EXC_CMDL
], "pattern",
496 "skip files matching pattern",
497 0, option_parse_exclude
},
498 { OPTION_CALLBACK
, 'X', "exclude-from", &dir
, "file",
499 "exclude patterns are read from <file>",
500 0, option_parse_exclude_from
},
501 OPT_STRING(0, "exclude-per-directory", &dir
.exclude_per_dir
, "file",
502 "read additional per-directory exclude patterns in <file>"),
503 { OPTION_CALLBACK
, 0, "exclude-standard", &dir
, NULL
,
504 "add the standard git exclusions",
505 PARSE_OPT_NOARG
, option_parse_exclude_standard
},
506 { OPTION_SET_INT
, 0, "full-name", &prefix_offset
, NULL
,
507 "make the output relative to the project top directory",
508 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
},
509 OPT_BOOLEAN(0, "error-unmatch", &error_unmatch
,
510 "if any <file> is not in the index, treat this as an error"),
511 OPT_STRING(0, "with-tree", &with_tree
, "tree-ish",
512 "pretend that paths removed since <tree-ish> are still present"),
513 OPT__ABBREV(&abbrev
),
517 memset(&dir
, 0, sizeof(dir
));
519 prefix_offset
= strlen(prefix
);
520 git_config(git_default_config
, NULL
);
522 argc
= parse_options(argc
, argv
, prefix
, builtin_ls_files_options
,
524 if (show_tag
|| show_valid_bit
) {
531 tag_resolve_undo
= "U ";
533 if (show_modified
|| show_others
|| show_deleted
|| (dir
.flags
& DIR_SHOW_IGNORED
) || show_killed
)
534 require_work_tree
= 1;
537 * There's no point in showing unmerged unless
538 * you also show the stage information.
541 if (dir
.exclude_per_dir
)
544 if (require_work_tree
&& !is_inside_work_tree())
547 pathspec
= get_pathspec(prefix
, argv
);
549 /* be nice with submodule paths ending in a slash */
552 strip_trailing_slash_from_submodules();
554 /* Verify that the pathspec matches the prefix */
556 prefix
= verify_pathspec(prefix
);
558 /* Treat unmatching pathspec elements as errors */
559 if (pathspec
&& error_unmatch
) {
561 for (num
= 0; pathspec
[num
]; num
++)
563 ps_matched
= xcalloc(1, num
);
566 if ((dir
.flags
& DIR_SHOW_IGNORED
) && !exc_given
)
567 die("ls-files --ignored needs some exclude pattern");
569 /* With no flags, we default to showing the cached files */
570 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
|
571 show_killed
| show_modified
| show_resolve_undo
))
578 * Basic sanity check; show-stages and show-unmerged
579 * would not make any sense with this option.
581 if (show_stage
|| show_unmerged
)
582 die("ls-files --with-tree is incompatible with -s or -u");
583 overlay_tree_on_cache(with_tree
, prefix
);
585 show_files(&dir
, prefix
);
586 if (show_resolve_undo
)
587 show_ru_info(prefix
);
591 bad
= report_path_error(ps_matched
, pathspec
, prefix_offset
);
593 fprintf(stderr
, "Did you forget to 'git add'?\n");