Optimize directory listing with pathspec limiter.
[git/git.git] / builtin-ls-files.c
CommitLineData
8695c8bf
LT
1/*
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.
5 *
6 * Copyright (C) Linus Torvalds, 2005
7 */
8695c8bf 8#include "cache.h"
22ddf719 9#include "quote.h"
453ec4bd 10#include "dir.h"
0864f264 11#include "builtin.h"
8695c8bf 12
96f1e58f
DR
13static int abbrev;
14static int show_deleted;
15static int show_cached;
16static int show_others;
17static int show_stage;
18static int show_unmerged;
19static int show_modified;
20static int show_killed;
21static int show_valid_bit;
b83c8345 22static int line_terminator = '\n';
8695c8bf 23
96f1e58f
DR
24static int prefix_len;
25static int prefix_offset;
26static const char **pathspec;
27static int error_unmatch;
28static char *ps_matched;
5be4efbe 29
20d37ef6
PB
30static const char *tag_cached = "";
31static const char *tag_unmerged = "";
32static const char *tag_removed = "";
33static const char *tag_other = "";
6ca45943 34static const char *tag_killed = "";
b0391890 35static const char *tag_modified = "";
20d37ef6 36
8695c8bf 37
56fc5108
LT
38/*
39 * Match a pathspec against a filename. The first "len" characters
40 * are the common prefix
41 */
bba319b5
JH
42static int match(const char **spec, char *ps_matched,
43 const char *filename, int len)
56fc5108
LT
44{
45 const char *m;
46
47 while ((m = *spec++) != NULL) {
48 int matchlen = strlen(m + len);
49
50 if (!matchlen)
bba319b5 51 goto matched;
56fc5108
LT
52 if (!strncmp(m + len, filename + len, matchlen)) {
53 if (m[len + matchlen - 1] == '/')
bba319b5 54 goto matched;
56fc5108
LT
55 switch (filename[len + matchlen]) {
56 case '/': case '\0':
bba319b5 57 goto matched;
56fc5108
LT
58 }
59 }
60 if (!fnmatch(m + len, filename + len, 0))
bba319b5
JH
61 goto matched;
62 if (ps_matched)
63 ps_matched++;
64 continue;
65 matched:
66 if (ps_matched)
67 *ps_matched = 1;
68 return 1;
56fc5108
LT
69 }
70 return 0;
71}
72
453ec4bd 73static void show_dir_entry(const char *tag, struct dir_entry *ent)
5be4efbe
LT
74{
75 int len = prefix_len;
76 int offset = prefix_offset;
77
78 if (len >= ent->len)
79 die("git-ls-files: internal error - directory entry not superset of prefix");
80
bba319b5 81 if (pathspec && !match(pathspec, ps_matched, ent->name, len))
5be4efbe
LT
82 return;
83
22ddf719 84 fputs(tag, stdout);
9ef2b3cb 85 write_name_quoted("", 0, ent->name + offset, line_terminator, stdout);
22ddf719 86 putchar(line_terminator);
5be4efbe
LT
87}
88
453ec4bd 89static void show_other_files(struct dir_struct *dir)
fcbc3083
JH
90{
91 int i;
453ec4bd 92 for (i = 0; i < dir->nr; i++) {
fcbc3083
JH
93 /* We should not have a matching entry, but we
94 * may have an unmerged entry for this path.
95 */
453ec4bd 96 struct dir_entry *ent = dir->entries[i];
fcbc3083
JH
97 int pos = cache_name_pos(ent->name, ent->len);
98 struct cache_entry *ce;
99 if (0 <= pos)
100 die("bug in show-other-files");
101 pos = -pos - 1;
102 if (pos < active_nr) {
103 ce = active_cache[pos];
104 if (ce_namelen(ce) == ent->len &&
105 !memcmp(ce->name, ent->name, ent->len))
106 continue; /* Yup, this one exists unmerged */
107 }
108 show_dir_entry(tag_other, ent);
109 }
110}
111
453ec4bd 112static void show_killed_files(struct dir_struct *dir)
6ca45943
JH
113{
114 int i;
453ec4bd
LT
115 for (i = 0; i < dir->nr; i++) {
116 struct dir_entry *ent = dir->entries[i];
6ca45943
JH
117 char *cp, *sp;
118 int pos, len, killed = 0;
119
120 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
121 sp = strchr(cp, '/');
122 if (!sp) {
123 /* If ent->name is prefix of an entry in the
124 * cache, it will be killed.
125 */
126 pos = cache_name_pos(ent->name, ent->len);
127 if (0 <= pos)
128 die("bug in show-killed-files");
129 pos = -pos - 1;
130 while (pos < active_nr &&
131 ce_stage(active_cache[pos]))
132 pos++; /* skip unmerged */
133 if (active_nr <= pos)
134 break;
135 /* pos points at a name immediately after
136 * ent->name in the cache. Does it expect
137 * ent->name to be a directory?
138 */
139 len = ce_namelen(active_cache[pos]);
140 if ((ent->len < len) &&
141 !strncmp(active_cache[pos]->name,
142 ent->name, ent->len) &&
143 active_cache[pos]->name[ent->len] == '/')
144 killed = 1;
145 break;
146 }
147 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
148 /* If any of the leading directories in
149 * ent->name is registered in the cache,
150 * ent->name will be killed.
151 */
152 killed = 1;
153 break;
154 }
155 }
156 if (killed)
453ec4bd 157 show_dir_entry(tag_killed, dir->entries[i]);
6ca45943 158 }
8695c8bf
LT
159}
160
5be4efbe
LT
161static void show_ce_entry(const char *tag, struct cache_entry *ce)
162{
163 int len = prefix_len;
164 int offset = prefix_offset;
165
166 if (len >= ce_namelen(ce))
167 die("git-ls-files: internal error - cache entry not superset of prefix");
168
bba319b5 169 if (pathspec && !match(pathspec, ps_matched, ce->name, len))
5be4efbe
LT
170 return;
171
8bb2e03b
JH
172 if (tag && *tag && show_valid_bit &&
173 (ce->ce_flags & htons(CE_VALID))) {
2bcab240
JH
174 static char alttag[4];
175 memcpy(alttag, tag, 3);
176 if (isalpha(tag[0]))
177 alttag[0] = tolower(tag[0]);
178 else if (tag[0] == '?')
179 alttag[0] = '!';
180 else {
181 alttag[0] = 'v';
182 alttag[1] = tag[0];
183 alttag[2] = ' ';
184 alttag[3] = 0;
185 }
186 tag = alttag;
187 }
188
22ddf719
JH
189 if (!show_stage) {
190 fputs(tag, stdout);
9ef2b3cb
JH
191 write_name_quoted("", 0, ce->name + offset,
192 line_terminator, stdout);
22ddf719
JH
193 putchar(line_terminator);
194 }
195 else {
196 printf("%s%06o %s %d\t",
5be4efbe
LT
197 tag,
198 ntohl(ce->ce_mode),
ad0cae4c
EW
199 abbrev ? find_unique_abbrev(ce->sha1,abbrev)
200 : sha1_to_hex(ce->sha1),
22ddf719 201 ce_stage(ce));
9ef2b3cb
JH
202 write_name_quoted("", 0, ce->name + offset,
203 line_terminator, stdout);
22ddf719
JH
204 putchar(line_terminator);
205 }
5be4efbe
LT
206}
207
3e04228b 208static void show_files(struct dir_struct *dir, const char *prefix)
8695c8bf
LT
209{
210 int i;
211
212 /* For cached/deleted files we don't need to even do the readdir */
6ca45943 213 if (show_others || show_killed) {
5be4efbe
LT
214 const char *path = ".", *base = "";
215 int baselen = prefix_len;
216
b4189aa8 217 if (baselen)
5be4efbe 218 path = base = prefix;
9fc42d60 219 read_directory(dir, path, base, baselen, pathspec);
6ca45943 220 if (show_others)
453ec4bd 221 show_other_files(dir);
6ca45943 222 if (show_killed)
453ec4bd 223 show_killed_files(dir);
8695c8bf 224 }
aee46198 225 if (show_cached | show_stage) {
8695c8bf
LT
226 for (i = 0; i < active_nr; i++) {
227 struct cache_entry *ce = active_cache[i];
453ec4bd 228 if (excluded(dir, ce->name) != dir->show_ignored)
9ff768e9 229 continue;
eec8c633
LT
230 if (show_unmerged && !ce_stage(ce))
231 continue;
5be4efbe 232 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
8695c8bf
LT
233 }
234 }
b0391890 235 if (show_deleted | show_modified) {
8695c8bf
LT
236 for (i = 0; i < active_nr; i++) {
237 struct cache_entry *ce = active_cache[i];
238 struct stat st;
b0391890 239 int err;
453ec4bd 240 if (excluded(dir, ce->name) != dir->show_ignored)
9ff768e9 241 continue;
b0391890
JH
242 err = lstat(ce->name, &st);
243 if (show_deleted && err)
244 show_ce_entry(tag_removed, ce);
2bcab240 245 if (show_modified && ce_modified(ce, &st, 0))
b0391890 246 show_ce_entry(tag_modified, ce);
5be4efbe
LT
247 }
248 }
249}
250
251/*
252 * Prune the index to only contain stuff starting with "prefix"
253 */
3e04228b 254static void prune_cache(const char *prefix)
5be4efbe
LT
255{
256 int pos = cache_name_pos(prefix, prefix_len);
257 unsigned int first, last;
258
259 if (pos < 0)
260 pos = -pos-1;
261 active_cache += pos;
262 active_nr -= pos;
263 first = 0;
264 last = active_nr;
265 while (last > first) {
266 int next = (last + first) >> 1;
267 struct cache_entry *ce = active_cache[next];
268 if (!strncmp(ce->name, prefix, prefix_len)) {
269 first = next+1;
270 continue;
8695c8bf 271 }
5be4efbe
LT
272 last = next;
273 }
274 active_nr = last;
275}
276
3e04228b 277static const char *verify_pathspec(const char *prefix)
5be4efbe 278{
56fc5108
LT
279 const char **p, *n, *prev;
280 char *real_prefix;
281 unsigned long max;
282
283 prev = NULL;
284 max = PATH_MAX;
285 for (p = pathspec; (n = *p) != NULL; p++) {
286 int i, len = 0;
287 for (i = 0; i < max; i++) {
288 char c = n[i];
289 if (prev && prev[i] != c)
290 break;
56906143 291 if (!c || c == '*' || c == '?')
56fc5108
LT
292 break;
293 if (c == '/')
294 len = i+1;
295 }
296 prev = n;
297 if (len < max) {
298 max = len;
299 if (!max)
300 break;
301 }
5be4efbe 302 }
56fc5108
LT
303
304 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
305 die("git-ls-files: cannot generate relative filenames containing '..'");
306
307 real_prefix = NULL;
308 prefix_len = max;
309 if (max) {
310 real_prefix = xmalloc(max + 1);
311 memcpy(real_prefix, prev, max);
312 real_prefix[max] = 0;
8695c8bf 313 }
3e04228b 314 return real_prefix;
8695c8bf
LT
315}
316
4d1f1190 317static const char ls_files_usage[] =
8bb2e03b 318 "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
f87f9497 319 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
ad0cae4c
EW
320 "[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] "
321 "[--] [<file>]*";
cf9a113d 322
a633fca0 323int cmd_ls_files(int argc, const char **argv, const char *prefix)
8695c8bf
LT
324{
325 int i;
6d9ba67b 326 int exc_given = 0, require_work_tree = 0;
453ec4bd 327 struct dir_struct dir;
8695c8bf 328
453ec4bd 329 memset(&dir, 0, sizeof(dir));
5be4efbe 330 if (prefix)
56fc5108 331 prefix_offset = strlen(prefix);
39b4ac99 332 git_config(git_default_config);
5be4efbe 333
8695c8bf 334 for (i = 1; i < argc; i++) {
6b5ee137 335 const char *arg = argv[i];
8695c8bf 336
500b97e4
FK
337 if (!strcmp(arg, "--")) {
338 i++;
339 break;
340 }
b83c8345
JH
341 if (!strcmp(arg, "-z")) {
342 line_terminator = 0;
5be4efbe
LT
343 continue;
344 }
8bb2e03b 345 if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) {
20d37ef6
PB
346 tag_cached = "H ";
347 tag_unmerged = "M ";
348 tag_removed = "R ";
b0391890 349 tag_modified = "C ";
20d37ef6 350 tag_other = "? ";
6ca45943 351 tag_killed = "K ";
8bb2e03b
JH
352 if (arg[1] == 'v')
353 show_valid_bit = 1;
5be4efbe
LT
354 continue;
355 }
356 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
8695c8bf 357 show_cached = 1;
5be4efbe
LT
358 continue;
359 }
360 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
8695c8bf 361 show_deleted = 1;
5be4efbe
LT
362 continue;
363 }
b0391890
JH
364 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
365 show_modified = 1;
6d9ba67b 366 require_work_tree = 1;
b0391890
JH
367 continue;
368 }
5be4efbe 369 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
8695c8bf 370 show_others = 1;
6d9ba67b 371 require_work_tree = 1;
5be4efbe
LT
372 continue;
373 }
374 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
453ec4bd 375 dir.show_ignored = 1;
6d9ba67b 376 require_work_tree = 1;
5be4efbe
LT
377 continue;
378 }
379 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
aee46198 380 show_stage = 1;
5be4efbe
LT
381 continue;
382 }
383 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
6ca45943 384 show_killed = 1;
6d9ba67b 385 require_work_tree = 1;
5be4efbe
LT
386 continue;
387 }
9518eb26 388 if (!strcmp(arg, "--directory")) {
453ec4bd 389 dir.show_other_directories = 1;
9518eb26
LT
390 continue;
391 }
b0a3de42 392 if (!strcmp(arg, "--no-empty-directory")) {
453ec4bd 393 dir.hide_empty_directories = 1;
b0a3de42
PB
394 continue;
395 }
5be4efbe 396 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
20d37ef6
PB
397 /* There's no point in showing unmerged unless
398 * you also show the stage information.
399 */
eec8c633
LT
400 show_stage = 1;
401 show_unmerged = 1;
5be4efbe
LT
402 continue;
403 }
404 if (!strcmp(arg, "-x") && i+1 < argc) {
fee88256 405 exc_given = 1;
453ec4bd 406 add_exclude(argv[++i], "", 0, &dir.exclude_list[EXC_CMDL]);
5be4efbe
LT
407 continue;
408 }
cc44c765 409 if (!prefixcmp(arg, "--exclude=")) {
fee88256 410 exc_given = 1;
453ec4bd 411 add_exclude(arg+10, "", 0, &dir.exclude_list[EXC_CMDL]);
5be4efbe
LT
412 continue;
413 }
414 if (!strcmp(arg, "-X") && i+1 < argc) {
fee88256 415 exc_given = 1;
453ec4bd 416 add_excludes_from_file(&dir, argv[++i]);
5be4efbe
LT
417 continue;
418 }
cc44c765 419 if (!prefixcmp(arg, "--exclude-from=")) {
fee88256 420 exc_given = 1;
453ec4bd 421 add_excludes_from_file(&dir, arg+15);
5be4efbe
LT
422 continue;
423 }
cc44c765 424 if (!prefixcmp(arg, "--exclude-per-directory=")) {
fee88256 425 exc_given = 1;
453ec4bd 426 dir.exclude_per_dir = arg + 24;
5be4efbe
LT
427 continue;
428 }
429 if (!strcmp(arg, "--full-name")) {
430 prefix_offset = 0;
431 continue;
432 }
bba319b5
JH
433 if (!strcmp(arg, "--error-unmatch")) {
434 error_unmatch = 1;
435 continue;
436 }
cc44c765 437 if (!prefixcmp(arg, "--abbrev=")) {
ad0cae4c
EW
438 abbrev = strtoul(arg+9, NULL, 10);
439 if (abbrev && abbrev < MINIMUM_ABBREV)
440 abbrev = MINIMUM_ABBREV;
441 else if (abbrev > 40)
442 abbrev = 40;
443 continue;
444 }
445 if (!strcmp(arg, "--abbrev")) {
446 abbrev = DEFAULT_ABBREV;
447 continue;
448 }
56fc5108 449 if (*arg == '-')
17710391 450 usage(ls_files_usage);
56fc5108 451 break;
9ff768e9
NP
452 }
453
6d9ba67b
JS
454 if (require_work_tree &&
455 (is_bare_repository() || is_inside_git_dir()))
456 die("This operation must be run in a work tree");
457
56fc5108
LT
458 pathspec = get_pathspec(prefix, argv + i);
459
460 /* Verify that the pathspec matches the prefix */
461 if (pathspec)
3e04228b 462 prefix = verify_pathspec(prefix);
5be4efbe 463
bba319b5
JH
464 /* Treat unmatching pathspec elements as errors */
465 if (pathspec && error_unmatch) {
466 int num;
467 for (num = 0; pathspec[num]; num++)
468 ;
469 ps_matched = xcalloc(1, num);
470 }
471
453ec4bd 472 if (dir.show_ignored && !exc_given) {
20d37ef6
PB
473 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
474 argv[0]);
9ff768e9 475 exit(1);
8695c8bf
LT
476 }
477
478 /* With no flags, we default to showing the cached files */
b0391890
JH
479 if (!(show_stage | show_deleted | show_others | show_unmerged |
480 show_killed | show_modified))
8695c8bf
LT
481 show_cached = 1;
482
483 read_cache();
5be4efbe 484 if (prefix)
3e04228b
LT
485 prune_cache(prefix);
486 show_files(&dir, prefix);
bba319b5
JH
487
488 if (ps_matched) {
489 /* We need to make sure all pathspec matched otherwise
490 * it is an error.
491 */
492 int num, errors = 0;
493 for (num = 0; pathspec[num]; num++) {
494 if (ps_matched[num])
495 continue;
ced7b828 496 error("pathspec '%s' did not match any file(s) known to git.",
6becd7da 497 pathspec[num] + prefix_offset);
c8af25ca 498 errors++;
bba319b5 499 }
ced7b828
AE
500
501 if (errors)
502 fprintf(stderr, "Did you forget to 'git add'?\n");
503
bba319b5
JH
504 return errors ? 1 : 0;
505 }
506
8695c8bf
LT
507 return 0;
508}