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