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
15 static int show_deleted
;
16 static int show_cached
;
17 static int show_others
;
18 static int show_stage
;
19 static int show_unmerged
;
20 static int show_modified
;
21 static int show_killed
;
22 static int show_valid_bit
;
23 static int line_terminator
= '\n';
25 static int prefix_len
;
26 static int prefix_offset
;
27 static const char **pathspec
;
28 static int error_unmatch
;
29 static char *ps_matched
;
30 static const char *with_tree
;
32 static const char *tag_cached
= "";
33 static const char *tag_unmerged
= "";
34 static const char *tag_removed
= "";
35 static const char *tag_other
= "";
36 static const char *tag_killed
= "";
37 static const char *tag_modified
= "";
41 * Match a pathspec against a filename. The first "skiplen" characters
42 * are the common prefix
44 int pathspec_match(const char **spec
, char *ps_matched
,
45 const char *filename
, int skiplen
)
49 while ((m
= *spec
++) != NULL
) {
50 int matchlen
= strlen(m
+ skiplen
);
54 if (!strncmp(m
+ skiplen
, filename
+ skiplen
, matchlen
)) {
55 if (m
[skiplen
+ matchlen
- 1] == '/')
57 switch (filename
[skiplen
+ matchlen
]) {
62 if (!fnmatch(m
+ skiplen
, filename
+ skiplen
, 0))
75 static void show_dir_entry(const char *tag
, struct dir_entry
*ent
)
78 int offset
= prefix_offset
;
81 die("git ls-files: internal error - directory entry not superset of prefix");
83 if (pathspec
&& !pathspec_match(pathspec
, ps_matched
, ent
->name
, len
))
87 write_name_quoted(ent
->name
+ offset
, stdout
, line_terminator
);
90 static void show_other_files(struct dir_struct
*dir
)
96 * Skip matching and unmerged entries for the paths,
97 * since we want just "others".
99 * (Matching entries are normally pruned during
100 * the directory tree walk, but will show up for
101 * gitlinks because we don't necessarily have
102 * dir->show_other_directories set to suppress
105 for (i
= 0; i
< dir
->nr
; i
++) {
106 struct dir_entry
*ent
= dir
->entries
[i
];
108 struct cache_entry
*ce
;
111 * Remove the '/' at the end that directory
112 * walking adds for directory entries.
115 if (len
&& ent
->name
[len
-1] == '/')
117 pos
= cache_name_pos(ent
->name
, len
);
119 continue; /* exact match */
121 if (pos
< active_nr
) {
122 ce
= active_cache
[pos
];
123 if (ce_namelen(ce
) == len
&&
124 !memcmp(ce
->name
, ent
->name
, len
))
125 continue; /* Yup, this one exists unmerged */
127 show_dir_entry(tag_other
, ent
);
131 static void show_killed_files(struct dir_struct
*dir
)
134 for (i
= 0; i
< dir
->nr
; i
++) {
135 struct dir_entry
*ent
= dir
->entries
[i
];
137 int pos
, len
, killed
= 0;
139 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
140 sp
= strchr(cp
, '/');
142 /* If ent->name is prefix of an entry in the
143 * cache, it will be killed.
145 pos
= cache_name_pos(ent
->name
, ent
->len
);
147 die("bug in show-killed-files");
149 while (pos
< active_nr
&&
150 ce_stage(active_cache
[pos
]))
151 pos
++; /* skip unmerged */
152 if (active_nr
<= pos
)
154 /* pos points at a name immediately after
155 * ent->name in the cache. Does it expect
156 * ent->name to be a directory?
158 len
= ce_namelen(active_cache
[pos
]);
159 if ((ent
->len
< len
) &&
160 !strncmp(active_cache
[pos
]->name
,
161 ent
->name
, ent
->len
) &&
162 active_cache
[pos
]->name
[ent
->len
] == '/')
166 if (0 <= cache_name_pos(ent
->name
, sp
- ent
->name
)) {
167 /* If any of the leading directories in
168 * ent->name is registered in the cache,
169 * ent->name will be killed.
176 show_dir_entry(tag_killed
, dir
->entries
[i
]);
180 static void show_ce_entry(const char *tag
, struct cache_entry
*ce
)
182 int len
= prefix_len
;
183 int offset
= prefix_offset
;
185 if (len
>= ce_namelen(ce
))
186 die("git ls-files: internal error - cache entry not superset of prefix");
188 if (pathspec
&& !pathspec_match(pathspec
, ps_matched
, ce
->name
, len
))
191 if (tag
&& *tag
&& show_valid_bit
&&
192 (ce
->ce_flags
& CE_VALID
)) {
193 static char alttag
[4];
194 memcpy(alttag
, tag
, 3);
196 alttag
[0] = tolower(tag
[0]);
197 else if (tag
[0] == '?')
211 printf("%s%06o %s %d\t",
214 abbrev
? find_unique_abbrev(ce
->sha1
,abbrev
)
215 : sha1_to_hex(ce
->sha1
),
218 write_name_quoted(ce
->name
+ offset
, stdout
, line_terminator
);
221 static void show_files(struct dir_struct
*dir
, const char *prefix
)
225 /* For cached/deleted files we don't need to even do the readdir */
226 if (show_others
|| show_killed
) {
227 const char *path
= ".", *base
= "";
228 int baselen
= prefix_len
;
231 path
= base
= prefix
;
232 read_directory(dir
, path
, base
, baselen
, pathspec
);
234 show_other_files(dir
);
236 show_killed_files(dir
);
238 if (show_cached
| show_stage
) {
239 for (i
= 0; i
< active_nr
; i
++) {
240 struct cache_entry
*ce
= active_cache
[i
];
241 int dtype
= ce_to_dtype(ce
);
242 if (excluded(dir
, ce
->name
, &dtype
) != dir
->show_ignored
)
244 if (show_unmerged
&& !ce_stage(ce
))
246 if (ce
->ce_flags
& CE_UPDATE
)
248 show_ce_entry(ce_stage(ce
) ? tag_unmerged
: tag_cached
, ce
);
251 if (show_deleted
| show_modified
) {
252 for (i
= 0; i
< active_nr
; i
++) {
253 struct cache_entry
*ce
= active_cache
[i
];
256 int dtype
= ce_to_dtype(ce
);
257 if (excluded(dir
, ce
->name
, &dtype
) != dir
->show_ignored
)
259 err
= lstat(ce
->name
, &st
);
260 if (show_deleted
&& err
)
261 show_ce_entry(tag_removed
, ce
);
262 if (show_modified
&& ce_modified(ce
, &st
, 0))
263 show_ce_entry(tag_modified
, ce
);
269 * Prune the index to only contain stuff starting with "prefix"
271 static void prune_cache(const char *prefix
)
273 int pos
= cache_name_pos(prefix
, prefix_len
);
274 unsigned int first
, last
;
278 memmove(active_cache
, active_cache
+ pos
,
279 (active_nr
- pos
) * sizeof(struct cache_entry
*));
283 while (last
> first
) {
284 int next
= (last
+ first
) >> 1;
285 struct cache_entry
*ce
= active_cache
[next
];
286 if (!strncmp(ce
->name
, prefix
, prefix_len
)) {
295 static const char *verify_pathspec(const char *prefix
)
297 const char **p
, *n
, *prev
;
302 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
304 for (i
= 0; i
< max
; i
++) {
306 if (prev
&& prev
[i
] != c
)
308 if (!c
|| c
== '*' || c
== '?')
321 if (prefix_offset
> max
|| memcmp(prev
, prefix
, prefix_offset
))
322 die("git ls-files: cannot generate relative filenames containing '..'");
325 return max
? xmemdupz(prev
, max
) : NULL
;
329 * Read the tree specified with --with-tree option
330 * (typically, HEAD) into stage #1 and then
331 * squash them down to stage #0. This is used for
332 * --error-unmatch to list and check the path patterns
333 * that were given from the command line. We are not
334 * going to write this index out.
336 void overlay_tree_on_cache(const char *tree_name
, const char *prefix
)
339 unsigned char sha1
[20];
341 struct cache_entry
*last_stage0
= NULL
;
344 if (get_sha1(tree_name
, sha1
))
345 die("tree-ish %s not found.", tree_name
);
346 tree
= parse_tree_indirect(sha1
);
348 die("bad tree-ish %s", tree_name
);
350 /* Hoist the unmerged entries up to stage #3 to make room */
351 for (i
= 0; i
< active_nr
; i
++) {
352 struct cache_entry
*ce
= active_cache
[i
];
355 ce
->ce_flags
|= CE_STAGEMASK
;
359 static const char *(matchbuf
[2]);
360 matchbuf
[0] = prefix
;
365 if (read_tree(tree
, 1, match
))
366 die("unable to read tree entries %s", tree_name
);
368 for (i
= 0; i
< active_nr
; i
++) {
369 struct cache_entry
*ce
= active_cache
[i
];
370 switch (ce_stage(ce
)) {
378 * If there is stage #0 entry for this, we do not
379 * need to show it. We use CE_UPDATE bit to mark
383 !strcmp(last_stage0
->name
, ce
->name
))
384 ce
->ce_flags
|= CE_UPDATE
;
389 int report_path_error(const char *ps_matched
, const char **pathspec
, int prefix_offset
)
392 * Make sure all pathspec matched; otherwise it is an error.
395 for (num
= 0; pathspec
[num
]; num
++) {
396 int other
, found_dup
;
401 * The caller might have fed identical pathspec
402 * twice. Do not barf on such a mistake.
404 for (found_dup
= other
= 0;
405 !found_dup
&& pathspec
[other
];
407 if (other
== num
|| !ps_matched
[other
])
409 if (!strcmp(pathspec
[other
], pathspec
[num
]))
411 * Ok, we have a match already.
418 error("pathspec '%s' did not match any file(s) known to git.",
419 pathspec
[num
] + prefix_offset
);
425 static const char ls_files_usage
[] =
426 "git ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
427 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
428 "[ --exclude-per-directory=<filename> ] [--exclude-standard] "
429 "[--full-name] [--abbrev] [--] [<file>]*";
431 int cmd_ls_files(int argc
, const char **argv
, const char *prefix
)
434 int exc_given
= 0, require_work_tree
= 0;
435 struct dir_struct dir
;
437 memset(&dir
, 0, sizeof(dir
));
439 prefix_offset
= strlen(prefix
);
440 git_config(git_default_config
, NULL
);
442 for (i
= 1; i
< argc
; i
++) {
443 const char *arg
= argv
[i
];
445 if (!strcmp(arg
, "--")) {
449 if (!strcmp(arg
, "-z")) {
453 if (!strcmp(arg
, "-t") || !strcmp(arg
, "-v")) {
464 if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
468 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
472 if (!strcmp(arg
, "-m") || !strcmp(arg
, "--modified")) {
474 require_work_tree
= 1;
477 if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
479 require_work_tree
= 1;
482 if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
483 dir
.show_ignored
= 1;
484 require_work_tree
= 1;
487 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
491 if (!strcmp(arg
, "-k") || !strcmp(arg
, "--killed")) {
493 require_work_tree
= 1;
496 if (!strcmp(arg
, "--directory")) {
497 dir
.show_other_directories
= 1;
500 if (!strcmp(arg
, "--no-empty-directory")) {
501 dir
.hide_empty_directories
= 1;
504 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
505 /* There's no point in showing unmerged unless
506 * you also show the stage information.
512 if (!strcmp(arg
, "-x") && i
+1 < argc
) {
514 add_exclude(argv
[++i
], "", 0, &dir
.exclude_list
[EXC_CMDL
]);
517 if (!prefixcmp(arg
, "--exclude=")) {
519 add_exclude(arg
+10, "", 0, &dir
.exclude_list
[EXC_CMDL
]);
522 if (!strcmp(arg
, "-X") && i
+1 < argc
) {
524 add_excludes_from_file(&dir
, argv
[++i
]);
527 if (!prefixcmp(arg
, "--exclude-from=")) {
529 add_excludes_from_file(&dir
, arg
+15);
532 if (!prefixcmp(arg
, "--exclude-per-directory=")) {
534 dir
.exclude_per_dir
= arg
+ 24;
537 if (!strcmp(arg
, "--exclude-standard")) {
539 setup_standard_excludes(&dir
);
542 if (!strcmp(arg
, "--full-name")) {
546 if (!strcmp(arg
, "--error-unmatch")) {
550 if (!prefixcmp(arg
, "--with-tree=")) {
551 with_tree
= arg
+ 12;
554 if (!prefixcmp(arg
, "--abbrev=")) {
555 abbrev
= strtoul(arg
+9, NULL
, 10);
556 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
557 abbrev
= MINIMUM_ABBREV
;
558 else if (abbrev
> 40)
562 if (!strcmp(arg
, "--abbrev")) {
563 abbrev
= DEFAULT_ABBREV
;
567 usage(ls_files_usage
);
571 if (require_work_tree
&& !is_inside_work_tree())
574 pathspec
= get_pathspec(prefix
, argv
+ i
);
576 /* Verify that the pathspec matches the prefix */
578 prefix
= verify_pathspec(prefix
);
580 /* Treat unmatching pathspec elements as errors */
581 if (pathspec
&& error_unmatch
) {
583 for (num
= 0; pathspec
[num
]; num
++)
585 ps_matched
= xcalloc(1, num
);
588 if (dir
.show_ignored
&& !exc_given
) {
589 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
594 /* With no flags, we default to showing the cached files */
595 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
|
596 show_killed
| show_modified
))
604 * Basic sanity check; show-stages and show-unmerged
605 * would not make any sense with this option.
607 if (show_stage
|| show_unmerged
)
608 die("ls-files --with-tree is incompatible with -s or -u");
609 overlay_tree_on_cache(with_tree
, prefix
);
611 show_files(&dir
, prefix
);
615 bad
= report_path_error(ps_matched
, pathspec
, prefix_offset
);
617 fprintf(stderr
, "Did you forget to 'git add'?\n");