Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 8 | #include "cache.h" |
| 9 | #include "dir.h" |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 10 | #include "refs.h" |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 11 | |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 12 | struct path_simplify { |
| 13 | int len; |
| 14 | const char *path; |
| 15 | }; |
| 16 | |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 17 | static int read_directory_recursive(struct dir_struct *dir, const char *path, int len, |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 18 | int check_only, const struct path_simplify *simplify); |
Linus Torvalds | caa6b78 | 2009-07-08 19:31:49 -0700 | [diff] [blame] | 19 | static int get_dtype(struct dirent *de, const char *path, int len); |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 21 | static int common_prefix(const char **pathspec) |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 22 | { |
| 23 | const char *path, *slash, *next; |
| 24 | int prefix; |
| 25 | |
| 26 | if (!pathspec) |
| 27 | return 0; |
| 28 | |
| 29 | path = *pathspec; |
| 30 | slash = strrchr(path, '/'); |
| 31 | if (!slash) |
| 32 | return 0; |
| 33 | |
| 34 | prefix = slash - path + 1; |
| 35 | while ((next = *++pathspec) != NULL) { |
| 36 | int len = strlen(next); |
Johannes Schindelin | c7f34c1 | 2007-04-23 10:21:25 +0200 | [diff] [blame] | 37 | if (len >= prefix && !memcmp(path, next, prefix)) |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 38 | continue; |
Johannes Schindelin | c7f34c1 | 2007-04-23 10:21:25 +0200 | [diff] [blame] | 39 | len = prefix - 1; |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 40 | for (;;) { |
| 41 | if (!len) |
| 42 | return 0; |
| 43 | if (next[--len] != '/') |
| 44 | continue; |
| 45 | if (memcmp(path, next, len+1)) |
| 46 | continue; |
| 47 | prefix = len + 1; |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | return prefix; |
| 52 | } |
| 53 | |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 54 | int fill_directory(struct dir_struct *dir, const char **pathspec) |
| 55 | { |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 56 | const char *path; |
| 57 | int len; |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * Calculate common prefix for the pathspec, and |
| 61 | * use that to optimize the directory walk |
| 62 | */ |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 63 | len = common_prefix(pathspec); |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 64 | path = ""; |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 65 | |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 66 | if (len) |
| 67 | path = xmemdupz(*pathspec, len); |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 68 | |
| 69 | /* Read the directory and prune it */ |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 70 | read_directory(dir, path, len, pathspec); |
| 71 | return len; |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Junio C Hamano | e813d50 | 2006-12-25 03:09:52 -0800 | [diff] [blame] | 74 | /* |
Allan Caffee | 2c5b011 | 2009-05-04 13:37:30 -0400 | [diff] [blame] | 75 | * Does 'match' match the given name? |
Junio C Hamano | e813d50 | 2006-12-25 03:09:52 -0800 | [diff] [blame] | 76 | * A match is found if |
| 77 | * |
| 78 | * (1) the 'match' string is leading directory of 'name', or |
| 79 | * (2) the 'match' string is a wildcard and matches 'name', or |
| 80 | * (3) the 'match' string is exactly the same as 'name'. |
| 81 | * |
| 82 | * and the return value tells which case it was. |
| 83 | * |
| 84 | * It returns 0 when there is no match. |
| 85 | */ |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 86 | static int match_one(const char *match, const char *name, int namelen) |
| 87 | { |
| 88 | int matchlen; |
| 89 | |
| 90 | /* If the match was just the prefix, we matched */ |
Linus Torvalds | 88ea811 | 2008-04-19 14:22:38 -0700 | [diff] [blame] | 91 | if (!*match) |
Junio C Hamano | e813d50 | 2006-12-25 03:09:52 -0800 | [diff] [blame] | 92 | return MATCHED_RECURSIVELY; |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 93 | |
Linus Torvalds | 88ea811 | 2008-04-19 14:22:38 -0700 | [diff] [blame] | 94 | for (;;) { |
| 95 | unsigned char c1 = *match; |
| 96 | unsigned char c2 = *name; |
René Scharfe | 8cc3299 | 2009-01-17 16:50:34 +0100 | [diff] [blame] | 97 | if (c1 == '\0' || is_glob_special(c1)) |
Linus Torvalds | 88ea811 | 2008-04-19 14:22:38 -0700 | [diff] [blame] | 98 | break; |
| 99 | if (c1 != c2) |
| 100 | return 0; |
| 101 | match++; |
| 102 | name++; |
| 103 | namelen--; |
| 104 | } |
| 105 | |
| 106 | |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 107 | /* |
| 108 | * If we don't match the matchstring exactly, |
| 109 | * we need to match by fnmatch |
| 110 | */ |
Linus Torvalds | 88ea811 | 2008-04-19 14:22:38 -0700 | [diff] [blame] | 111 | matchlen = strlen(match); |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 112 | if (strncmp(match, name, matchlen)) |
Junio C Hamano | e813d50 | 2006-12-25 03:09:52 -0800 | [diff] [blame] | 113 | return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0; |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 114 | |
Shawn Bohrer | f2d0df7 | 2008-04-14 22:14:09 -0500 | [diff] [blame] | 115 | if (namelen == matchlen) |
Junio C Hamano | e813d50 | 2006-12-25 03:09:52 -0800 | [diff] [blame] | 116 | return MATCHED_EXACTLY; |
| 117 | if (match[matchlen-1] == '/' || name[matchlen] == '/') |
| 118 | return MATCHED_RECURSIVELY; |
| 119 | return 0; |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Junio C Hamano | e813d50 | 2006-12-25 03:09:52 -0800 | [diff] [blame] | 122 | /* |
| 123 | * Given a name and a list of pathspecs, see if the name matches |
| 124 | * any of the pathspecs. The caller is also interested in seeing |
| 125 | * all pathspec matches some names it calls this function with |
| 126 | * (otherwise the user could have mistyped the unmatched pathspec), |
| 127 | * and a mark is left in seen[] array for pathspec element that |
| 128 | * actually matched anything. |
| 129 | */ |
Clemens Buchacher | 0b50922 | 2009-01-14 15:54:35 +0100 | [diff] [blame] | 130 | int match_pathspec(const char **pathspec, const char *name, int namelen, |
| 131 | int prefix, char *seen) |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 132 | { |
Clemens Buchacher | 0b50922 | 2009-01-14 15:54:35 +0100 | [diff] [blame] | 133 | int i, retval = 0; |
| 134 | |
| 135 | if (!pathspec) |
| 136 | return 1; |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 137 | |
| 138 | name += prefix; |
| 139 | namelen -= prefix; |
| 140 | |
Clemens Buchacher | 0b50922 | 2009-01-14 15:54:35 +0100 | [diff] [blame] | 141 | for (i = 0; pathspec[i] != NULL; i++) { |
Junio C Hamano | e813d50 | 2006-12-25 03:09:52 -0800 | [diff] [blame] | 142 | int how; |
Clemens Buchacher | 0b50922 | 2009-01-14 15:54:35 +0100 | [diff] [blame] | 143 | const char *match = pathspec[i] + prefix; |
| 144 | if (seen && seen[i] == MATCHED_EXACTLY) |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 145 | continue; |
Junio C Hamano | e813d50 | 2006-12-25 03:09:52 -0800 | [diff] [blame] | 146 | how = match_one(match, name, namelen); |
| 147 | if (how) { |
| 148 | if (retval < how) |
| 149 | retval = how; |
Clemens Buchacher | 0b50922 | 2009-01-14 15:54:35 +0100 | [diff] [blame] | 150 | if (seen && seen[i] < how) |
| 151 | seen[i] = how; |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | return retval; |
| 155 | } |
| 156 | |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 157 | static int no_wildcard(const char *string) |
| 158 | { |
Finn Arne Gangstad | dd482ee | 2009-02-10 15:20:17 +0100 | [diff] [blame] | 159 | return string[strcspn(string, "*?[{\\")] == '\0'; |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 162 | void add_exclude(const char *string, const char *base, |
| 163 | int baselen, struct exclude_list *which) |
| 164 | { |
Junio C Hamano | d6b8fc3 | 2008-01-31 01:17:48 -0800 | [diff] [blame] | 165 | struct exclude *x; |
| 166 | size_t len; |
| 167 | int to_exclude = 1; |
| 168 | int flags = 0; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 169 | |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 170 | if (*string == '!') { |
Junio C Hamano | d6b8fc3 | 2008-01-31 01:17:48 -0800 | [diff] [blame] | 171 | to_exclude = 0; |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 172 | string++; |
| 173 | } |
Junio C Hamano | d6b8fc3 | 2008-01-31 01:17:48 -0800 | [diff] [blame] | 174 | len = strlen(string); |
| 175 | if (len && string[len - 1] == '/') { |
| 176 | char *s; |
| 177 | x = xmalloc(sizeof(*x) + len); |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 178 | s = (char *)(x+1); |
Junio C Hamano | d6b8fc3 | 2008-01-31 01:17:48 -0800 | [diff] [blame] | 179 | memcpy(s, string, len - 1); |
| 180 | s[len - 1] = '\0'; |
| 181 | string = s; |
| 182 | x->pattern = s; |
| 183 | flags = EXC_FLAG_MUSTBEDIR; |
| 184 | } else { |
| 185 | x = xmalloc(sizeof(*x)); |
| 186 | x->pattern = string; |
| 187 | } |
| 188 | x->to_exclude = to_exclude; |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 189 | x->patternlen = strlen(string); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 190 | x->base = base; |
| 191 | x->baselen = baselen; |
Junio C Hamano | d6b8fc3 | 2008-01-31 01:17:48 -0800 | [diff] [blame] | 192 | x->flags = flags; |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 193 | if (!strchr(string, '/')) |
| 194 | x->flags |= EXC_FLAG_NODIR; |
| 195 | if (no_wildcard(string)) |
| 196 | x->flags |= EXC_FLAG_NOWILDCARD; |
| 197 | if (*string == '*' && no_wildcard(string+1)) |
| 198 | x->flags |= EXC_FLAG_ENDSWITH; |
Junio C Hamano | 686a4a0 | 2007-11-29 01:11:46 -0800 | [diff] [blame] | 199 | ALLOC_GROW(which->excludes, which->nr + 1, which->alloc); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 200 | which->excludes[which->nr++] = x; |
| 201 | } |
| 202 | |
| 203 | static int add_excludes_from_file_1(const char *fname, |
| 204 | const char *base, |
| 205 | int baselen, |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 206 | char **buf_p, |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 207 | struct exclude_list *which) |
| 208 | { |
Jonas Fonseca | c470701 | 2006-08-28 01:55:46 +0200 | [diff] [blame] | 209 | struct stat st; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 210 | int fd, i; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 211 | size_t size; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 212 | char *buf, *entry; |
| 213 | |
| 214 | fd = open(fname, O_RDONLY); |
Jonas Fonseca | c470701 | 2006-08-28 01:55:46 +0200 | [diff] [blame] | 215 | if (fd < 0 || fstat(fd, &st) < 0) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 216 | goto err; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 217 | size = xsize_t(st.st_size); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 218 | if (size == 0) { |
| 219 | close(fd); |
| 220 | return 0; |
| 221 | } |
| 222 | buf = xmalloc(size+1); |
Andy Whitcroft | 93d26e4 | 2007-01-08 15:58:08 +0000 | [diff] [blame] | 223 | if (read_in_full(fd, buf, size) != size) |
李鸿 | 6ba7823 | 2007-12-16 12:53:26 +0800 | [diff] [blame] | 224 | { |
| 225 | free(buf); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 226 | goto err; |
李鸿 | 6ba7823 | 2007-12-16 12:53:26 +0800 | [diff] [blame] | 227 | } |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 228 | close(fd); |
| 229 | |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 230 | if (buf_p) |
| 231 | *buf_p = buf; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 232 | buf[size++] = '\n'; |
| 233 | entry = buf; |
| 234 | for (i = 0; i < size; i++) { |
| 235 | if (buf[i] == '\n') { |
| 236 | if (entry != buf + i && entry[0] != '#') { |
| 237 | buf[i - (i && buf[i-1] == '\r')] = 0; |
| 238 | add_exclude(entry, base, baselen, which); |
| 239 | } |
| 240 | entry = buf + i + 1; |
| 241 | } |
| 242 | } |
| 243 | return 0; |
| 244 | |
| 245 | err: |
| 246 | if (0 <= fd) |
| 247 | close(fd); |
| 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | void add_excludes_from_file(struct dir_struct *dir, const char *fname) |
| 252 | { |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 253 | if (add_excludes_from_file_1(fname, "", 0, NULL, |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 254 | &dir->exclude_list[EXC_FILE]) < 0) |
| 255 | die("cannot use %s as an exclude file", fname); |
| 256 | } |
| 257 | |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 258 | static void prep_exclude(struct dir_struct *dir, const char *base, int baselen) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 259 | { |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 260 | struct exclude_list *el; |
| 261 | struct exclude_stack *stk = NULL; |
| 262 | int current; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 263 | |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 264 | if ((!dir->exclude_per_dir) || |
| 265 | (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX)) |
| 266 | return; /* too long a path -- ignore */ |
| 267 | |
| 268 | /* Pop the ones that are not the prefix of the path being checked. */ |
| 269 | el = &dir->exclude_list[EXC_DIRS]; |
| 270 | while ((stk = dir->exclude_stack) != NULL) { |
| 271 | if (stk->baselen <= baselen && |
| 272 | !strncmp(dir->basebuf, base, stk->baselen)) |
| 273 | break; |
| 274 | dir->exclude_stack = stk->prev; |
| 275 | while (stk->exclude_ix < el->nr) |
| 276 | free(el->excludes[--el->nr]); |
| 277 | free(stk->filebuf); |
| 278 | free(stk); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 279 | } |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 280 | |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 281 | /* Read from the parent directories and push them down. */ |
| 282 | current = stk ? stk->baselen : -1; |
| 283 | while (current < baselen) { |
| 284 | struct exclude_stack *stk = xcalloc(1, sizeof(*stk)); |
| 285 | const char *cp; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 286 | |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 287 | if (current < 0) { |
| 288 | cp = base; |
| 289 | current = 0; |
| 290 | } |
| 291 | else { |
| 292 | cp = strchr(base + current + 1, '/'); |
| 293 | if (!cp) |
| 294 | die("oops in prep_exclude"); |
| 295 | cp++; |
| 296 | } |
| 297 | stk->prev = dir->exclude_stack; |
| 298 | stk->baselen = cp - base; |
| 299 | stk->exclude_ix = el->nr; |
| 300 | memcpy(dir->basebuf + current, base + current, |
| 301 | stk->baselen - current); |
| 302 | strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir); |
| 303 | add_excludes_from_file_1(dir->basebuf, |
| 304 | dir->basebuf, stk->baselen, |
| 305 | &stk->filebuf, el); |
| 306 | dir->exclude_stack = stk; |
| 307 | current = stk->baselen; |
| 308 | } |
| 309 | dir->basebuf[baselen] = '\0'; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Allan Caffee | 2c5b011 | 2009-05-04 13:37:30 -0400 | [diff] [blame] | 312 | /* Scan the list and let the last match determine the fate. |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 313 | * Return 1 for exclude, 0 for include and -1 for undecided. |
| 314 | */ |
| 315 | static int excluded_1(const char *pathname, |
Junio C Hamano | 6831a88 | 2008-01-31 20:23:25 -0800 | [diff] [blame] | 316 | int pathlen, const char *basename, int *dtype, |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 317 | struct exclude_list *el) |
| 318 | { |
| 319 | int i; |
| 320 | |
| 321 | if (el->nr) { |
| 322 | for (i = el->nr - 1; 0 <= i; i--) { |
| 323 | struct exclude *x = el->excludes[i]; |
| 324 | const char *exclude = x->pattern; |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 325 | int to_exclude = x->to_exclude; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 326 | |
Junio C Hamano | 6831a88 | 2008-01-31 20:23:25 -0800 | [diff] [blame] | 327 | if (x->flags & EXC_FLAG_MUSTBEDIR) { |
| 328 | if (*dtype == DT_UNKNOWN) |
Linus Torvalds | caa6b78 | 2009-07-08 19:31:49 -0700 | [diff] [blame] | 329 | *dtype = get_dtype(NULL, pathname, pathlen); |
Junio C Hamano | 6831a88 | 2008-01-31 20:23:25 -0800 | [diff] [blame] | 330 | if (*dtype != DT_DIR) |
| 331 | continue; |
| 332 | } |
Junio C Hamano | d6b8fc3 | 2008-01-31 01:17:48 -0800 | [diff] [blame] | 333 | |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 334 | if (x->flags & EXC_FLAG_NODIR) { |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 335 | /* match basename */ |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 336 | if (x->flags & EXC_FLAG_NOWILDCARD) { |
| 337 | if (!strcmp(exclude, basename)) |
| 338 | return to_exclude; |
| 339 | } else if (x->flags & EXC_FLAG_ENDSWITH) { |
| 340 | if (x->patternlen - 1 <= pathlen && |
| 341 | !strcmp(exclude + 1, pathname + pathlen - x->patternlen + 1)) |
| 342 | return to_exclude; |
| 343 | } else { |
| 344 | if (fnmatch(exclude, basename, 0) == 0) |
| 345 | return to_exclude; |
| 346 | } |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 347 | } |
| 348 | else { |
| 349 | /* match with FNM_PATHNAME: |
| 350 | * exclude has base (baselen long) implicitly |
| 351 | * in front of it. |
| 352 | */ |
| 353 | int baselen = x->baselen; |
| 354 | if (*exclude == '/') |
| 355 | exclude++; |
| 356 | |
| 357 | if (pathlen < baselen || |
| 358 | (baselen && pathname[baselen-1] != '/') || |
| 359 | strncmp(pathname, x->base, baselen)) |
| 360 | continue; |
| 361 | |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 362 | if (x->flags & EXC_FLAG_NOWILDCARD) { |
| 363 | if (!strcmp(exclude, pathname + baselen)) |
| 364 | return to_exclude; |
| 365 | } else { |
| 366 | if (fnmatch(exclude, pathname+baselen, |
| 367 | FNM_PATHNAME) == 0) |
| 368 | return to_exclude; |
| 369 | } |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | } |
| 373 | return -1; /* undecided */ |
| 374 | } |
| 375 | |
Junio C Hamano | 6831a88 | 2008-01-31 20:23:25 -0800 | [diff] [blame] | 376 | int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 377 | { |
| 378 | int pathlen = strlen(pathname); |
| 379 | int st; |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 380 | const char *basename = strrchr(pathname, '/'); |
| 381 | basename = (basename) ? basename+1 : pathname; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 382 | |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 383 | prep_exclude(dir, pathname, basename-pathname); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 384 | for (st = EXC_CMDL; st <= EXC_FILE; st++) { |
Junio C Hamano | d6b8fc3 | 2008-01-31 01:17:48 -0800 | [diff] [blame] | 385 | switch (excluded_1(pathname, pathlen, basename, |
Junio C Hamano | 6831a88 | 2008-01-31 20:23:25 -0800 | [diff] [blame] | 386 | dtype_p, &dir->exclude_list[st])) { |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 387 | case 0: |
| 388 | return 0; |
| 389 | case 1: |
| 390 | return 1; |
| 391 | } |
| 392 | } |
| 393 | return 0; |
| 394 | } |
| 395 | |
Junio C Hamano | f3fa183 | 2007-11-08 15:35:32 -0800 | [diff] [blame] | 396 | static struct dir_entry *dir_entry_new(const char *pathname, int len) |
| 397 | { |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 398 | struct dir_entry *ent; |
| 399 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 400 | ent = xmalloc(sizeof(*ent) + len + 1); |
| 401 | ent->len = len; |
| 402 | memcpy(ent->name, pathname, len); |
| 403 | ent->name[len] = 0; |
Junio C Hamano | 4d06f8a | 2006-12-29 11:01:31 -0800 | [diff] [blame] | 404 | return ent; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Nanako Shiraishi | 159b321 | 2008-10-02 19:14:23 +0900 | [diff] [blame] | 407 | static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len) |
Jeff King | 6815e56 | 2007-06-11 09:39:44 -0400 | [diff] [blame] | 408 | { |
Linus Torvalds | 0a9b88b | 2008-03-21 16:52:46 -0700 | [diff] [blame] | 409 | if (cache_name_exists(pathname, len, ignore_case)) |
Jeff King | 6815e56 | 2007-06-11 09:39:44 -0400 | [diff] [blame] | 410 | return NULL; |
| 411 | |
Jeff King | 25fd2f7 | 2007-06-16 18:43:40 -0400 | [diff] [blame] | 412 | ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); |
Jeff King | 6815e56 | 2007-06-11 09:39:44 -0400 | [diff] [blame] | 413 | return dir->entries[dir->nr++] = dir_entry_new(pathname, len); |
| 414 | } |
| 415 | |
Nanako Shiraishi | 159b321 | 2008-10-02 19:14:23 +0900 | [diff] [blame] | 416 | static struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len) |
Jeff King | 2abd31b | 2007-06-11 09:39:50 -0400 | [diff] [blame] | 417 | { |
Jeff King | 6e4f981 | 2009-05-30 17:54:18 -0400 | [diff] [blame] | 418 | if (!cache_name_is_other(pathname, len)) |
Jeff King | 2abd31b | 2007-06-11 09:39:50 -0400 | [diff] [blame] | 419 | return NULL; |
| 420 | |
Jeff King | 25fd2f7 | 2007-06-16 18:43:40 -0400 | [diff] [blame] | 421 | ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); |
Jeff King | 2abd31b | 2007-06-11 09:39:50 -0400 | [diff] [blame] | 422 | return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len); |
| 423 | } |
| 424 | |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 425 | enum exist_status { |
| 426 | index_nonexistent = 0, |
| 427 | index_directory, |
| 428 | index_gitdir, |
| 429 | }; |
| 430 | |
| 431 | /* |
| 432 | * The index sorts alphabetically by entry name, which |
| 433 | * means that a gitlink sorts as '\0' at the end, while |
| 434 | * a directory (which is defined not as an entry, but as |
| 435 | * the files it contains) will sort with the '/' at the |
| 436 | * end. |
| 437 | */ |
| 438 | static enum exist_status directory_exists_in_index(const char *dirname, int len) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 439 | { |
| 440 | int pos = cache_name_pos(dirname, len); |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 441 | if (pos < 0) |
| 442 | pos = -pos-1; |
| 443 | while (pos < active_nr) { |
| 444 | struct cache_entry *ce = active_cache[pos++]; |
| 445 | unsigned char endchar; |
| 446 | |
| 447 | if (strncmp(ce->name, dirname, len)) |
| 448 | break; |
| 449 | endchar = ce->name[len]; |
| 450 | if (endchar > '/') |
| 451 | break; |
| 452 | if (endchar == '/') |
| 453 | return index_directory; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 454 | if (!endchar && S_ISGITLINK(ce->ce_mode)) |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 455 | return index_gitdir; |
| 456 | } |
| 457 | return index_nonexistent; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * When we find a directory when traversing the filesystem, we |
| 462 | * have three distinct cases: |
| 463 | * |
| 464 | * - ignore it |
| 465 | * - see it as a directory |
| 466 | * - recurse into it |
| 467 | * |
| 468 | * and which one we choose depends on a combination of existing |
| 469 | * git index contents and the flags passed into the directory |
| 470 | * traversal routine. |
| 471 | * |
| 472 | * Case 1: If we *already* have entries in the index under that |
| 473 | * directory name, we always recurse into the directory to see |
| 474 | * all the files. |
| 475 | * |
| 476 | * Case 2: If we *already* have that directory name as a gitlink, |
| 477 | * we always continue to see it as a gitlink, regardless of whether |
| 478 | * there is an actual git directory there or not (it might not |
| 479 | * be checked out as a subproject!) |
| 480 | * |
| 481 | * Case 3: if we didn't have it in the index previously, we |
| 482 | * have a few sub-cases: |
| 483 | * |
| 484 | * (a) if "show_other_directories" is true, we show it as |
| 485 | * just a directory, unless "hide_empty_directories" is |
| 486 | * also true and the directory is empty, in which case |
| 487 | * we just ignore it entirely. |
| 488 | * (b) if it looks like a git directory, and we don't have |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 489 | * 'no_gitlinks' set we treat it as a gitlink, and show it |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 490 | * as a directory. |
| 491 | * (c) otherwise, we recurse into it. |
| 492 | */ |
| 493 | enum directory_treatment { |
| 494 | show_directory, |
| 495 | ignore_directory, |
| 496 | recurse_into_directory, |
| 497 | }; |
| 498 | |
| 499 | static enum directory_treatment treat_directory(struct dir_struct *dir, |
| 500 | const char *dirname, int len, |
| 501 | const struct path_simplify *simplify) |
| 502 | { |
| 503 | /* The "len-1" is to strip the final '/' */ |
| 504 | switch (directory_exists_in_index(dirname, len-1)) { |
| 505 | case index_directory: |
| 506 | return recurse_into_directory; |
| 507 | |
| 508 | case index_gitdir: |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 509 | if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES) |
Linus Torvalds | ab22aed | 2007-04-12 14:32:21 -0700 | [diff] [blame] | 510 | return ignore_directory; |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 511 | return show_directory; |
| 512 | |
| 513 | case index_nonexistent: |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 514 | if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES) |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 515 | break; |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 516 | if (!(dir->flags & DIR_NO_GITLINKS)) { |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 517 | unsigned char sha1[20]; |
| 518 | if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0) |
| 519 | return show_directory; |
| 520 | } |
| 521 | return recurse_into_directory; |
| 522 | } |
| 523 | |
| 524 | /* This is the "show_other_directories" case */ |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 525 | if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 526 | return show_directory; |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 527 | if (!read_directory_recursive(dir, dirname, len, 1, simplify)) |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 528 | return ignore_directory; |
| 529 | return show_directory; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /* |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 533 | * This is an inexact early pruning of any recursive directory |
| 534 | * reading - if the path cannot possibly be in the pathspec, |
| 535 | * return true, and we'll skip it early. |
| 536 | */ |
| 537 | static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify) |
| 538 | { |
| 539 | if (simplify) { |
| 540 | for (;;) { |
| 541 | const char *match = simplify->path; |
| 542 | int len = simplify->len; |
| 543 | |
| 544 | if (!match) |
| 545 | break; |
| 546 | if (len > pathlen) |
| 547 | len = pathlen; |
| 548 | if (!memcmp(path, match, len)) |
| 549 | return 0; |
| 550 | simplify++; |
| 551 | } |
| 552 | return 1; |
| 553 | } |
| 554 | return 0; |
| 555 | } |
| 556 | |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 557 | static int in_pathspec(const char *path, int len, const struct path_simplify *simplify) |
| 558 | { |
| 559 | if (simplify) { |
| 560 | for (; simplify->path; simplify++) { |
| 561 | if (len == simplify->len |
| 562 | && !memcmp(path, simplify->path, len)) |
| 563 | return 1; |
| 564 | } |
| 565 | } |
| 566 | return 0; |
| 567 | } |
| 568 | |
Linus Torvalds | 443e061 | 2009-07-09 13:14:28 -0700 | [diff] [blame] | 569 | static int get_index_dtype(const char *path, int len) |
| 570 | { |
| 571 | int pos; |
| 572 | struct cache_entry *ce; |
| 573 | |
| 574 | ce = cache_name_exists(path, len, 0); |
| 575 | if (ce) { |
| 576 | if (!ce_uptodate(ce)) |
| 577 | return DT_UNKNOWN; |
| 578 | if (S_ISGITLINK(ce->ce_mode)) |
| 579 | return DT_DIR; |
| 580 | /* |
| 581 | * Nobody actually cares about the |
| 582 | * difference between DT_LNK and DT_REG |
| 583 | */ |
| 584 | return DT_REG; |
| 585 | } |
| 586 | |
| 587 | /* Try to look it up as a directory */ |
| 588 | pos = cache_name_pos(path, len); |
| 589 | if (pos >= 0) |
| 590 | return DT_UNKNOWN; |
| 591 | pos = -pos-1; |
| 592 | while (pos < active_nr) { |
| 593 | ce = active_cache[pos++]; |
| 594 | if (strncmp(ce->name, path, len)) |
| 595 | break; |
| 596 | if (ce->name[len] > '/') |
| 597 | break; |
| 598 | if (ce->name[len] < '/') |
| 599 | continue; |
| 600 | if (!ce_uptodate(ce)) |
| 601 | break; /* continue? */ |
| 602 | return DT_DIR; |
| 603 | } |
| 604 | return DT_UNKNOWN; |
| 605 | } |
| 606 | |
Linus Torvalds | caa6b78 | 2009-07-08 19:31:49 -0700 | [diff] [blame] | 607 | static int get_dtype(struct dirent *de, const char *path, int len) |
Linus Torvalds | 0713442 | 2007-10-19 10:59:22 -0700 | [diff] [blame] | 608 | { |
Junio C Hamano | 6831a88 | 2008-01-31 20:23:25 -0800 | [diff] [blame] | 609 | int dtype = de ? DTYPE(de) : DT_UNKNOWN; |
Linus Torvalds | 0713442 | 2007-10-19 10:59:22 -0700 | [diff] [blame] | 610 | struct stat st; |
| 611 | |
| 612 | if (dtype != DT_UNKNOWN) |
| 613 | return dtype; |
Linus Torvalds | 443e061 | 2009-07-09 13:14:28 -0700 | [diff] [blame] | 614 | dtype = get_index_dtype(path, len); |
| 615 | if (dtype != DT_UNKNOWN) |
| 616 | return dtype; |
| 617 | if (lstat(path, &st)) |
Linus Torvalds | 0713442 | 2007-10-19 10:59:22 -0700 | [diff] [blame] | 618 | return dtype; |
| 619 | if (S_ISREG(st.st_mode)) |
| 620 | return DT_REG; |
| 621 | if (S_ISDIR(st.st_mode)) |
| 622 | return DT_DIR; |
| 623 | if (S_ISLNK(st.st_mode)) |
| 624 | return DT_LNK; |
| 625 | return dtype; |
| 626 | } |
| 627 | |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 628 | /* |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 629 | * Read a directory tree. We currently ignore anything but |
| 630 | * directories, regular files and symlinks. That's because git |
| 631 | * doesn't handle them at all yet. Maybe that will change some |
| 632 | * day. |
| 633 | * |
| 634 | * Also, we ignore the name ".git" (even if it is not a directory). |
| 635 | * That likely will not change. |
| 636 | */ |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 637 | static int read_directory_recursive(struct dir_struct *dir, const char *base, int baselen, int check_only, const struct path_simplify *simplify) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 638 | { |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 639 | DIR *fdir = opendir(*base ? base : "."); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 640 | int contents = 0; |
| 641 | |
| 642 | if (fdir) { |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 643 | struct dirent *de; |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 644 | char path[PATH_MAX + 1]; |
| 645 | memcpy(path, base, baselen); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 646 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 647 | while ((de = readdir(fdir)) != NULL) { |
Linus Torvalds | 0713442 | 2007-10-19 10:59:22 -0700 | [diff] [blame] | 648 | int len, dtype; |
Michael Spang | b991625 | 2007-05-06 22:35:04 -0400 | [diff] [blame] | 649 | int exclude; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 650 | |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 651 | if (is_dot_or_dotdot(de->d_name) || |
| 652 | !strcmp(de->d_name, ".git")) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 653 | continue; |
| 654 | len = strlen(de->d_name); |
Linus Torvalds | 5d5cea6 | 2007-04-09 21:13:58 -0700 | [diff] [blame] | 655 | /* Ignore overly long pathnames! */ |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 656 | if (len + baselen + 8 > sizeof(path)) |
Linus Torvalds | 5d5cea6 | 2007-04-09 21:13:58 -0700 | [diff] [blame] | 657 | continue; |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 658 | memcpy(path + baselen, de->d_name, len+1); |
| 659 | len = baselen + len; |
| 660 | if (simplify_away(path, len, simplify)) |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 661 | continue; |
Michael Spang | b991625 | 2007-05-06 22:35:04 -0400 | [diff] [blame] | 662 | |
Junio C Hamano | 6831a88 | 2008-01-31 20:23:25 -0800 | [diff] [blame] | 663 | dtype = DTYPE(de); |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 664 | exclude = excluded(dir, path, &dtype); |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 665 | if (exclude && (dir->flags & DIR_COLLECT_IGNORED) |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 666 | && in_pathspec(path, len, simplify)) |
| 667 | dir_add_ignored(dir, path,len); |
Linus Torvalds | 0713442 | 2007-10-19 10:59:22 -0700 | [diff] [blame] | 668 | |
| 669 | /* |
| 670 | * Excluded? If we don't explicitly want to show |
| 671 | * ignored files, ignore it |
| 672 | */ |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 673 | if (exclude && !(dir->flags & DIR_SHOW_IGNORED)) |
Linus Torvalds | 0713442 | 2007-10-19 10:59:22 -0700 | [diff] [blame] | 674 | continue; |
| 675 | |
Junio C Hamano | 6831a88 | 2008-01-31 20:23:25 -0800 | [diff] [blame] | 676 | if (dtype == DT_UNKNOWN) |
Linus Torvalds | caa6b78 | 2009-07-08 19:31:49 -0700 | [diff] [blame] | 677 | dtype = get_dtype(de, path, len); |
Linus Torvalds | 0713442 | 2007-10-19 10:59:22 -0700 | [diff] [blame] | 678 | |
| 679 | /* |
| 680 | * Do we want to see just the ignored files? |
| 681 | * We still need to recurse into directories, |
| 682 | * even if we don't ignore them, since the |
| 683 | * directory may contain files that we do.. |
| 684 | */ |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 685 | if (!exclude && (dir->flags & DIR_SHOW_IGNORED)) { |
Linus Torvalds | 0713442 | 2007-10-19 10:59:22 -0700 | [diff] [blame] | 686 | if (dtype != DT_DIR) |
Junio C Hamano | c889763 | 2006-12-29 10:08:19 -0800 | [diff] [blame] | 687 | continue; |
Junio C Hamano | c889763 | 2006-12-29 10:08:19 -0800 | [diff] [blame] | 688 | } |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 689 | |
Linus Torvalds | 0713442 | 2007-10-19 10:59:22 -0700 | [diff] [blame] | 690 | switch (dtype) { |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 691 | default: |
| 692 | continue; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 693 | case DT_DIR: |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 694 | memcpy(path + len, "/", 2); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 695 | len++; |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 696 | switch (treat_directory(dir, path, len, simplify)) { |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 697 | case show_directory: |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 698 | if (exclude != !!(dir->flags |
| 699 | & DIR_SHOW_IGNORED)) |
Michael Spang | b991625 | 2007-05-06 22:35:04 -0400 | [diff] [blame] | 700 | continue; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 701 | break; |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 702 | case recurse_into_directory: |
| 703 | contents += read_directory_recursive(dir, |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 704 | path, len, 0, simplify); |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 705 | continue; |
| 706 | case ignore_directory: |
| 707 | continue; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 708 | } |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 709 | break; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 710 | case DT_REG: |
| 711 | case DT_LNK: |
| 712 | break; |
| 713 | } |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 714 | contents++; |
Johannes Schindelin | 07ccbff | 2006-09-28 02:44:30 +0200 | [diff] [blame] | 715 | if (check_only) |
| 716 | goto exit_early; |
| 717 | else |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 718 | dir_add_name(dir, path, len); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 719 | } |
Johannes Schindelin | 07ccbff | 2006-09-28 02:44:30 +0200 | [diff] [blame] | 720 | exit_early: |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 721 | closedir(fdir); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | return contents; |
| 725 | } |
| 726 | |
| 727 | static int cmp_name(const void *p1, const void *p2) |
| 728 | { |
| 729 | const struct dir_entry *e1 = *(const struct dir_entry **)p1; |
| 730 | const struct dir_entry *e2 = *(const struct dir_entry **)p2; |
| 731 | |
| 732 | return cache_name_compare(e1->name, e1->len, |
| 733 | e2->name, e2->len); |
| 734 | } |
| 735 | |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 736 | /* |
| 737 | * Return the length of the "simple" part of a path match limiter. |
| 738 | */ |
| 739 | static int simple_length(const char *match) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 740 | { |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 741 | int len = -1; |
| 742 | |
| 743 | for (;;) { |
| 744 | unsigned char c = *match++; |
| 745 | len++; |
René Scharfe | 8cc3299 | 2009-01-17 16:50:34 +0100 | [diff] [blame] | 746 | if (c == '\0' || is_glob_special(c)) |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 747 | return len; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | static struct path_simplify *create_simplify(const char **pathspec) |
| 752 | { |
| 753 | int nr, alloc = 0; |
| 754 | struct path_simplify *simplify = NULL; |
| 755 | |
| 756 | if (!pathspec) |
| 757 | return NULL; |
| 758 | |
| 759 | for (nr = 0 ; ; nr++) { |
| 760 | const char *match; |
| 761 | if (nr >= alloc) { |
| 762 | alloc = alloc_nr(alloc); |
| 763 | simplify = xrealloc(simplify, alloc * sizeof(*simplify)); |
| 764 | } |
| 765 | match = *pathspec++; |
| 766 | if (!match) |
| 767 | break; |
| 768 | simplify[nr].path = match; |
| 769 | simplify[nr].len = simple_length(match); |
| 770 | } |
| 771 | simplify[nr].path = NULL; |
| 772 | simplify[nr].len = 0; |
| 773 | return simplify; |
| 774 | } |
| 775 | |
| 776 | static void free_simplify(struct path_simplify *simplify) |
| 777 | { |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 778 | free(simplify); |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 781 | int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec) |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 782 | { |
Junio C Hamano | 725b060 | 2008-08-04 00:52:37 -0700 | [diff] [blame] | 783 | struct path_simplify *simplify; |
Linus Torvalds | b4189aa | 2006-05-16 19:46:16 -0700 | [diff] [blame] | 784 | |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 785 | if (has_symlink_leading_path(path, len)) |
Junio C Hamano | 725b060 | 2008-08-04 00:52:37 -0700 | [diff] [blame] | 786 | return dir->nr; |
| 787 | |
| 788 | simplify = create_simplify(pathspec); |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 789 | read_directory_recursive(dir, path, len, 0, simplify); |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 790 | free_simplify(simplify); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 791 | qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name); |
Jeff King | 2abd31b | 2007-06-11 09:39:50 -0400 | [diff] [blame] | 792 | qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 793 | return dir->nr; |
| 794 | } |
Jeff King | c91f0d9 | 2006-09-08 04:05:34 -0400 | [diff] [blame] | 795 | |
Junio C Hamano | 686a4a0 | 2007-11-29 01:11:46 -0800 | [diff] [blame] | 796 | int file_exists(const char *f) |
Jeff King | c91f0d9 | 2006-09-08 04:05:34 -0400 | [diff] [blame] | 797 | { |
Junio C Hamano | 686a4a0 | 2007-11-29 01:11:46 -0800 | [diff] [blame] | 798 | struct stat sb; |
Junio C Hamano | a50f9fc5 | 2007-11-18 01:58:16 -0800 | [diff] [blame] | 799 | return lstat(f, &sb) == 0; |
Jeff King | c91f0d9 | 2006-09-08 04:05:34 -0400 | [diff] [blame] | 800 | } |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 801 | |
| 802 | /* |
| 803 | * get_relative_cwd() gets the prefix of the current working directory |
| 804 | * relative to 'dir'. If we are not inside 'dir', it returns NULL. |
Johannes Schindelin | 420acb3 | 2007-08-01 19:26:59 +0100 | [diff] [blame] | 805 | * |
| 806 | * As a convenience, it also returns NULL if 'dir' is already NULL. The |
| 807 | * reason for this behaviour is that it is natural for functions returning |
| 808 | * directory names to return NULL to say "this directory does not exist" |
| 809 | * or "this directory is invalid". These cases are usually handled the |
| 810 | * same as if the cwd is not inside 'dir' at all, so get_relative_cwd() |
| 811 | * returns NULL for both of them. |
| 812 | * |
| 813 | * Most notably, get_relative_cwd(buffer, size, get_git_work_tree()) |
| 814 | * unifies the handling of "outside work tree" with "no work tree at all". |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 815 | */ |
| 816 | char *get_relative_cwd(char *buffer, int size, const char *dir) |
| 817 | { |
| 818 | char *cwd = buffer; |
| 819 | |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 820 | if (!dir) |
| 821 | return NULL; |
| 822 | if (!getcwd(buffer, size)) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 823 | die_errno("can't find the current directory"); |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 824 | |
| 825 | if (!is_absolute_path(dir)) |
| 826 | dir = make_absolute_path(dir); |
| 827 | |
| 828 | while (*dir && *dir == *cwd) { |
| 829 | dir++; |
| 830 | cwd++; |
| 831 | } |
| 832 | if (*dir) |
| 833 | return NULL; |
| 834 | if (*cwd == '/') |
| 835 | return cwd + 1; |
| 836 | return cwd; |
| 837 | } |
| 838 | |
| 839 | int is_inside_dir(const char *dir) |
| 840 | { |
| 841 | char buffer[PATH_MAX]; |
| 842 | return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL; |
| 843 | } |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 844 | |
Alexander Potashev | 55892d2 | 2009-01-11 15:19:12 +0300 | [diff] [blame] | 845 | int is_empty_dir(const char *path) |
| 846 | { |
| 847 | DIR *dir = opendir(path); |
| 848 | struct dirent *e; |
| 849 | int ret = 1; |
| 850 | |
| 851 | if (!dir) |
| 852 | return 0; |
| 853 | |
| 854 | while ((e = readdir(dir)) != NULL) |
| 855 | if (!is_dot_or_dotdot(e->d_name)) { |
| 856 | ret = 0; |
| 857 | break; |
| 858 | } |
| 859 | |
| 860 | closedir(dir); |
| 861 | return ret; |
| 862 | } |
| 863 | |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 864 | int remove_dir_recursively(struct strbuf *path, int flag) |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 865 | { |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 866 | DIR *dir; |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 867 | struct dirent *e; |
| 868 | int ret = 0, original_len = path->len, len; |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 869 | int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY); |
| 870 | unsigned char submodule_head[20]; |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 871 | |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 872 | if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) && |
| 873 | !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) |
| 874 | /* Do not descend and nuke a nested git work tree. */ |
| 875 | return 0; |
| 876 | |
| 877 | dir = opendir(path->buf); |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 878 | if (!dir) |
| 879 | return -1; |
| 880 | if (path->buf[original_len - 1] != '/') |
| 881 | strbuf_addch(path, '/'); |
| 882 | |
| 883 | len = path->len; |
| 884 | while ((e = readdir(dir)) != NULL) { |
| 885 | struct stat st; |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 886 | if (is_dot_or_dotdot(e->d_name)) |
| 887 | continue; |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 888 | |
| 889 | strbuf_setlen(path, len); |
| 890 | strbuf_addstr(path, e->d_name); |
| 891 | if (lstat(path->buf, &st)) |
| 892 | ; /* fall thru */ |
| 893 | else if (S_ISDIR(st.st_mode)) { |
| 894 | if (!remove_dir_recursively(path, only_empty)) |
| 895 | continue; /* happy */ |
| 896 | } else if (!only_empty && !unlink(path->buf)) |
| 897 | continue; /* happy, too */ |
| 898 | |
| 899 | /* path too long, stat fails, or non-directory still exists */ |
| 900 | ret = -1; |
| 901 | break; |
| 902 | } |
| 903 | closedir(dir); |
| 904 | |
| 905 | strbuf_setlen(path, original_len); |
| 906 | if (!ret) |
| 907 | ret = rmdir(path->buf); |
| 908 | return ret; |
| 909 | } |
Junio C Hamano | 039bc64 | 2007-11-14 00:05:00 -0800 | [diff] [blame] | 910 | |
| 911 | void setup_standard_excludes(struct dir_struct *dir) |
| 912 | { |
| 913 | const char *path; |
| 914 | |
| 915 | dir->exclude_per_dir = ".gitignore"; |
| 916 | path = git_path("info/exclude"); |
| 917 | if (!access(path, R_OK)) |
| 918 | add_excludes_from_file(dir, path); |
| 919 | if (excludes_file && !access(excludes_file, R_OK)) |
| 920 | add_excludes_from_file(dir, excludes_file); |
| 921 | } |
Alex Riesen | 4a92d1b | 2008-09-27 00:56:46 +0200 | [diff] [blame] | 922 | |
| 923 | int remove_path(const char *name) |
| 924 | { |
| 925 | char *slash; |
| 926 | |
| 927 | if (unlink(name) && errno != ENOENT) |
| 928 | return -1; |
| 929 | |
| 930 | slash = strrchr(name, '/'); |
| 931 | if (slash) { |
| 932 | char *dirs = xstrdup(name); |
| 933 | slash = dirs + (slash - name); |
| 934 | do { |
| 935 | *slash = '\0'; |
| 936 | } while (rmdir(dirs) && (slash = strrchr(dirs, '/'))); |
| 937 | free(dirs); |
| 938 | } |
| 939 | return 0; |
| 940 | } |
| 941 | |