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