blob: d79762c7c0bc9e762ed8dc5f00fb2fd3ce01ad57 [file] [log] [blame]
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001/*
2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
4 *
5 * Copyright (C) Linus Torvalds, 2005-2006
6 * Junio Hamano, 2005-2006
7 */
Linus Torvalds453ec4b2006-05-16 19:02:14 -07008#include "cache.h"
9#include "dir.h"
Linus Torvalds09595252007-04-11 14:49:44 -070010#include "refs.h"
Linus Torvalds453ec4b2006-05-16 19:02:14 -070011
Linus Torvalds9fc42d62007-03-30 20:39:30 -070012struct path_simplify {
13 int len;
14 const char *path;
15};
16
Linus Torvalds09595252007-04-11 14:49:44 -070017static 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);
Junio C Hamano6831a882008-01-31 20:23:25 -080020static int get_dtype(struct dirent *de, const char *path);
Linus Torvalds09595252007-04-11 14:49:44 -070021
Linus Torvalds3c6a3702006-05-19 16:07:51 -070022int common_prefix(const char **pathspec)
23{
24 const char *path, *slash, *next;
25 int prefix;
26
27 if (!pathspec)
28 return 0;
29
30 path = *pathspec;
31 slash = strrchr(path, '/');
32 if (!slash)
33 return 0;
34
35 prefix = slash - path + 1;
36 while ((next = *++pathspec) != NULL) {
37 int len = strlen(next);
Johannes Schindelinc7f34c12007-04-23 10:21:25 +020038 if (len >= prefix && !memcmp(path, next, prefix))
Linus Torvalds3c6a3702006-05-19 16:07:51 -070039 continue;
Johannes Schindelinc7f34c12007-04-23 10:21:25 +020040 len = prefix - 1;
Linus Torvalds3c6a3702006-05-19 16:07:51 -070041 for (;;) {
42 if (!len)
43 return 0;
44 if (next[--len] != '/')
45 continue;
46 if (memcmp(path, next, len+1))
47 continue;
48 prefix = len + 1;
49 break;
50 }
51 }
52 return prefix;
53}
54
Junio C Hamanoe813d502006-12-25 03:09:52 -080055/*
56 * Does 'match' matches the given name?
57 * A match is found if
58 *
59 * (1) the 'match' string is leading directory of 'name', or
60 * (2) the 'match' string is a wildcard and matches 'name', or
61 * (3) the 'match' string is exactly the same as 'name'.
62 *
63 * and the return value tells which case it was.
64 *
65 * It returns 0 when there is no match.
66 */
Linus Torvalds3c6a3702006-05-19 16:07:51 -070067static int match_one(const char *match, const char *name, int namelen)
68{
69 int matchlen;
70
71 /* If the match was just the prefix, we matched */
72 matchlen = strlen(match);
73 if (!matchlen)
Junio C Hamanoe813d502006-12-25 03:09:52 -080074 return MATCHED_RECURSIVELY;
Linus Torvalds3c6a3702006-05-19 16:07:51 -070075
76 /*
77 * If we don't match the matchstring exactly,
78 * we need to match by fnmatch
79 */
80 if (strncmp(match, name, matchlen))
Junio C Hamanoe813d502006-12-25 03:09:52 -080081 return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
Linus Torvalds3c6a3702006-05-19 16:07:51 -070082
Shawn Bohrerf2d0df72008-04-14 22:14:09 -050083 if (namelen == matchlen)
Junio C Hamanoe813d502006-12-25 03:09:52 -080084 return MATCHED_EXACTLY;
85 if (match[matchlen-1] == '/' || name[matchlen] == '/')
86 return MATCHED_RECURSIVELY;
87 return 0;
Linus Torvalds3c6a3702006-05-19 16:07:51 -070088}
89
Junio C Hamanoe813d502006-12-25 03:09:52 -080090/*
91 * Given a name and a list of pathspecs, see if the name matches
92 * any of the pathspecs. The caller is also interested in seeing
93 * all pathspec matches some names it calls this function with
94 * (otherwise the user could have mistyped the unmatched pathspec),
95 * and a mark is left in seen[] array for pathspec element that
96 * actually matched anything.
97 */
Linus Torvalds3c6a3702006-05-19 16:07:51 -070098int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
99{
100 int retval;
101 const char *match;
102
103 name += prefix;
104 namelen -= prefix;
105
106 for (retval = 0; (match = *pathspec++) != NULL; seen++) {
Junio C Hamanoe813d502006-12-25 03:09:52 -0800107 int how;
108 if (retval && *seen == MATCHED_EXACTLY)
Linus Torvalds3c6a3702006-05-19 16:07:51 -0700109 continue;
110 match += prefix;
Junio C Hamanoe813d502006-12-25 03:09:52 -0800111 how = match_one(match, name, namelen);
112 if (how) {
113 if (retval < how)
114 retval = how;
115 if (*seen < how)
116 *seen = how;
Linus Torvalds3c6a3702006-05-19 16:07:51 -0700117 }
118 }
119 return retval;
120}
121
Lars Knoll68492fc2007-10-28 21:27:13 +0100122static int no_wildcard(const char *string)
123{
124 return string[strcspn(string, "*?[{")] == '\0';
125}
126
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700127void add_exclude(const char *string, const char *base,
128 int baselen, struct exclude_list *which)
129{
Junio C Hamanod6b8fc32008-01-31 01:17:48 -0800130 struct exclude *x;
131 size_t len;
132 int to_exclude = 1;
133 int flags = 0;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700134
Lars Knoll68492fc2007-10-28 21:27:13 +0100135 if (*string == '!') {
Junio C Hamanod6b8fc32008-01-31 01:17:48 -0800136 to_exclude = 0;
Lars Knoll68492fc2007-10-28 21:27:13 +0100137 string++;
138 }
Junio C Hamanod6b8fc32008-01-31 01:17:48 -0800139 len = strlen(string);
140 if (len && string[len - 1] == '/') {
141 char *s;
142 x = xmalloc(sizeof(*x) + len);
143 s = (char*)(x+1);
144 memcpy(s, string, len - 1);
145 s[len - 1] = '\0';
146 string = s;
147 x->pattern = s;
148 flags = EXC_FLAG_MUSTBEDIR;
149 } else {
150 x = xmalloc(sizeof(*x));
151 x->pattern = string;
152 }
153 x->to_exclude = to_exclude;
Lars Knoll68492fc2007-10-28 21:27:13 +0100154 x->patternlen = strlen(string);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700155 x->base = base;
156 x->baselen = baselen;
Junio C Hamanod6b8fc32008-01-31 01:17:48 -0800157 x->flags = flags;
Lars Knoll68492fc2007-10-28 21:27:13 +0100158 if (!strchr(string, '/'))
159 x->flags |= EXC_FLAG_NODIR;
160 if (no_wildcard(string))
161 x->flags |= EXC_FLAG_NOWILDCARD;
162 if (*string == '*' && no_wildcard(string+1))
163 x->flags |= EXC_FLAG_ENDSWITH;
Junio C Hamano686a4a02007-11-29 01:11:46 -0800164 ALLOC_GROW(which->excludes, which->nr + 1, which->alloc);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700165 which->excludes[which->nr++] = x;
166}
167
168static int add_excludes_from_file_1(const char *fname,
169 const char *base,
170 int baselen,
Junio C Hamano63d285c2007-11-29 02:17:44 -0800171 char **buf_p,
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700172 struct exclude_list *which)
173{
Jonas Fonsecac4707012006-08-28 01:55:46 +0200174 struct stat st;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700175 int fd, i;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500176 size_t size;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700177 char *buf, *entry;
178
179 fd = open(fname, O_RDONLY);
Jonas Fonsecac4707012006-08-28 01:55:46 +0200180 if (fd < 0 || fstat(fd, &st) < 0)
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700181 goto err;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500182 size = xsize_t(st.st_size);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700183 if (size == 0) {
184 close(fd);
185 return 0;
186 }
187 buf = xmalloc(size+1);
Andy Whitcroft93d26e42007-01-08 15:58:08 +0000188 if (read_in_full(fd, buf, size) != size)
李鸿6ba78232007-12-16 12:53:26 +0800189 {
190 free(buf);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700191 goto err;
李鸿6ba78232007-12-16 12:53:26 +0800192 }
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700193 close(fd);
194
Junio C Hamano63d285c2007-11-29 02:17:44 -0800195 if (buf_p)
196 *buf_p = buf;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700197 buf[size++] = '\n';
198 entry = buf;
199 for (i = 0; i < size; i++) {
200 if (buf[i] == '\n') {
201 if (entry != buf + i && entry[0] != '#') {
202 buf[i - (i && buf[i-1] == '\r')] = 0;
203 add_exclude(entry, base, baselen, which);
204 }
205 entry = buf + i + 1;
206 }
207 }
208 return 0;
209
210 err:
211 if (0 <= fd)
212 close(fd);
213 return -1;
214}
215
216void add_excludes_from_file(struct dir_struct *dir, const char *fname)
217{
Junio C Hamano63d285c2007-11-29 02:17:44 -0800218 if (add_excludes_from_file_1(fname, "", 0, NULL,
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700219 &dir->exclude_list[EXC_FILE]) < 0)
220 die("cannot use %s as an exclude file", fname);
221}
222
Junio C Hamano63d285c2007-11-29 02:17:44 -0800223static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700224{
Junio C Hamano63d285c2007-11-29 02:17:44 -0800225 struct exclude_list *el;
226 struct exclude_stack *stk = NULL;
227 int current;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700228
Junio C Hamano63d285c2007-11-29 02:17:44 -0800229 if ((!dir->exclude_per_dir) ||
230 (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
231 return; /* too long a path -- ignore */
232
233 /* Pop the ones that are not the prefix of the path being checked. */
234 el = &dir->exclude_list[EXC_DIRS];
235 while ((stk = dir->exclude_stack) != NULL) {
236 if (stk->baselen <= baselen &&
237 !strncmp(dir->basebuf, base, stk->baselen))
238 break;
239 dir->exclude_stack = stk->prev;
240 while (stk->exclude_ix < el->nr)
241 free(el->excludes[--el->nr]);
242 free(stk->filebuf);
243 free(stk);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700244 }
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700245
Junio C Hamano63d285c2007-11-29 02:17:44 -0800246 /* Read from the parent directories and push them down. */
247 current = stk ? stk->baselen : -1;
248 while (current < baselen) {
249 struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
250 const char *cp;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700251
Junio C Hamano63d285c2007-11-29 02:17:44 -0800252 if (current < 0) {
253 cp = base;
254 current = 0;
255 }
256 else {
257 cp = strchr(base + current + 1, '/');
258 if (!cp)
259 die("oops in prep_exclude");
260 cp++;
261 }
262 stk->prev = dir->exclude_stack;
263 stk->baselen = cp - base;
264 stk->exclude_ix = el->nr;
265 memcpy(dir->basebuf + current, base + current,
266 stk->baselen - current);
267 strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
268 add_excludes_from_file_1(dir->basebuf,
269 dir->basebuf, stk->baselen,
270 &stk->filebuf, el);
271 dir->exclude_stack = stk;
272 current = stk->baselen;
273 }
274 dir->basebuf[baselen] = '\0';
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700275}
276
277/* Scan the list and let the last match determines the fate.
278 * Return 1 for exclude, 0 for include and -1 for undecided.
279 */
280static int excluded_1(const char *pathname,
Junio C Hamano6831a882008-01-31 20:23:25 -0800281 int pathlen, const char *basename, int *dtype,
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700282 struct exclude_list *el)
283{
284 int i;
285
286 if (el->nr) {
287 for (i = el->nr - 1; 0 <= i; i--) {
288 struct exclude *x = el->excludes[i];
289 const char *exclude = x->pattern;
Lars Knoll68492fc2007-10-28 21:27:13 +0100290 int to_exclude = x->to_exclude;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700291
Junio C Hamano6831a882008-01-31 20:23:25 -0800292 if (x->flags & EXC_FLAG_MUSTBEDIR) {
293 if (*dtype == DT_UNKNOWN)
294 *dtype = get_dtype(NULL, pathname);
295 if (*dtype != DT_DIR)
296 continue;
297 }
Junio C Hamanod6b8fc32008-01-31 01:17:48 -0800298
Lars Knoll68492fc2007-10-28 21:27:13 +0100299 if (x->flags & EXC_FLAG_NODIR) {
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700300 /* match basename */
Lars Knoll68492fc2007-10-28 21:27:13 +0100301 if (x->flags & EXC_FLAG_NOWILDCARD) {
302 if (!strcmp(exclude, basename))
303 return to_exclude;
304 } else if (x->flags & EXC_FLAG_ENDSWITH) {
305 if (x->patternlen - 1 <= pathlen &&
306 !strcmp(exclude + 1, pathname + pathlen - x->patternlen + 1))
307 return to_exclude;
308 } else {
309 if (fnmatch(exclude, basename, 0) == 0)
310 return to_exclude;
311 }
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700312 }
313 else {
314 /* match with FNM_PATHNAME:
315 * exclude has base (baselen long) implicitly
316 * in front of it.
317 */
318 int baselen = x->baselen;
319 if (*exclude == '/')
320 exclude++;
321
322 if (pathlen < baselen ||
323 (baselen && pathname[baselen-1] != '/') ||
324 strncmp(pathname, x->base, baselen))
325 continue;
326
Lars Knoll68492fc2007-10-28 21:27:13 +0100327 if (x->flags & EXC_FLAG_NOWILDCARD) {
328 if (!strcmp(exclude, pathname + baselen))
329 return to_exclude;
330 } else {
331 if (fnmatch(exclude, pathname+baselen,
332 FNM_PATHNAME) == 0)
333 return to_exclude;
334 }
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700335 }
336 }
337 }
338 return -1; /* undecided */
339}
340
Junio C Hamano6831a882008-01-31 20:23:25 -0800341int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700342{
343 int pathlen = strlen(pathname);
344 int st;
Lars Knoll68492fc2007-10-28 21:27:13 +0100345 const char *basename = strrchr(pathname, '/');
346 basename = (basename) ? basename+1 : pathname;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700347
Junio C Hamano63d285c2007-11-29 02:17:44 -0800348 prep_exclude(dir, pathname, basename-pathname);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700349 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
Junio C Hamanod6b8fc32008-01-31 01:17:48 -0800350 switch (excluded_1(pathname, pathlen, basename,
Junio C Hamano6831a882008-01-31 20:23:25 -0800351 dtype_p, &dir->exclude_list[st])) {
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700352 case 0:
353 return 0;
354 case 1:
355 return 1;
356 }
357 }
358 return 0;
359}
360
Junio C Hamanof3fa1832007-11-08 15:35:32 -0800361static struct dir_entry *dir_entry_new(const char *pathname, int len)
362{
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700363 struct dir_entry *ent;
364
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700365 ent = xmalloc(sizeof(*ent) + len + 1);
366 ent->len = len;
367 memcpy(ent->name, pathname, len);
368 ent->name[len] = 0;
Junio C Hamano4d06f8a2006-12-29 11:01:31 -0800369 return ent;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700370}
371
Jeff King6815e562007-06-11 09:39:44 -0400372struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
373{
Linus Torvaldscf558702008-01-22 18:41:14 -0800374 if (cache_name_exists(pathname, len))
Jeff King6815e562007-06-11 09:39:44 -0400375 return NULL;
376
Jeff King25fd2f72007-06-16 18:43:40 -0400377 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
Jeff King6815e562007-06-11 09:39:44 -0400378 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
379}
380
Jeff King2abd31b2007-06-11 09:39:50 -0400381struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
382{
383 if (cache_name_pos(pathname, len) >= 0)
384 return NULL;
385
Jeff King25fd2f72007-06-16 18:43:40 -0400386 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
Jeff King2abd31b2007-06-11 09:39:50 -0400387 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
388}
389
Linus Torvalds09595252007-04-11 14:49:44 -0700390enum exist_status {
391 index_nonexistent = 0,
392 index_directory,
393 index_gitdir,
394};
395
396/*
397 * The index sorts alphabetically by entry name, which
398 * means that a gitlink sorts as '\0' at the end, while
399 * a directory (which is defined not as an entry, but as
400 * the files it contains) will sort with the '/' at the
401 * end.
402 */
403static enum exist_status directory_exists_in_index(const char *dirname, int len)
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700404{
405 int pos = cache_name_pos(dirname, len);
Linus Torvalds09595252007-04-11 14:49:44 -0700406 if (pos < 0)
407 pos = -pos-1;
408 while (pos < active_nr) {
409 struct cache_entry *ce = active_cache[pos++];
410 unsigned char endchar;
411
412 if (strncmp(ce->name, dirname, len))
413 break;
414 endchar = ce->name[len];
415 if (endchar > '/')
416 break;
417 if (endchar == '/')
418 return index_directory;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800419 if (!endchar && S_ISGITLINK(ce->ce_mode))
Linus Torvalds09595252007-04-11 14:49:44 -0700420 return index_gitdir;
421 }
422 return index_nonexistent;
423}
424
425/*
426 * When we find a directory when traversing the filesystem, we
427 * have three distinct cases:
428 *
429 * - ignore it
430 * - see it as a directory
431 * - recurse into it
432 *
433 * and which one we choose depends on a combination of existing
434 * git index contents and the flags passed into the directory
435 * traversal routine.
436 *
437 * Case 1: If we *already* have entries in the index under that
438 * directory name, we always recurse into the directory to see
439 * all the files.
440 *
441 * Case 2: If we *already* have that directory name as a gitlink,
442 * we always continue to see it as a gitlink, regardless of whether
443 * there is an actual git directory there or not (it might not
444 * be checked out as a subproject!)
445 *
446 * Case 3: if we didn't have it in the index previously, we
447 * have a few sub-cases:
448 *
449 * (a) if "show_other_directories" is true, we show it as
450 * just a directory, unless "hide_empty_directories" is
451 * also true and the directory is empty, in which case
452 * we just ignore it entirely.
453 * (b) if it looks like a git directory, and we don't have
Martin Waitz302b9282007-05-21 22:08:28 +0200454 * 'no_gitlinks' set we treat it as a gitlink, and show it
Linus Torvalds09595252007-04-11 14:49:44 -0700455 * as a directory.
456 * (c) otherwise, we recurse into it.
457 */
458enum directory_treatment {
459 show_directory,
460 ignore_directory,
461 recurse_into_directory,
462};
463
464static enum directory_treatment treat_directory(struct dir_struct *dir,
465 const char *dirname, int len,
466 const struct path_simplify *simplify)
467{
468 /* The "len-1" is to strip the final '/' */
469 switch (directory_exists_in_index(dirname, len-1)) {
470 case index_directory:
471 return recurse_into_directory;
472
473 case index_gitdir:
Linus Torvaldsab22aed2007-04-12 14:32:21 -0700474 if (dir->show_other_directories)
475 return ignore_directory;
Linus Torvalds09595252007-04-11 14:49:44 -0700476 return show_directory;
477
478 case index_nonexistent:
479 if (dir->show_other_directories)
480 break;
Martin Waitz302b9282007-05-21 22:08:28 +0200481 if (!dir->no_gitlinks) {
Linus Torvalds09595252007-04-11 14:49:44 -0700482 unsigned char sha1[20];
483 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
484 return show_directory;
485 }
486 return recurse_into_directory;
487 }
488
489 /* This is the "show_other_directories" case */
490 if (!dir->hide_empty_directories)
491 return show_directory;
492 if (!read_directory_recursive(dir, dirname, dirname, len, 1, simplify))
493 return ignore_directory;
494 return show_directory;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700495}
496
497/*
Linus Torvalds9fc42d62007-03-30 20:39:30 -0700498 * This is an inexact early pruning of any recursive directory
499 * reading - if the path cannot possibly be in the pathspec,
500 * return true, and we'll skip it early.
501 */
502static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
503{
504 if (simplify) {
505 for (;;) {
506 const char *match = simplify->path;
507 int len = simplify->len;
508
509 if (!match)
510 break;
511 if (len > pathlen)
512 len = pathlen;
513 if (!memcmp(path, match, len))
514 return 0;
515 simplify++;
516 }
517 return 1;
518 }
519 return 0;
520}
521
Jeff Kinge96980e2007-06-12 23:42:14 +0200522static int in_pathspec(const char *path, int len, const struct path_simplify *simplify)
523{
524 if (simplify) {
525 for (; simplify->path; simplify++) {
526 if (len == simplify->len
527 && !memcmp(path, simplify->path, len))
528 return 1;
529 }
530 }
531 return 0;
532}
533
Linus Torvalds07134422007-10-19 10:59:22 -0700534static int get_dtype(struct dirent *de, const char *path)
535{
Junio C Hamano6831a882008-01-31 20:23:25 -0800536 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
Linus Torvalds07134422007-10-19 10:59:22 -0700537 struct stat st;
538
539 if (dtype != DT_UNKNOWN)
540 return dtype;
541 if (lstat(path, &st))
542 return dtype;
543 if (S_ISREG(st.st_mode))
544 return DT_REG;
545 if (S_ISDIR(st.st_mode))
546 return DT_DIR;
547 if (S_ISLNK(st.st_mode))
548 return DT_LNK;
549 return dtype;
550}
551
Linus Torvalds9fc42d62007-03-30 20:39:30 -0700552/*
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700553 * Read a directory tree. We currently ignore anything but
554 * directories, regular files and symlinks. That's because git
555 * doesn't handle them at all yet. Maybe that will change some
556 * day.
557 *
558 * Also, we ignore the name ".git" (even if it is not a directory).
559 * That likely will not change.
560 */
Linus Torvalds9fc42d62007-03-30 20:39:30 -0700561static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen, int check_only, const struct path_simplify *simplify)
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700562{
563 DIR *fdir = opendir(path);
564 int contents = 0;
565
566 if (fdir) {
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700567 struct dirent *de;
Jonas Fonseca095c4242006-08-26 16:09:17 +0200568 char fullname[PATH_MAX + 1];
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700569 memcpy(fullname, base, baselen);
570
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700571 while ((de = readdir(fdir)) != NULL) {
Linus Torvalds07134422007-10-19 10:59:22 -0700572 int len, dtype;
Michael Spangb9916252007-05-06 22:35:04 -0400573 int exclude;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700574
575 if ((de->d_name[0] == '.') &&
576 (de->d_name[1] == 0 ||
577 !strcmp(de->d_name + 1, ".") ||
578 !strcmp(de->d_name + 1, "git")))
579 continue;
580 len = strlen(de->d_name);
Linus Torvalds5d5cea62007-04-09 21:13:58 -0700581 /* Ignore overly long pathnames! */
582 if (len + baselen + 8 > sizeof(fullname))
583 continue;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700584 memcpy(fullname + baselen, de->d_name, len+1);
Linus Torvalds9fc42d62007-03-30 20:39:30 -0700585 if (simplify_away(fullname, baselen + len, simplify))
586 continue;
Michael Spangb9916252007-05-06 22:35:04 -0400587
Junio C Hamano6831a882008-01-31 20:23:25 -0800588 dtype = DTYPE(de);
589 exclude = excluded(dir, fullname, &dtype);
Jeff Kinge96980e2007-06-12 23:42:14 +0200590 if (exclude && dir->collect_ignored
591 && in_pathspec(fullname, baselen + len, simplify))
Jeff King2abd31b2007-06-11 09:39:50 -0400592 dir_add_ignored(dir, fullname, baselen + len);
Linus Torvalds07134422007-10-19 10:59:22 -0700593
594 /*
595 * Excluded? If we don't explicitly want to show
596 * ignored files, ignore it
597 */
598 if (exclude && !dir->show_ignored)
599 continue;
600
Junio C Hamano6831a882008-01-31 20:23:25 -0800601 if (dtype == DT_UNKNOWN)
602 dtype = get_dtype(de, fullname);
Linus Torvalds07134422007-10-19 10:59:22 -0700603
604 /*
605 * Do we want to see just the ignored files?
606 * We still need to recurse into directories,
607 * even if we don't ignore them, since the
608 * directory may contain files that we do..
609 */
610 if (!exclude && dir->show_ignored) {
611 if (dtype != DT_DIR)
Junio C Hamanoc8897632006-12-29 10:08:19 -0800612 continue;
Junio C Hamanoc8897632006-12-29 10:08:19 -0800613 }
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700614
Linus Torvalds07134422007-10-19 10:59:22 -0700615 switch (dtype) {
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700616 default:
617 continue;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700618 case DT_DIR:
619 memcpy(fullname + baselen + len, "/", 2);
620 len++;
Linus Torvalds09595252007-04-11 14:49:44 -0700621 switch (treat_directory(dir, fullname, baselen + len, simplify)) {
622 case show_directory:
Michael Spangb9916252007-05-06 22:35:04 -0400623 if (exclude != dir->show_ignored)
624 continue;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700625 break;
Linus Torvalds09595252007-04-11 14:49:44 -0700626 case recurse_into_directory:
627 contents += read_directory_recursive(dir,
628 fullname, fullname, baselen + len, 0, simplify);
629 continue;
630 case ignore_directory:
631 continue;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700632 }
Linus Torvalds09595252007-04-11 14:49:44 -0700633 break;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700634 case DT_REG:
635 case DT_LNK:
636 break;
637 }
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700638 contents++;
Johannes Schindelin07ccbff2006-09-28 02:44:30 +0200639 if (check_only)
640 goto exit_early;
641 else
Junio C Hamano4d06f8a2006-12-29 11:01:31 -0800642 dir_add_name(dir, fullname, baselen + len);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700643 }
Johannes Schindelin07ccbff2006-09-28 02:44:30 +0200644exit_early:
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700645 closedir(fdir);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700646 }
647
648 return contents;
649}
650
651static int cmp_name(const void *p1, const void *p2)
652{
653 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
654 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
655
656 return cache_name_compare(e1->name, e1->len,
657 e2->name, e2->len);
658}
659
Linus Torvalds9fc42d62007-03-30 20:39:30 -0700660/*
661 * Return the length of the "simple" part of a path match limiter.
662 */
663static int simple_length(const char *match)
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700664{
Linus Torvalds9fc42d62007-03-30 20:39:30 -0700665 const char special[256] = {
666 [0] = 1, ['?'] = 1,
667 ['\\'] = 1, ['*'] = 1,
668 ['['] = 1
669 };
670 int len = -1;
671
672 for (;;) {
673 unsigned char c = *match++;
674 len++;
675 if (special[c])
676 return len;
677 }
678}
679
680static struct path_simplify *create_simplify(const char **pathspec)
681{
682 int nr, alloc = 0;
683 struct path_simplify *simplify = NULL;
684
685 if (!pathspec)
686 return NULL;
687
688 for (nr = 0 ; ; nr++) {
689 const char *match;
690 if (nr >= alloc) {
691 alloc = alloc_nr(alloc);
692 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
693 }
694 match = *pathspec++;
695 if (!match)
696 break;
697 simplify[nr].path = match;
698 simplify[nr].len = simple_length(match);
699 }
700 simplify[nr].path = NULL;
701 simplify[nr].len = 0;
702 return simplify;
703}
704
705static void free_simplify(struct path_simplify *simplify)
706{
Jim Meyering8e0f7002008-01-31 18:26:32 +0100707 free(simplify);
Linus Torvalds9fc42d62007-03-30 20:39:30 -0700708}
709
710int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
711{
712 struct path_simplify *simplify = create_simplify(pathspec);
Linus Torvaldsb4189aa2006-05-16 19:46:16 -0700713
Linus Torvalds9fc42d62007-03-30 20:39:30 -0700714 read_directory_recursive(dir, path, base, baselen, 0, simplify);
715 free_simplify(simplify);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700716 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
Jeff King2abd31b2007-06-11 09:39:50 -0400717 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700718 return dir->nr;
719}
Jeff Kingc91f0d92006-09-08 04:05:34 -0400720
Junio C Hamano686a4a02007-11-29 01:11:46 -0800721int file_exists(const char *f)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400722{
Junio C Hamano686a4a02007-11-29 01:11:46 -0800723 struct stat sb;
Junio C Hamanoa50f9fc52007-11-18 01:58:16 -0800724 return lstat(f, &sb) == 0;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400725}
Johannes Schindeline6636742007-08-01 01:29:17 +0100726
727/*
728 * get_relative_cwd() gets the prefix of the current working directory
729 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
Johannes Schindelin420acb32007-08-01 19:26:59 +0100730 *
731 * As a convenience, it also returns NULL if 'dir' is already NULL. The
732 * reason for this behaviour is that it is natural for functions returning
733 * directory names to return NULL to say "this directory does not exist"
734 * or "this directory is invalid". These cases are usually handled the
735 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
736 * returns NULL for both of them.
737 *
738 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
739 * unifies the handling of "outside work tree" with "no work tree at all".
Johannes Schindeline6636742007-08-01 01:29:17 +0100740 */
741char *get_relative_cwd(char *buffer, int size, const char *dir)
742{
743 char *cwd = buffer;
744
Johannes Schindeline6636742007-08-01 01:29:17 +0100745 if (!dir)
746 return NULL;
747 if (!getcwd(buffer, size))
748 die("can't find the current directory: %s", strerror(errno));
749
750 if (!is_absolute_path(dir))
751 dir = make_absolute_path(dir);
752
753 while (*dir && *dir == *cwd) {
754 dir++;
755 cwd++;
756 }
757 if (*dir)
758 return NULL;
759 if (*cwd == '/')
760 return cwd + 1;
761 return cwd;
762}
763
764int is_inside_dir(const char *dir)
765{
766 char buffer[PATH_MAX];
767 return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
768}
Johannes Schindelin7155b722007-09-28 16:28:54 +0100769
770int remove_dir_recursively(struct strbuf *path, int only_empty)
771{
772 DIR *dir = opendir(path->buf);
773 struct dirent *e;
774 int ret = 0, original_len = path->len, len;
775
776 if (!dir)
777 return -1;
778 if (path->buf[original_len - 1] != '/')
779 strbuf_addch(path, '/');
780
781 len = path->len;
782 while ((e = readdir(dir)) != NULL) {
783 struct stat st;
784 if ((e->d_name[0] == '.') &&
785 ((e->d_name[1] == 0) ||
786 ((e->d_name[1] == '.') && e->d_name[2] == 0)))
787 continue; /* "." and ".." */
788
789 strbuf_setlen(path, len);
790 strbuf_addstr(path, e->d_name);
791 if (lstat(path->buf, &st))
792 ; /* fall thru */
793 else if (S_ISDIR(st.st_mode)) {
794 if (!remove_dir_recursively(path, only_empty))
795 continue; /* happy */
796 } else if (!only_empty && !unlink(path->buf))
797 continue; /* happy, too */
798
799 /* path too long, stat fails, or non-directory still exists */
800 ret = -1;
801 break;
802 }
803 closedir(dir);
804
805 strbuf_setlen(path, original_len);
806 if (!ret)
807 ret = rmdir(path->buf);
808 return ret;
809}
Junio C Hamano039bc642007-11-14 00:05:00 -0800810
811void setup_standard_excludes(struct dir_struct *dir)
812{
813 const char *path;
814
815 dir->exclude_per_dir = ".gitignore";
816 path = git_path("info/exclude");
817 if (!access(path, R_OK))
818 add_excludes_from_file(dir, path);
819 if (excludes_file && !access(excludes_file, R_OK))
820 add_excludes_from_file(dir, excludes_file);
821}