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