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
14 static int show_deleted
;
15 static int show_cached
;
16 static int show_others
;
17 static int show_stage
;
18 static int show_unmerged
;
19 static int show_modified
;
20 static int show_killed
;
21 static int show_valid_bit
;
22 static int line_terminator
= '\n';
24 static int prefix_len
;
25 static int prefix_offset
;
26 static const char **pathspec
;
27 static int error_unmatch
;
28 static char *ps_matched
;
30 static const char *tag_cached
= "";
31 static const char *tag_unmerged
= "";
32 static const char *tag_removed
= "";
33 static const char *tag_other
= "";
34 static const char *tag_killed
= "";
35 static const char *tag_modified
= "";
39 * Match a pathspec against a filename. The first "len" characters
40 * are the common prefix
42 static int match(const char **spec
, char *ps_matched
,
43 const char *filename
, int len
)
47 while ((m
= *spec
++) != NULL
) {
48 int matchlen
= strlen(m
+ len
);
52 if (!strncmp(m
+ len
, filename
+ len
, matchlen
)) {
53 if (m
[len
+ matchlen
- 1] == '/')
55 switch (filename
[len
+ matchlen
]) {
60 if (!fnmatch(m
+ len
, filename
+ len
, 0))
73 static void show_dir_entry(const char *tag
, struct dir_entry
*ent
)
76 int offset
= prefix_offset
;
79 die("git-ls-files: internal error - directory entry not superset of prefix");
81 if (pathspec
&& !match(pathspec
, ps_matched
, ent
->name
, len
))
85 write_name_quoted("", 0, ent
->name
+ offset
, line_terminator
, stdout
);
86 putchar(line_terminator
);
89 static void show_other_files(struct dir_struct
*dir
)
95 * Skip matching and unmerged entries for the paths,
96 * since we want just "others".
98 * (Matching entries are normally pruned during
99 * the directory tree walk, but will show up for
100 * gitlinks because we don't necessarily have
101 * dir->show_other_directories set to suppress
104 for (i
= 0; i
< dir
->nr
; i
++) {
105 struct dir_entry
*ent
= dir
->entries
[i
];
107 struct cache_entry
*ce
;
110 * Remove the '/' at the end that directory
111 * walking adds for directory entries.
114 if (len
&& ent
->name
[len
-1] == '/')
116 pos
= cache_name_pos(ent
->name
, len
);
118 continue; /* exact match */
120 if (pos
< active_nr
) {
121 ce
= active_cache
[pos
];
122 if (ce_namelen(ce
) == len
&&
123 !memcmp(ce
->name
, ent
->name
, len
))
124 continue; /* Yup, this one exists unmerged */
126 show_dir_entry(tag_other
, ent
);
130 static void show_killed_files(struct dir_struct
*dir
)
133 for (i
= 0; i
< dir
->nr
; i
++) {
134 struct dir_entry
*ent
= dir
->entries
[i
];
136 int pos
, len
, killed
= 0;
138 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
139 sp
= strchr(cp
, '/');
141 /* If ent->name is prefix of an entry in the
142 * cache, it will be killed.
144 pos
= cache_name_pos(ent
->name
, ent
->len
);
146 die("bug in show-killed-files");
148 while (pos
< active_nr
&&
149 ce_stage(active_cache
[pos
]))
150 pos
++; /* skip unmerged */
151 if (active_nr
<= pos
)
153 /* pos points at a name immediately after
154 * ent->name in the cache. Does it expect
155 * ent->name to be a directory?
157 len
= ce_namelen(active_cache
[pos
]);
158 if ((ent
->len
< len
) &&
159 !strncmp(active_cache
[pos
]->name
,
160 ent
->name
, ent
->len
) &&
161 active_cache
[pos
]->name
[ent
->len
] == '/')
165 if (0 <= cache_name_pos(ent
->name
, sp
- ent
->name
)) {
166 /* If any of the leading directories in
167 * ent->name is registered in the cache,
168 * ent->name will be killed.
175 show_dir_entry(tag_killed
, dir
->entries
[i
]);
179 static void show_ce_entry(const char *tag
, struct cache_entry
*ce
)
181 int len
= prefix_len
;
182 int offset
= prefix_offset
;
184 if (len
>= ce_namelen(ce
))
185 die("git-ls-files: internal error - cache entry not superset of prefix");
187 if (pathspec
&& !match(pathspec
, ps_matched
, ce
->name
, len
))
190 if (tag
&& *tag
&& show_valid_bit
&&
191 (ce
->ce_flags
& htons(CE_VALID
))) {
192 static char alttag
[4];
193 memcpy(alttag
, tag
, 3);
195 alttag
[0] = tolower(tag
[0]);
196 else if (tag
[0] == '?')
209 write_name_quoted("", 0, ce
->name
+ offset
,
210 line_terminator
, stdout
);
211 putchar(line_terminator
);
214 printf("%s%06lo %s %u\t",
217 abbrev
? find_unique_abbrev(ce
->sha1
,abbrev
)
218 : sha1_to_hex(ce
->sha1
),
220 write_name_quoted("", 0, ce
->name
+ offset
,
221 line_terminator
, stdout
);
222 putchar(line_terminator
);
226 static void show_files(struct dir_struct
*dir
, const char *prefix
)
230 /* For cached/deleted files we don't need to even do the readdir */
231 if (show_others
|| show_killed
) {
232 const char *path
= ".", *base
= "";
233 int baselen
= prefix_len
;
236 path
= base
= prefix
;
237 read_directory(dir
, path
, base
, baselen
, pathspec
);
239 show_other_files(dir
);
241 show_killed_files(dir
);
243 if (show_cached
| show_stage
) {
244 for (i
= 0; i
< active_nr
; i
++) {
245 struct cache_entry
*ce
= active_cache
[i
];
246 if (excluded(dir
, ce
->name
) != dir
->show_ignored
)
248 if (show_unmerged
&& !ce_stage(ce
))
250 show_ce_entry(ce_stage(ce
) ? tag_unmerged
: tag_cached
, ce
);
253 if (show_deleted
| show_modified
) {
254 for (i
= 0; i
< active_nr
; i
++) {
255 struct cache_entry
*ce
= active_cache
[i
];
258 if (excluded(dir
, ce
->name
) != dir
->show_ignored
)
260 err
= lstat(ce
->name
, &st
);
261 if (show_deleted
&& err
)
262 show_ce_entry(tag_removed
, ce
);
263 if (show_modified
&& ce_modified(ce
, &st
, 0))
264 show_ce_entry(tag_modified
, ce
);
270 * Prune the index to only contain stuff starting with "prefix"
272 static void prune_cache(const char *prefix
)
274 int pos
= cache_name_pos(prefix
, prefix_len
);
275 unsigned int first
, last
;
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
;
303 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
305 for (i
= 0; i
< max
; i
++) {
307 if (prev
&& prev
[i
] != c
)
309 if (!c
|| c
== '*' || c
== '?')
322 if (prefix_offset
> max
|| memcmp(prev
, prefix
, prefix_offset
))
323 die("git-ls-files: cannot generate relative filenames containing '..'");
328 real_prefix
= xmalloc(max
+ 1);
329 memcpy(real_prefix
, prev
, max
);
330 real_prefix
[max
] = 0;
335 static const char ls_files_usage
[] =
336 "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
337 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
338 "[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] "
341 int cmd_ls_files(int argc
, const char **argv
, const char *prefix
)
344 int exc_given
= 0, require_work_tree
= 0;
345 struct dir_struct dir
;
347 memset(&dir
, 0, sizeof(dir
));
349 prefix_offset
= strlen(prefix
);
350 git_config(git_default_config
);
352 for (i
= 1; i
< argc
; i
++) {
353 const char *arg
= argv
[i
];
355 if (!strcmp(arg
, "--")) {
359 if (!strcmp(arg
, "-z")) {
363 if (!strcmp(arg
, "-t") || !strcmp(arg
, "-v")) {
374 if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
378 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
382 if (!strcmp(arg
, "-m") || !strcmp(arg
, "--modified")) {
384 require_work_tree
= 1;
387 if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
389 require_work_tree
= 1;
392 if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
393 dir
.show_ignored
= 1;
394 require_work_tree
= 1;
397 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
401 if (!strcmp(arg
, "-k") || !strcmp(arg
, "--killed")) {
403 require_work_tree
= 1;
406 if (!strcmp(arg
, "--directory")) {
407 dir
.show_other_directories
= 1;
410 if (!strcmp(arg
, "--no-empty-directory")) {
411 dir
.hide_empty_directories
= 1;
414 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
415 /* There's no point in showing unmerged unless
416 * you also show the stage information.
422 if (!strcmp(arg
, "-x") && i
+1 < argc
) {
424 add_exclude(argv
[++i
], "", 0, &dir
.exclude_list
[EXC_CMDL
]);
427 if (!prefixcmp(arg
, "--exclude=")) {
429 add_exclude(arg
+10, "", 0, &dir
.exclude_list
[EXC_CMDL
]);
432 if (!strcmp(arg
, "-X") && i
+1 < argc
) {
434 add_excludes_from_file(&dir
, argv
[++i
]);
437 if (!prefixcmp(arg
, "--exclude-from=")) {
439 add_excludes_from_file(&dir
, arg
+15);
442 if (!prefixcmp(arg
, "--exclude-per-directory=")) {
444 dir
.exclude_per_dir
= arg
+ 24;
447 if (!strcmp(arg
, "--full-name")) {
451 if (!strcmp(arg
, "--error-unmatch")) {
455 if (!prefixcmp(arg
, "--abbrev=")) {
456 abbrev
= strtoul(arg
+9, NULL
, 10);
457 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
458 abbrev
= MINIMUM_ABBREV
;
459 else if (abbrev
> 40)
463 if (!strcmp(arg
, "--abbrev")) {
464 abbrev
= DEFAULT_ABBREV
;
468 usage(ls_files_usage
);
472 if (require_work_tree
&& !is_inside_work_tree()) {
473 const char *work_tree
= get_git_work_tree();
474 if (!work_tree
|| chdir(work_tree
))
475 die("This operation must be run in a work tree");
478 pathspec
= get_pathspec(prefix
, argv
+ i
);
480 /* Verify that the pathspec matches the prefix */
482 prefix
= verify_pathspec(prefix
);
484 /* Treat unmatching pathspec elements as errors */
485 if (pathspec
&& error_unmatch
) {
487 for (num
= 0; pathspec
[num
]; num
++)
489 ps_matched
= xcalloc(1, num
);
492 if (dir
.show_ignored
&& !exc_given
) {
493 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
498 /* With no flags, we default to showing the cached files */
499 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
|
500 show_killed
| show_modified
))
506 show_files(&dir
, prefix
);
509 /* We need to make sure all pathspec matched otherwise
513 for (num
= 0; pathspec
[num
]; num
++) {
516 error("pathspec '%s' did not match any file(s) known to git.",
517 pathspec
[num
] + prefix_offset
);
522 fprintf(stderr
, "Did you forget to 'git add'?\n");
524 return errors
? 1 : 0;