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