]>
Commit | Line | Data |
---|---|---|
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 | */ | |
8 | #include <dirent.h> | |
9ff768e9 | 9 | #include <fnmatch.h> |
8695c8bf LT |
10 | |
11 | #include "cache.h" | |
22ddf719 | 12 | #include "quote.h" |
8695c8bf | 13 | |
ad0cae4c | 14 | static int abbrev = 0; |
8695c8bf LT |
15 | static int show_deleted = 0; |
16 | static int show_cached = 0; | |
17 | static int show_others = 0; | |
18 | static int show_ignored = 0; | |
aee46198 | 19 | static int show_stage = 0; |
eec8c633 | 20 | static int show_unmerged = 0; |
b0391890 | 21 | static int show_modified = 0; |
6ca45943 | 22 | static int show_killed = 0; |
9518eb26 | 23 | static int show_other_directories = 0; |
b0a3de42 | 24 | static int hide_empty_directories = 0; |
8bb2e03b | 25 | static int show_valid_bit = 0; |
b83c8345 | 26 | static int line_terminator = '\n'; |
8695c8bf | 27 | |
5be4efbe LT |
28 | static int prefix_len = 0, prefix_offset = 0; |
29 | static const char *prefix = NULL; | |
56fc5108 | 30 | static const char **pathspec = NULL; |
bba319b5 JH |
31 | static int error_unmatch = 0; |
32 | static char *ps_matched = NULL; | |
5be4efbe | 33 | |
20d37ef6 PB |
34 | static const char *tag_cached = ""; |
35 | static const char *tag_unmerged = ""; | |
36 | static const char *tag_removed = ""; | |
37 | static const char *tag_other = ""; | |
6ca45943 | 38 | static const char *tag_killed = ""; |
b0391890 | 39 | static const char *tag_modified = ""; |
20d37ef6 | 40 | |
6b5ee137 | 41 | static const char *exclude_per_dir = NULL; |
fee88256 JH |
42 | |
43 | /* We maintain three exclude pattern lists: | |
44 | * EXC_CMDL lists patterns explicitly given on the command line. | |
45 | * EXC_DIRS lists patterns obtained from per-directory ignore files. | |
46 | * EXC_FILE lists patterns from fallback ignore files. | |
47 | */ | |
48 | #define EXC_CMDL 0 | |
49 | #define EXC_DIRS 1 | |
50 | #define EXC_FILE 2 | |
51 | static struct exclude_list { | |
52 | int nr; | |
53 | int alloc; | |
54 | struct exclude { | |
55 | const char *pattern; | |
56 | const char *base; | |
57 | int baselen; | |
58 | } **excludes; | |
59 | } exclude_list[3]; | |
60 | ||
61 | static void add_exclude(const char *string, const char *base, | |
62 | int baselen, struct exclude_list *which) | |
9ff768e9 | 63 | { |
f87f9497 JH |
64 | struct exclude *x = xmalloc(sizeof (*x)); |
65 | ||
66 | x->pattern = string; | |
67 | x->base = base; | |
68 | x->baselen = baselen; | |
fee88256 JH |
69 | if (which->nr == which->alloc) { |
70 | which->alloc = alloc_nr(which->alloc); | |
71 | which->excludes = realloc(which->excludes, | |
72 | which->alloc * sizeof(x)); | |
9ff768e9 | 73 | } |
fee88256 | 74 | which->excludes[which->nr++] = x; |
9ff768e9 NP |
75 | } |
76 | ||
f87f9497 | 77 | static int add_excludes_from_file_1(const char *fname, |
fee88256 JH |
78 | const char *base, |
79 | int baselen, | |
80 | struct exclude_list *which) | |
9ff768e9 NP |
81 | { |
82 | int fd, i; | |
83 | long size; | |
84 | char *buf, *entry; | |
85 | ||
86 | fd = open(fname, O_RDONLY); | |
87 | if (fd < 0) | |
88 | goto err; | |
89 | size = lseek(fd, 0, SEEK_END); | |
90 | if (size < 0) | |
91 | goto err; | |
92 | lseek(fd, 0, SEEK_SET); | |
93 | if (size == 0) { | |
94 | close(fd); | |
f87f9497 | 95 | return 0; |
9ff768e9 | 96 | } |
451d7b47 | 97 | buf = xmalloc(size+1); |
9ff768e9 NP |
98 | if (read(fd, buf, size) != size) |
99 | goto err; | |
100 | close(fd); | |
101 | ||
451d7b47 | 102 | buf[size++] = '\n'; |
9ff768e9 NP |
103 | entry = buf; |
104 | for (i = 0; i < size; i++) { | |
105 | if (buf[i] == '\n') { | |
f87f9497 | 106 | if (entry != buf + i && entry[0] != '#') { |
d317e438 | 107 | buf[i - (i && buf[i-1] == '\r')] = 0; |
fee88256 | 108 | add_exclude(entry, base, baselen, which); |
9ff768e9 NP |
109 | } |
110 | entry = buf + i + 1; | |
111 | } | |
112 | } | |
f87f9497 JH |
113 | return 0; |
114 | ||
115 | err: | |
116 | if (0 <= fd) | |
117 | close(fd); | |
118 | return -1; | |
119 | } | |
120 | ||
121 | static void add_excludes_from_file(const char *fname) | |
122 | { | |
fee88256 JH |
123 | if (add_excludes_from_file_1(fname, "", 0, |
124 | &exclude_list[EXC_FILE]) < 0) | |
f87f9497 JH |
125 | die("cannot use %s as an exclude file", fname); |
126 | } | |
127 | ||
128 | static int push_exclude_per_directory(const char *base, int baselen) | |
129 | { | |
130 | char exclude_file[PATH_MAX]; | |
fee88256 JH |
131 | struct exclude_list *el = &exclude_list[EXC_DIRS]; |
132 | int current_nr = el->nr; | |
f87f9497 JH |
133 | |
134 | if (exclude_per_dir) { | |
135 | memcpy(exclude_file, base, baselen); | |
136 | strcpy(exclude_file + baselen, exclude_per_dir); | |
fee88256 | 137 | add_excludes_from_file_1(exclude_file, base, baselen, el); |
f87f9497 JH |
138 | } |
139 | return current_nr; | |
140 | } | |
9ff768e9 | 141 | |
f87f9497 JH |
142 | static void pop_exclude_per_directory(int stk) |
143 | { | |
fee88256 JH |
144 | struct exclude_list *el = &exclude_list[EXC_DIRS]; |
145 | ||
146 | while (stk < el->nr) | |
147 | free(el->excludes[--el->nr]); | |
9ff768e9 NP |
148 | } |
149 | ||
fee88256 JH |
150 | /* Scan the list and let the last match determines the fate. |
151 | * Return 1 for exclude, 0 for include and -1 for undecided. | |
152 | */ | |
153 | static int excluded_1(const char *pathname, | |
154 | int pathlen, | |
155 | struct exclude_list *el) | |
9ff768e9 NP |
156 | { |
157 | int i; | |
f87f9497 | 158 | |
fee88256 JH |
159 | if (el->nr) { |
160 | for (i = el->nr - 1; 0 <= i; i--) { | |
161 | struct exclude *x = el->excludes[i]; | |
f87f9497 JH |
162 | const char *exclude = x->pattern; |
163 | int to_exclude = 1; | |
164 | ||
165 | if (*exclude == '!') { | |
166 | to_exclude = 0; | |
167 | exclude++; | |
168 | } | |
169 | ||
170 | if (!strchr(exclude, '/')) { | |
171 | /* match basename */ | |
172 | const char *basename = strrchr(pathname, '/'); | |
173 | basename = (basename) ? basename+1 : pathname; | |
174 | if (fnmatch(exclude, basename, 0) == 0) | |
175 | return to_exclude; | |
176 | } | |
177 | else { | |
178 | /* match with FNM_PATHNAME: | |
82f9d58a | 179 | * exclude has base (baselen long) implicitly |
f87f9497 JH |
180 | * in front of it. |
181 | */ | |
182 | int baselen = x->baselen; | |
183 | if (*exclude == '/') | |
184 | exclude++; | |
185 | ||
186 | if (pathlen < baselen || | |
187 | (baselen && pathname[baselen-1] != '/') || | |
188 | strncmp(pathname, x->base, baselen)) | |
189 | continue; | |
190 | ||
191 | if (fnmatch(exclude, pathname+baselen, | |
192 | FNM_PATHNAME) == 0) | |
193 | return to_exclude; | |
194 | } | |
195 | } | |
9ff768e9 | 196 | } |
fee88256 JH |
197 | return -1; /* undecided */ |
198 | } | |
199 | ||
200 | static int excluded(const char *pathname) | |
201 | { | |
202 | int pathlen = strlen(pathname); | |
203 | int st; | |
204 | ||
205 | for (st = EXC_CMDL; st <= EXC_FILE; st++) { | |
206 | switch (excluded_1(pathname, pathlen, &exclude_list[st])) { | |
207 | case 0: | |
208 | return 0; | |
209 | case 1: | |
210 | return 1; | |
211 | } | |
212 | } | |
9ff768e9 NP |
213 | return 0; |
214 | } | |
215 | ||
6ca45943 JH |
216 | struct nond_on_fs { |
217 | int len; | |
8f1d2e6f | 218 | char name[FLEX_ARRAY]; /* more */ |
6ca45943 JH |
219 | }; |
220 | ||
221 | static struct nond_on_fs **dir; | |
8695c8bf LT |
222 | static int nr_dir; |
223 | static int dir_alloc; | |
224 | ||
225 | static void add_name(const char *pathname, int len) | |
226 | { | |
6ca45943 | 227 | struct nond_on_fs *ent; |
8695c8bf LT |
228 | |
229 | if (cache_name_pos(pathname, len) >= 0) | |
230 | return; | |
231 | ||
232 | if (nr_dir == dir_alloc) { | |
233 | dir_alloc = alloc_nr(dir_alloc); | |
6ca45943 | 234 | dir = xrealloc(dir, dir_alloc*sizeof(ent)); |
8695c8bf | 235 | } |
6ca45943 JH |
236 | ent = xmalloc(sizeof(*ent) + len + 1); |
237 | ent->len = len; | |
238 | memcpy(ent->name, pathname, len); | |
5be4efbe | 239 | ent->name[len] = 0; |
6ca45943 | 240 | dir[nr_dir++] = ent; |
8695c8bf LT |
241 | } |
242 | ||
9518eb26 LT |
243 | static int dir_exists(const char *dirname, int len) |
244 | { | |
245 | int pos = cache_name_pos(dirname, len); | |
246 | if (pos >= 0) | |
247 | return 1; | |
248 | pos = -pos-1; | |
657907e7 | 249 | if (pos >= active_nr) /* can't */ |
9518eb26 | 250 | return 0; |
657907e7 | 251 | return !strncmp(active_cache[pos]->name, dirname, len); |
9518eb26 LT |
252 | } |
253 | ||
8695c8bf LT |
254 | /* |
255 | * Read a directory tree. We currently ignore anything but | |
a15c1c60 JH |
256 | * directories, regular files and symlinks. That's because git |
257 | * doesn't handle them at all yet. Maybe that will change some | |
258 | * day. | |
8695c8bf | 259 | * |
f87f9497 | 260 | * Also, we ignore the name ".git" (even if it is not a directory). |
aebb2679 | 261 | * That likely will not change. |
8695c8bf | 262 | */ |
b0a3de42 | 263 | static int read_directory(const char *path, const char *base, int baselen) |
8695c8bf | 264 | { |
b0a3de42 PB |
265 | DIR *fdir = opendir(path); |
266 | int contents = 0; | |
8695c8bf | 267 | |
b0a3de42 | 268 | if (fdir) { |
f87f9497 | 269 | int exclude_stk; |
8695c8bf LT |
270 | struct dirent *de; |
271 | char fullname[MAXPATHLEN + 1]; | |
272 | memcpy(fullname, base, baselen); | |
273 | ||
f87f9497 JH |
274 | exclude_stk = push_exclude_per_directory(base, baselen); |
275 | ||
b0a3de42 | 276 | while ((de = readdir(fdir)) != NULL) { |
8695c8bf LT |
277 | int len; |
278 | ||
c4ee2952 JH |
279 | if ((de->d_name[0] == '.') && |
280 | (de->d_name[1] == 0 || | |
281 | !strcmp(de->d_name + 1, ".") || | |
282 | !strcmp(de->d_name + 1, "git"))) | |
8695c8bf LT |
283 | continue; |
284 | len = strlen(de->d_name); | |
285 | memcpy(fullname + baselen, de->d_name, len+1); | |
1e358405 SP |
286 | if (excluded(fullname) != show_ignored) { |
287 | if (!show_ignored || DTYPE(de) != DT_DIR) { | |
288 | continue; | |
289 | } | |
290 | } | |
8695c8bf | 291 | |
b6829693 | 292 | switch (DTYPE(de)) { |
8695c8bf | 293 | struct stat st; |
b0a3de42 | 294 | int subdir, rewind_base; |
8695c8bf LT |
295 | default: |
296 | continue; | |
297 | case DT_UNKNOWN: | |
298 | if (lstat(fullname, &st)) | |
299 | continue; | |
a15c1c60 | 300 | if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) |
8695c8bf LT |
301 | break; |
302 | if (!S_ISDIR(st.st_mode)) | |
303 | continue; | |
304 | /* fallthrough */ | |
305 | case DT_DIR: | |
306 | memcpy(fullname + baselen + len, "/", 2); | |
0907fedb | 307 | len++; |
b0a3de42 PB |
308 | rewind_base = nr_dir; |
309 | subdir = read_directory(fullname, fullname, | |
310 | baselen + len); | |
657907e7 | 311 | if (show_other_directories && |
b0a3de42 PB |
312 | (subdir || !hide_empty_directories) && |
313 | !dir_exists(fullname, baselen + len)) { | |
314 | // Rewind the read subdirectory | |
315 | while (nr_dir > rewind_base) | |
316 | free(dir[--nr_dir]); | |
657907e7 | 317 | break; |
b0a3de42 PB |
318 | } |
319 | contents += subdir; | |
8695c8bf LT |
320 | continue; |
321 | case DT_REG: | |
a15c1c60 | 322 | case DT_LNK: |
8695c8bf LT |
323 | break; |
324 | } | |
325 | add_name(fullname, baselen + len); | |
b0a3de42 | 326 | contents++; |
8695c8bf | 327 | } |
b0a3de42 | 328 | closedir(fdir); |
f87f9497 JH |
329 | |
330 | pop_exclude_per_directory(exclude_stk); | |
8695c8bf | 331 | } |
b0a3de42 PB |
332 | |
333 | return contents; | |
8695c8bf LT |
334 | } |
335 | ||
336 | static int cmp_name(const void *p1, const void *p2) | |
337 | { | |
6ca45943 JH |
338 | const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1; |
339 | const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2; | |
340 | ||
341 | return cache_name_compare(e1->name, e1->len, | |
342 | e2->name, e2->len); | |
343 | } | |
8695c8bf | 344 | |
56fc5108 LT |
345 | /* |
346 | * Match a pathspec against a filename. The first "len" characters | |
347 | * are the common prefix | |
348 | */ | |
bba319b5 JH |
349 | static int match(const char **spec, char *ps_matched, |
350 | const char *filename, int len) | |
56fc5108 LT |
351 | { |
352 | const char *m; | |
353 | ||
354 | while ((m = *spec++) != NULL) { | |
355 | int matchlen = strlen(m + len); | |
356 | ||
357 | if (!matchlen) | |
bba319b5 | 358 | goto matched; |
56fc5108 LT |
359 | if (!strncmp(m + len, filename + len, matchlen)) { |
360 | if (m[len + matchlen - 1] == '/') | |
bba319b5 | 361 | goto matched; |
56fc5108 LT |
362 | switch (filename[len + matchlen]) { |
363 | case '/': case '\0': | |
bba319b5 | 364 | goto matched; |
56fc5108 LT |
365 | } |
366 | } | |
367 | if (!fnmatch(m + len, filename + len, 0)) | |
bba319b5 JH |
368 | goto matched; |
369 | if (ps_matched) | |
370 | ps_matched++; | |
371 | continue; | |
372 | matched: | |
373 | if (ps_matched) | |
374 | *ps_matched = 1; | |
375 | return 1; | |
56fc5108 LT |
376 | } |
377 | return 0; | |
378 | } | |
379 | ||
5be4efbe LT |
380 | static void show_dir_entry(const char *tag, struct nond_on_fs *ent) |
381 | { | |
382 | int len = prefix_len; | |
383 | int offset = prefix_offset; | |
384 | ||
385 | if (len >= ent->len) | |
386 | die("git-ls-files: internal error - directory entry not superset of prefix"); | |
387 | ||
bba319b5 | 388 | if (pathspec && !match(pathspec, ps_matched, ent->name, len)) |
5be4efbe LT |
389 | return; |
390 | ||
22ddf719 | 391 | fputs(tag, stdout); |
9ef2b3cb | 392 | write_name_quoted("", 0, ent->name + offset, line_terminator, stdout); |
22ddf719 | 393 | putchar(line_terminator); |
5be4efbe LT |
394 | } |
395 | ||
fcbc3083 JH |
396 | static void show_other_files(void) |
397 | { | |
398 | int i; | |
399 | for (i = 0; i < nr_dir; i++) { | |
400 | /* We should not have a matching entry, but we | |
401 | * may have an unmerged entry for this path. | |
402 | */ | |
403 | struct nond_on_fs *ent = dir[i]; | |
404 | int pos = cache_name_pos(ent->name, ent->len); | |
405 | struct cache_entry *ce; | |
406 | if (0 <= pos) | |
407 | die("bug in show-other-files"); | |
408 | pos = -pos - 1; | |
409 | if (pos < active_nr) { | |
410 | ce = active_cache[pos]; | |
411 | if (ce_namelen(ce) == ent->len && | |
412 | !memcmp(ce->name, ent->name, ent->len)) | |
413 | continue; /* Yup, this one exists unmerged */ | |
414 | } | |
415 | show_dir_entry(tag_other, ent); | |
416 | } | |
417 | } | |
418 | ||
e99d59ff | 419 | static void show_killed_files(void) |
6ca45943 JH |
420 | { |
421 | int i; | |
422 | for (i = 0; i < nr_dir; i++) { | |
423 | struct nond_on_fs *ent = dir[i]; | |
424 | char *cp, *sp; | |
425 | int pos, len, killed = 0; | |
426 | ||
427 | for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) { | |
428 | sp = strchr(cp, '/'); | |
429 | if (!sp) { | |
430 | /* If ent->name is prefix of an entry in the | |
431 | * cache, it will be killed. | |
432 | */ | |
433 | pos = cache_name_pos(ent->name, ent->len); | |
434 | if (0 <= pos) | |
435 | die("bug in show-killed-files"); | |
436 | pos = -pos - 1; | |
437 | while (pos < active_nr && | |
438 | ce_stage(active_cache[pos])) | |
439 | pos++; /* skip unmerged */ | |
440 | if (active_nr <= pos) | |
441 | break; | |
442 | /* pos points at a name immediately after | |
443 | * ent->name in the cache. Does it expect | |
444 | * ent->name to be a directory? | |
445 | */ | |
446 | len = ce_namelen(active_cache[pos]); | |
447 | if ((ent->len < len) && | |
448 | !strncmp(active_cache[pos]->name, | |
449 | ent->name, ent->len) && | |
450 | active_cache[pos]->name[ent->len] == '/') | |
451 | killed = 1; | |
452 | break; | |
453 | } | |
454 | if (0 <= cache_name_pos(ent->name, sp - ent->name)) { | |
455 | /* If any of the leading directories in | |
456 | * ent->name is registered in the cache, | |
457 | * ent->name will be killed. | |
458 | */ | |
459 | killed = 1; | |
460 | break; | |
461 | } | |
462 | } | |
463 | if (killed) | |
5be4efbe | 464 | show_dir_entry(tag_killed, dir[i]); |
6ca45943 | 465 | } |
8695c8bf LT |
466 | } |
467 | ||
5be4efbe LT |
468 | static void show_ce_entry(const char *tag, struct cache_entry *ce) |
469 | { | |
470 | int len = prefix_len; | |
471 | int offset = prefix_offset; | |
472 | ||
473 | if (len >= ce_namelen(ce)) | |
474 | die("git-ls-files: internal error - cache entry not superset of prefix"); | |
475 | ||
bba319b5 | 476 | if (pathspec && !match(pathspec, ps_matched, ce->name, len)) |
5be4efbe LT |
477 | return; |
478 | ||
8bb2e03b JH |
479 | if (tag && *tag && show_valid_bit && |
480 | (ce->ce_flags & htons(CE_VALID))) { | |
2bcab240 JH |
481 | static char alttag[4]; |
482 | memcpy(alttag, tag, 3); | |
483 | if (isalpha(tag[0])) | |
484 | alttag[0] = tolower(tag[0]); | |
485 | else if (tag[0] == '?') | |
486 | alttag[0] = '!'; | |
487 | else { | |
488 | alttag[0] = 'v'; | |
489 | alttag[1] = tag[0]; | |
490 | alttag[2] = ' '; | |
491 | alttag[3] = 0; | |
492 | } | |
493 | tag = alttag; | |
494 | } | |
495 | ||
22ddf719 JH |
496 | if (!show_stage) { |
497 | fputs(tag, stdout); | |
9ef2b3cb JH |
498 | write_name_quoted("", 0, ce->name + offset, |
499 | line_terminator, stdout); | |
22ddf719 JH |
500 | putchar(line_terminator); |
501 | } | |
502 | else { | |
503 | printf("%s%06o %s %d\t", | |
5be4efbe LT |
504 | tag, |
505 | ntohl(ce->ce_mode), | |
ad0cae4c EW |
506 | abbrev ? find_unique_abbrev(ce->sha1,abbrev) |
507 | : sha1_to_hex(ce->sha1), | |
22ddf719 | 508 | ce_stage(ce)); |
9ef2b3cb JH |
509 | write_name_quoted("", 0, ce->name + offset, |
510 | line_terminator, stdout); | |
22ddf719 JH |
511 | putchar(line_terminator); |
512 | } | |
5be4efbe LT |
513 | } |
514 | ||
8695c8bf LT |
515 | static void show_files(void) |
516 | { | |
517 | int i; | |
518 | ||
519 | /* For cached/deleted files we don't need to even do the readdir */ | |
6ca45943 | 520 | if (show_others || show_killed) { |
5be4efbe LT |
521 | const char *path = ".", *base = ""; |
522 | int baselen = prefix_len; | |
523 | ||
701ca744 | 524 | if (baselen) { |
5be4efbe | 525 | path = base = prefix; |
701ca744 JH |
526 | if (exclude_per_dir) { |
527 | char *p, *pp = xmalloc(baselen+1); | |
528 | memcpy(pp, prefix, baselen+1); | |
529 | p = pp; | |
530 | while (1) { | |
531 | char save = *p; | |
532 | *p = 0; | |
533 | push_exclude_per_directory(pp, p-pp); | |
534 | *p++ = save; | |
535 | if (!save) | |
536 | break; | |
537 | p = strchr(p, '/'); | |
538 | if (p) | |
539 | p++; | |
540 | else | |
541 | p = pp + baselen; | |
542 | } | |
543 | free(pp); | |
544 | } | |
545 | } | |
5be4efbe | 546 | read_directory(path, base, baselen); |
6ca45943 JH |
547 | qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name); |
548 | if (show_others) | |
fcbc3083 | 549 | show_other_files(); |
6ca45943 JH |
550 | if (show_killed) |
551 | show_killed_files(); | |
8695c8bf | 552 | } |
aee46198 | 553 | if (show_cached | show_stage) { |
8695c8bf LT |
554 | for (i = 0; i < active_nr; i++) { |
555 | struct cache_entry *ce = active_cache[i]; | |
9ff768e9 NP |
556 | if (excluded(ce->name) != show_ignored) |
557 | continue; | |
eec8c633 LT |
558 | if (show_unmerged && !ce_stage(ce)) |
559 | continue; | |
5be4efbe | 560 | show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce); |
8695c8bf LT |
561 | } |
562 | } | |
b0391890 | 563 | if (show_deleted | show_modified) { |
8695c8bf LT |
564 | for (i = 0; i < active_nr; i++) { |
565 | struct cache_entry *ce = active_cache[i]; | |
566 | struct stat st; | |
b0391890 | 567 | int err; |
9ff768e9 NP |
568 | if (excluded(ce->name) != show_ignored) |
569 | continue; | |
b0391890 JH |
570 | err = lstat(ce->name, &st); |
571 | if (show_deleted && err) | |
572 | show_ce_entry(tag_removed, ce); | |
2bcab240 | 573 | if (show_modified && ce_modified(ce, &st, 0)) |
b0391890 | 574 | show_ce_entry(tag_modified, ce); |
5be4efbe LT |
575 | } |
576 | } | |
577 | } | |
578 | ||
579 | /* | |
580 | * Prune the index to only contain stuff starting with "prefix" | |
581 | */ | |
582 | static void prune_cache(void) | |
583 | { | |
584 | int pos = cache_name_pos(prefix, prefix_len); | |
585 | unsigned int first, last; | |
586 | ||
587 | if (pos < 0) | |
588 | pos = -pos-1; | |
589 | active_cache += pos; | |
590 | active_nr -= pos; | |
591 | first = 0; | |
592 | last = active_nr; | |
593 | while (last > first) { | |
594 | int next = (last + first) >> 1; | |
595 | struct cache_entry *ce = active_cache[next]; | |
596 | if (!strncmp(ce->name, prefix, prefix_len)) { | |
597 | first = next+1; | |
598 | continue; | |
8695c8bf | 599 | } |
5be4efbe LT |
600 | last = next; |
601 | } | |
602 | active_nr = last; | |
603 | } | |
604 | ||
56fc5108 | 605 | static void verify_pathspec(void) |
5be4efbe | 606 | { |
56fc5108 LT |
607 | const char **p, *n, *prev; |
608 | char *real_prefix; | |
609 | unsigned long max; | |
610 | ||
611 | prev = NULL; | |
612 | max = PATH_MAX; | |
613 | for (p = pathspec; (n = *p) != NULL; p++) { | |
614 | int i, len = 0; | |
615 | for (i = 0; i < max; i++) { | |
616 | char c = n[i]; | |
617 | if (prev && prev[i] != c) | |
618 | break; | |
56906143 | 619 | if (!c || c == '*' || c == '?') |
56fc5108 LT |
620 | break; |
621 | if (c == '/') | |
622 | len = i+1; | |
623 | } | |
624 | prev = n; | |
625 | if (len < max) { | |
626 | max = len; | |
627 | if (!max) | |
628 | break; | |
629 | } | |
5be4efbe | 630 | } |
56fc5108 LT |
631 | |
632 | if (prefix_offset > max || memcmp(prev, prefix, prefix_offset)) | |
633 | die("git-ls-files: cannot generate relative filenames containing '..'"); | |
634 | ||
635 | real_prefix = NULL; | |
636 | prefix_len = max; | |
637 | if (max) { | |
638 | real_prefix = xmalloc(max + 1); | |
639 | memcpy(real_prefix, prev, max); | |
640 | real_prefix[max] = 0; | |
8695c8bf | 641 | } |
56fc5108 | 642 | prefix = real_prefix; |
8695c8bf LT |
643 | } |
644 | ||
4d1f1190 | 645 | static const char ls_files_usage[] = |
8bb2e03b | 646 | "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* " |
f87f9497 | 647 | "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] " |
ad0cae4c EW |
648 | "[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] " |
649 | "[--] [<file>]*"; | |
cf9a113d | 650 | |
6b5ee137 | 651 | int main(int argc, const char **argv) |
8695c8bf LT |
652 | { |
653 | int i; | |
fee88256 | 654 | int exc_given = 0; |
8695c8bf | 655 | |
5be4efbe LT |
656 | prefix = setup_git_directory(); |
657 | if (prefix) | |
56fc5108 | 658 | prefix_offset = strlen(prefix); |
39b4ac99 | 659 | git_config(git_default_config); |
5be4efbe | 660 | |
8695c8bf | 661 | for (i = 1; i < argc; i++) { |
6b5ee137 | 662 | const char *arg = argv[i]; |
8695c8bf | 663 | |
500b97e4 FK |
664 | if (!strcmp(arg, "--")) { |
665 | i++; | |
666 | break; | |
667 | } | |
b83c8345 JH |
668 | if (!strcmp(arg, "-z")) { |
669 | line_terminator = 0; | |
5be4efbe LT |
670 | continue; |
671 | } | |
8bb2e03b | 672 | if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) { |
20d37ef6 PB |
673 | tag_cached = "H "; |
674 | tag_unmerged = "M "; | |
675 | tag_removed = "R "; | |
b0391890 | 676 | tag_modified = "C "; |
20d37ef6 | 677 | tag_other = "? "; |
6ca45943 | 678 | tag_killed = "K "; |
8bb2e03b JH |
679 | if (arg[1] == 'v') |
680 | show_valid_bit = 1; | |
5be4efbe LT |
681 | continue; |
682 | } | |
683 | if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) { | |
8695c8bf | 684 | show_cached = 1; |
5be4efbe LT |
685 | continue; |
686 | } | |
687 | if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) { | |
8695c8bf | 688 | show_deleted = 1; |
5be4efbe LT |
689 | continue; |
690 | } | |
b0391890 JH |
691 | if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) { |
692 | show_modified = 1; | |
693 | continue; | |
694 | } | |
5be4efbe | 695 | if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) { |
8695c8bf | 696 | show_others = 1; |
5be4efbe LT |
697 | continue; |
698 | } | |
699 | if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) { | |
8695c8bf | 700 | show_ignored = 1; |
5be4efbe LT |
701 | continue; |
702 | } | |
703 | if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) { | |
aee46198 | 704 | show_stage = 1; |
5be4efbe LT |
705 | continue; |
706 | } | |
707 | if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) { | |
6ca45943 | 708 | show_killed = 1; |
5be4efbe LT |
709 | continue; |
710 | } | |
9518eb26 LT |
711 | if (!strcmp(arg, "--directory")) { |
712 | show_other_directories = 1; | |
713 | continue; | |
714 | } | |
b0a3de42 PB |
715 | if (!strcmp(arg, "--no-empty-directory")) { |
716 | hide_empty_directories = 1; | |
717 | continue; | |
718 | } | |
5be4efbe | 719 | if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) { |
20d37ef6 PB |
720 | /* There's no point in showing unmerged unless |
721 | * you also show the stage information. | |
722 | */ | |
eec8c633 LT |
723 | show_stage = 1; |
724 | show_unmerged = 1; | |
5be4efbe LT |
725 | continue; |
726 | } | |
727 | if (!strcmp(arg, "-x") && i+1 < argc) { | |
fee88256 JH |
728 | exc_given = 1; |
729 | add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]); | |
5be4efbe LT |
730 | continue; |
731 | } | |
732 | if (!strncmp(arg, "--exclude=", 10)) { | |
fee88256 JH |
733 | exc_given = 1; |
734 | add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]); | |
5be4efbe LT |
735 | continue; |
736 | } | |
737 | if (!strcmp(arg, "-X") && i+1 < argc) { | |
fee88256 | 738 | exc_given = 1; |
9ff768e9 | 739 | add_excludes_from_file(argv[++i]); |
5be4efbe LT |
740 | continue; |
741 | } | |
742 | if (!strncmp(arg, "--exclude-from=", 15)) { | |
fee88256 | 743 | exc_given = 1; |
9ff768e9 | 744 | add_excludes_from_file(arg+15); |
5be4efbe LT |
745 | continue; |
746 | } | |
747 | if (!strncmp(arg, "--exclude-per-directory=", 24)) { | |
fee88256 | 748 | exc_given = 1; |
f87f9497 | 749 | exclude_per_dir = arg + 24; |
5be4efbe LT |
750 | continue; |
751 | } | |
752 | if (!strcmp(arg, "--full-name")) { | |
753 | prefix_offset = 0; | |
754 | continue; | |
755 | } | |
bba319b5 JH |
756 | if (!strcmp(arg, "--error-unmatch")) { |
757 | error_unmatch = 1; | |
758 | continue; | |
759 | } | |
ad0cae4c EW |
760 | if (!strncmp(arg, "--abbrev=", 9)) { |
761 | abbrev = strtoul(arg+9, NULL, 10); | |
762 | if (abbrev && abbrev < MINIMUM_ABBREV) | |
763 | abbrev = MINIMUM_ABBREV; | |
764 | else if (abbrev > 40) | |
765 | abbrev = 40; | |
766 | continue; | |
767 | } | |
768 | if (!strcmp(arg, "--abbrev")) { | |
769 | abbrev = DEFAULT_ABBREV; | |
770 | continue; | |
771 | } | |
56fc5108 | 772 | if (*arg == '-') |
17710391 | 773 | usage(ls_files_usage); |
56fc5108 | 774 | break; |
9ff768e9 NP |
775 | } |
776 | ||
56fc5108 LT |
777 | pathspec = get_pathspec(prefix, argv + i); |
778 | ||
779 | /* Verify that the pathspec matches the prefix */ | |
780 | if (pathspec) | |
781 | verify_pathspec(); | |
5be4efbe | 782 | |
bba319b5 JH |
783 | /* Treat unmatching pathspec elements as errors */ |
784 | if (pathspec && error_unmatch) { | |
785 | int num; | |
786 | for (num = 0; pathspec[num]; num++) | |
787 | ; | |
788 | ps_matched = xcalloc(1, num); | |
789 | } | |
790 | ||
fee88256 | 791 | if (show_ignored && !exc_given) { |
20d37ef6 PB |
792 | fprintf(stderr, "%s: --ignored needs some exclude pattern\n", |
793 | argv[0]); | |
9ff768e9 | 794 | exit(1); |
8695c8bf LT |
795 | } |
796 | ||
797 | /* With no flags, we default to showing the cached files */ | |
b0391890 JH |
798 | if (!(show_stage | show_deleted | show_others | show_unmerged | |
799 | show_killed | show_modified)) | |
8695c8bf LT |
800 | show_cached = 1; |
801 | ||
802 | read_cache(); | |
5be4efbe LT |
803 | if (prefix) |
804 | prune_cache(); | |
8695c8bf | 805 | show_files(); |
bba319b5 JH |
806 | |
807 | if (ps_matched) { | |
808 | /* We need to make sure all pathspec matched otherwise | |
809 | * it is an error. | |
810 | */ | |
811 | int num, errors = 0; | |
812 | for (num = 0; pathspec[num]; num++) { | |
813 | if (ps_matched[num]) | |
814 | continue; | |
815 | error("pathspec '%s' did not match any.", | |
6becd7da | 816 | pathspec[num] + prefix_offset); |
c8af25ca | 817 | errors++; |
bba319b5 JH |
818 | } |
819 | return errors ? 1 : 0; | |
820 | } | |
821 | ||
8695c8bf LT |
822 | return 0; |
823 | } |