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 if (ce
->ce_flags
& CE_UPDATE
)
232 err
= lstat(ce
->name
, &st
);
233 if (show_deleted
&& err
)
234 show_ce_entry(tag_removed
, ce
);
235 if (show_modified
&& ce_modified(ce
, &st
, 0))
236 show_ce_entry(tag_modified
, ce
);
242 * Prune the index to only contain stuff starting with "prefix"
244 static void prune_cache(const char *prefix
)
246 int pos
= cache_name_pos(prefix
, prefix_len
);
247 unsigned int first
, last
;
251 memmove(active_cache
, active_cache
+ pos
,
252 (active_nr
- pos
) * sizeof(struct cache_entry
*));
256 while (last
> first
) {
257 int next
= (last
+ first
) >> 1;
258 struct cache_entry
*ce
= active_cache
[next
];
259 if (!strncmp(ce
->name
, prefix
, prefix_len
)) {
268 static const char *verify_pathspec(const char *prefix
)
270 const char **p
, *n
, *prev
;
275 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
277 for (i
= 0; i
< max
; i
++) {
279 if (prev
&& prev
[i
] != c
)
281 if (!c
|| c
== '*' || c
== '?')
294 if (prefix_offset
> max
|| memcmp(prev
, prefix
, prefix_offset
))
295 die("git ls-files: cannot generate relative filenames containing '..'");
298 return max
? xmemdupz(prev
, max
) : NULL
;
302 * Read the tree specified with --with-tree option
303 * (typically, HEAD) into stage #1 and then
304 * squash them down to stage #0. This is used for
305 * --error-unmatch to list and check the path patterns
306 * that were given from the command line. We are not
307 * going to write this index out.
309 void overlay_tree_on_cache(const char *tree_name
, const char *prefix
)
312 unsigned char sha1
[20];
314 struct cache_entry
*last_stage0
= NULL
;
317 if (get_sha1(tree_name
, sha1
))
318 die("tree-ish %s not found.", tree_name
);
319 tree
= parse_tree_indirect(sha1
);
321 die("bad tree-ish %s", tree_name
);
323 /* Hoist the unmerged entries up to stage #3 to make room */
324 for (i
= 0; i
< active_nr
; i
++) {
325 struct cache_entry
*ce
= active_cache
[i
];
328 ce
->ce_flags
|= CE_STAGEMASK
;
332 static const char *(matchbuf
[2]);
333 matchbuf
[0] = prefix
;
338 if (read_tree(tree
, 1, match
))
339 die("unable to read tree entries %s", tree_name
);
341 for (i
= 0; i
< active_nr
; i
++) {
342 struct cache_entry
*ce
= active_cache
[i
];
343 switch (ce_stage(ce
)) {
351 * If there is stage #0 entry for this, we do not
352 * need to show it. We use CE_UPDATE bit to mark
356 !strcmp(last_stage0
->name
, ce
->name
))
357 ce
->ce_flags
|= CE_UPDATE
;
362 int report_path_error(const char *ps_matched
, const char **pathspec
, int prefix_offset
)
365 * Make sure all pathspec matched; otherwise it is an error.
368 for (num
= 0; pathspec
[num
]; num
++) {
369 int other
, found_dup
;
374 * The caller might have fed identical pathspec
375 * twice. Do not barf on such a mistake.
377 for (found_dup
= other
= 0;
378 !found_dup
&& pathspec
[other
];
380 if (other
== num
|| !ps_matched
[other
])
382 if (!strcmp(pathspec
[other
], pathspec
[num
]))
384 * Ok, we have a match already.
391 error("pathspec '%s' did not match any file(s) known to git.",
392 pathspec
[num
] + prefix_offset
);
398 static const char ls_files_usage
[] =
399 "git ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
400 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
401 "[ --exclude-per-directory=<filename> ] [--exclude-standard] "
402 "[--full-name] [--abbrev] [--] [<file>]*";
404 int cmd_ls_files(int argc
, const char **argv
, const char *prefix
)
407 int exc_given
= 0, require_work_tree
= 0;
408 struct dir_struct dir
;
410 memset(&dir
, 0, sizeof(dir
));
412 prefix_offset
= strlen(prefix
);
413 git_config(git_default_config
, NULL
);
415 for (i
= 1; i
< argc
; i
++) {
416 const char *arg
= argv
[i
];
418 if (!strcmp(arg
, "--")) {
422 if (!strcmp(arg
, "-z")) {
426 if (!strcmp(arg
, "-t") || !strcmp(arg
, "-v")) {
437 if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
441 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
445 if (!strcmp(arg
, "-m") || !strcmp(arg
, "--modified")) {
447 require_work_tree
= 1;
450 if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
452 require_work_tree
= 1;
455 if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
456 dir
.show_ignored
= 1;
457 require_work_tree
= 1;
460 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
464 if (!strcmp(arg
, "-k") || !strcmp(arg
, "--killed")) {
466 require_work_tree
= 1;
469 if (!strcmp(arg
, "--directory")) {
470 dir
.show_other_directories
= 1;
473 if (!strcmp(arg
, "--no-empty-directory")) {
474 dir
.hide_empty_directories
= 1;
477 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
478 /* There's no point in showing unmerged unless
479 * you also show the stage information.
485 if (!strcmp(arg
, "-x") && i
+1 < argc
) {
487 add_exclude(argv
[++i
], "", 0, &dir
.exclude_list
[EXC_CMDL
]);
490 if (!prefixcmp(arg
, "--exclude=")) {
492 add_exclude(arg
+10, "", 0, &dir
.exclude_list
[EXC_CMDL
]);
495 if (!strcmp(arg
, "-X") && i
+1 < argc
) {
497 add_excludes_from_file(&dir
, argv
[++i
]);
500 if (!prefixcmp(arg
, "--exclude-from=")) {
502 add_excludes_from_file(&dir
, arg
+15);
505 if (!prefixcmp(arg
, "--exclude-per-directory=")) {
507 dir
.exclude_per_dir
= arg
+ 24;
510 if (!strcmp(arg
, "--exclude-standard")) {
512 setup_standard_excludes(&dir
);
515 if (!strcmp(arg
, "--full-name")) {
519 if (!strcmp(arg
, "--error-unmatch")) {
523 if (!prefixcmp(arg
, "--with-tree=")) {
524 with_tree
= arg
+ 12;
527 if (!prefixcmp(arg
, "--abbrev=")) {
528 abbrev
= strtoul(arg
+9, NULL
, 10);
529 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
530 abbrev
= MINIMUM_ABBREV
;
531 else if (abbrev
> 40)
535 if (!strcmp(arg
, "--abbrev")) {
536 abbrev
= DEFAULT_ABBREV
;
540 usage(ls_files_usage
);
544 if (require_work_tree
&& !is_inside_work_tree())
547 pathspec
= get_pathspec(prefix
, argv
+ i
);
549 /* Verify that the pathspec matches the prefix */
551 prefix
= verify_pathspec(prefix
);
553 /* Treat unmatching pathspec elements as errors */
554 if (pathspec
&& error_unmatch
) {
556 for (num
= 0; pathspec
[num
]; num
++)
558 ps_matched
= xcalloc(1, num
);
561 if (dir
.show_ignored
&& !exc_given
) {
562 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
567 /* With no flags, we default to showing the cached files */
568 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
|
569 show_killed
| show_modified
))
577 * Basic sanity check; show-stages and show-unmerged
578 * would not make any sense with this option.
580 if (show_stage
|| show_unmerged
)
581 die("ls-files --with-tree is incompatible with -s or -u");
582 overlay_tree_on_cache(with_tree
, prefix
);
584 show_files(&dir
, prefix
);
588 bad
= report_path_error(ps_matched
, pathspec
, prefix_offset
);
590 fprintf(stderr
, "Did you forget to 'git add'?\n");