🌐 AI搜索 & 代理 主页
blob: db2288aeee61bf64f30fdcc4b11e91c0cd3038a0 [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"
Junio C Hamano22ddf712005-10-15 04:56:4612#include "quote.h"
Linus Torvalds8695c8b2005-04-12 01:55:3813
14static int show_deleted = 0;
15static int show_cached = 0;
16static int show_others = 0;
17static int show_ignored = 0;
Junio C Hamanoaee46192005-04-16 15:33:2318static int show_stage = 0;
Linus Torvaldseec8c632005-04-16 19:43:3219static int show_unmerged = 0;
Junio C Hamanob0391892005-09-19 22:11:1520static int show_modified = 0;
Junio C Hamano6ca45942005-05-13 00:17:5421static int show_killed = 0;
Junio C Hamanob83c8342005-04-15 18:11:0122static int line_terminator = '\n';
Linus Torvalds8695c8b2005-04-12 01:55:3823
Linus Torvalds5be4efb2005-08-21 19:55:3324static int prefix_len = 0, prefix_offset = 0;
25static const char *prefix = NULL;
Linus Torvalds56fc5102005-08-22 00:27:5026static const char **pathspec = NULL;
Linus Torvalds5be4efb2005-08-21 19:55:3327
Petr Baudis20d37ef2005-04-22 02:47:0828static const char *tag_cached = "";
29static const char *tag_unmerged = "";
30static const char *tag_removed = "";
31static const char *tag_other = "";
Junio C Hamano6ca45942005-05-13 00:17:5432static const char *tag_killed = "";
Junio C Hamanob0391892005-09-19 22:11:1533static const char *tag_modified = "";
Petr Baudis20d37ef2005-04-22 02:47:0834
Junio C Hamano6b5ee132005-09-21 07:00:4735static const char *exclude_per_dir = NULL;
Nicolas Pitre9ff768e2005-04-28 18:44:0436
Junio C Hamanofee88252005-07-29 06:32:2037/* We maintain three exclude pattern lists:
38 * EXC_CMDL lists patterns explicitly given on the command line.
39 * EXC_DIRS lists patterns obtained from per-directory ignore files.
40 * EXC_FILE lists patterns from fallback ignore files.
41 */
42#define EXC_CMDL 0
43#define EXC_DIRS 1
44#define EXC_FILE 2
45static struct exclude_list {
46 int nr;
47 int alloc;
48 struct exclude {
49 const char *pattern;
50 const char *base;
51 int baselen;
52 } **excludes;
53} exclude_list[3];
54
55static void add_exclude(const char *string, const char *base,
56 int baselen, struct exclude_list *which)
Nicolas Pitre9ff768e2005-04-28 18:44:0457{
Junio C Hamanof87f9492005-07-24 22:26:0958 struct exclude *x = xmalloc(sizeof (*x));
59
60 x->pattern = string;
61 x->base = base;
62 x->baselen = baselen;
Junio C Hamanofee88252005-07-29 06:32:2063 if (which->nr == which->alloc) {
64 which->alloc = alloc_nr(which->alloc);
65 which->excludes = realloc(which->excludes,
66 which->alloc * sizeof(x));
Nicolas Pitre9ff768e2005-04-28 18:44:0467 }
Junio C Hamanofee88252005-07-29 06:32:2068 which->excludes[which->nr++] = x;
Nicolas Pitre9ff768e2005-04-28 18:44:0469}
70
Junio C Hamanof87f9492005-07-24 22:26:0971static int add_excludes_from_file_1(const char *fname,
Junio C Hamanofee88252005-07-29 06:32:2072 const char *base,
73 int baselen,
74 struct exclude_list *which)
Nicolas Pitre9ff768e2005-04-28 18:44:0475{
76 int fd, i;
77 long size;
78 char *buf, *entry;
79
80 fd = open(fname, O_RDONLY);
81 if (fd < 0)
82 goto err;
83 size = lseek(fd, 0, SEEK_END);
84 if (size < 0)
85 goto err;
86 lseek(fd, 0, SEEK_SET);
87 if (size == 0) {
88 close(fd);
Junio C Hamanof87f9492005-07-24 22:26:0989 return 0;
Nicolas Pitre9ff768e2005-04-28 18:44:0490 }
91 buf = xmalloc(size);
92 if (read(fd, buf, size) != size)
93 goto err;
94 close(fd);
95
96 entry = buf;
97 for (i = 0; i < size; i++) {
98 if (buf[i] == '\n') {
Junio C Hamanof87f9492005-07-24 22:26:0999 if (entry != buf + i && entry[0] != '#') {
Alex Riesend317e432005-11-02 13:05:45100 buf[i - (i && buf[i-1] == '\r')] = 0;
Junio C Hamanofee88252005-07-29 06:32:20101 add_exclude(entry, base, baselen, which);
Nicolas Pitre9ff768e2005-04-28 18:44:04102 }
103 entry = buf + i + 1;
104 }
105 }
Junio C Hamanof87f9492005-07-24 22:26:09106 return 0;
Nicolas Pitre9ff768e2005-04-28 18:44:04107
Junio C Hamanof87f9492005-07-24 22:26:09108 err:
109 if (0 <= fd)
110 close(fd);
111 return -1;
112}
113
114static void add_excludes_from_file(const char *fname)
115{
Junio C Hamanofee88252005-07-29 06:32:20116 if (add_excludes_from_file_1(fname, "", 0,
117 &exclude_list[EXC_FILE]) < 0)
Junio C Hamanof87f9492005-07-24 22:26:09118 die("cannot use %s as an exclude file", fname);
119}
120
121static int push_exclude_per_directory(const char *base, int baselen)
122{
123 char exclude_file[PATH_MAX];
Junio C Hamanofee88252005-07-29 06:32:20124 struct exclude_list *el = &exclude_list[EXC_DIRS];
125 int current_nr = el->nr;
Junio C Hamanof87f9492005-07-24 22:26:09126
127 if (exclude_per_dir) {
128 memcpy(exclude_file, base, baselen);
129 strcpy(exclude_file + baselen, exclude_per_dir);
Junio C Hamanofee88252005-07-29 06:32:20130 add_excludes_from_file_1(exclude_file, base, baselen, el);
Junio C Hamanof87f9492005-07-24 22:26:09131 }
132 return current_nr;
133}
134
135static void pop_exclude_per_directory(int stk)
136{
Junio C Hamanofee88252005-07-29 06:32:20137 struct exclude_list *el = &exclude_list[EXC_DIRS];
138
139 while (stk < el->nr)
140 free(el->excludes[--el->nr]);
Nicolas Pitre9ff768e2005-04-28 18:44:04141}
142
Junio C Hamanofee88252005-07-29 06:32:20143/* Scan the list and let the last match determines the fate.
144 * Return 1 for exclude, 0 for include and -1 for undecided.
145 */
146static int excluded_1(const char *pathname,
147 int pathlen,
148 struct exclude_list *el)
Nicolas Pitre9ff768e2005-04-28 18:44:04149{
150 int i;
Junio C Hamanof87f9492005-07-24 22:26:09151
Junio C Hamanofee88252005-07-29 06:32:20152 if (el->nr) {
153 for (i = el->nr - 1; 0 <= i; i--) {
154 struct exclude *x = el->excludes[i];
Junio C Hamanof87f9492005-07-24 22:26:09155 const char *exclude = x->pattern;
156 int to_exclude = 1;
157
158 if (*exclude == '!') {
159 to_exclude = 0;
160 exclude++;
161 }
162
163 if (!strchr(exclude, '/')) {
164 /* match basename */
165 const char *basename = strrchr(pathname, '/');
166 basename = (basename) ? basename+1 : pathname;
167 if (fnmatch(exclude, basename, 0) == 0)
168 return to_exclude;
169 }
170 else {
171 /* match with FNM_PATHNAME:
172 * exclude has base (baselen long) inplicitly
173 * in front of it.
174 */
175 int baselen = x->baselen;
176 if (*exclude == '/')
177 exclude++;
178
179 if (pathlen < baselen ||
180 (baselen && pathname[baselen-1] != '/') ||
181 strncmp(pathname, x->base, baselen))
182 continue;
183
184 if (fnmatch(exclude, pathname+baselen,
185 FNM_PATHNAME) == 0)
186 return to_exclude;
187 }
188 }
Nicolas Pitre9ff768e2005-04-28 18:44:04189 }
Junio C Hamanofee88252005-07-29 06:32:20190 return -1; /* undecided */
191}
192
193static int excluded(const char *pathname)
194{
195 int pathlen = strlen(pathname);
196 int st;
197
198 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
199 switch (excluded_1(pathname, pathlen, &exclude_list[st])) {
200 case 0:
201 return 0;
202 case 1:
203 return 1;
204 }
205 }
Nicolas Pitre9ff768e2005-04-28 18:44:04206 return 0;
207}
208
Junio C Hamano6ca45942005-05-13 00:17:54209struct nond_on_fs {
210 int len;
Junio C Hamano2c046622005-08-29 19:41:03211 char name[0];
Junio C Hamano6ca45942005-05-13 00:17:54212};
213
214static struct nond_on_fs **dir;
Linus Torvalds8695c8b2005-04-12 01:55:38215static int nr_dir;
216static int dir_alloc;
217
218static void add_name(const char *pathname, int len)
219{
Junio C Hamano6ca45942005-05-13 00:17:54220 struct nond_on_fs *ent;
Linus Torvalds8695c8b2005-04-12 01:55:38221
222 if (cache_name_pos(pathname, len) >= 0)
223 return;
224
225 if (nr_dir == dir_alloc) {
226 dir_alloc = alloc_nr(dir_alloc);
Junio C Hamano6ca45942005-05-13 00:17:54227 dir = xrealloc(dir, dir_alloc*sizeof(ent));
Linus Torvalds8695c8b2005-04-12 01:55:38228 }
Junio C Hamano6ca45942005-05-13 00:17:54229 ent = xmalloc(sizeof(*ent) + len + 1);
230 ent->len = len;
231 memcpy(ent->name, pathname, len);
Linus Torvalds5be4efb2005-08-21 19:55:33232 ent->name[len] = 0;
Junio C Hamano6ca45942005-05-13 00:17:54233 dir[nr_dir++] = ent;
Linus Torvalds8695c8b2005-04-12 01:55:38234}
235
236/*
237 * Read a directory tree. We currently ignore anything but
Junio C Hamanoa15c1c62005-05-13 00:16:04238 * directories, regular files and symlinks. That's because git
239 * doesn't handle them at all yet. Maybe that will change some
240 * day.
Linus Torvalds8695c8b2005-04-12 01:55:38241 *
Junio C Hamanof87f9492005-07-24 22:26:09242 * Also, we ignore the name ".git" (even if it is not a directory).
Ingo Molnaraebb2672005-04-12 18:36:26243 * That likely will not change.
Linus Torvalds8695c8b2005-04-12 01:55:38244 */
245static void read_directory(const char *path, const char *base, int baselen)
246{
247 DIR *dir = opendir(path);
248
249 if (dir) {
Junio C Hamanof87f9492005-07-24 22:26:09250 int exclude_stk;
Linus Torvalds8695c8b2005-04-12 01:55:38251 struct dirent *de;
252 char fullname[MAXPATHLEN + 1];
253 memcpy(fullname, base, baselen);
254
Junio C Hamanof87f9492005-07-24 22:26:09255 exclude_stk = push_exclude_per_directory(base, baselen);
256
Linus Torvalds8695c8b2005-04-12 01:55:38257 while ((de = readdir(dir)) != NULL) {
258 int len;
259
Junio C Hamanoc4ee2952005-05-25 01:20:08260 if ((de->d_name[0] == '.') &&
261 (de->d_name[1] == 0 ||
262 !strcmp(de->d_name + 1, ".") ||
263 !strcmp(de->d_name + 1, "git")))
Linus Torvalds8695c8b2005-04-12 01:55:38264 continue;
265 len = strlen(de->d_name);
266 memcpy(fullname + baselen, de->d_name, len+1);
Junio C Hamanof87f9492005-07-24 22:26:09267 if (excluded(fullname) != show_ignored)
268 continue;
Linus Torvalds8695c8b2005-04-12 01:55:38269
Edgar Toernigb6829692005-04-30 16:51:03270 switch (DTYPE(de)) {
Linus Torvalds8695c8b2005-04-12 01:55:38271 struct stat st;
272 default:
273 continue;
274 case DT_UNKNOWN:
275 if (lstat(fullname, &st))
276 continue;
Junio C Hamanoa15c1c62005-05-13 00:16:04277 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
Linus Torvalds8695c8b2005-04-12 01:55:38278 break;
279 if (!S_ISDIR(st.st_mode))
280 continue;
281 /* fallthrough */
282 case DT_DIR:
283 memcpy(fullname + baselen + len, "/", 2);
Petr Baudis20d37ef2005-04-22 02:47:08284 read_directory(fullname, fullname,
285 baselen + len + 1);
Linus Torvalds8695c8b2005-04-12 01:55:38286 continue;
287 case DT_REG:
Junio C Hamanoa15c1c62005-05-13 00:16:04288 case DT_LNK:
Linus Torvalds8695c8b2005-04-12 01:55:38289 break;
290 }
291 add_name(fullname, baselen + len);
292 }
293 closedir(dir);
Junio C Hamanof87f9492005-07-24 22:26:09294
295 pop_exclude_per_directory(exclude_stk);
Linus Torvalds8695c8b2005-04-12 01:55:38296 }
297}
298
299static int cmp_name(const void *p1, const void *p2)
300{
Junio C Hamano6ca45942005-05-13 00:17:54301 const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1;
302 const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2;
Linus Torvalds8695c8b2005-04-12 01:55:38303
Junio C Hamano6ca45942005-05-13 00:17:54304 return cache_name_compare(e1->name, e1->len,
305 e2->name, e2->len);
306}
307
Linus Torvalds56fc5102005-08-22 00:27:50308/*
309 * Match a pathspec against a filename. The first "len" characters
310 * are the common prefix
311 */
312static int match(const char **spec, const char *filename, int len)
313{
314 const char *m;
315
316 while ((m = *spec++) != NULL) {
317 int matchlen = strlen(m + len);
318
319 if (!matchlen)
320 return 1;
321 if (!strncmp(m + len, filename + len, matchlen)) {
322 if (m[len + matchlen - 1] == '/')
323 return 1;
324 switch (filename[len + matchlen]) {
325 case '/': case '\0':
326 return 1;
327 }
328 }
329 if (!fnmatch(m + len, filename + len, 0))
330 return 1;
331 }
332 return 0;
333}
334
Linus Torvalds5be4efb2005-08-21 19:55:33335static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
336{
337 int len = prefix_len;
338 int offset = prefix_offset;
339
340 if (len >= ent->len)
341 die("git-ls-files: internal error - directory entry not superset of prefix");
342
Linus Torvalds56fc5102005-08-22 00:27:50343 if (pathspec && !match(pathspec, ent->name, len))
Linus Torvalds5be4efb2005-08-21 19:55:33344 return;
345
Junio C Hamano22ddf712005-10-15 04:56:46346 fputs(tag, stdout);
347 write_name_quoted("", ent->name + offset, line_terminator, stdout);
348 putchar(line_terminator);
Linus Torvalds5be4efb2005-08-21 19:55:33349}
350
Junio C Hamanofcbc3082005-11-07 01:26:31351static void show_other_files(void)
352{
353 int i;
354 for (i = 0; i < nr_dir; i++) {
355 /* We should not have a matching entry, but we
356 * may have an unmerged entry for this path.
357 */
358 struct nond_on_fs *ent = dir[i];
359 int pos = cache_name_pos(ent->name, ent->len);
360 struct cache_entry *ce;
361 if (0 <= pos)
362 die("bug in show-other-files");
363 pos = -pos - 1;
364 if (pos < active_nr) {
365 ce = active_cache[pos];
366 if (ce_namelen(ce) == ent->len &&
367 !memcmp(ce->name, ent->name, ent->len))
368 continue; /* Yup, this one exists unmerged */
369 }
370 show_dir_entry(tag_other, ent);
371 }
372}
373
Linus Torvaldse99d59f2005-05-20 18:46:10374static void show_killed_files(void)
Junio C Hamano6ca45942005-05-13 00:17:54375{
376 int i;
377 for (i = 0; i < nr_dir; i++) {
378 struct nond_on_fs *ent = dir[i];
379 char *cp, *sp;
380 int pos, len, killed = 0;
381
382 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
383 sp = strchr(cp, '/');
384 if (!sp) {
385 /* If ent->name is prefix of an entry in the
386 * cache, it will be killed.
387 */
388 pos = cache_name_pos(ent->name, ent->len);
389 if (0 <= pos)
390 die("bug in show-killed-files");
391 pos = -pos - 1;
392 while (pos < active_nr &&
393 ce_stage(active_cache[pos]))
394 pos++; /* skip unmerged */
395 if (active_nr <= pos)
396 break;
397 /* pos points at a name immediately after
398 * ent->name in the cache. Does it expect
399 * ent->name to be a directory?
400 */
401 len = ce_namelen(active_cache[pos]);
402 if ((ent->len < len) &&
403 !strncmp(active_cache[pos]->name,
404 ent->name, ent->len) &&
405 active_cache[pos]->name[ent->len] == '/')
406 killed = 1;
407 break;
408 }
409 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
410 /* If any of the leading directories in
411 * ent->name is registered in the cache,
412 * ent->name will be killed.
413 */
414 killed = 1;
415 break;
416 }
417 }
418 if (killed)
Linus Torvalds5be4efb2005-08-21 19:55:33419 show_dir_entry(tag_killed, dir[i]);
Junio C Hamano6ca45942005-05-13 00:17:54420 }
Linus Torvalds8695c8b2005-04-12 01:55:38421}
422
Linus Torvalds5be4efb2005-08-21 19:55:33423static void show_ce_entry(const char *tag, struct cache_entry *ce)
424{
425 int len = prefix_len;
426 int offset = prefix_offset;
427
428 if (len >= ce_namelen(ce))
429 die("git-ls-files: internal error - cache entry not superset of prefix");
430
Linus Torvalds56fc5102005-08-22 00:27:50431 if (pathspec && !match(pathspec, ce->name, len))
Linus Torvalds5be4efb2005-08-21 19:55:33432 return;
433
Junio C Hamano22ddf712005-10-15 04:56:46434 if (!show_stage) {
435 fputs(tag, stdout);
436 write_name_quoted("", ce->name + offset, line_terminator, stdout);
437 putchar(line_terminator);
438 }
439 else {
440 printf("%s%06o %s %d\t",
Linus Torvalds5be4efb2005-08-21 19:55:33441 tag,
442 ntohl(ce->ce_mode),
443 sha1_to_hex(ce->sha1),
Junio C Hamano22ddf712005-10-15 04:56:46444 ce_stage(ce));
445 write_name_quoted("", ce->name + offset, line_terminator, stdout);
446 putchar(line_terminator);
447 }
Linus Torvalds5be4efb2005-08-21 19:55:33448}
449
Linus Torvalds8695c8b2005-04-12 01:55:38450static void show_files(void)
451{
452 int i;
453
454 /* For cached/deleted files we don't need to even do the readdir */
Junio C Hamano6ca45942005-05-13 00:17:54455 if (show_others || show_killed) {
Linus Torvalds5be4efb2005-08-21 19:55:33456 const char *path = ".", *base = "";
457 int baselen = prefix_len;
458
459 if (baselen)
460 path = base = prefix;
461 read_directory(path, base, baselen);
Junio C Hamano6ca45942005-05-13 00:17:54462 qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
463 if (show_others)
Junio C Hamanofcbc3082005-11-07 01:26:31464 show_other_files();
Junio C Hamano6ca45942005-05-13 00:17:54465 if (show_killed)
466 show_killed_files();
Linus Torvalds8695c8b2005-04-12 01:55:38467 }
Junio C Hamanoaee46192005-04-16 15:33:23468 if (show_cached | show_stage) {
Linus Torvalds8695c8b2005-04-12 01:55:38469 for (i = 0; i < active_nr; i++) {
470 struct cache_entry *ce = active_cache[i];
Nicolas Pitre9ff768e2005-04-28 18:44:04471 if (excluded(ce->name) != show_ignored)
472 continue;
Linus Torvaldseec8c632005-04-16 19:43:32473 if (show_unmerged && !ce_stage(ce))
474 continue;
Linus Torvalds5be4efb2005-08-21 19:55:33475 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
Linus Torvalds8695c8b2005-04-12 01:55:38476 }
477 }
Junio C Hamanob0391892005-09-19 22:11:15478 if (show_deleted | show_modified) {
Linus Torvalds8695c8b2005-04-12 01:55:38479 for (i = 0; i < active_nr; i++) {
480 struct cache_entry *ce = active_cache[i];
481 struct stat st;
Junio C Hamanob0391892005-09-19 22:11:15482 int err;
Nicolas Pitre9ff768e2005-04-28 18:44:04483 if (excluded(ce->name) != show_ignored)
484 continue;
Junio C Hamanob0391892005-09-19 22:11:15485 err = lstat(ce->name, &st);
486 if (show_deleted && err)
487 show_ce_entry(tag_removed, ce);
488 if (show_modified && ce_modified(ce, &st))
489 show_ce_entry(tag_modified, ce);
Linus Torvalds8695c8b2005-04-12 01:55:38490 }
491 }
Linus Torvalds8695c8b2005-04-12 01:55:38492}
493
Linus Torvalds5be4efb2005-08-21 19:55:33494/*
495 * Prune the index to only contain stuff starting with "prefix"
496 */
497static void prune_cache(void)
498{
499 int pos = cache_name_pos(prefix, prefix_len);
500 unsigned int first, last;
501
502 if (pos < 0)
503 pos = -pos-1;
504 active_cache += pos;
505 active_nr -= pos;
506 first = 0;
507 last = active_nr;
508 while (last > first) {
509 int next = (last + first) >> 1;
510 struct cache_entry *ce = active_cache[next];
511 if (!strncmp(ce->name, prefix, prefix_len)) {
512 first = next+1;
513 continue;
514 }
515 last = next;
516 }
517 active_nr = last;
518}
519
Linus Torvalds56fc5102005-08-22 00:27:50520static void verify_pathspec(void)
Linus Torvalds5be4efb2005-08-21 19:55:33521{
Linus Torvalds56fc5102005-08-22 00:27:50522 const char **p, *n, *prev;
523 char *real_prefix;
524 unsigned long max;
Linus Torvalds5be4efb2005-08-21 19:55:33525
Linus Torvalds56fc5102005-08-22 00:27:50526 prev = NULL;
527 max = PATH_MAX;
528 for (p = pathspec; (n = *p) != NULL; p++) {
529 int i, len = 0;
530 for (i = 0; i < max; i++) {
531 char c = n[i];
532 if (prev && prev[i] != c)
533 break;
Linus Torvalds56906142005-08-24 00:14:13534 if (!c || c == '*' || c == '?')
Linus Torvalds56fc5102005-08-22 00:27:50535 break;
536 if (c == '/')
537 len = i+1;
538 }
539 prev = n;
540 if (len < max) {
541 max = len;
542 if (!max)
543 break;
544 }
Linus Torvalds5be4efb2005-08-21 19:55:33545 }
Linus Torvalds56fc5102005-08-22 00:27:50546
547 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
548 die("git-ls-files: cannot generate relative filenames containing '..'");
549
550 real_prefix = NULL;
551 prefix_len = max;
552 if (max) {
553 real_prefix = xmalloc(max + 1);
554 memcpy(real_prefix, prev, max);
555 real_prefix[max] = 0;
Linus Torvalds5be4efb2005-08-21 19:55:33556 }
Linus Torvalds56fc5102005-08-22 00:27:50557 prefix = real_prefix;
Linus Torvalds5be4efb2005-08-21 19:55:33558}
559
Petr Baudis4d1f1192005-07-29 09:01:26560static const char ls_files_usage[] =
Junio C Hamanob0391892005-09-19 22:11:15561 "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
Junio C Hamanof87f9492005-07-24 22:26:09562 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
Fredrik Kuivinen500b97e2005-10-02 15:33:38563 "[ --exclude-per-directory=<filename> ] [--] [<file>]*";
Nicolas Pitrecf9a1132005-04-28 22:06:25564
Junio C Hamano6b5ee132005-09-21 07:00:47565int main(int argc, const char **argv)
Linus Torvalds8695c8b2005-04-12 01:55:38566{
567 int i;
Junio C Hamanofee88252005-07-29 06:32:20568 int exc_given = 0;
Linus Torvalds8695c8b2005-04-12 01:55:38569
Linus Torvalds5be4efb2005-08-21 19:55:33570 prefix = setup_git_directory();
571 if (prefix)
Linus Torvalds56fc5102005-08-22 00:27:50572 prefix_offset = strlen(prefix);
Alex Riesen39b4ac92005-11-08 08:23:37573 git_config(git_default_config);
Linus Torvalds5be4efb2005-08-21 19:55:33574
Linus Torvalds8695c8b2005-04-12 01:55:38575 for (i = 1; i < argc; i++) {
Junio C Hamano6b5ee132005-09-21 07:00:47576 const char *arg = argv[i];
Linus Torvalds8695c8b2005-04-12 01:55:38577
Fredrik Kuivinen500b97e2005-10-02 15:33:38578 if (!strcmp(arg, "--")) {
579 i++;
580 break;
581 }
Junio C Hamanob83c8342005-04-15 18:11:01582 if (!strcmp(arg, "-z")) {
583 line_terminator = 0;
Linus Torvalds5be4efb2005-08-21 19:55:33584 continue;
585 }
586 if (!strcmp(arg, "-t")) {
Petr Baudis20d37ef2005-04-22 02:47:08587 tag_cached = "H ";
588 tag_unmerged = "M ";
589 tag_removed = "R ";
Junio C Hamanob0391892005-09-19 22:11:15590 tag_modified = "C ";
Petr Baudis20d37ef2005-04-22 02:47:08591 tag_other = "? ";
Junio C Hamano6ca45942005-05-13 00:17:54592 tag_killed = "K ";
Linus Torvalds5be4efb2005-08-21 19:55:33593 continue;
594 }
595 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
Linus Torvalds8695c8b2005-04-12 01:55:38596 show_cached = 1;
Linus Torvalds5be4efb2005-08-21 19:55:33597 continue;
598 }
599 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
Linus Torvalds8695c8b2005-04-12 01:55:38600 show_deleted = 1;
Linus Torvalds5be4efb2005-08-21 19:55:33601 continue;
602 }
Junio C Hamanob0391892005-09-19 22:11:15603 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
604 show_modified = 1;
605 continue;
606 }
Linus Torvalds5be4efb2005-08-21 19:55:33607 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
Linus Torvalds8695c8b2005-04-12 01:55:38608 show_others = 1;
Linus Torvalds5be4efb2005-08-21 19:55:33609 continue;
610 }
611 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
Linus Torvalds8695c8b2005-04-12 01:55:38612 show_ignored = 1;
Linus Torvalds5be4efb2005-08-21 19:55:33613 continue;
614 }
615 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
Junio C Hamanoaee46192005-04-16 15:33:23616 show_stage = 1;
Linus Torvalds5be4efb2005-08-21 19:55:33617 continue;
618 }
619 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
Junio C Hamano6ca45942005-05-13 00:17:54620 show_killed = 1;
Linus Torvalds5be4efb2005-08-21 19:55:33621 continue;
622 }
623 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
Petr Baudis20d37ef2005-04-22 02:47:08624 /* There's no point in showing unmerged unless
625 * you also show the stage information.
626 */
Linus Torvaldseec8c632005-04-16 19:43:32627 show_stage = 1;
628 show_unmerged = 1;
Linus Torvalds5be4efb2005-08-21 19:55:33629 continue;
630 }
631 if (!strcmp(arg, "-x") && i+1 < argc) {
Junio C Hamanofee88252005-07-29 06:32:20632 exc_given = 1;
633 add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]);
Linus Torvalds5be4efb2005-08-21 19:55:33634 continue;
635 }
636 if (!strncmp(arg, "--exclude=", 10)) {
Junio C Hamanofee88252005-07-29 06:32:20637 exc_given = 1;
638 add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]);
Linus Torvalds5be4efb2005-08-21 19:55:33639 continue;
640 }
641 if (!strcmp(arg, "-X") && i+1 < argc) {
Junio C Hamanofee88252005-07-29 06:32:20642 exc_given = 1;
Nicolas Pitre9ff768e2005-04-28 18:44:04643 add_excludes_from_file(argv[++i]);
Linus Torvalds5be4efb2005-08-21 19:55:33644 continue;
645 }
646 if (!strncmp(arg, "--exclude-from=", 15)) {
Junio C Hamanofee88252005-07-29 06:32:20647 exc_given = 1;
Nicolas Pitre9ff768e2005-04-28 18:44:04648 add_excludes_from_file(arg+15);
Linus Torvalds5be4efb2005-08-21 19:55:33649 continue;
650 }
651 if (!strncmp(arg, "--exclude-per-directory=", 24)) {
Junio C Hamanofee88252005-07-29 06:32:20652 exc_given = 1;
Junio C Hamanof87f9492005-07-24 22:26:09653 exclude_per_dir = arg + 24;
Linus Torvalds5be4efb2005-08-21 19:55:33654 continue;
655 }
656 if (!strcmp(arg, "--full-name")) {
657 prefix_offset = 0;
658 continue;
659 }
Linus Torvalds56fc5102005-08-22 00:27:50660 if (*arg == '-')
Nicolas Pitre17710392005-04-30 20:59:38661 usage(ls_files_usage);
Linus Torvalds56fc5102005-08-22 00:27:50662 break;
Nicolas Pitre9ff768e2005-04-28 18:44:04663 }
664
Linus Torvalds56fc5102005-08-22 00:27:50665 pathspec = get_pathspec(prefix, argv + i);
666
667 /* Verify that the pathspec matches the prefix */
668 if (pathspec)
669 verify_pathspec();
Linus Torvalds5be4efb2005-08-21 19:55:33670
Junio C Hamanofee88252005-07-29 06:32:20671 if (show_ignored && !exc_given) {
Petr Baudis20d37ef2005-04-22 02:47:08672 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
673 argv[0]);
Nicolas Pitre9ff768e2005-04-28 18:44:04674 exit(1);
Linus Torvalds8695c8b2005-04-12 01:55:38675 }
676
677 /* With no flags, we default to showing the cached files */
Junio C Hamanob0391892005-09-19 22:11:15678 if (!(show_stage | show_deleted | show_others | show_unmerged |
679 show_killed | show_modified))
Linus Torvalds8695c8b2005-04-12 01:55:38680 show_cached = 1;
681
682 read_cache();
Linus Torvalds5be4efb2005-08-21 19:55:33683 if (prefix)
684 prune_cache();
Linus Torvalds8695c8b2005-04-12 01:55:38685 show_files();
686 return 0;
687}