]>
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 | */ | |
8695c8bf | 8 | #include "cache.h" |
22ddf719 | 9 | #include "quote.h" |
453ec4bd | 10 | #include "dir.h" |
0864f264 | 11 | #include "builtin.h" |
64586e75 | 12 | #include "tree.h" |
8695c8bf | 13 | |
96f1e58f DR |
14 | static int abbrev; |
15 | static int show_deleted; | |
16 | static int show_cached; | |
17 | static int show_others; | |
18 | static int show_stage; | |
19 | static int show_unmerged; | |
20 | static int show_modified; | |
21 | static int show_killed; | |
22 | static int show_valid_bit; | |
b83c8345 | 23 | static int line_terminator = '\n'; |
8695c8bf | 24 | |
96f1e58f DR |
25 | static int prefix_len; |
26 | static int prefix_offset; | |
27 | static const char **pathspec; | |
28 | static int error_unmatch; | |
29 | static char *ps_matched; | |
64586e75 | 30 | static const char *with_tree; |
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 | |
8695c8bf | 39 | |
56fc5108 | 40 | /* |
ee425e46 | 41 | * Match a pathspec against a filename. The first "skiplen" characters |
56fc5108 LT |
42 | * are the common prefix |
43 | */ | |
ee425e46 JH |
44 | int pathspec_match(const char **spec, char *ps_matched, |
45 | const char *filename, int skiplen) | |
56fc5108 LT |
46 | { |
47 | const char *m; | |
48 | ||
49 | while ((m = *spec++) != NULL) { | |
ee425e46 | 50 | int matchlen = strlen(m + skiplen); |
56fc5108 LT |
51 | |
52 | if (!matchlen) | |
bba319b5 | 53 | goto matched; |
ee425e46 JH |
54 | if (!strncmp(m + skiplen, filename + skiplen, matchlen)) { |
55 | if (m[skiplen + matchlen - 1] == '/') | |
bba319b5 | 56 | goto matched; |
ee425e46 | 57 | switch (filename[skiplen + matchlen]) { |
56fc5108 | 58 | case '/': case '\0': |
bba319b5 | 59 | goto matched; |
56fc5108 LT |
60 | } |
61 | } | |
ee425e46 | 62 | if (!fnmatch(m + skiplen, filename + skiplen, 0)) |
bba319b5 JH |
63 | goto matched; |
64 | if (ps_matched) | |
65 | ps_matched++; | |
66 | continue; | |
67 | matched: | |
68 | if (ps_matched) | |
69 | *ps_matched = 1; | |
70 | return 1; | |
56fc5108 LT |
71 | } |
72 | return 0; | |
73 | } | |
74 | ||
453ec4bd | 75 | static void show_dir_entry(const char *tag, struct dir_entry *ent) |
5be4efbe LT |
76 | { |
77 | int len = prefix_len; | |
78 | int offset = prefix_offset; | |
79 | ||
80 | if (len >= ent->len) | |
81 | die("git-ls-files: internal error - directory entry not superset of prefix"); | |
82 | ||
ee425e46 | 83 | if (pathspec && !pathspec_match(pathspec, ps_matched, ent->name, len)) |
5be4efbe LT |
84 | return; |
85 | ||
22ddf719 | 86 | fputs(tag, stdout); |
663af342 | 87 | write_name_quoted(ent->name + offset, stdout, line_terminator); |
5be4efbe LT |
88 | } |
89 | ||
453ec4bd | 90 | static void show_other_files(struct dir_struct *dir) |
fcbc3083 JH |
91 | { |
92 | int i; | |
5698454e LT |
93 | |
94 | ||
95 | /* | |
96 | * Skip matching and unmerged entries for the paths, | |
97 | * since we want just "others". | |
98 | * | |
99 | * (Matching entries are normally pruned during | |
100 | * the directory tree walk, but will show up for | |
101 | * gitlinks because we don't necessarily have | |
102 | * dir->show_other_directories set to suppress | |
103 | * them). | |
104 | */ | |
453ec4bd | 105 | for (i = 0; i < dir->nr; i++) { |
453ec4bd | 106 | struct dir_entry *ent = dir->entries[i]; |
5698454e | 107 | int len, pos; |
fcbc3083 | 108 | struct cache_entry *ce; |
5698454e LT |
109 | |
110 | /* | |
111 | * Remove the '/' at the end that directory | |
112 | * walking adds for directory entries. | |
113 | */ | |
114 | len = ent->len; | |
115 | if (len && ent->name[len-1] == '/') | |
116 | len--; | |
117 | pos = cache_name_pos(ent->name, len); | |
fcbc3083 | 118 | if (0 <= pos) |
5698454e | 119 | continue; /* exact match */ |
fcbc3083 | 120 | pos = -pos - 1; |
a6080a0a | 121 | if (pos < active_nr) { |
fcbc3083 | 122 | ce = active_cache[pos]; |
5698454e LT |
123 | if (ce_namelen(ce) == len && |
124 | !memcmp(ce->name, ent->name, len)) | |
fcbc3083 JH |
125 | continue; /* Yup, this one exists unmerged */ |
126 | } | |
127 | show_dir_entry(tag_other, ent); | |
128 | } | |
129 | } | |
130 | ||
453ec4bd | 131 | static void show_killed_files(struct dir_struct *dir) |
6ca45943 JH |
132 | { |
133 | int i; | |
453ec4bd LT |
134 | for (i = 0; i < dir->nr; i++) { |
135 | struct dir_entry *ent = dir->entries[i]; | |
6ca45943 JH |
136 | char *cp, *sp; |
137 | int pos, len, killed = 0; | |
138 | ||
139 | for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) { | |
140 | sp = strchr(cp, '/'); | |
141 | if (!sp) { | |
142 | /* If ent->name is prefix of an entry in the | |
143 | * cache, it will be killed. | |
144 | */ | |
145 | pos = cache_name_pos(ent->name, ent->len); | |
146 | if (0 <= pos) | |
147 | die("bug in show-killed-files"); | |
148 | pos = -pos - 1; | |
149 | while (pos < active_nr && | |
150 | ce_stage(active_cache[pos])) | |
151 | pos++; /* skip unmerged */ | |
152 | if (active_nr <= pos) | |
153 | break; | |
154 | /* pos points at a name immediately after | |
155 | * ent->name in the cache. Does it expect | |
156 | * ent->name to be a directory? | |
157 | */ | |
158 | len = ce_namelen(active_cache[pos]); | |
159 | if ((ent->len < len) && | |
160 | !strncmp(active_cache[pos]->name, | |
161 | ent->name, ent->len) && | |
162 | active_cache[pos]->name[ent->len] == '/') | |
163 | killed = 1; | |
164 | break; | |
165 | } | |
166 | if (0 <= cache_name_pos(ent->name, sp - ent->name)) { | |
167 | /* If any of the leading directories in | |
168 | * ent->name is registered in the cache, | |
169 | * ent->name will be killed. | |
170 | */ | |
171 | killed = 1; | |
172 | break; | |
173 | } | |
174 | } | |
175 | if (killed) | |
453ec4bd | 176 | show_dir_entry(tag_killed, dir->entries[i]); |
6ca45943 | 177 | } |
8695c8bf LT |
178 | } |
179 | ||
5be4efbe LT |
180 | static void show_ce_entry(const char *tag, struct cache_entry *ce) |
181 | { | |
182 | int len = prefix_len; | |
183 | int offset = prefix_offset; | |
184 | ||
185 | if (len >= ce_namelen(ce)) | |
186 | die("git-ls-files: internal error - cache entry not superset of prefix"); | |
187 | ||
ee425e46 | 188 | if (pathspec && !pathspec_match(pathspec, ps_matched, ce->name, len)) |
5be4efbe LT |
189 | return; |
190 | ||
8bb2e03b | 191 | if (tag && *tag && show_valid_bit && |
7a51ed66 | 192 | (ce->ce_flags & CE_VALID)) { |
2bcab240 JH |
193 | static char alttag[4]; |
194 | memcpy(alttag, tag, 3); | |
195 | if (isalpha(tag[0])) | |
196 | alttag[0] = tolower(tag[0]); | |
197 | else if (tag[0] == '?') | |
198 | alttag[0] = '!'; | |
199 | else { | |
200 | alttag[0] = 'v'; | |
201 | alttag[1] = tag[0]; | |
202 | alttag[2] = ' '; | |
203 | alttag[3] = 0; | |
204 | } | |
205 | tag = alttag; | |
206 | } | |
207 | ||
22ddf719 JH |
208 | if (!show_stage) { |
209 | fputs(tag, stdout); | |
663af342 | 210 | } else { |
22ddf719 | 211 | printf("%s%06o %s %d\t", |
5be4efbe | 212 | tag, |
7a51ed66 | 213 | ce->ce_mode, |
ad0cae4c EW |
214 | abbrev ? find_unique_abbrev(ce->sha1,abbrev) |
215 | : sha1_to_hex(ce->sha1), | |
22ddf719 | 216 | ce_stage(ce)); |
22ddf719 | 217 | } |
663af342 | 218 | write_name_quoted(ce->name + offset, stdout, line_terminator); |
5be4efbe LT |
219 | } |
220 | ||
3e04228b | 221 | static void show_files(struct dir_struct *dir, const char *prefix) |
8695c8bf LT |
222 | { |
223 | int i; | |
224 | ||
225 | /* For cached/deleted files we don't need to even do the readdir */ | |
6ca45943 | 226 | if (show_others || show_killed) { |
5be4efbe LT |
227 | const char *path = ".", *base = ""; |
228 | int baselen = prefix_len; | |
229 | ||
b4189aa8 | 230 | if (baselen) |
5be4efbe | 231 | path = base = prefix; |
9fc42d60 | 232 | read_directory(dir, path, base, baselen, pathspec); |
6ca45943 | 233 | if (show_others) |
453ec4bd | 234 | show_other_files(dir); |
6ca45943 | 235 | if (show_killed) |
453ec4bd | 236 | show_killed_files(dir); |
8695c8bf | 237 | } |
aee46198 | 238 | if (show_cached | show_stage) { |
8695c8bf LT |
239 | for (i = 0; i < active_nr; i++) { |
240 | struct cache_entry *ce = active_cache[i]; | |
6831a88a JH |
241 | int dtype = ce_to_dtype(ce); |
242 | if (excluded(dir, ce->name, &dtype) != dir->show_ignored) | |
9ff768e9 | 243 | continue; |
eec8c633 LT |
244 | if (show_unmerged && !ce_stage(ce)) |
245 | continue; | |
7a51ed66 | 246 | if (ce->ce_flags & CE_UPDATE) |
64586e75 | 247 | continue; |
5be4efbe | 248 | show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce); |
8695c8bf LT |
249 | } |
250 | } | |
b0391890 | 251 | if (show_deleted | show_modified) { |
8695c8bf LT |
252 | for (i = 0; i < active_nr; i++) { |
253 | struct cache_entry *ce = active_cache[i]; | |
254 | struct stat st; | |
b0391890 | 255 | int err; |
6831a88a JH |
256 | int dtype = ce_to_dtype(ce); |
257 | if (excluded(dir, ce->name, &dtype) != dir->show_ignored) | |
9ff768e9 | 258 | continue; |
b0391890 JH |
259 | err = lstat(ce->name, &st); |
260 | if (show_deleted && err) | |
261 | show_ce_entry(tag_removed, ce); | |
2bcab240 | 262 | if (show_modified && ce_modified(ce, &st, 0)) |
b0391890 | 263 | show_ce_entry(tag_modified, ce); |
5be4efbe LT |
264 | } |
265 | } | |
266 | } | |
267 | ||
268 | /* | |
269 | * Prune the index to only contain stuff starting with "prefix" | |
270 | */ | |
3e04228b | 271 | static void prune_cache(const char *prefix) |
5be4efbe LT |
272 | { |
273 | int pos = cache_name_pos(prefix, prefix_len); | |
274 | unsigned int first, last; | |
275 | ||
276 | if (pos < 0) | |
277 | pos = -pos-1; | |
95af39fc KP |
278 | memmove(active_cache, active_cache + pos, |
279 | (active_nr - pos) * sizeof(struct cache_entry *)); | |
5be4efbe LT |
280 | active_nr -= pos; |
281 | first = 0; | |
282 | last = active_nr; | |
283 | while (last > first) { | |
284 | int next = (last + first) >> 1; | |
285 | struct cache_entry *ce = active_cache[next]; | |
286 | if (!strncmp(ce->name, prefix, prefix_len)) { | |
287 | first = next+1; | |
288 | continue; | |
8695c8bf | 289 | } |
5be4efbe LT |
290 | last = next; |
291 | } | |
292 | active_nr = last; | |
293 | } | |
294 | ||
3e04228b | 295 | static const char *verify_pathspec(const char *prefix) |
5be4efbe | 296 | { |
56fc5108 | 297 | const char **p, *n, *prev; |
56fc5108 LT |
298 | unsigned long max; |
299 | ||
300 | prev = NULL; | |
301 | max = PATH_MAX; | |
302 | for (p = pathspec; (n = *p) != NULL; p++) { | |
303 | int i, len = 0; | |
304 | for (i = 0; i < max; i++) { | |
305 | char c = n[i]; | |
306 | if (prev && prev[i] != c) | |
307 | break; | |
56906143 | 308 | if (!c || c == '*' || c == '?') |
56fc5108 LT |
309 | break; |
310 | if (c == '/') | |
311 | len = i+1; | |
312 | } | |
313 | prev = n; | |
314 | if (len < max) { | |
315 | max = len; | |
316 | if (!max) | |
317 | break; | |
318 | } | |
5be4efbe | 319 | } |
56fc5108 LT |
320 | |
321 | if (prefix_offset > max || memcmp(prev, prefix, prefix_offset)) | |
322 | die("git-ls-files: cannot generate relative filenames containing '..'"); | |
323 | ||
56fc5108 | 324 | prefix_len = max; |
182af834 | 325 | return max ? xmemdupz(prev, max) : NULL; |
8695c8bf LT |
326 | } |
327 | ||
64586e75 JH |
328 | /* |
329 | * Read the tree specified with --with-tree option | |
330 | * (typically, HEAD) into stage #1 and then | |
331 | * squash them down to stage #0. This is used for | |
332 | * --error-unmatch to list and check the path patterns | |
333 | * that were given from the command line. We are not | |
334 | * going to write this index out. | |
335 | */ | |
ee425e46 | 336 | void overlay_tree_on_cache(const char *tree_name, const char *prefix) |
64586e75 JH |
337 | { |
338 | struct tree *tree; | |
339 | unsigned char sha1[20]; | |
340 | const char **match; | |
341 | struct cache_entry *last_stage0 = NULL; | |
342 | int i; | |
343 | ||
344 | if (get_sha1(tree_name, sha1)) | |
345 | die("tree-ish %s not found.", tree_name); | |
346 | tree = parse_tree_indirect(sha1); | |
347 | if (!tree) | |
348 | die("bad tree-ish %s", tree_name); | |
349 | ||
350 | /* Hoist the unmerged entries up to stage #3 to make room */ | |
351 | for (i = 0; i < active_nr; i++) { | |
352 | struct cache_entry *ce = active_cache[i]; | |
353 | if (!ce_stage(ce)) | |
354 | continue; | |
7a51ed66 | 355 | ce->ce_flags |= CE_STAGEMASK; |
64586e75 JH |
356 | } |
357 | ||
358 | if (prefix) { | |
359 | static const char *(matchbuf[2]); | |
360 | matchbuf[0] = prefix; | |
361 | matchbuf [1] = NULL; | |
362 | match = matchbuf; | |
363 | } else | |
364 | match = NULL; | |
365 | if (read_tree(tree, 1, match)) | |
366 | die("unable to read tree entries %s", tree_name); | |
367 | ||
368 | for (i = 0; i < active_nr; i++) { | |
369 | struct cache_entry *ce = active_cache[i]; | |
370 | switch (ce_stage(ce)) { | |
371 | case 0: | |
372 | last_stage0 = ce; | |
373 | /* fallthru */ | |
374 | default: | |
375 | continue; | |
376 | case 1: | |
377 | /* | |
378 | * If there is stage #0 entry for this, we do not | |
379 | * need to show it. We use CE_UPDATE bit to mark | |
380 | * such an entry. | |
381 | */ | |
382 | if (last_stage0 && | |
383 | !strcmp(last_stage0->name, ce->name)) | |
7a51ed66 | 384 | ce->ce_flags |= CE_UPDATE; |
64586e75 JH |
385 | } |
386 | } | |
387 | } | |
388 | ||
ee425e46 JH |
389 | int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset) |
390 | { | |
391 | /* | |
392 | * Make sure all pathspec matched; otherwise it is an error. | |
393 | */ | |
394 | int num, errors = 0; | |
395 | for (num = 0; pathspec[num]; num++) { | |
396 | int other, found_dup; | |
397 | ||
398 | if (ps_matched[num]) | |
399 | continue; | |
400 | /* | |
401 | * The caller might have fed identical pathspec | |
402 | * twice. Do not barf on such a mistake. | |
403 | */ | |
404 | for (found_dup = other = 0; | |
405 | !found_dup && pathspec[other]; | |
406 | other++) { | |
407 | if (other == num || !ps_matched[other]) | |
408 | continue; | |
409 | if (!strcmp(pathspec[other], pathspec[num])) | |
410 | /* | |
411 | * Ok, we have a match already. | |
412 | */ | |
413 | found_dup = 1; | |
414 | } | |
415 | if (found_dup) | |
416 | continue; | |
417 | ||
418 | error("pathspec '%s' did not match any file(s) known to git.", | |
419 | pathspec[num] + prefix_offset); | |
420 | errors++; | |
421 | } | |
422 | return errors; | |
423 | } | |
424 | ||
4d1f1190 | 425 | static const char ls_files_usage[] = |
1b1dd23f | 426 | "git ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* " |
f87f9497 | 427 | "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] " |
8e7b07c8 JK |
428 | "[ --exclude-per-directory=<filename> ] [--exclude-standard] " |
429 | "[--full-name] [--abbrev] [--] [<file>]*"; | |
cf9a113d | 430 | |
a633fca0 | 431 | int cmd_ls_files(int argc, const char **argv, const char *prefix) |
8695c8bf LT |
432 | { |
433 | int i; | |
6d9ba67b | 434 | int exc_given = 0, require_work_tree = 0; |
453ec4bd | 435 | struct dir_struct dir; |
8695c8bf | 436 | |
453ec4bd | 437 | memset(&dir, 0, sizeof(dir)); |
5be4efbe | 438 | if (prefix) |
56fc5108 | 439 | prefix_offset = strlen(prefix); |
ef90d6d4 | 440 | git_config(git_default_config, NULL); |
5be4efbe | 441 | |
8695c8bf | 442 | for (i = 1; i < argc; i++) { |
6b5ee137 | 443 | const char *arg = argv[i]; |
8695c8bf | 444 | |
500b97e4 FK |
445 | if (!strcmp(arg, "--")) { |
446 | i++; | |
447 | break; | |
448 | } | |
b83c8345 JH |
449 | if (!strcmp(arg, "-z")) { |
450 | line_terminator = 0; | |
5be4efbe LT |
451 | continue; |
452 | } | |
8bb2e03b | 453 | if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) { |
20d37ef6 PB |
454 | tag_cached = "H "; |
455 | tag_unmerged = "M "; | |
456 | tag_removed = "R "; | |
b0391890 | 457 | tag_modified = "C "; |
20d37ef6 | 458 | tag_other = "? "; |
6ca45943 | 459 | tag_killed = "K "; |
8bb2e03b JH |
460 | if (arg[1] == 'v') |
461 | show_valid_bit = 1; | |
5be4efbe LT |
462 | continue; |
463 | } | |
464 | if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) { | |
8695c8bf | 465 | show_cached = 1; |
5be4efbe LT |
466 | continue; |
467 | } | |
468 | if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) { | |
8695c8bf | 469 | show_deleted = 1; |
5be4efbe LT |
470 | continue; |
471 | } | |
b0391890 JH |
472 | if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) { |
473 | show_modified = 1; | |
6d9ba67b | 474 | require_work_tree = 1; |
b0391890 JH |
475 | continue; |
476 | } | |
5be4efbe | 477 | if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) { |
8695c8bf | 478 | show_others = 1; |
6d9ba67b | 479 | require_work_tree = 1; |
5be4efbe LT |
480 | continue; |
481 | } | |
482 | if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) { | |
453ec4bd | 483 | dir.show_ignored = 1; |
6d9ba67b | 484 | require_work_tree = 1; |
5be4efbe LT |
485 | continue; |
486 | } | |
487 | if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) { | |
aee46198 | 488 | show_stage = 1; |
5be4efbe LT |
489 | continue; |
490 | } | |
491 | if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) { | |
6ca45943 | 492 | show_killed = 1; |
6d9ba67b | 493 | require_work_tree = 1; |
5be4efbe LT |
494 | continue; |
495 | } | |
9518eb26 | 496 | if (!strcmp(arg, "--directory")) { |
453ec4bd | 497 | dir.show_other_directories = 1; |
9518eb26 LT |
498 | continue; |
499 | } | |
b0a3de42 | 500 | if (!strcmp(arg, "--no-empty-directory")) { |
453ec4bd | 501 | dir.hide_empty_directories = 1; |
b0a3de42 PB |
502 | continue; |
503 | } | |
5be4efbe | 504 | if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) { |
20d37ef6 PB |
505 | /* There's no point in showing unmerged unless |
506 | * you also show the stage information. | |
507 | */ | |
eec8c633 LT |
508 | show_stage = 1; |
509 | show_unmerged = 1; | |
5be4efbe LT |
510 | continue; |
511 | } | |
512 | if (!strcmp(arg, "-x") && i+1 < argc) { | |
fee88256 | 513 | exc_given = 1; |
453ec4bd | 514 | add_exclude(argv[++i], "", 0, &dir.exclude_list[EXC_CMDL]); |
5be4efbe LT |
515 | continue; |
516 | } | |
cc44c765 | 517 | if (!prefixcmp(arg, "--exclude=")) { |
fee88256 | 518 | exc_given = 1; |
453ec4bd | 519 | add_exclude(arg+10, "", 0, &dir.exclude_list[EXC_CMDL]); |
5be4efbe LT |
520 | continue; |
521 | } | |
522 | if (!strcmp(arg, "-X") && i+1 < argc) { | |
fee88256 | 523 | exc_given = 1; |
453ec4bd | 524 | add_excludes_from_file(&dir, argv[++i]); |
5be4efbe LT |
525 | continue; |
526 | } | |
cc44c765 | 527 | if (!prefixcmp(arg, "--exclude-from=")) { |
fee88256 | 528 | exc_given = 1; |
453ec4bd | 529 | add_excludes_from_file(&dir, arg+15); |
5be4efbe LT |
530 | continue; |
531 | } | |
cc44c765 | 532 | if (!prefixcmp(arg, "--exclude-per-directory=")) { |
fee88256 | 533 | exc_given = 1; |
453ec4bd | 534 | dir.exclude_per_dir = arg + 24; |
5be4efbe LT |
535 | continue; |
536 | } | |
8e7b07c8 JK |
537 | if (!strcmp(arg, "--exclude-standard")) { |
538 | exc_given = 1; | |
539 | setup_standard_excludes(&dir); | |
540 | continue; | |
541 | } | |
5be4efbe LT |
542 | if (!strcmp(arg, "--full-name")) { |
543 | prefix_offset = 0; | |
544 | continue; | |
545 | } | |
bba319b5 JH |
546 | if (!strcmp(arg, "--error-unmatch")) { |
547 | error_unmatch = 1; | |
548 | continue; | |
549 | } | |
64586e75 JH |
550 | if (!prefixcmp(arg, "--with-tree=")) { |
551 | with_tree = arg + 12; | |
552 | continue; | |
553 | } | |
cc44c765 | 554 | if (!prefixcmp(arg, "--abbrev=")) { |
ad0cae4c EW |
555 | abbrev = strtoul(arg+9, NULL, 10); |
556 | if (abbrev && abbrev < MINIMUM_ABBREV) | |
557 | abbrev = MINIMUM_ABBREV; | |
558 | else if (abbrev > 40) | |
559 | abbrev = 40; | |
560 | continue; | |
561 | } | |
562 | if (!strcmp(arg, "--abbrev")) { | |
563 | abbrev = DEFAULT_ABBREV; | |
564 | continue; | |
565 | } | |
56fc5108 | 566 | if (*arg == '-') |
17710391 | 567 | usage(ls_files_usage); |
56fc5108 | 568 | break; |
9ff768e9 NP |
569 | } |
570 | ||
7d8ae932 MH |
571 | if (require_work_tree && !is_inside_work_tree()) |
572 | setup_work_tree(); | |
6d9ba67b | 573 | |
56fc5108 LT |
574 | pathspec = get_pathspec(prefix, argv + i); |
575 | ||
576 | /* Verify that the pathspec matches the prefix */ | |
79418599 | 577 | if (pathspec) |
3e04228b | 578 | prefix = verify_pathspec(prefix); |
5be4efbe | 579 | |
bba319b5 JH |
580 | /* Treat unmatching pathspec elements as errors */ |
581 | if (pathspec && error_unmatch) { | |
582 | int num; | |
583 | for (num = 0; pathspec[num]; num++) | |
584 | ; | |
585 | ps_matched = xcalloc(1, num); | |
586 | } | |
587 | ||
453ec4bd | 588 | if (dir.show_ignored && !exc_given) { |
20d37ef6 PB |
589 | fprintf(stderr, "%s: --ignored needs some exclude pattern\n", |
590 | argv[0]); | |
9ff768e9 | 591 | exit(1); |
8695c8bf LT |
592 | } |
593 | ||
594 | /* With no flags, we default to showing the cached files */ | |
b0391890 JH |
595 | if (!(show_stage | show_deleted | show_others | show_unmerged | |
596 | show_killed | show_modified)) | |
8695c8bf LT |
597 | show_cached = 1; |
598 | ||
599 | read_cache(); | |
5be4efbe | 600 | if (prefix) |
3e04228b | 601 | prune_cache(prefix); |
64586e75 JH |
602 | if (with_tree) { |
603 | /* | |
604 | * Basic sanity check; show-stages and show-unmerged | |
605 | * would not make any sense with this option. | |
606 | */ | |
607 | if (show_stage || show_unmerged) | |
608 | die("ls-files --with-tree is incompatible with -s or -u"); | |
ee425e46 | 609 | overlay_tree_on_cache(with_tree, prefix); |
64586e75 | 610 | } |
3e04228b | 611 | show_files(&dir, prefix); |
bba319b5 JH |
612 | |
613 | if (ps_matched) { | |
ee425e46 JH |
614 | int bad; |
615 | bad = report_path_error(ps_matched, pathspec, prefix_offset); | |
616 | if (bad) | |
ced7b828 AE |
617 | fprintf(stderr, "Did you forget to 'git add'?\n"); |
618 | ||
ee425e46 | 619 | return bad ? 1 : 0; |
bba319b5 JH |
620 | } |
621 | ||
8695c8bf LT |
622 | return 0; |
623 | } |