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