Support 'diff=pgm' attribute
[git/vmiklos.git] / dir.c
blob6564a929ff66cdffca0666e175c9361149b25b5e
1 /*
2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
5 * Copyright (C) Linus Torvalds, 2005-2006
6 * Junio Hamano, 2005-2006
7 */
8 #include "cache.h"
9 #include "dir.h"
10 #include "refs.h"
12 struct path_simplify {
13 int len;
14 const char *path;
17 static int read_directory_recursive(struct dir_struct *dir,
18 const char *path, const char *base, int baselen,
19 int check_only, const struct path_simplify *simplify);
21 int common_prefix(const char **pathspec)
23 const char *path, *slash, *next;
24 int prefix;
26 if (!pathspec)
27 return 0;
29 path = *pathspec;
30 slash = strrchr(path, '/');
31 if (!slash)
32 return 0;
34 prefix = slash - path + 1;
35 while ((next = *++pathspec) != NULL) {
36 int len = strlen(next);
37 if (len >= prefix && !memcmp(path, next, len))
38 continue;
39 for (;;) {
40 if (!len)
41 return 0;
42 if (next[--len] != '/')
43 continue;
44 if (memcmp(path, next, len+1))
45 continue;
46 prefix = len + 1;
47 break;
50 return prefix;
54 * Does 'match' matches the given name?
55 * A match is found if
57 * (1) the 'match' string is leading directory of 'name', or
58 * (2) the 'match' string is a wildcard and matches 'name', or
59 * (3) the 'match' string is exactly the same as 'name'.
61 * and the return value tells which case it was.
63 * It returns 0 when there is no match.
65 static int match_one(const char *match, const char *name, int namelen)
67 int matchlen;
69 /* If the match was just the prefix, we matched */
70 matchlen = strlen(match);
71 if (!matchlen)
72 return MATCHED_RECURSIVELY;
75 * If we don't match the matchstring exactly,
76 * we need to match by fnmatch
78 if (strncmp(match, name, matchlen))
79 return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
81 if (!name[matchlen])
82 return MATCHED_EXACTLY;
83 if (match[matchlen-1] == '/' || name[matchlen] == '/')
84 return MATCHED_RECURSIVELY;
85 return 0;
89 * Given a name and a list of pathspecs, see if the name matches
90 * any of the pathspecs. The caller is also interested in seeing
91 * all pathspec matches some names it calls this function with
92 * (otherwise the user could have mistyped the unmatched pathspec),
93 * and a mark is left in seen[] array for pathspec element that
94 * actually matched anything.
96 int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
98 int retval;
99 const char *match;
101 name += prefix;
102 namelen -= prefix;
104 for (retval = 0; (match = *pathspec++) != NULL; seen++) {
105 int how;
106 if (retval && *seen == MATCHED_EXACTLY)
107 continue;
108 match += prefix;
109 how = match_one(match, name, namelen);
110 if (how) {
111 if (retval < how)
112 retval = how;
113 if (*seen < how)
114 *seen = how;
117 return retval;
120 void add_exclude(const char *string, const char *base,
121 int baselen, struct exclude_list *which)
123 struct exclude *x = xmalloc(sizeof (*x));
125 x->pattern = string;
126 x->base = base;
127 x->baselen = baselen;
128 if (which->nr == which->alloc) {
129 which->alloc = alloc_nr(which->alloc);
130 which->excludes = xrealloc(which->excludes,
131 which->alloc * sizeof(x));
133 which->excludes[which->nr++] = x;
136 static int add_excludes_from_file_1(const char *fname,
137 const char *base,
138 int baselen,
139 struct exclude_list *which)
141 struct stat st;
142 int fd, i;
143 size_t size;
144 char *buf, *entry;
146 fd = open(fname, O_RDONLY);
147 if (fd < 0 || fstat(fd, &st) < 0)
148 goto err;
149 size = xsize_t(st.st_size);
150 if (size == 0) {
151 close(fd);
152 return 0;
154 buf = xmalloc(size+1);
155 if (read_in_full(fd, buf, size) != size)
156 goto err;
157 close(fd);
159 buf[size++] = '\n';
160 entry = buf;
161 for (i = 0; i < size; i++) {
162 if (buf[i] == '\n') {
163 if (entry != buf + i && entry[0] != '#') {
164 buf[i - (i && buf[i-1] == '\r')] = 0;
165 add_exclude(entry, base, baselen, which);
167 entry = buf + i + 1;
170 return 0;
172 err:
173 if (0 <= fd)
174 close(fd);
175 return -1;
178 void add_excludes_from_file(struct dir_struct *dir, const char *fname)
180 if (add_excludes_from_file_1(fname, "", 0,
181 &dir->exclude_list[EXC_FILE]) < 0)
182 die("cannot use %s as an exclude file", fname);
185 int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
187 char exclude_file[PATH_MAX];
188 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
189 int current_nr = el->nr;
191 if (dir->exclude_per_dir) {
192 memcpy(exclude_file, base, baselen);
193 strcpy(exclude_file + baselen, dir->exclude_per_dir);
194 add_excludes_from_file_1(exclude_file, base, baselen, el);
196 return current_nr;
199 void pop_exclude_per_directory(struct dir_struct *dir, int stk)
201 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
203 while (stk < el->nr)
204 free(el->excludes[--el->nr]);
207 /* Scan the list and let the last match determines the fate.
208 * Return 1 for exclude, 0 for include and -1 for undecided.
210 static int excluded_1(const char *pathname,
211 int pathlen,
212 struct exclude_list *el)
214 int i;
216 if (el->nr) {
217 for (i = el->nr - 1; 0 <= i; i--) {
218 struct exclude *x = el->excludes[i];
219 const char *exclude = x->pattern;
220 int to_exclude = 1;
222 if (*exclude == '!') {
223 to_exclude = 0;
224 exclude++;
227 if (!strchr(exclude, '/')) {
228 /* match basename */
229 const char *basename = strrchr(pathname, '/');
230 basename = (basename) ? basename+1 : pathname;
231 if (fnmatch(exclude, basename, 0) == 0)
232 return to_exclude;
234 else {
235 /* match with FNM_PATHNAME:
236 * exclude has base (baselen long) implicitly
237 * in front of it.
239 int baselen = x->baselen;
240 if (*exclude == '/')
241 exclude++;
243 if (pathlen < baselen ||
244 (baselen && pathname[baselen-1] != '/') ||
245 strncmp(pathname, x->base, baselen))
246 continue;
248 if (fnmatch(exclude, pathname+baselen,
249 FNM_PATHNAME) == 0)
250 return to_exclude;
254 return -1; /* undecided */
257 int excluded(struct dir_struct *dir, const char *pathname)
259 int pathlen = strlen(pathname);
260 int st;
262 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
263 switch (excluded_1(pathname, pathlen, &dir->exclude_list[st])) {
264 case 0:
265 return 0;
266 case 1:
267 return 1;
270 return 0;
273 struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
275 struct dir_entry *ent;
277 if (cache_name_pos(pathname, len) >= 0)
278 return NULL;
280 if (dir->nr == dir->alloc) {
281 int alloc = alloc_nr(dir->alloc);
282 dir->alloc = alloc;
283 dir->entries = xrealloc(dir->entries, alloc*sizeof(ent));
285 ent = xmalloc(sizeof(*ent) + len + 1);
286 ent->ignored = ent->ignored_dir = 0;
287 ent->len = len;
288 memcpy(ent->name, pathname, len);
289 ent->name[len] = 0;
290 dir->entries[dir->nr++] = ent;
291 return ent;
294 enum exist_status {
295 index_nonexistent = 0,
296 index_directory,
297 index_gitdir,
301 * The index sorts alphabetically by entry name, which
302 * means that a gitlink sorts as '\0' at the end, while
303 * a directory (which is defined not as an entry, but as
304 * the files it contains) will sort with the '/' at the
305 * end.
307 static enum exist_status directory_exists_in_index(const char *dirname, int len)
309 int pos = cache_name_pos(dirname, len);
310 if (pos < 0)
311 pos = -pos-1;
312 while (pos < active_nr) {
313 struct cache_entry *ce = active_cache[pos++];
314 unsigned char endchar;
316 if (strncmp(ce->name, dirname, len))
317 break;
318 endchar = ce->name[len];
319 if (endchar > '/')
320 break;
321 if (endchar == '/')
322 return index_directory;
323 if (!endchar && S_ISDIRLNK(ntohl(ce->ce_mode)))
324 return index_gitdir;
326 return index_nonexistent;
330 * When we find a directory when traversing the filesystem, we
331 * have three distinct cases:
333 * - ignore it
334 * - see it as a directory
335 * - recurse into it
337 * and which one we choose depends on a combination of existing
338 * git index contents and the flags passed into the directory
339 * traversal routine.
341 * Case 1: If we *already* have entries in the index under that
342 * directory name, we always recurse into the directory to see
343 * all the files.
345 * Case 2: If we *already* have that directory name as a gitlink,
346 * we always continue to see it as a gitlink, regardless of whether
347 * there is an actual git directory there or not (it might not
348 * be checked out as a subproject!)
350 * Case 3: if we didn't have it in the index previously, we
351 * have a few sub-cases:
353 * (a) if "show_other_directories" is true, we show it as
354 * just a directory, unless "hide_empty_directories" is
355 * also true and the directory is empty, in which case
356 * we just ignore it entirely.
357 * (b) if it looks like a git directory, and we don't have
358 * 'no_dirlinks' set we treat it as a gitlink, and show it
359 * as a directory.
360 * (c) otherwise, we recurse into it.
362 enum directory_treatment {
363 show_directory,
364 ignore_directory,
365 recurse_into_directory,
368 static enum directory_treatment treat_directory(struct dir_struct *dir,
369 const char *dirname, int len,
370 const struct path_simplify *simplify)
372 /* The "len-1" is to strip the final '/' */
373 switch (directory_exists_in_index(dirname, len-1)) {
374 case index_directory:
375 return recurse_into_directory;
377 case index_gitdir:
378 if (dir->show_other_directories)
379 return ignore_directory;
380 return show_directory;
382 case index_nonexistent:
383 if (dir->show_other_directories)
384 break;
385 if (!dir->no_dirlinks) {
386 unsigned char sha1[20];
387 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
388 return show_directory;
390 return recurse_into_directory;
393 /* This is the "show_other_directories" case */
394 if (!dir->hide_empty_directories)
395 return show_directory;
396 if (!read_directory_recursive(dir, dirname, dirname, len, 1, simplify))
397 return ignore_directory;
398 return show_directory;
402 * This is an inexact early pruning of any recursive directory
403 * reading - if the path cannot possibly be in the pathspec,
404 * return true, and we'll skip it early.
406 static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
408 if (simplify) {
409 for (;;) {
410 const char *match = simplify->path;
411 int len = simplify->len;
413 if (!match)
414 break;
415 if (len > pathlen)
416 len = pathlen;
417 if (!memcmp(path, match, len))
418 return 0;
419 simplify++;
421 return 1;
423 return 0;
427 * Read a directory tree. We currently ignore anything but
428 * directories, regular files and symlinks. That's because git
429 * doesn't handle them at all yet. Maybe that will change some
430 * day.
432 * Also, we ignore the name ".git" (even if it is not a directory).
433 * That likely will not change.
435 static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen, int check_only, const struct path_simplify *simplify)
437 DIR *fdir = opendir(path);
438 int contents = 0;
440 if (fdir) {
441 int exclude_stk;
442 struct dirent *de;
443 char fullname[PATH_MAX + 1];
444 memcpy(fullname, base, baselen);
446 exclude_stk = push_exclude_per_directory(dir, base, baselen);
448 while ((de = readdir(fdir)) != NULL) {
449 int len;
451 if ((de->d_name[0] == '.') &&
452 (de->d_name[1] == 0 ||
453 !strcmp(de->d_name + 1, ".") ||
454 !strcmp(de->d_name + 1, "git")))
455 continue;
456 len = strlen(de->d_name);
457 /* Ignore overly long pathnames! */
458 if (len + baselen + 8 > sizeof(fullname))
459 continue;
460 memcpy(fullname + baselen, de->d_name, len+1);
461 if (simplify_away(fullname, baselen + len, simplify))
462 continue;
463 if (excluded(dir, fullname) != dir->show_ignored) {
464 if (!dir->show_ignored || DTYPE(de) != DT_DIR) {
465 continue;
469 switch (DTYPE(de)) {
470 struct stat st;
471 default:
472 continue;
473 case DT_UNKNOWN:
474 if (lstat(fullname, &st))
475 continue;
476 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
477 break;
478 if (!S_ISDIR(st.st_mode))
479 continue;
480 /* fallthrough */
481 case DT_DIR:
482 memcpy(fullname + baselen + len, "/", 2);
483 len++;
484 switch (treat_directory(dir, fullname, baselen + len, simplify)) {
485 case show_directory:
486 break;
487 case recurse_into_directory:
488 contents += read_directory_recursive(dir,
489 fullname, fullname, baselen + len, 0, simplify);
490 continue;
491 case ignore_directory:
492 continue;
494 break;
495 case DT_REG:
496 case DT_LNK:
497 break;
499 contents++;
500 if (check_only)
501 goto exit_early;
502 else
503 dir_add_name(dir, fullname, baselen + len);
505 exit_early:
506 closedir(fdir);
508 pop_exclude_per_directory(dir, exclude_stk);
511 return contents;
514 static int cmp_name(const void *p1, const void *p2)
516 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
517 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
519 return cache_name_compare(e1->name, e1->len,
520 e2->name, e2->len);
524 * Return the length of the "simple" part of a path match limiter.
526 static int simple_length(const char *match)
528 const char special[256] = {
529 [0] = 1, ['?'] = 1,
530 ['\\'] = 1, ['*'] = 1,
531 ['['] = 1
533 int len = -1;
535 for (;;) {
536 unsigned char c = *match++;
537 len++;
538 if (special[c])
539 return len;
543 static struct path_simplify *create_simplify(const char **pathspec)
545 int nr, alloc = 0;
546 struct path_simplify *simplify = NULL;
548 if (!pathspec)
549 return NULL;
551 for (nr = 0 ; ; nr++) {
552 const char *match;
553 if (nr >= alloc) {
554 alloc = alloc_nr(alloc);
555 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
557 match = *pathspec++;
558 if (!match)
559 break;
560 simplify[nr].path = match;
561 simplify[nr].len = simple_length(match);
563 simplify[nr].path = NULL;
564 simplify[nr].len = 0;
565 return simplify;
568 static void free_simplify(struct path_simplify *simplify)
570 if (simplify)
571 free(simplify);
574 int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
576 struct path_simplify *simplify = create_simplify(pathspec);
579 * Make sure to do the per-directory exclude for all the
580 * directories leading up to our base.
582 if (baselen) {
583 if (dir->exclude_per_dir) {
584 char *p, *pp = xmalloc(baselen+1);
585 memcpy(pp, base, baselen+1);
586 p = pp;
587 while (1) {
588 char save = *p;
589 *p = 0;
590 push_exclude_per_directory(dir, pp, p-pp);
591 *p++ = save;
592 if (!save)
593 break;
594 p = strchr(p, '/');
595 if (p)
596 p++;
597 else
598 p = pp + baselen;
600 free(pp);
604 read_directory_recursive(dir, path, base, baselen, 0, simplify);
605 free_simplify(simplify);
606 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
607 return dir->nr;
611 file_exists(const char *f)
613 struct stat sb;
614 return stat(f, &sb) == 0;