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