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