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"
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
;
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
)
44 int offset
= prefix_offset
;
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
))
53 write_name_quoted(ent
->name
+ offset
, stdout
, line_terminator
);
56 static void show_other_files(struct dir_struct
*dir
)
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
))
64 show_dir_entry(tag_other
, ent
);
68 static void show_killed_files(struct dir_struct
*dir
)
71 for (i
= 0; i
< dir
->nr
; i
++) {
72 struct dir_entry
*ent
= dir
->entries
[i
];
74 int pos
, len
, killed
= 0;
76 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
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
);
84 die("bug in show-killed-files");
86 while (pos
< active_nr
&&
87 ce_stage(active_cache
[pos
]))
88 pos
++; /* skip unmerged */
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
] == '/')
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.
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
))
128 if (tag
&& *tag
&& show_valid_bit
&&
129 (ce
->ce_flags
& CE_VALID
)) {
130 static char alttag
[4];
131 memcpy(alttag
, tag
, 3);
133 alttag
[0] = tolower(tag
[0]);
134 else if (tag
[0] == '?')
148 printf("%s%06o %s %d\t",
151 abbrev
? find_unique_abbrev(ce
->sha1
,abbrev
)
152 : sha1_to_hex(ce
->sha1
),
155 write_name_quoted(ce
->name
+ offset
, stdout
, line_terminator
);
158 static void show_files(struct dir_struct
*dir
, const char *prefix
)
162 /* For cached/deleted files we don't need to even do the readdir */
163 if (show_others
|| show_killed
) {
164 const char *path
= ".", *base
= "";
165 int baselen
= prefix_len
;
168 path
= base
= prefix
;
169 read_directory(dir
, path
, base
, baselen
, pathspec
);
171 show_other_files(dir
);
173 show_killed_files(dir
);
175 if (show_cached
| show_stage
) {
176 for (i
= 0; i
< active_nr
; i
++) {
177 struct cache_entry
*ce
= active_cache
[i
];
178 int dtype
= ce_to_dtype(ce
);
179 if (excluded(dir
, ce
->name
, &dtype
) !=
180 !!(dir
->flags
& DIR_SHOW_IGNORED
))
182 if (show_unmerged
&& !ce_stage(ce
))
184 if (ce
->ce_flags
& CE_UPDATE
)
186 show_ce_entry(ce_stage(ce
) ? tag_unmerged
: tag_cached
, ce
);
189 if (show_deleted
| show_modified
) {
190 for (i
= 0; i
< active_nr
; i
++) {
191 struct cache_entry
*ce
= active_cache
[i
];
194 int dtype
= ce_to_dtype(ce
);
195 if (excluded(dir
, ce
->name
, &dtype
) !=
196 !!(dir
->flags
& DIR_SHOW_IGNORED
))
198 if (ce
->ce_flags
& CE_UPDATE
)
200 err
= lstat(ce
->name
, &st
);
201 if (show_deleted
&& err
)
202 show_ce_entry(tag_removed
, ce
);
203 if (show_modified
&& ce_modified(ce
, &st
, 0))
204 show_ce_entry(tag_modified
, ce
);
210 * Prune the index to only contain stuff starting with "prefix"
212 static void prune_cache(const char *prefix
)
214 int pos
= cache_name_pos(prefix
, prefix_len
);
215 unsigned int first
, last
;
219 memmove(active_cache
, active_cache
+ pos
,
220 (active_nr
- pos
) * sizeof(struct cache_entry
*));
224 while (last
> first
) {
225 int next
= (last
+ first
) >> 1;
226 struct cache_entry
*ce
= active_cache
[next
];
227 if (!strncmp(ce
->name
, prefix
, prefix_len
)) {
236 static const char *verify_pathspec(const char *prefix
)
238 const char **p
, *n
, *prev
;
243 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
245 for (i
= 0; i
< max
; i
++) {
247 if (prev
&& prev
[i
] != c
)
249 if (!c
|| c
== '*' || c
== '?')
262 if (prefix_offset
> max
|| memcmp(prev
, prefix
, prefix_offset
))
263 die("git ls-files: cannot generate relative filenames containing '..'");
266 return max
? xmemdupz(prev
, max
) : NULL
;
269 static void strip_trailing_slash_from_submodules(void)
273 for (p
= pathspec
; *p
!= NULL
; p
++) {
274 int len
= strlen(*p
), pos
;
276 if (len
< 1 || (*p
)[len
- 1] != '/')
278 pos
= cache_name_pos(*p
, len
- 1);
279 if (pos
>= 0 && S_ISGITLINK(active_cache
[pos
]->ce_mode
))
280 *p
= xstrndup(*p
, len
- 1);
285 * Read the tree specified with --with-tree option
286 * (typically, HEAD) into stage #1 and then
287 * squash them down to stage #0. This is used for
288 * --error-unmatch to list and check the path patterns
289 * that were given from the command line. We are not
290 * going to write this index out.
292 void overlay_tree_on_cache(const char *tree_name
, const char *prefix
)
295 unsigned char sha1
[20];
297 struct cache_entry
*last_stage0
= NULL
;
300 if (get_sha1(tree_name
, sha1
))
301 die("tree-ish %s not found.", tree_name
);
302 tree
= parse_tree_indirect(sha1
);
304 die("bad tree-ish %s", tree_name
);
306 /* Hoist the unmerged entries up to stage #3 to make room */
307 for (i
= 0; i
< active_nr
; i
++) {
308 struct cache_entry
*ce
= active_cache
[i
];
311 ce
->ce_flags
|= CE_STAGEMASK
;
315 static const char *(matchbuf
[2]);
316 matchbuf
[0] = prefix
;
321 if (read_tree(tree
, 1, match
))
322 die("unable to read tree entries %s", tree_name
);
324 for (i
= 0; i
< active_nr
; i
++) {
325 struct cache_entry
*ce
= active_cache
[i
];
326 switch (ce_stage(ce
)) {
334 * If there is stage #0 entry for this, we do not
335 * need to show it. We use CE_UPDATE bit to mark
339 !strcmp(last_stage0
->name
, ce
->name
))
340 ce
->ce_flags
|= CE_UPDATE
;
345 int report_path_error(const char *ps_matched
, const char **pathspec
, int prefix_offset
)
348 * Make sure all pathspec matched; otherwise it is an error.
351 for (num
= 0; pathspec
[num
]; num
++) {
352 int other
, found_dup
;
357 * The caller might have fed identical pathspec
358 * twice. Do not barf on such a mistake.
360 for (found_dup
= other
= 0;
361 !found_dup
&& pathspec
[other
];
363 if (other
== num
|| !ps_matched
[other
])
365 if (!strcmp(pathspec
[other
], pathspec
[num
]))
367 * Ok, we have a match already.
374 error("pathspec '%s' did not match any file(s) known to git.",
375 pathspec
[num
] + prefix_offset
);
381 static const char * const ls_files_usage
[] = {
382 "git ls-files [options] [<file>]*",
386 static int option_parse_z(const struct option
*opt
,
387 const char *arg
, int unset
)
389 line_terminator
= unset
? '\n' : '\0';
394 static int option_parse_exclude(const struct option
*opt
,
395 const char *arg
, int unset
)
397 struct exclude_list
*list
= opt
->value
;
400 add_exclude(arg
, "", 0, list
);
405 static int option_parse_exclude_from(const struct option
*opt
,
406 const char *arg
, int unset
)
408 struct dir_struct
*dir
= opt
->value
;
411 add_excludes_from_file(dir
, arg
);
416 static int option_parse_exclude_standard(const struct option
*opt
,
417 const char *arg
, int unset
)
419 struct dir_struct
*dir
= opt
->value
;
422 setup_standard_excludes(dir
);
427 int cmd_ls_files(int argc
, const char **argv
, const char *prefix
)
429 int require_work_tree
= 0, show_tag
= 0;
430 struct dir_struct dir
;
431 struct option builtin_ls_files_options
[] = {
432 { OPTION_CALLBACK
, 'z', NULL
, NULL
, NULL
,
433 "paths are separated with NUL character",
434 PARSE_OPT_NOARG
, option_parse_z
},
435 OPT_BOOLEAN('t', NULL
, &show_tag
,
436 "identify the file status with tags"),
437 OPT_BOOLEAN('v', NULL
, &show_valid_bit
,
438 "use lowercase letters for 'assume unchanged' files"),
439 OPT_BOOLEAN('c', "cached", &show_cached
,
440 "show cached files in the output (default)"),
441 OPT_BOOLEAN('d', "deleted", &show_deleted
,
442 "show deleted files in the output"),
443 OPT_BOOLEAN('m', "modified", &show_modified
,
444 "show modified files in the output"),
445 OPT_BOOLEAN('o', "others", &show_others
,
446 "show other files in the output"),
447 OPT_BIT('i', "ignored", &dir
.flags
,
448 "show ignored files in the output",
450 OPT_BOOLEAN('s', "stage", &show_stage
,
451 "show staged contents' object name in the output"),
452 OPT_BOOLEAN('k', "killed", &show_killed
,
453 "show files on the filesystem that need to be removed"),
454 OPT_BIT(0, "directory", &dir
.flags
,
455 "show 'other' directories' name only",
456 DIR_SHOW_OTHER_DIRECTORIES
),
457 OPT_BIT(0, "no-empty-directory", &dir
.flags
,
458 "don't show empty directories",
459 DIR_HIDE_EMPTY_DIRECTORIES
),
460 OPT_BOOLEAN('u', "unmerged", &show_unmerged
,
461 "show unmerged files in the output"),
462 { OPTION_CALLBACK
, 'x', "exclude", &dir
.exclude_list
[EXC_CMDL
], "pattern",
463 "skip files matching pattern",
464 0, option_parse_exclude
},
465 { OPTION_CALLBACK
, 'X', "exclude-from", &dir
, "file",
466 "exclude patterns are read from <file>",
467 0, option_parse_exclude_from
},
468 OPT_STRING(0, "exclude-per-directory", &dir
.exclude_per_dir
, "file",
469 "read additional per-directory exclude patterns in <file>"),
470 { OPTION_CALLBACK
, 0, "exclude-standard", &dir
, NULL
,
471 "add the standard git exclusions",
472 PARSE_OPT_NOARG
, option_parse_exclude_standard
},
473 { OPTION_SET_INT
, 0, "full-name", &prefix_offset
, NULL
,
474 "make the output relative to the project top directory",
475 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
},
476 OPT_BOOLEAN(0, "error-unmatch", &error_unmatch
,
477 "if any <file> is not in the index, treat this as an error"),
478 OPT_STRING(0, "with-tree", &with_tree
, "tree-ish",
479 "pretend that paths removed since <tree-ish> are still present"),
480 OPT__ABBREV(&abbrev
),
484 memset(&dir
, 0, sizeof(dir
));
486 prefix_offset
= strlen(prefix
);
487 git_config(git_default_config
, NULL
);
489 argc
= parse_options(argc
, argv
, builtin_ls_files_options
,
491 if (show_tag
|| show_valid_bit
) {
499 if (show_modified
|| show_others
|| (dir
.flags
& DIR_SHOW_IGNORED
) || show_killed
)
500 require_work_tree
= 1;
503 * There's no point in showing unmerged unless
504 * you also show the stage information.
507 if (dir
.exclude_per_dir
)
510 if (require_work_tree
&& !is_inside_work_tree())
513 pathspec
= get_pathspec(prefix
, argv
);
515 /* be nice with submodule patsh ending in a slash */
518 strip_trailing_slash_from_submodules();
520 /* Verify that the pathspec matches the prefix */
522 prefix
= verify_pathspec(prefix
);
524 /* Treat unmatching pathspec elements as errors */
525 if (pathspec
&& error_unmatch
) {
527 for (num
= 0; pathspec
[num
]; num
++)
529 ps_matched
= xcalloc(1, num
);
532 if ((dir
.flags
& DIR_SHOW_IGNORED
) && !exc_given
) {
533 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
538 /* With no flags, we default to showing the cached files */
539 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
|
540 show_killed
| show_modified
))
547 * Basic sanity check; show-stages and show-unmerged
548 * would not make any sense with this option.
550 if (show_stage
|| show_unmerged
)
551 die("ls-files --with-tree is incompatible with -s or -u");
552 overlay_tree_on_cache(with_tree
, prefix
);
554 show_files(&dir
, prefix
);
558 bad
= report_path_error(ps_matched
, pathspec
, prefix_offset
);
560 fprintf(stderr
, "Did you forget to 'git add'?\n");