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
;
23 static int show_unmerged
;
24 static int show_resolve_undo
;
25 static int show_modified
;
26 static int show_killed
;
27 static int show_valid_bit
;
28 static int line_terminator
= '\n';
30 static int 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 show_dir_entry(const char *tag
, struct dir_entry
*ent
)
50 int offset
= prefix_offset
;
53 die("git ls-files: internal error - directory entry not superset of prefix");
55 if (!match_pathspec(pathspec
, ent
->name
, ent
->len
, len
, ps_matched
))
59 write_name_quoted(ent
->name
+ offset
, stdout
, line_terminator
);
62 static void show_other_files(struct dir_struct
*dir
)
66 for (i
= 0; i
< dir
->nr
; i
++) {
67 struct dir_entry
*ent
= dir
->entries
[i
];
68 if (!cache_name_is_other(ent
->name
, ent
->len
))
70 show_dir_entry(tag_other
, ent
);
74 static void show_killed_files(struct dir_struct
*dir
)
77 for (i
= 0; i
< dir
->nr
; i
++) {
78 struct dir_entry
*ent
= dir
->entries
[i
];
80 int pos
, len
, killed
= 0;
82 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
85 /* If ent->name is prefix of an entry in the
86 * cache, it will be killed.
88 pos
= cache_name_pos(ent
->name
, ent
->len
);
90 die("bug in show-killed-files");
92 while (pos
< active_nr
&&
93 ce_stage(active_cache
[pos
]))
94 pos
++; /* skip unmerged */
97 /* pos points at a name immediately after
98 * ent->name in the cache. Does it expect
99 * ent->name to be a directory?
101 len
= ce_namelen(active_cache
[pos
]);
102 if ((ent
->len
< len
) &&
103 !strncmp(active_cache
[pos
]->name
,
104 ent
->name
, ent
->len
) &&
105 active_cache
[pos
]->name
[ent
->len
] == '/')
109 if (0 <= cache_name_pos(ent
->name
, sp
- ent
->name
)) {
110 /* If any of the leading directories in
111 * ent->name is registered in the cache,
112 * ent->name will be killed.
119 show_dir_entry(tag_killed
, dir
->entries
[i
]);
123 static void show_ce_entry(const char *tag
, struct cache_entry
*ce
)
125 int len
= prefix_len
;
126 int offset
= prefix_offset
;
128 if (len
>= ce_namelen(ce
))
129 die("git ls-files: internal error - cache entry not superset of prefix");
131 if (!match_pathspec(pathspec
, ce
->name
, ce_namelen(ce
), len
, ps_matched
))
134 if (tag
&& *tag
&& show_valid_bit
&&
135 (ce
->ce_flags
& CE_VALID
)) {
136 static char alttag
[4];
137 memcpy(alttag
, tag
, 3);
139 alttag
[0] = tolower(tag
[0]);
140 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 printf("ctime=%u, mtime=%u, ino=%u, mode=%o, uid=%u, gid=%u\t",
160 ce
->ce_ctime
.sec
, ce
->ce_mtime
.sec
, ce
->ce_ino
,
161 ce
->ce_mode
, ce
->ce_uid
, ce
->ce_gid
);
165 write_name_quoted(ce
->name
+ offset
, stdout
, line_terminator
);
168 static int show_one_ru(struct string_list_item
*item
, void *cbdata
)
170 int offset
= prefix_offset
;
171 const char *path
= item
->string
;
172 struct resolve_undo_info
*ui
= item
->util
;
176 if (len
< prefix_len
)
177 return 0; /* outside of the prefix */
178 if (!match_pathspec(pathspec
, path
, len
, prefix_len
, ps_matched
))
179 return 0; /* uninterested */
180 for (i
= 0; i
< 3; i
++) {
183 printf("%s%06o %s %d\t", tag_resolve_undo
, ui
->mode
[i
],
185 ? find_unique_abbrev(ui
->sha1
[i
], abbrev
)
186 : sha1_to_hex(ui
->sha1
[i
]),
188 write_name_quoted(path
+ offset
, stdout
, line_terminator
);
193 static void show_ru_info(const char *prefix
)
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
, const char *prefix
)
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
| show_stat
) {
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
, 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
, prefix_len
)) {
276 static const char *verify_pathspec(const char *prefix
)
278 const char **p
, *n
, *prev
;
283 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
285 for (i
= 0; i
< max
; i
++) {
287 if (prev
&& prev
[i
] != c
)
289 if (!c
|| c
== '*' || c
== '?')
302 if (prefix_offset
> max
|| memcmp(prev
, prefix
, prefix_offset
))
303 die("git ls-files: cannot generate relative filenames containing '..'");
306 return max
? xmemdupz(prev
, max
) : NULL
;
309 static void strip_trailing_slash_from_submodules(void)
313 for (p
= pathspec
; *p
!= NULL
; p
++) {
314 int len
= strlen(*p
), pos
;
316 if (len
< 1 || (*p
)[len
- 1] != '/')
318 pos
= cache_name_pos(*p
, len
- 1);
319 if (pos
>= 0 && S_ISGITLINK(active_cache
[pos
]->ce_mode
))
320 *p
= xstrndup(*p
, len
- 1);
325 * Read the tree specified with --with-tree option
326 * (typically, HEAD) into stage #1 and then
327 * squash them down to stage #0. This is used for
328 * --error-unmatch to list and check the path patterns
329 * that were given from the command line. We are not
330 * going to write this index out.
332 void overlay_tree_on_cache(const char *tree_name
, const char *prefix
)
335 unsigned char sha1
[20];
337 struct cache_entry
*last_stage0
= NULL
;
340 if (get_sha1(tree_name
, sha1
))
341 die("tree-ish %s not found.", tree_name
);
342 tree
= parse_tree_indirect(sha1
);
344 die("bad tree-ish %s", tree_name
);
346 /* Hoist the unmerged entries up to stage #3 to make room */
347 for (i
= 0; i
< active_nr
; i
++) {
348 struct cache_entry
*ce
= active_cache
[i
];
351 ce
->ce_flags
|= CE_STAGEMASK
;
355 static const char *(matchbuf
[2]);
356 matchbuf
[0] = prefix
;
361 if (read_tree(tree
, 1, match
))
362 die("unable to read tree entries %s", tree_name
);
364 for (i
= 0; i
< active_nr
; i
++) {
365 struct cache_entry
*ce
= active_cache
[i
];
366 switch (ce_stage(ce
)) {
374 * If there is stage #0 entry for this, we do not
375 * need to show it. We use CE_UPDATE bit to mark
379 !strcmp(last_stage0
->name
, ce
->name
))
380 ce
->ce_flags
|= CE_UPDATE
;
385 int report_path_error(const char *ps_matched
, const char **pathspec
, int prefix_offset
)
388 * Make sure all pathspec matched; otherwise it is an error.
391 for (num
= 0; pathspec
[num
]; num
++) {
392 int other
, found_dup
;
397 * The caller might have fed identical pathspec
398 * twice. Do not barf on such a mistake.
400 for (found_dup
= other
= 0;
401 !found_dup
&& pathspec
[other
];
403 if (other
== num
|| !ps_matched
[other
])
405 if (!strcmp(pathspec
[other
], pathspec
[num
]))
407 * Ok, we have a match already.
414 error("pathspec '%s' did not match any file(s) known to git.",
415 pathspec
[num
] + prefix_offset
);
421 static const char * const ls_files_usage
[] = {
422 "git ls-files [options] [<file>]*",
426 static int option_parse_z(const struct option
*opt
,
427 const char *arg
, int unset
)
429 line_terminator
= unset
? '\n' : '\0';
434 static int option_parse_exclude(const struct option
*opt
,
435 const char *arg
, int unset
)
437 struct exclude_list
*list
= opt
->value
;
440 add_exclude(arg
, "", 0, list
);
445 static int option_parse_exclude_from(const struct option
*opt
,
446 const char *arg
, int unset
)
448 struct dir_struct
*dir
= opt
->value
;
451 add_excludes_from_file(dir
, arg
);
456 static int option_parse_exclude_standard(const struct option
*opt
,
457 const char *arg
, int unset
)
459 struct dir_struct
*dir
= opt
->value
;
462 setup_standard_excludes(dir
);
467 int cmd_ls_files(int argc
, const char **argv
, const char *prefix
)
469 int require_work_tree
= 0, show_tag
= 0;
470 struct dir_struct dir
;
471 struct option builtin_ls_files_options
[] = {
472 { OPTION_CALLBACK
, 'z', NULL
, NULL
, NULL
,
473 "paths are separated with NUL character",
474 PARSE_OPT_NOARG
, option_parse_z
},
475 OPT_BOOLEAN('t', NULL
, &show_tag
,
476 "identify the file status with tags"),
477 OPT_BOOLEAN('v', NULL
, &show_valid_bit
,
478 "use lowercase letters for 'assume unchanged' files"),
479 OPT_BOOLEAN('c', "cached", &show_cached
,
480 "show cached files in the output (default)"),
481 OPT_BOOLEAN('d', "deleted", &show_deleted
,
482 "show deleted files in the output"),
483 OPT_BOOLEAN('m', "modified", &show_modified
,
484 "show modified files in the output"),
485 OPT_BOOLEAN('o', "others", &show_others
,
486 "show other files in the output"),
487 OPT_BIT('i', "ignored", &dir
.flags
,
488 "show ignored files in the output",
490 OPT_BOOLEAN('s', "stage", &show_stage
,
491 "show staged contents' object name in the output"),
492 OPT_BOOLEAN('S', "stat", &show_stat
,
493 "show stat metadata"),
494 OPT_BOOLEAN('k', "killed", &show_killed
,
495 "show files on the filesystem that need to be removed"),
496 OPT_BIT(0, "directory", &dir
.flags
,
497 "show 'other' directories' name only",
498 DIR_SHOW_OTHER_DIRECTORIES
),
499 OPT_NEGBIT(0, "empty-directory", &dir
.flags
,
500 "don't show empty directories",
501 DIR_HIDE_EMPTY_DIRECTORIES
),
502 OPT_BOOLEAN('u', "unmerged", &show_unmerged
,
503 "show unmerged files in the output"),
504 OPT_BOOLEAN(0, "resolve-undo", &show_resolve_undo
,
505 "show resolve-undo information"),
506 { OPTION_CALLBACK
, 'x', "exclude", &dir
.exclude_list
[EXC_CMDL
], "pattern",
507 "skip files matching pattern",
508 0, option_parse_exclude
},
509 { OPTION_CALLBACK
, 'X', "exclude-from", &dir
, "file",
510 "exclude patterns are read from <file>",
511 0, option_parse_exclude_from
},
512 OPT_STRING(0, "exclude-per-directory", &dir
.exclude_per_dir
, "file",
513 "read additional per-directory exclude patterns in <file>"),
514 { OPTION_CALLBACK
, 0, "exclude-standard", &dir
, NULL
,
515 "add the standard git exclusions",
516 PARSE_OPT_NOARG
, option_parse_exclude_standard
},
517 { OPTION_SET_INT
, 0, "full-name", &prefix_offset
, NULL
,
518 "make the output relative to the project top directory",
519 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
},
520 OPT_BOOLEAN(0, "error-unmatch", &error_unmatch
,
521 "if any <file> is not in the index, treat this as an error"),
522 OPT_STRING(0, "with-tree", &with_tree
, "tree-ish",
523 "pretend that paths removed since <tree-ish> are still present"),
524 OPT__ABBREV(&abbrev
),
528 memset(&dir
, 0, sizeof(dir
));
530 prefix_offset
= strlen(prefix
);
531 git_config(git_default_config
, NULL
);
533 if (read_cache() < 0)
534 die("index file corrupt");
536 argc
= parse_options(argc
, argv
, prefix
, builtin_ls_files_options
,
538 if (show_tag
|| show_valid_bit
) {
545 tag_skip_worktree
= "S ";
546 tag_resolve_undo
= "U ";
548 if (show_modified
|| show_others
|| show_deleted
|| (dir
.flags
& DIR_SHOW_IGNORED
) || show_killed
)
549 require_work_tree
= 1;
552 * There's no point in showing unmerged unless
553 * you also show the stage information.
556 if (dir
.exclude_per_dir
)
559 if (require_work_tree
&& !is_inside_work_tree())
562 pathspec
= get_pathspec(prefix
, argv
);
564 /* be nice with submodule paths ending in a slash */
566 strip_trailing_slash_from_submodules();
568 /* Verify that the pathspec matches the prefix */
570 prefix
= verify_pathspec(prefix
);
572 /* Treat unmatching pathspec elements as errors */
573 if (pathspec
&& error_unmatch
) {
575 for (num
= 0; pathspec
[num
]; num
++)
577 ps_matched
= xcalloc(1, num
);
580 if ((dir
.flags
& DIR_SHOW_IGNORED
) && !exc_given
)
581 die("ls-files --ignored needs some exclude pattern");
583 /* With no flags, we default to showing the cached files */
584 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
|
585 show_killed
| show_modified
| show_resolve_undo
| show_stat
))
592 * Basic sanity check; show-stages and show-unmerged
593 * would not make any sense with this option.
595 if (show_stage
|| show_unmerged
)
596 die("ls-files --with-tree is incompatible with -s or -u");
597 overlay_tree_on_cache(with_tree
, prefix
);
599 show_files(&dir
, prefix
);
600 if (show_resolve_undo
)
601 show_ru_info(prefix
);
605 bad
= report_path_error(ps_matched
, pathspec
, prefix_offset
);
607 fprintf(stderr
, "Did you forget to 'git add'?\n");