blob: f47114a168f3a7479cbe147f953e5f56af3d2dfc [file] [log] [blame]
Linus Torvalds8695c8b2005-04-12 01:55:381/*
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>
Nicolas Pitre9ff768e2005-04-28 18:44:049#include <fnmatch.h>
Linus Torvalds8695c8b2005-04-12 01:55:3810
11#include "cache.h"
12
13static int show_deleted = 0;
14static int show_cached = 0;
15static int show_others = 0;
16static int show_ignored = 0;
Junio C Hamanoaee46192005-04-16 15:33:2317static int show_stage = 0;
Linus Torvaldseec8c632005-04-16 19:43:3218static int show_unmerged = 0;
Junio C Hamanob0391892005-09-19 22:11:1519static int show_modified = 0;
Junio C Hamano6ca45942005-05-13 00:17:5420static int show_killed = 0;
Junio C Hamanob83c8342005-04-15 18:11:0121static int line_terminator = '\n';
Linus Torvalds8695c8b2005-04-12 01:55:3822
Linus Torvalds5be4efb2005-08-21 19:55:3323static int prefix_len = 0, prefix_offset = 0;
24static const char *prefix = NULL;
Linus Torvalds56fc5102005-08-22 00:27:5025static const char **pathspec = NULL;
Linus Torvalds5be4efb2005-08-21 19:55:3326
Petr Baudis20d37ef2005-04-22 02:47:0827static const char *tag_cached = "";
28static const char *tag_unmerged = "";
29static const char *tag_removed = "";
30static const char *tag_other = "";
Junio C Hamano6ca45942005-05-13 00:17:5431static const char *tag_killed = "";
Junio C Hamanob0391892005-09-19 22:11:1532static const char *tag_modified = "";
Petr Baudis20d37ef2005-04-22 02:47:0833
Junio C Hamano6b5ee132005-09-21 07:00:4734static const char *exclude_per_dir = NULL;
Nicolas Pitre9ff768e2005-04-28 18:44:0435
Junio C Hamanofee88252005-07-29 06:32:2036/* We maintain three exclude pattern lists:
37 * EXC_CMDL lists patterns explicitly given on the command line.
38 * EXC_DIRS lists patterns obtained from per-directory ignore files.
39 * EXC_FILE lists patterns from fallback ignore files.
40 */
41#define EXC_CMDL 0
42#define EXC_DIRS 1
43#define EXC_FILE 2
44static struct exclude_list {
45 int nr;
46 int alloc;
47 struct exclude {
48 const char *pattern;
49 const char *base;
50 int baselen;
51 } **excludes;
52} exclude_list[3];
53
54static void add_exclude(const char *string, const char *base,
55 int baselen, struct exclude_list *which)
Nicolas Pitre9ff768e2005-04-28 18:44:0456{
Junio C Hamanof87f9492005-07-24 22:26:0957 struct exclude *x = xmalloc(sizeof (*x));
58
59 x->pattern = string;
60 x->base = base;
61 x->baselen = baselen;
Junio C Hamanofee88252005-07-29 06:32:2062 if (which->nr == which->alloc) {
63 which->alloc = alloc_nr(which->alloc);
64 which->excludes = realloc(which->excludes,
65 which->alloc * sizeof(x));
Nicolas Pitre9ff768e2005-04-28 18:44:0466 }
Junio C Hamanofee88252005-07-29 06:32:2067 which->excludes[which->nr++] = x;
Nicolas Pitre9ff768e2005-04-28 18:44:0468}
69
Junio C Hamanof87f9492005-07-24 22:26:0970static int add_excludes_from_file_1(const char *fname,
Junio C Hamanofee88252005-07-29 06:32:2071 const char *base,
72 int baselen,
73 struct exclude_list *which)
Nicolas Pitre9ff768e2005-04-28 18:44:0474{
75 int fd, i;
76 long size;
77 char *buf, *entry;
78
79 fd = open(fname, O_RDONLY);
80 if (fd < 0)
81 goto err;
82 size = lseek(fd, 0, SEEK_END);
83 if (size < 0)
84 goto err;
85 lseek(fd, 0, SEEK_SET);
86 if (size == 0) {
87 close(fd);
Junio C Hamanof87f9492005-07-24 22:26:0988 return 0;
Nicolas Pitre9ff768e2005-04-28 18:44:0489 }
90 buf = xmalloc(size);
91 if (read(fd, buf, size) != size)
92 goto err;
93 close(fd);
94
95 entry = buf;
96 for (i = 0; i < size; i++) {
97 if (buf[i] == '\n') {
Junio C Hamanof87f9492005-07-24 22:26:0998 if (entry != buf + i && entry[0] != '#') {
Nicolas Pitre9ff768e2005-04-28 18:44:0499 buf[i] = 0;
Junio C Hamanofee88252005-07-29 06:32:20100 add_exclude(entry, base, baselen, which);
Nicolas Pitre9ff768e2005-04-28 18:44:04101 }
102 entry = buf + i + 1;
103 }
104 }
Junio C Hamanof87f9492005-07-24 22:26:09105 return 0;
Nicolas Pitre9ff768e2005-04-28 18:44:04106
Junio C Hamanof87f9492005-07-24 22:26:09107 err:
108 if (0 <= fd)
109 close(fd);
110 return -1;
111}
112
113static void add_excludes_from_file(const char *fname)
114{
Junio C Hamanofee88252005-07-29 06:32:20115 if (add_excludes_from_file_1(fname, "", 0,
116 &exclude_list[EXC_FILE]) < 0)
Junio C Hamanof87f9492005-07-24 22:26:09117 die("cannot use %s as an exclude file", fname);
118}
119
120static int push_exclude_per_directory(const char *base, int baselen)
121{
122 char exclude_file[PATH_MAX];
Junio C Hamanofee88252005-07-29 06:32:20123 struct exclude_list *el = &exclude_list[EXC_DIRS];
124 int current_nr = el->nr;
Junio C Hamanof87f9492005-07-24 22:26:09125
126 if (exclude_per_dir) {
127 memcpy(exclude_file, base, baselen);
128 strcpy(exclude_file + baselen, exclude_per_dir);
Junio C Hamanofee88252005-07-29 06:32:20129 add_excludes_from_file_1(exclude_file, base, baselen, el);
Junio C Hamanof87f9492005-07-24 22:26:09130 }
131 return current_nr;
132}
133
134static void pop_exclude_per_directory(int stk)
135{
Junio C Hamanofee88252005-07-29 06:32:20136 struct exclude_list *el = &exclude_list[EXC_DIRS];
137
138 while (stk < el->nr)
139 free(el->excludes[--el->nr]);
Nicolas Pitre9ff768e2005-04-28 18:44:04140}
141
Junio C Hamanofee88252005-07-29 06:32:20142/* Scan the list and let the last match determines the fate.
143 * Return 1 for exclude, 0 for include and -1 for undecided.
144 */
145static int excluded_1(const char *pathname,
146 int pathlen,
147 struct exclude_list *el)
Nicolas Pitre9ff768e2005-04-28 18:44:04148{
149 int i;
Junio C Hamanof87f9492005-07-24 22:26:09150
Junio C Hamanofee88252005-07-29 06:32:20151 if (el->nr) {
152 for (i = el->nr - 1; 0 <= i; i--) {
153 struct exclude *x = el->excludes[i];
Junio C Hamanof87f9492005-07-24 22:26:09154 const char *exclude = x->pattern;
155 int to_exclude = 1;
156
157 if (*exclude == '!') {
158 to_exclude = 0;
159 exclude++;
160 }
161
162 if (!strchr(exclude, '/')) {
163 /* match basename */
164 const char *basename = strrchr(pathname, '/');
165 basename = (basename) ? basename+1 : pathname;
166 if (fnmatch(exclude, basename, 0) == 0)
167 return to_exclude;
168 }
169 else {
170 /* match with FNM_PATHNAME:
171 * exclude has base (baselen long) inplicitly
172 * in front of it.
173 */
174 int baselen = x->baselen;
175 if (*exclude == '/')
176 exclude++;
177
178 if (pathlen < baselen ||
179 (baselen && pathname[baselen-1] != '/') ||
180 strncmp(pathname, x->base, baselen))
181 continue;
182
183 if (fnmatch(exclude, pathname+baselen,
184 FNM_PATHNAME) == 0)
185 return to_exclude;
186 }
187 }
Nicolas Pitre9ff768e2005-04-28 18:44:04188 }
Junio C Hamanofee88252005-07-29 06:32:20189 return -1; /* undecided */
190}
191
192static int excluded(const char *pathname)
193{
194 int pathlen = strlen(pathname);
195 int st;
196
197 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
198 switch (excluded_1(pathname, pathlen, &exclude_list[st])) {
199 case 0:
200 return 0;
201 case 1:
202 return 1;
203 }
204 }
Nicolas Pitre9ff768e2005-04-28 18:44:04205 return 0;
206}
207
Junio C Hamano6ca45942005-05-13 00:17:54208struct nond_on_fs {
209 int len;
Junio C Hamano2c046622005-08-29 19:41:03210 char name[0];
Junio C Hamano6ca45942005-05-13 00:17:54211};
212
213static struct nond_on_fs **dir;
Linus Torvalds8695c8b2005-04-12 01:55:38214static int nr_dir;
215static int dir_alloc;
216
217static void add_name(const char *pathname, int len)
218{
Junio C Hamano6ca45942005-05-13 00:17:54219 struct nond_on_fs *ent;
Linus Torvalds8695c8b2005-04-12 01:55:38220
221 if (cache_name_pos(pathname, len) >= 0)
222 return;
223
224 if (nr_dir == dir_alloc) {
225 dir_alloc = alloc_nr(dir_alloc);
Junio C Hamano6ca45942005-05-13 00:17:54226 dir = xrealloc(dir, dir_alloc*sizeof(ent));
Linus Torvalds8695c8b2005-04-12 01:55:38227 }
Junio C Hamano6ca45942005-05-13 00:17:54228 ent = xmalloc(sizeof(*ent) + len + 1);
229 ent->len = len;
230 memcpy(ent->name, pathname, len);
Linus Torvalds5be4efb2005-08-21 19:55:33231 ent->name[len] = 0;
Junio C Hamano6ca45942005-05-13 00:17:54232 dir[nr_dir++] = ent;
Linus Torvalds8695c8b2005-04-12 01:55:38233}
234
235/*
236 * Read a directory tree. We currently ignore anything but
Junio C Hamanoa15c1c62005-05-13 00:16:04237 * directories, regular files and symlinks. That's because git
238 * doesn't handle them at all yet. Maybe that will change some
239 * day.
Linus Torvalds8695c8b2005-04-12 01:55:38240 *
Junio C Hamanof87f9492005-07-24 22:26:09241 * Also, we ignore the name ".git" (even if it is not a directory).
Ingo Molnaraebb2672005-04-12 18:36:26242 * That likely will not change.
Linus Torvalds8695c8b2005-04-12 01:55:38243 */
244static void read_directory(const char *path, const char *base, int baselen)
245{
246 DIR *dir = opendir(path);
247
248 if (dir) {
Junio C Hamanof87f9492005-07-24 22:26:09249 int exclude_stk;
Linus Torvalds8695c8b2005-04-12 01:55:38250 struct dirent *de;
251 char fullname[MAXPATHLEN + 1];
252 memcpy(fullname, base, baselen);
253
Junio C Hamanof87f9492005-07-24 22:26:09254 exclude_stk = push_exclude_per_directory(base, baselen);
255
Linus Torvalds8695c8b2005-04-12 01:55:38256 while ((de = readdir(dir)) != NULL) {
257 int len;
258
Junio C Hamanoc4ee2952005-05-25 01:20:08259 if ((de->d_name[0] == '.') &&
260 (de->d_name[1] == 0 ||
261 !strcmp(de->d_name + 1, ".") ||
262 !strcmp(de->d_name + 1, "git")))
Linus Torvalds8695c8b2005-04-12 01:55:38263 continue;
264 len = strlen(de->d_name);
265 memcpy(fullname + baselen, de->d_name, len+1);
Junio C Hamanof87f9492005-07-24 22:26:09266 if (excluded(fullname) != show_ignored)
267 continue;
Linus Torvalds8695c8b2005-04-12 01:55:38268
Edgar Toernigb6829692005-04-30 16:51:03269 switch (DTYPE(de)) {
Linus Torvalds8695c8b2005-04-12 01:55:38270 struct stat st;
271 default:
272 continue;
273 case DT_UNKNOWN:
274 if (lstat(fullname, &st))
275 continue;
Junio C Hamanoa15c1c62005-05-13 00:16:04276 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
Linus Torvalds8695c8b2005-04-12 01:55:38277 break;
278 if (!S_ISDIR(st.st_mode))
279 continue;
280 /* fallthrough */
281 case DT_DIR:
282 memcpy(fullname + baselen + len, "/", 2);
Petr Baudis20d37ef2005-04-22 02:47:08283 read_directory(fullname, fullname,
284 baselen + len + 1);
Linus Torvalds8695c8b2005-04-12 01:55:38285 continue;
286 case DT_REG:
Junio C Hamanoa15c1c62005-05-13 00:16:04287 case DT_LNK:
Linus Torvalds8695c8b2005-04-12 01:55:38288 break;
289 }
290 add_name(fullname, baselen + len);
291 }
292 closedir(dir);
Junio C Hamanof87f9492005-07-24 22:26:09293
294 pop_exclude_per_directory(exclude_stk);
Linus Torvalds8695c8b2005-04-12 01:55:38295 }
296}
297
298static int cmp_name(const void *p1, const void *p2)
299{
Junio C Hamano6ca45942005-05-13 00:17:54300 const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1;
301 const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2;
Linus Torvalds8695c8b2005-04-12 01:55:38302
Junio C Hamano6ca45942005-05-13 00:17:54303 return cache_name_compare(e1->name, e1->len,
304 e2->name, e2->len);
305}
306
Linus Torvalds56fc5102005-08-22 00:27:50307/*
308 * Match a pathspec against a filename. The first "len" characters
309 * are the common prefix
310 */
311static int match(const char **spec, const char *filename, int len)
312{
313 const char *m;
314
315 while ((m = *spec++) != NULL) {
316 int matchlen = strlen(m + len);
317
318 if (!matchlen)
319 return 1;
320 if (!strncmp(m + len, filename + len, matchlen)) {
321 if (m[len + matchlen - 1] == '/')
322 return 1;
323 switch (filename[len + matchlen]) {
324 case '/': case '\0':
325 return 1;
326 }
327 }
328 if (!fnmatch(m + len, filename + len, 0))
329 return 1;
330 }
331 return 0;
332}
333
Linus Torvalds5be4efb2005-08-21 19:55:33334static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
335{
336 int len = prefix_len;
337 int offset = prefix_offset;
338
339 if (len >= ent->len)
340 die("git-ls-files: internal error - directory entry not superset of prefix");
341
Linus Torvalds56fc5102005-08-22 00:27:50342 if (pathspec && !match(pathspec, ent->name, len))
Linus Torvalds5be4efb2005-08-21 19:55:33343 return;
344
345 printf("%s%s%c", tag, ent->name + offset, line_terminator);
346}
347
Linus Torvaldse99d59f2005-05-20 18:46:10348static void show_killed_files(void)
Junio C Hamano6ca45942005-05-13 00:17:54349{
350 int i;
351 for (i = 0; i < nr_dir; i++) {
352 struct nond_on_fs *ent = dir[i];
353 char *cp, *sp;
354 int pos, len, killed = 0;
355
356 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
357 sp = strchr(cp, '/');
358 if (!sp) {
359 /* If ent->name is prefix of an entry in the
360 * cache, it will be killed.
361 */
362 pos = cache_name_pos(ent->name, ent->len);
363 if (0 <= pos)
364 die("bug in show-killed-files");
365 pos = -pos - 1;
366 while (pos < active_nr &&
367 ce_stage(active_cache[pos]))
368 pos++; /* skip unmerged */
369 if (active_nr <= pos)
370 break;
371 /* pos points at a name immediately after
372 * ent->name in the cache. Does it expect
373 * ent->name to be a directory?
374 */
375 len = ce_namelen(active_cache[pos]);
376 if ((ent->len < len) &&
377 !strncmp(active_cache[pos]->name,
378 ent->name, ent->len) &&
379 active_cache[pos]->name[ent->len] == '/')
380 killed = 1;
381 break;
382 }
383 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
384 /* If any of the leading directories in
385 * ent->name is registered in the cache,
386 * ent->name will be killed.
387 */
388 killed = 1;
389 break;
390 }
391 }
392 if (killed)
Linus Torvalds5be4efb2005-08-21 19:55:33393 show_dir_entry(tag_killed, dir[i]);
Junio C Hamano6ca45942005-05-13 00:17:54394 }
Linus Torvalds8695c8b2005-04-12 01:55:38395}
396
Linus Torvalds5be4efb2005-08-21 19:55:33397static void show_ce_entry(const char *tag, struct cache_entry *ce)
398{
399 int len = prefix_len;
400 int offset = prefix_offset;
401
402 if (len >= ce_namelen(ce))
403 die("git-ls-files: internal error - cache entry not superset of prefix");
404
Linus Torvalds56fc5102005-08-22 00:27:50405 if (pathspec && !match(pathspec, ce->name, len))
Linus Torvalds5be4efb2005-08-21 19:55:33406 return;
407
408 if (!show_stage)
409 printf("%s%s%c", tag, ce->name + offset, line_terminator);
410 else
411 printf("%s%06o %s %d\t%s%c",
412 tag,
413 ntohl(ce->ce_mode),
414 sha1_to_hex(ce->sha1),
415 ce_stage(ce),
416 ce->name + offset, line_terminator);
417}
418
Linus Torvalds8695c8b2005-04-12 01:55:38419static void show_files(void)
420{
421 int i;
422
423 /* For cached/deleted files we don't need to even do the readdir */
Junio C Hamano6ca45942005-05-13 00:17:54424 if (show_others || show_killed) {
Linus Torvalds5be4efb2005-08-21 19:55:33425 const char *path = ".", *base = "";
426 int baselen = prefix_len;
427
428 if (baselen)
429 path = base = prefix;
430 read_directory(path, base, baselen);
Junio C Hamano6ca45942005-05-13 00:17:54431 qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
432 if (show_others)
433 for (i = 0; i < nr_dir; i++)
Linus Torvalds5be4efb2005-08-21 19:55:33434 show_dir_entry(tag_other, dir[i]);
Junio C Hamano6ca45942005-05-13 00:17:54435 if (show_killed)
436 show_killed_files();
Linus Torvalds8695c8b2005-04-12 01:55:38437 }
Junio C Hamanoaee46192005-04-16 15:33:23438 if (show_cached | show_stage) {
Linus Torvalds8695c8b2005-04-12 01:55:38439 for (i = 0; i < active_nr; i++) {
440 struct cache_entry *ce = active_cache[i];
Nicolas Pitre9ff768e2005-04-28 18:44:04441 if (excluded(ce->name) != show_ignored)
442 continue;
Linus Torvaldseec8c632005-04-16 19:43:32443 if (show_unmerged && !ce_stage(ce))
444 continue;
Linus Torvalds5be4efb2005-08-21 19:55:33445 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
Linus Torvalds8695c8b2005-04-12 01:55:38446 }
447 }
Junio C Hamanob0391892005-09-19 22:11:15448 if (show_deleted | show_modified) {
Linus Torvalds8695c8b2005-04-12 01:55:38449 for (i = 0; i < active_nr; i++) {
450 struct cache_entry *ce = active_cache[i];
451 struct stat st;
Junio C Hamanob0391892005-09-19 22:11:15452 int err;
Nicolas Pitre9ff768e2005-04-28 18:44:04453 if (excluded(ce->name) != show_ignored)
454 continue;
Junio C Hamanob0391892005-09-19 22:11:15455 err = lstat(ce->name, &st);
456 if (show_deleted && err)
457 show_ce_entry(tag_removed, ce);
458 if (show_modified && ce_modified(ce, &st))
459 show_ce_entry(tag_modified, ce);
Linus Torvalds8695c8b2005-04-12 01:55:38460 }
461 }
Linus Torvalds8695c8b2005-04-12 01:55:38462}
463
Linus Torvalds5be4efb2005-08-21 19:55:33464/*
465 * Prune the index to only contain stuff starting with "prefix"
466 */
467static void prune_cache(void)
468{
469 int pos = cache_name_pos(prefix, prefix_len);
470 unsigned int first, last;
471
472 if (pos < 0)
473 pos = -pos-1;
474 active_cache += pos;
475 active_nr -= pos;
476 first = 0;
477 last = active_nr;
478 while (last > first) {
479 int next = (last + first) >> 1;
480 struct cache_entry *ce = active_cache[next];
481 if (!strncmp(ce->name, prefix, prefix_len)) {
482 first = next+1;
483 continue;
484 }
485 last = next;
486 }
487 active_nr = last;
488}
489
Linus Torvalds56fc5102005-08-22 00:27:50490static void verify_pathspec(void)
Linus Torvalds5be4efb2005-08-21 19:55:33491{
Linus Torvalds56fc5102005-08-22 00:27:50492 const char **p, *n, *prev;
493 char *real_prefix;
494 unsigned long max;
Linus Torvalds5be4efb2005-08-21 19:55:33495
Linus Torvalds56fc5102005-08-22 00:27:50496 prev = NULL;
497 max = PATH_MAX;
498 for (p = pathspec; (n = *p) != NULL; p++) {
499 int i, len = 0;
500 for (i = 0; i < max; i++) {
501 char c = n[i];
502 if (prev && prev[i] != c)
503 break;
Linus Torvalds56906142005-08-24 00:14:13504 if (!c || c == '*' || c == '?')
Linus Torvalds56fc5102005-08-22 00:27:50505 break;
506 if (c == '/')
507 len = i+1;
508 }
509 prev = n;
510 if (len < max) {
511 max = len;
512 if (!max)
513 break;
514 }
Linus Torvalds5be4efb2005-08-21 19:55:33515 }
Linus Torvalds56fc5102005-08-22 00:27:50516
517 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
518 die("git-ls-files: cannot generate relative filenames containing '..'");
519
520 real_prefix = NULL;
521 prefix_len = max;
522 if (max) {
523 real_prefix = xmalloc(max + 1);
524 memcpy(real_prefix, prev, max);
525 real_prefix[max] = 0;
Linus Torvalds5be4efb2005-08-21 19:55:33526 }
Linus Torvalds56fc5102005-08-22 00:27:50527 prefix = real_prefix;
Linus Torvalds5be4efb2005-08-21 19:55:33528}
529
Petr Baudis4d1f1192005-07-29 09:01:26530static const char ls_files_usage[] =
Junio C Hamanob0391892005-09-19 22:11:15531 "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
Junio C Hamanof87f9492005-07-24 22:26:09532 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
Fredrik Kuivinen500b97e2005-10-02 15:33:38533 "[ --exclude-per-directory=<filename> ] [--] [<file>]*";
Nicolas Pitrecf9a1132005-04-28 22:06:25534
Junio C Hamano6b5ee132005-09-21 07:00:47535int main(int argc, const char **argv)
Linus Torvalds8695c8b2005-04-12 01:55:38536{
537 int i;
Junio C Hamanofee88252005-07-29 06:32:20538 int exc_given = 0;
Linus Torvalds8695c8b2005-04-12 01:55:38539
Linus Torvalds5be4efb2005-08-21 19:55:33540 prefix = setup_git_directory();
541 if (prefix)
Linus Torvalds56fc5102005-08-22 00:27:50542 prefix_offset = strlen(prefix);
Linus Torvalds5be4efb2005-08-21 19:55:33543
Linus Torvalds8695c8b2005-04-12 01:55:38544 for (i = 1; i < argc; i++) {
Junio C Hamano6b5ee132005-09-21 07:00:47545 const char *arg = argv[i];
Linus Torvalds8695c8b2005-04-12 01:55:38546
Fredrik Kuivinen500b97e2005-10-02 15:33:38547 if (!strcmp(arg, "--")) {
548 i++;
549 break;
550 }
Junio C Hamanob83c8342005-04-15 18:11:01551 if (!strcmp(arg, "-z")) {
552 line_terminator = 0;
Linus Torvalds5be4efb2005-08-21 19:55:33553 continue;
554 }
555 if (!strcmp(arg, "-t")) {
Petr Baudis20d37ef2005-04-22 02:47:08556 tag_cached = "H ";
557 tag_unmerged = "M ";
558 tag_removed = "R ";
Junio C Hamanob0391892005-09-19 22:11:15559 tag_modified = "C ";
Petr Baudis20d37ef2005-04-22 02:47:08560 tag_other = "? ";
Junio C Hamano6ca45942005-05-13 00:17:54561 tag_killed = "K ";
Linus Torvalds5be4efb2005-08-21 19:55:33562 continue;
563 }
564 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
Linus Torvalds8695c8b2005-04-12 01:55:38565 show_cached = 1;
Linus Torvalds5be4efb2005-08-21 19:55:33566 continue;
567 }
568 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
Linus Torvalds8695c8b2005-04-12 01:55:38569 show_deleted = 1;
Linus Torvalds5be4efb2005-08-21 19:55:33570 continue;
571 }
Junio C Hamanob0391892005-09-19 22:11:15572 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
573 show_modified = 1;
574 continue;
575 }
Linus Torvalds5be4efb2005-08-21 19:55:33576 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
Linus Torvalds8695c8b2005-04-12 01:55:38577 show_others = 1;
Linus Torvalds5be4efb2005-08-21 19:55:33578 continue;
579 }
580 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
Linus Torvalds8695c8b2005-04-12 01:55:38581 show_ignored = 1;
Linus Torvalds5be4efb2005-08-21 19:55:33582 continue;
583 }
584 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
Junio C Hamanoaee46192005-04-16 15:33:23585 show_stage = 1;
Linus Torvalds5be4efb2005-08-21 19:55:33586 continue;
587 }
588 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
Junio C Hamano6ca45942005-05-13 00:17:54589 show_killed = 1;
Linus Torvalds5be4efb2005-08-21 19:55:33590 continue;
591 }
592 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
Petr Baudis20d37ef2005-04-22 02:47:08593 /* There's no point in showing unmerged unless
594 * you also show the stage information.
595 */
Linus Torvaldseec8c632005-04-16 19:43:32596 show_stage = 1;
597 show_unmerged = 1;
Linus Torvalds5be4efb2005-08-21 19:55:33598 continue;
599 }
600 if (!strcmp(arg, "-x") && i+1 < argc) {
Junio C Hamanofee88252005-07-29 06:32:20601 exc_given = 1;
602 add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]);
Linus Torvalds5be4efb2005-08-21 19:55:33603 continue;
604 }
605 if (!strncmp(arg, "--exclude=", 10)) {
Junio C Hamanofee88252005-07-29 06:32:20606 exc_given = 1;
607 add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]);
Linus Torvalds5be4efb2005-08-21 19:55:33608 continue;
609 }
610 if (!strcmp(arg, "-X") && i+1 < argc) {
Junio C Hamanofee88252005-07-29 06:32:20611 exc_given = 1;
Nicolas Pitre9ff768e2005-04-28 18:44:04612 add_excludes_from_file(argv[++i]);
Linus Torvalds5be4efb2005-08-21 19:55:33613 continue;
614 }
615 if (!strncmp(arg, "--exclude-from=", 15)) {
Junio C Hamanofee88252005-07-29 06:32:20616 exc_given = 1;
Nicolas Pitre9ff768e2005-04-28 18:44:04617 add_excludes_from_file(arg+15);
Linus Torvalds5be4efb2005-08-21 19:55:33618 continue;
619 }
620 if (!strncmp(arg, "--exclude-per-directory=", 24)) {
Junio C Hamanofee88252005-07-29 06:32:20621 exc_given = 1;
Junio C Hamanof87f9492005-07-24 22:26:09622 exclude_per_dir = arg + 24;
Linus Torvalds5be4efb2005-08-21 19:55:33623 continue;
624 }
625 if (!strcmp(arg, "--full-name")) {
626 prefix_offset = 0;
627 continue;
628 }
Linus Torvalds56fc5102005-08-22 00:27:50629 if (*arg == '-')
Nicolas Pitre17710392005-04-30 20:59:38630 usage(ls_files_usage);
Linus Torvalds56fc5102005-08-22 00:27:50631 break;
Nicolas Pitre9ff768e2005-04-28 18:44:04632 }
633
Linus Torvalds56fc5102005-08-22 00:27:50634 pathspec = get_pathspec(prefix, argv + i);
635
636 /* Verify that the pathspec matches the prefix */
637 if (pathspec)
638 verify_pathspec();
Linus Torvalds5be4efb2005-08-21 19:55:33639
Junio C Hamanofee88252005-07-29 06:32:20640 if (show_ignored && !exc_given) {
Petr Baudis20d37ef2005-04-22 02:47:08641 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
642 argv[0]);
Nicolas Pitre9ff768e2005-04-28 18:44:04643 exit(1);
Linus Torvalds8695c8b2005-04-12 01:55:38644 }
645
646 /* With no flags, we default to showing the cached files */
Junio C Hamanob0391892005-09-19 22:11:15647 if (!(show_stage | show_deleted | show_others | show_unmerged |
648 show_killed | show_modified))
Linus Torvalds8695c8b2005-04-12 01:55:38649 show_cached = 1;
650
651 read_cache();
Linus Torvalds5be4efb2005-08-21 19:55:33652 if (prefix)
653 prune_cache();
Linus Torvalds8695c8b2005-04-12 01:55:38654 show_files();
655 return 0;
656}