[PATCH] Add support for directories to git-rename-script.
[git/jrn.git] / ls-files.c
blobfadfa01318e48c4f00dac88945022be2392c311c
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.
6 * Copyright (C) Linus Torvalds, 2005
7 */
8 #include <dirent.h>
9 #include <fnmatch.h>
11 #include "cache.h"
13 static int show_deleted = 0;
14 static int show_cached = 0;
15 static int show_others = 0;
16 static int show_ignored = 0;
17 static int show_stage = 0;
18 static int show_unmerged = 0;
19 static int show_killed = 0;
20 static int line_terminator = '\n';
22 static const char *tag_cached = "";
23 static const char *tag_unmerged = "";
24 static const char *tag_removed = "";
25 static const char *tag_other = "";
26 static const char *tag_killed = "";
28 static char *exclude_per_dir = NULL;
29 static int nr_excludes;
30 static int excludes_alloc;
31 static struct exclude {
32 const char *pattern;
33 const char *base;
34 int baselen;
35 } **excludes;
37 static void add_exclude(const char *string, const char *base, int baselen)
39 struct exclude *x = xmalloc(sizeof (*x));
41 x->pattern = string;
42 x->base = base;
43 x->baselen = baselen;
44 if (nr_excludes == excludes_alloc) {
45 excludes_alloc = alloc_nr(excludes_alloc);
46 excludes = realloc(excludes, excludes_alloc*sizeof(char *));
48 excludes[nr_excludes++] = x;
51 static int add_excludes_from_file_1(const char *fname,
52 const char *base, int baselen)
54 int fd, i;
55 long size;
56 char *buf, *entry;
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);
67 return 0;
69 buf = xmalloc(size);
70 if (read(fd, buf, size) != size)
71 goto err;
72 close(fd);
74 entry = buf;
75 for (i = 0; i < size; i++) {
76 if (buf[i] == '\n') {
77 if (entry != buf + i && entry[0] != '#') {
78 buf[i] = 0;
79 add_exclude(entry, base, baselen);
81 entry = buf + i + 1;
84 return 0;
86 err:
87 if (0 <= fd)
88 close(fd);
89 return -1;
92 static void add_excludes_from_file(const char *fname)
94 if (add_excludes_from_file_1(fname, "", 0) < 0)
95 die("cannot use %s as an exclude file", fname);
98 static int push_exclude_per_directory(const char *base, int baselen)
100 char exclude_file[PATH_MAX];
101 int current_nr = nr_excludes;
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);
108 return current_nr;
111 static void pop_exclude_per_directory(int stk)
113 while (stk < nr_excludes)
114 free(excludes[--nr_excludes]);
117 static int excluded(const char *pathname)
119 int i;
121 if (nr_excludes) {
122 int pathlen = strlen(pathname);
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;
129 if (*exclude == '!') {
130 to_exclude = 0;
131 exclude++;
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;
141 else {
142 /* match with FNM_PATHNAME:
143 * exclude has base (baselen long) inplicitly
144 * in front of it.
146 int baselen = x->baselen;
147 if (*exclude == '/')
148 exclude++;
150 if (pathlen < baselen ||
151 (baselen && pathname[baselen-1] != '/') ||
152 strncmp(pathname, x->base, baselen))
153 continue;
155 if (fnmatch(exclude, pathname+baselen,
156 FNM_PATHNAME) == 0)
157 return to_exclude;
161 return 0;
164 struct nond_on_fs {
165 int len;
166 char name[0];
169 static struct nond_on_fs **dir;
170 static int nr_dir;
171 static int dir_alloc;
173 static void add_name(const char *pathname, int len)
175 struct nond_on_fs *ent;
177 if (cache_name_pos(pathname, len) >= 0)
178 return;
180 if (nr_dir == dir_alloc) {
181 dir_alloc = alloc_nr(dir_alloc);
182 dir = xrealloc(dir, dir_alloc*sizeof(ent));
184 ent = xmalloc(sizeof(*ent) + len + 1);
185 ent->len = len;
186 memcpy(ent->name, pathname, len);
187 dir[nr_dir++] = ent;
191 * Read a directory tree. We currently ignore anything but
192 * directories, regular files and symlinks. That's because git
193 * doesn't handle them at all yet. Maybe that will change some
194 * day.
196 * Also, we ignore the name ".git" (even if it is not a directory).
197 * That likely will not change.
199 static void read_directory(const char *path, const char *base, int baselen)
201 DIR *dir = opendir(path);
203 if (dir) {
204 int exclude_stk;
205 struct dirent *de;
206 char fullname[MAXPATHLEN + 1];
207 memcpy(fullname, base, baselen);
209 exclude_stk = push_exclude_per_directory(base, baselen);
211 while ((de = readdir(dir)) != NULL) {
212 int len;
214 if ((de->d_name[0] == '.') &&
215 (de->d_name[1] == 0 ||
216 !strcmp(de->d_name + 1, ".") ||
217 !strcmp(de->d_name + 1, "git")))
218 continue;
219 len = strlen(de->d_name);
220 memcpy(fullname + baselen, de->d_name, len+1);
221 if (excluded(fullname) != show_ignored)
222 continue;
224 switch (DTYPE(de)) {
225 struct stat st;
226 default:
227 continue;
228 case DT_UNKNOWN:
229 if (lstat(fullname, &st))
230 continue;
231 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
232 break;
233 if (!S_ISDIR(st.st_mode))
234 continue;
235 /* fallthrough */
236 case DT_DIR:
237 memcpy(fullname + baselen + len, "/", 2);
238 read_directory(fullname, fullname,
239 baselen + len + 1);
240 continue;
241 case DT_REG:
242 case DT_LNK:
243 break;
245 add_name(fullname, baselen + len);
247 closedir(dir);
249 pop_exclude_per_directory(exclude_stk);
253 static int cmp_name(const void *p1, const void *p2)
255 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;
258 return cache_name_compare(e1->name, e1->len,
259 e2->name, e2->len);
262 static void show_killed_files(void)
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;
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.
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?
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;
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.
302 killed = 1;
303 break;
306 if (killed)
307 printf("%s%.*s%c", tag_killed,
308 dir[i]->len, dir[i]->name,
309 line_terminator);
313 static void show_files(void)
315 int i;
317 /* For cached/deleted files we don't need to even do the readdir */
318 if (show_others || show_killed) {
319 read_directory(".", "", 0);
320 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();
329 if (show_cached | show_stage) {
330 for (i = 0; i < active_nr; i++) {
331 struct cache_entry *ce = active_cache[i];
332 if (excluded(ce->name) != show_ignored)
333 continue;
334 if (show_unmerged && !ce_stage(ce))
335 continue;
336 if (!show_stage)
337 printf("%s%s%c",
338 ce_stage(ce) ? tag_unmerged :
339 tag_cached,
340 ce->name, line_terminator);
341 else
342 printf("%s%06o %s %d\t%s%c",
343 ce_stage(ce) ? tag_unmerged :
344 tag_cached,
345 ntohl(ce->ce_mode),
346 sha1_to_hex(ce->sha1),
347 ce_stage(ce),
348 ce->name, line_terminator);
351 if (show_deleted) {
352 for (i = 0; i < active_nr; i++) {
353 struct cache_entry *ce = active_cache[i];
354 struct stat st;
355 if (excluded(ce->name) != show_ignored)
356 continue;
357 if (!lstat(ce->name, &st))
358 continue;
359 printf("%s%s%c", tag_removed, ce->name,
360 line_terminator);
365 static const char *ls_files_usage =
366 "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed])* "
367 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
368 "[ --exclude-per-directory=<filename> ]";
371 int main(int argc, char **argv)
373 int i;
375 for (i = 1; i < argc; i++) {
376 char *arg = argv[i];
378 if (!strcmp(arg, "-z")) {
379 line_terminator = 0;
380 } else if (!strcmp(arg, "-t")) {
381 tag_cached = "H ";
382 tag_unmerged = "M ";
383 tag_removed = "R ";
384 tag_other = "? ";
385 tag_killed = "K ";
386 } else if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
387 show_cached = 1;
388 } else if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
389 show_deleted = 1;
390 } else if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
391 show_others = 1;
392 } else if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
393 show_ignored = 1;
394 } else if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
395 show_stage = 1;
396 } else if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
397 show_killed = 1;
398 } else if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
399 /* There's no point in showing unmerged unless
400 * you also show the stage information.
402 show_stage = 1;
403 show_unmerged = 1;
404 } else if (!strcmp(arg, "-x") && i+1 < argc) {
405 add_exclude(argv[++i], "", 0);
406 } else if (!strncmp(arg, "--exclude=", 10)) {
407 add_exclude(arg+10, "", 0);
408 } else if (!strcmp(arg, "-X") && i+1 < argc) {
409 add_excludes_from_file(argv[++i]);
410 } else if (!strncmp(arg, "--exclude-from=", 15)) {
411 add_excludes_from_file(arg+15);
412 } else if (!strncmp(arg, "--exclude-per-directory=", 24)) {
413 exclude_per_dir = arg + 24;
414 } else
415 usage(ls_files_usage);
418 if (show_ignored && !nr_excludes) {
419 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
420 argv[0]);
421 exit(1);
424 /* With no flags, we default to showing the cached files */
425 if (!(show_stage | show_deleted | show_others | show_unmerged | show_killed))
426 show_cached = 1;
428 read_cache();
429 show_files();
430 return 0;