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
)
94 for (i
= 0; i
< dir
->nr
; i
++) {
95 struct dir_entry
*ent
= dir
->entries
[i
];
96 if (!cache_name_is_other(ent
->name
, ent
->len
))
98 show_dir_entry(tag_other
, ent
);
102 static void show_killed_files(struct dir_struct
*dir
)
105 for (i
= 0; i
< dir
->nr
; i
++) {
106 struct dir_entry
*ent
= dir
->entries
[i
];
108 int pos
, len
, killed
= 0;
110 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
111 sp
= strchr(cp
, '/');
113 /* If ent->name is prefix of an entry in the
114 * cache, it will be killed.
116 pos
= cache_name_pos(ent
->name
, ent
->len
);
118 die("bug in show-killed-files");
120 while (pos
< active_nr
&&
121 ce_stage(active_cache
[pos
]))
122 pos
++; /* skip unmerged */
123 if (active_nr
<= pos
)
125 /* pos points at a name immediately after
126 * ent->name in the cache. Does it expect
127 * ent->name to be a directory?
129 len
= ce_namelen(active_cache
[pos
]);
130 if ((ent
->len
< len
) &&
131 !strncmp(active_cache
[pos
]->name
,
132 ent
->name
, ent
->len
) &&
133 active_cache
[pos
]->name
[ent
->len
] == '/')
137 if (0 <= cache_name_pos(ent
->name
, sp
- ent
->name
)) {
138 /* If any of the leading directories in
139 * ent->name is registered in the cache,
140 * ent->name will be killed.
147 show_dir_entry(tag_killed
, dir
->entries
[i
]);
151 static void show_ce_entry(const char *tag
, struct cache_entry
*ce
)
153 int len
= prefix_len
;
154 int offset
= prefix_offset
;
156 if (len
>= ce_namelen(ce
))
157 die("git ls-files: internal error - cache entry not superset of prefix");
159 if (pathspec
&& !pathspec_match(pathspec
, ps_matched
, ce
->name
, len
))
162 if (tag
&& *tag
&& show_valid_bit
&&
163 (ce
->ce_flags
& CE_VALID
)) {
164 static char alttag
[4];
165 memcpy(alttag
, tag
, 3);
167 alttag
[0] = tolower(tag
[0]);
168 else if (tag
[0] == '?')
182 printf("%s%06o %s %d\t",
185 abbrev
? find_unique_abbrev(ce
->sha1
,abbrev
)
186 : sha1_to_hex(ce
->sha1
),
189 write_name_quoted(ce
->name
+ offset
, stdout
, line_terminator
);
192 static void show_files(struct dir_struct
*dir
, const char *prefix
)
196 /* For cached/deleted files we don't need to even do the readdir */
197 if (show_others
|| show_killed
) {
198 const char *path
= ".", *base
= "";
199 int baselen
= prefix_len
;
202 path
= base
= prefix
;
203 read_directory(dir
, path
, base
, baselen
, pathspec
);
205 show_other_files(dir
);
207 show_killed_files(dir
);
209 if (show_cached
| show_stage
) {
210 for (i
= 0; i
< active_nr
; i
++) {
211 struct cache_entry
*ce
= active_cache
[i
];
212 int dtype
= ce_to_dtype(ce
);
213 if (excluded(dir
, ce
->name
, &dtype
) != dir
->show_ignored
)
215 if (show_unmerged
&& !ce_stage(ce
))
217 if (ce
->ce_flags
& CE_UPDATE
)
219 show_ce_entry(ce_stage(ce
) ? tag_unmerged
: tag_cached
, ce
);
222 if (show_deleted
| show_modified
) {
223 for (i
= 0; i
< active_nr
; i
++) {
224 struct cache_entry
*ce
= active_cache
[i
];
227 int dtype
= ce_to_dtype(ce
);
228 if (excluded(dir
, ce
->name
, &dtype
) != dir
->show_ignored
)
230 err
= lstat(ce
->name
, &st
);
231 if (show_deleted
&& err
)
232 show_ce_entry(tag_removed
, ce
);
233 if (show_modified
&& ce_modified(ce
, &st
, 0))
234 show_ce_entry(tag_modified
, ce
);
240 * Prune the index to only contain stuff starting with "prefix"
242 static void prune_cache(const char *prefix
)
244 int pos
= cache_name_pos(prefix
, prefix_len
);
245 unsigned int first
, last
;
249 memmove(active_cache
, active_cache
+ pos
,
250 (active_nr
- pos
) * sizeof(struct cache_entry
*));
254 while (last
> first
) {
255 int next
= (last
+ first
) >> 1;
256 struct cache_entry
*ce
= active_cache
[next
];
257 if (!strncmp(ce
->name
, prefix
, prefix_len
)) {
266 static const char *verify_pathspec(const char *prefix
)
268 const char **p
, *n
, *prev
;
273 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
275 for (i
= 0; i
< max
; i
++) {
277 if (prev
&& prev
[i
] != c
)
279 if (!c
|| c
== '*' || c
== '?')
292 if (prefix_offset
> max
|| memcmp(prev
, prefix
, prefix_offset
))
293 die("git ls-files: cannot generate relative filenames containing '..'");
296 return max
? xmemdupz(prev
, max
) : NULL
;
300 * Read the tree specified with --with-tree option
301 * (typically, HEAD) into stage #1 and then
302 * squash them down to stage #0. This is used for
303 * --error-unmatch to list and check the path patterns
304 * that were given from the command line. We are not
305 * going to write this index out.
307 void overlay_tree_on_cache(const char *tree_name
, const char *prefix
)
310 unsigned char sha1
[20];
312 struct cache_entry
*last_stage0
= NULL
;
315 if (get_sha1(tree_name
, sha1
))
316 die("tree-ish %s not found.", tree_name
);
317 tree
= parse_tree_indirect(sha1
);
319 die("bad tree-ish %s", tree_name
);
321 /* Hoist the unmerged entries up to stage #3 to make room */
322 for (i
= 0; i
< active_nr
; i
++) {
323 struct cache_entry
*ce
= active_cache
[i
];
326 ce
->ce_flags
|= CE_STAGEMASK
;
330 static const char *(matchbuf
[2]);
331 matchbuf
[0] = prefix
;
336 if (read_tree(tree
, 1, match
))
337 die("unable to read tree entries %s", tree_name
);
339 for (i
= 0; i
< active_nr
; i
++) {
340 struct cache_entry
*ce
= active_cache
[i
];
341 switch (ce_stage(ce
)) {
349 * If there is stage #0 entry for this, we do not
350 * need to show it. We use CE_UPDATE bit to mark
354 !strcmp(last_stage0
->name
, ce
->name
))
355 ce
->ce_flags
|= CE_UPDATE
;
360 int report_path_error(const char *ps_matched
, const char **pathspec
, int prefix_offset
)
363 * Make sure all pathspec matched; otherwise it is an error.
366 for (num
= 0; pathspec
[num
]; num
++) {
367 int other
, found_dup
;
372 * The caller might have fed identical pathspec
373 * twice. Do not barf on such a mistake.
375 for (found_dup
= other
= 0;
376 !found_dup
&& pathspec
[other
];
378 if (other
== num
|| !ps_matched
[other
])
380 if (!strcmp(pathspec
[other
], pathspec
[num
]))
382 * Ok, we have a match already.
389 error("pathspec '%s' did not match any file(s) known to git.",
390 pathspec
[num
] + prefix_offset
);
396 static const char ls_files_usage
[] =
397 "git ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
398 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
399 "[ --exclude-per-directory=<filename> ] [--exclude-standard] "
400 "[--full-name] [--abbrev] [--] [<file>]*";
402 int cmd_ls_files(int argc
, const char **argv
, const char *prefix
)
405 int exc_given
= 0, require_work_tree
= 0;
406 struct dir_struct dir
;
408 memset(&dir
, 0, sizeof(dir
));
410 prefix_offset
= strlen(prefix
);
411 git_config(git_default_config
, NULL
);
413 for (i
= 1; i
< argc
; i
++) {
414 const char *arg
= argv
[i
];
416 if (!strcmp(arg
, "--")) {
420 if (!strcmp(arg
, "-z")) {
424 if (!strcmp(arg
, "-t") || !strcmp(arg
, "-v")) {
435 if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
439 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
443 if (!strcmp(arg
, "-m") || !strcmp(arg
, "--modified")) {
445 require_work_tree
= 1;
448 if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
450 require_work_tree
= 1;
453 if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
454 dir
.show_ignored
= 1;
455 require_work_tree
= 1;
458 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
462 if (!strcmp(arg
, "-k") || !strcmp(arg
, "--killed")) {
464 require_work_tree
= 1;
467 if (!strcmp(arg
, "--directory")) {
468 dir
.show_other_directories
= 1;
471 if (!strcmp(arg
, "--no-empty-directory")) {
472 dir
.hide_empty_directories
= 1;
475 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
476 /* There's no point in showing unmerged unless
477 * you also show the stage information.
483 if (!strcmp(arg
, "-x") && i
+1 < argc
) {
485 add_exclude(argv
[++i
], "", 0, &dir
.exclude_list
[EXC_CMDL
]);
488 if (!prefixcmp(arg
, "--exclude=")) {
490 add_exclude(arg
+10, "", 0, &dir
.exclude_list
[EXC_CMDL
]);
493 if (!strcmp(arg
, "-X") && i
+1 < argc
) {
495 add_excludes_from_file(&dir
, argv
[++i
]);
498 if (!prefixcmp(arg
, "--exclude-from=")) {
500 add_excludes_from_file(&dir
, arg
+15);
503 if (!prefixcmp(arg
, "--exclude-per-directory=")) {
505 dir
.exclude_per_dir
= arg
+ 24;
508 if (!strcmp(arg
, "--exclude-standard")) {
510 setup_standard_excludes(&dir
);
513 if (!strcmp(arg
, "--full-name")) {
517 if (!strcmp(arg
, "--error-unmatch")) {
521 if (!prefixcmp(arg
, "--with-tree=")) {
522 with_tree
= arg
+ 12;
525 if (!prefixcmp(arg
, "--abbrev=")) {
526 abbrev
= strtoul(arg
+9, NULL
, 10);
527 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
528 abbrev
= MINIMUM_ABBREV
;
529 else if (abbrev
> 40)
533 if (!strcmp(arg
, "--abbrev")) {
534 abbrev
= DEFAULT_ABBREV
;
538 usage(ls_files_usage
);
542 if (require_work_tree
&& !is_inside_work_tree())
545 pathspec
= get_pathspec(prefix
, argv
+ i
);
547 /* Verify that the pathspec matches the prefix */
549 prefix
= verify_pathspec(prefix
);
551 /* Treat unmatching pathspec elements as errors */
552 if (pathspec
&& error_unmatch
) {
554 for (num
= 0; pathspec
[num
]; num
++)
556 ps_matched
= xcalloc(1, num
);
559 if (dir
.show_ignored
&& !exc_given
) {
560 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
565 /* With no flags, we default to showing the cached files */
566 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
|
567 show_killed
| show_modified
))
575 * Basic sanity check; show-stages and show-unmerged
576 * would not make any sense with this option.
578 if (show_stage
|| show_unmerged
)
579 die("ls-files --with-tree is incompatible with -s or -u");
580 overlay_tree_on_cache(with_tree
, prefix
);
582 show_files(&dir
, prefix
);
586 bad
= report_path_error(ps_matched
, pathspec
, prefix_offset
);
588 fprintf(stderr
, "Did you forget to 'git add'?\n");