🌐 AI搜索 & 代理 主页
blob: fadfa01318e48c4f00dac88945022be2392c311c [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 Hamano6ca45942005-05-13 00:17:5419static int show_killed = 0;
Junio C Hamanob83c8342005-04-15 18:11:0120static int line_terminator = '\n';
Linus Torvalds8695c8b2005-04-12 01:55:3821
Petr Baudis20d37ef2005-04-22 02:47:0822static const char *tag_cached = "";
23static const char *tag_unmerged = "";
24static const char *tag_removed = "";
25static const char *tag_other = "";
Junio C Hamano6ca45942005-05-13 00:17:5426static const char *tag_killed = "";
Petr Baudis20d37ef2005-04-22 02:47:0827
Junio C Hamanof87f9492005-07-24 22:26:0928static char *exclude_per_dir = NULL;
Nicolas Pitre9ff768e2005-04-28 18:44:0429static int nr_excludes;
Nicolas Pitre9ff768e2005-04-28 18:44:0430static int excludes_alloc;
Junio C Hamanof87f9492005-07-24 22:26:0931static struct exclude {
32 const char *pattern;
33 const char *base;
34 int baselen;
35} **excludes;
Nicolas Pitre9ff768e2005-04-28 18:44:0436
Junio C Hamanof87f9492005-07-24 22:26:0937static void add_exclude(const char *string, const char *base, int baselen)
Nicolas Pitre9ff768e2005-04-28 18:44:0438{
Junio C Hamanof87f9492005-07-24 22:26:0939 struct exclude *x = xmalloc(sizeof (*x));
40
41 x->pattern = string;
42 x->base = base;
43 x->baselen = baselen;
Nicolas Pitre9ff768e2005-04-28 18:44:0444 if (nr_excludes == excludes_alloc) {
45 excludes_alloc = alloc_nr(excludes_alloc);
46 excludes = realloc(excludes, excludes_alloc*sizeof(char *));
47 }
Junio C Hamanof87f9492005-07-24 22:26:0948 excludes[nr_excludes++] = x;
Nicolas Pitre9ff768e2005-04-28 18:44:0449}
50
Junio C Hamanof87f9492005-07-24 22:26:0951static int add_excludes_from_file_1(const char *fname,
52 const char *base, int baselen)
Nicolas Pitre9ff768e2005-04-28 18:44:0453{
54 int fd, i;
55 long size;
56 char *buf, *entry;
57
58 fd = open(fname, O_RDONLY);
59 if (fd < 0)
60 goto err;
61 size = lseek(fd, 0, SEEK_END);
62 if (size < 0)
63 goto err;
64 lseek(fd, 0, SEEK_SET);
65 if (size == 0) {
66 close(fd);
Junio C Hamanof87f9492005-07-24 22:26:0967 return 0;
Nicolas Pitre9ff768e2005-04-28 18:44:0468 }
69 buf = xmalloc(size);
70 if (read(fd, buf, size) != size)
71 goto err;
72 close(fd);
73
74 entry = buf;
75 for (i = 0; i < size; i++) {
76 if (buf[i] == '\n') {
Junio C Hamanof87f9492005-07-24 22:26:0977 if (entry != buf + i && entry[0] != '#') {
Nicolas Pitre9ff768e2005-04-28 18:44:0478 buf[i] = 0;
Junio C Hamanof87f9492005-07-24 22:26:0979 add_exclude(entry, base, baselen);
Nicolas Pitre9ff768e2005-04-28 18:44:0480 }
81 entry = buf + i + 1;
82 }
83 }
Junio C Hamanof87f9492005-07-24 22:26:0984 return 0;
Nicolas Pitre9ff768e2005-04-28 18:44:0485
Junio C Hamanof87f9492005-07-24 22:26:0986 err:
87 if (0 <= fd)
88 close(fd);
89 return -1;
90}
91
92static void add_excludes_from_file(const char *fname)
93{
94 if (add_excludes_from_file_1(fname, "", 0) < 0)
95 die("cannot use %s as an exclude file", fname);
96}
97
98static int push_exclude_per_directory(const char *base, int baselen)
99{
100 char exclude_file[PATH_MAX];
101 int current_nr = nr_excludes;
102
103 if (exclude_per_dir) {
104 memcpy(exclude_file, base, baselen);
105 strcpy(exclude_file + baselen, exclude_per_dir);
106 add_excludes_from_file_1(exclude_file, base, baselen);
107 }
108 return current_nr;
109}
110
111static void pop_exclude_per_directory(int stk)
112{
113 while (stk < nr_excludes)
114 free(excludes[--nr_excludes]);
Nicolas Pitre9ff768e2005-04-28 18:44:04115}
116
117static int excluded(const char *pathname)
118{
119 int i;
Junio C Hamanof87f9492005-07-24 22:26:09120
Nicolas Pitre9ff768e2005-04-28 18:44:04121 if (nr_excludes) {
Junio C Hamanof87f9492005-07-24 22:26:09122 int pathlen = strlen(pathname);
123
124 for (i = 0; i < nr_excludes; i++) {
125 struct exclude *x = excludes[i];
126 const char *exclude = x->pattern;
127 int to_exclude = 1;
128
129 if (*exclude == '!') {
130 to_exclude = 0;
131 exclude++;
132 }
133
134 if (!strchr(exclude, '/')) {
135 /* match basename */
136 const char *basename = strrchr(pathname, '/');
137 basename = (basename) ? basename+1 : pathname;
138 if (fnmatch(exclude, basename, 0) == 0)
139 return to_exclude;
140 }
141 else {
142 /* match with FNM_PATHNAME:
143 * exclude has base (baselen long) inplicitly
144 * in front of it.
145 */
146 int baselen = x->baselen;
147 if (*exclude == '/')
148 exclude++;
149
150 if (pathlen < baselen ||
151 (baselen && pathname[baselen-1] != '/') ||
152 strncmp(pathname, x->base, baselen))
153 continue;
154
155 if (fnmatch(exclude, pathname+baselen,
156 FNM_PATHNAME) == 0)
157 return to_exclude;
158 }
159 }
Nicolas Pitre9ff768e2005-04-28 18:44:04160 }
161 return 0;
162}
163
Junio C Hamano6ca45942005-05-13 00:17:54164struct nond_on_fs {
165 int len;
166 char name[0];
167};
168
169static struct nond_on_fs **dir;
Linus Torvalds8695c8b2005-04-12 01:55:38170static int nr_dir;
171static int dir_alloc;
172
173static void add_name(const char *pathname, int len)
174{
Junio C Hamano6ca45942005-05-13 00:17:54175 struct nond_on_fs *ent;
Linus Torvalds8695c8b2005-04-12 01:55:38176
177 if (cache_name_pos(pathname, len) >= 0)
178 return;
179
180 if (nr_dir == dir_alloc) {
181 dir_alloc = alloc_nr(dir_alloc);
Junio C Hamano6ca45942005-05-13 00:17:54182 dir = xrealloc(dir, dir_alloc*sizeof(ent));
Linus Torvalds8695c8b2005-04-12 01:55:38183 }
Junio C Hamano6ca45942005-05-13 00:17:54184 ent = xmalloc(sizeof(*ent) + len + 1);
185 ent->len = len;
186 memcpy(ent->name, pathname, len);
187 dir[nr_dir++] = ent;
Linus Torvalds8695c8b2005-04-12 01:55:38188}
189
190/*
191 * Read a directory tree. We currently ignore anything but
Junio C Hamanoa15c1c62005-05-13 00:16:04192 * directories, regular files and symlinks. That's because git
193 * doesn't handle them at all yet. Maybe that will change some
194 * day.
Linus Torvalds8695c8b2005-04-12 01:55:38195 *
Junio C Hamanof87f9492005-07-24 22:26:09196 * Also, we ignore the name ".git" (even if it is not a directory).
Ingo Molnaraebb2672005-04-12 18:36:26197 * That likely will not change.
Linus Torvalds8695c8b2005-04-12 01:55:38198 */
199static void read_directory(const char *path, const char *base, int baselen)
200{
201 DIR *dir = opendir(path);
202
203 if (dir) {
Junio C Hamanof87f9492005-07-24 22:26:09204 int exclude_stk;
Linus Torvalds8695c8b2005-04-12 01:55:38205 struct dirent *de;
206 char fullname[MAXPATHLEN + 1];
207 memcpy(fullname, base, baselen);
208
Junio C Hamanof87f9492005-07-24 22:26:09209 exclude_stk = push_exclude_per_directory(base, baselen);
210
Linus Torvalds8695c8b2005-04-12 01:55:38211 while ((de = readdir(dir)) != NULL) {
212 int len;
213
Junio C Hamanoc4ee2952005-05-25 01:20:08214 if ((de->d_name[0] == '.') &&
215 (de->d_name[1] == 0 ||
216 !strcmp(de->d_name + 1, ".") ||
217 !strcmp(de->d_name + 1, "git")))
Linus Torvalds8695c8b2005-04-12 01:55:38218 continue;
Linus Torvalds8695c8b2005-04-12 01:55:38219 len = strlen(de->d_name);
220 memcpy(fullname + baselen, de->d_name, len+1);
Junio C Hamanof87f9492005-07-24 22:26:09221 if (excluded(fullname) != show_ignored)
222 continue;
Linus Torvalds8695c8b2005-04-12 01:55:38223
Edgar Toernigb6829692005-04-30 16:51:03224 switch (DTYPE(de)) {
Linus Torvalds8695c8b2005-04-12 01:55:38225 struct stat st;
226 default:
227 continue;
228 case DT_UNKNOWN:
229 if (lstat(fullname, &st))
230 continue;
Junio C Hamanoa15c1c62005-05-13 00:16:04231 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
Linus Torvalds8695c8b2005-04-12 01:55:38232 break;
233 if (!S_ISDIR(st.st_mode))
234 continue;
235 /* fallthrough */
236 case DT_DIR:
237 memcpy(fullname + baselen + len, "/", 2);
Petr Baudis20d37ef2005-04-22 02:47:08238 read_directory(fullname, fullname,
239 baselen + len + 1);
Linus Torvalds8695c8b2005-04-12 01:55:38240 continue;
241 case DT_REG:
Junio C Hamanoa15c1c62005-05-13 00:16:04242 case DT_LNK:
Linus Torvalds8695c8b2005-04-12 01:55:38243 break;
244 }
245 add_name(fullname, baselen + len);
246 }
247 closedir(dir);
Junio C Hamanof87f9492005-07-24 22:26:09248
249 pop_exclude_per_directory(exclude_stk);
Linus Torvalds8695c8b2005-04-12 01:55:38250 }
251}
252
253static int cmp_name(const void *p1, const void *p2)
254{
Junio C Hamano6ca45942005-05-13 00:17:54255 const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1;
256 const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2;
Linus Torvalds8695c8b2005-04-12 01:55:38257
Junio C Hamano6ca45942005-05-13 00:17:54258 return cache_name_compare(e1->name, e1->len,
259 e2->name, e2->len);
260}
261
Linus Torvaldse99d59f2005-05-20 18:46:10262static void show_killed_files(void)
Junio C Hamano6ca45942005-05-13 00:17:54263{
264 int i;
265 for (i = 0; i < nr_dir; i++) {
266 struct nond_on_fs *ent = dir[i];
267 char *cp, *sp;
268 int pos, len, killed = 0;
269
270 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
271 sp = strchr(cp, '/');
272 if (!sp) {
273 /* If ent->name is prefix of an entry in the
274 * cache, it will be killed.
275 */
276 pos = cache_name_pos(ent->name, ent->len);
277 if (0 <= pos)
278 die("bug in show-killed-files");
279 pos = -pos - 1;
280 while (pos < active_nr &&
281 ce_stage(active_cache[pos]))
282 pos++; /* skip unmerged */
283 if (active_nr <= pos)
284 break;
285 /* pos points at a name immediately after
286 * ent->name in the cache. Does it expect
287 * ent->name to be a directory?
288 */
289 len = ce_namelen(active_cache[pos]);
290 if ((ent->len < len) &&
291 !strncmp(active_cache[pos]->name,
292 ent->name, ent->len) &&
293 active_cache[pos]->name[ent->len] == '/')
294 killed = 1;
295 break;
296 }
297 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
298 /* If any of the leading directories in
299 * ent->name is registered in the cache,
300 * ent->name will be killed.
301 */
302 killed = 1;
303 break;
304 }
305 }
306 if (killed)
307 printf("%s%.*s%c", tag_killed,
308 dir[i]->len, dir[i]->name,
309 line_terminator);
310 }
Linus Torvalds8695c8b2005-04-12 01:55:38311}
312
313static void show_files(void)
314{
315 int i;
316
317 /* For cached/deleted files we don't need to even do the readdir */
Junio C Hamano6ca45942005-05-13 00:17:54318 if (show_others || show_killed) {
Linus Torvalds8695c8b2005-04-12 01:55:38319 read_directory(".", "", 0);
Junio C Hamano6ca45942005-05-13 00:17:54320 qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
321 if (show_others)
322 for (i = 0; i < nr_dir; i++)
323 printf("%s%.*s%c", tag_other,
324 dir[i]->len, dir[i]->name,
325 line_terminator);
326 if (show_killed)
327 show_killed_files();
Linus Torvalds8695c8b2005-04-12 01:55:38328 }
Junio C Hamanoaee46192005-04-16 15:33:23329 if (show_cached | show_stage) {
Linus Torvalds8695c8b2005-04-12 01:55:38330 for (i = 0; i < active_nr; i++) {
331 struct cache_entry *ce = active_cache[i];
Nicolas Pitre9ff768e2005-04-28 18:44:04332 if (excluded(ce->name) != show_ignored)
333 continue;
Linus Torvaldseec8c632005-04-16 19:43:32334 if (show_unmerged && !ce_stage(ce))
335 continue;
Junio C Hamanoaee46192005-04-16 15:33:23336 if (!show_stage)
Petr Baudis20d37ef2005-04-22 02:47:08337 printf("%s%s%c",
338 ce_stage(ce) ? tag_unmerged :
339 tag_cached,
340 ce->name, line_terminator);
Junio C Hamanoaee46192005-04-16 15:33:23341 else
Junio C Hamano2eab9452005-05-26 21:38:19342 printf("%s%06o %s %d\t%s%c",
Petr Baudis20d37ef2005-04-22 02:47:08343 ce_stage(ce) ? tag_unmerged :
344 tag_cached,
Junio C Hamanoaee46192005-04-16 15:33:23345 ntohl(ce->ce_mode),
346 sha1_to_hex(ce->sha1),
347 ce_stage(ce),
Junio C Hamanoaee46192005-04-16 15:33:23348 ce->name, line_terminator);
Linus Torvalds8695c8b2005-04-12 01:55:38349 }
350 }
351 if (show_deleted) {
352 for (i = 0; i < active_nr; i++) {
353 struct cache_entry *ce = active_cache[i];
354 struct stat st;
Nicolas Pitre9ff768e2005-04-28 18:44:04355 if (excluded(ce->name) != show_ignored)
356 continue;
Kay Sievers8ae0a8c2005-05-05 12:38:25357 if (!lstat(ce->name, &st))
Linus Torvalds8695c8b2005-04-12 01:55:38358 continue;
Petr Baudis20d37ef2005-04-22 02:47:08359 printf("%s%s%c", tag_removed, ce->name,
360 line_terminator);
Linus Torvalds8695c8b2005-04-12 01:55:38361 }
362 }
Linus Torvalds8695c8b2005-04-12 01:55:38363}
364
Nicolas Pitre17710392005-04-30 20:59:38365static const char *ls_files_usage =
Alexey Nezhdanov667bb592005-05-19 11:17:16366 "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed])* "
Junio C Hamanof87f9492005-07-24 22:26:09367 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
368 "[ --exclude-per-directory=<filename> ]";
369;
Nicolas Pitrecf9a1132005-04-28 22:06:25370
Linus Torvalds8695c8b2005-04-12 01:55:38371int main(int argc, char **argv)
372{
373 int i;
374
375 for (i = 1; i < argc; i++) {
376 char *arg = argv[i];
377
Junio C Hamanob83c8342005-04-15 18:11:01378 if (!strcmp(arg, "-z")) {
379 line_terminator = 0;
Petr Baudis20d37ef2005-04-22 02:47:08380 } else if (!strcmp(arg, "-t")) {
381 tag_cached = "H ";
382 tag_unmerged = "M ";
383 tag_removed = "R ";
384 tag_other = "? ";
Junio C Hamano6ca45942005-05-13 00:17:54385 tag_killed = "K ";
Nicolas Pitrecf9a1132005-04-28 22:06:25386 } else if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
Linus Torvalds8695c8b2005-04-12 01:55:38387 show_cached = 1;
Nicolas Pitrecf9a1132005-04-28 22:06:25388 } else if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
Linus Torvalds8695c8b2005-04-12 01:55:38389 show_deleted = 1;
Nicolas Pitrecf9a1132005-04-28 22:06:25390 } else if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
Linus Torvalds8695c8b2005-04-12 01:55:38391 show_others = 1;
Nicolas Pitrecf9a1132005-04-28 22:06:25392 } else if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
Linus Torvalds8695c8b2005-04-12 01:55:38393 show_ignored = 1;
Nicolas Pitrecf9a1132005-04-28 22:06:25394 } else if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
Junio C Hamanoaee46192005-04-16 15:33:23395 show_stage = 1;
Junio C Hamano6ca45942005-05-13 00:17:54396 } else if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
397 show_killed = 1;
Nicolas Pitrecf9a1132005-04-28 22:06:25398 } else if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
Petr Baudis20d37ef2005-04-22 02:47:08399 /* There's no point in showing unmerged unless
400 * you also show the stage information.
401 */
Linus Torvaldseec8c632005-04-16 19:43:32402 show_stage = 1;
403 show_unmerged = 1;
Nicolas Pitrecf9a1132005-04-28 22:06:25404 } else if (!strcmp(arg, "-x") && i+1 < argc) {
Junio C Hamanof87f9492005-07-24 22:26:09405 add_exclude(argv[++i], "", 0);
Nicolas Pitrecf9a1132005-04-28 22:06:25406 } else if (!strncmp(arg, "--exclude=", 10)) {
Junio C Hamanof87f9492005-07-24 22:26:09407 add_exclude(arg+10, "", 0);
Nicolas Pitrecf9a1132005-04-28 22:06:25408 } else if (!strcmp(arg, "-X") && i+1 < argc) {
Nicolas Pitre9ff768e2005-04-28 18:44:04409 add_excludes_from_file(argv[++i]);
Nicolas Pitrecf9a1132005-04-28 22:06:25410 } else if (!strncmp(arg, "--exclude-from=", 15)) {
Nicolas Pitre9ff768e2005-04-28 18:44:04411 add_excludes_from_file(arg+15);
Junio C Hamanof87f9492005-07-24 22:26:09412 } else if (!strncmp(arg, "--exclude-per-directory=", 24)) {
413 exclude_per_dir = arg + 24;
Nicolas Pitrecf9a1132005-04-28 22:06:25414 } else
Nicolas Pitre17710392005-04-30 20:59:38415 usage(ls_files_usage);
Nicolas Pitre9ff768e2005-04-28 18:44:04416 }
417
418 if (show_ignored && !nr_excludes) {
Petr Baudis20d37ef2005-04-22 02:47:08419 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
420 argv[0]);
Nicolas Pitre9ff768e2005-04-28 18:44:04421 exit(1);
Linus Torvalds8695c8b2005-04-12 01:55:38422 }
423
424 /* With no flags, we default to showing the cached files */
Junio C Hamano6ca45942005-05-13 00:17:54425 if (!(show_stage | show_deleted | show_others | show_unmerged | show_killed))
Linus Torvalds8695c8b2005-04-12 01:55:38426 show_cached = 1;
427
428 read_cache();
429 show_files();
430 return 0;
431}