Allow branch.*.merge to talk about remote tracking branches.
[git/repo.git] / dir.c
blob16401d8017c5d96dc17ac9d52ab77eabbc4e9270
1 /*
2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
5 * Copyright (C) Linus Torvalds, 2005-2006
6 * Junio Hamano, 2005-2006
7 */
8 #include "cache.h"
9 #include "dir.h"
11 int common_prefix(const char **pathspec)
13 const char *path, *slash, *next;
14 int prefix;
16 if (!pathspec)
17 return 0;
19 path = *pathspec;
20 slash = strrchr(path, '/');
21 if (!slash)
22 return 0;
24 prefix = slash - path + 1;
25 while ((next = *++pathspec) != NULL) {
26 int len = strlen(next);
27 if (len >= prefix && !memcmp(path, next, len))
28 continue;
29 for (;;) {
30 if (!len)
31 return 0;
32 if (next[--len] != '/')
33 continue;
34 if (memcmp(path, next, len+1))
35 continue;
36 prefix = len + 1;
37 break;
40 return prefix;
43 static int match_one(const char *match, const char *name, int namelen)
45 int matchlen;
47 /* If the match was just the prefix, we matched */
48 matchlen = strlen(match);
49 if (!matchlen)
50 return 1;
53 * If we don't match the matchstring exactly,
54 * we need to match by fnmatch
56 if (strncmp(match, name, matchlen))
57 return !fnmatch(match, name, 0);
60 * If we did match the string exactly, we still
61 * need to make sure that it happened on a path
62 * component boundary (ie either the last character
63 * of the match was '/', or the next character of
64 * the name was '/' or the terminating NUL.
66 return match[matchlen-1] == '/' ||
67 name[matchlen] == '/' ||
68 !name[matchlen];
71 int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
73 int retval;
74 const char *match;
76 name += prefix;
77 namelen -= prefix;
79 for (retval = 0; (match = *pathspec++) != NULL; seen++) {
80 if (retval & *seen)
81 continue;
82 match += prefix;
83 if (match_one(match, name, namelen)) {
84 retval = 1;
85 *seen = 1;
88 return retval;
91 void add_exclude(const char *string, const char *base,
92 int baselen, struct exclude_list *which)
94 struct exclude *x = xmalloc(sizeof (*x));
96 x->pattern = string;
97 x->base = base;
98 x->baselen = baselen;
99 if (which->nr == which->alloc) {
100 which->alloc = alloc_nr(which->alloc);
101 which->excludes = xrealloc(which->excludes,
102 which->alloc * sizeof(x));
104 which->excludes[which->nr++] = x;
107 static int add_excludes_from_file_1(const char *fname,
108 const char *base,
109 int baselen,
110 struct exclude_list *which)
112 struct stat st;
113 int fd, i;
114 long size;
115 char *buf, *entry;
117 fd = open(fname, O_RDONLY);
118 if (fd < 0 || fstat(fd, &st) < 0)
119 goto err;
120 size = st.st_size;
121 if (size == 0) {
122 close(fd);
123 return 0;
125 buf = xmalloc(size+1);
126 if (read(fd, buf, size) != size)
127 goto err;
128 close(fd);
130 buf[size++] = '\n';
131 entry = buf;
132 for (i = 0; i < size; i++) {
133 if (buf[i] == '\n') {
134 if (entry != buf + i && entry[0] != '#') {
135 buf[i - (i && buf[i-1] == '\r')] = 0;
136 add_exclude(entry, base, baselen, which);
138 entry = buf + i + 1;
141 return 0;
143 err:
144 if (0 <= fd)
145 close(fd);
146 return -1;
149 void add_excludes_from_file(struct dir_struct *dir, const char *fname)
151 if (add_excludes_from_file_1(fname, "", 0,
152 &dir->exclude_list[EXC_FILE]) < 0)
153 die("cannot use %s as an exclude file", fname);
156 int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
158 char exclude_file[PATH_MAX];
159 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
160 int current_nr = el->nr;
162 if (dir->exclude_per_dir) {
163 memcpy(exclude_file, base, baselen);
164 strcpy(exclude_file + baselen, dir->exclude_per_dir);
165 add_excludes_from_file_1(exclude_file, base, baselen, el);
167 return current_nr;
170 void pop_exclude_per_directory(struct dir_struct *dir, int stk)
172 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
174 while (stk < el->nr)
175 free(el->excludes[--el->nr]);
178 /* Scan the list and let the last match determines the fate.
179 * Return 1 for exclude, 0 for include and -1 for undecided.
181 static int excluded_1(const char *pathname,
182 int pathlen,
183 struct exclude_list *el)
185 int i;
187 if (el->nr) {
188 for (i = el->nr - 1; 0 <= i; i--) {
189 struct exclude *x = el->excludes[i];
190 const char *exclude = x->pattern;
191 int to_exclude = 1;
193 if (*exclude == '!') {
194 to_exclude = 0;
195 exclude++;
198 if (!strchr(exclude, '/')) {
199 /* match basename */
200 const char *basename = strrchr(pathname, '/');
201 basename = (basename) ? basename+1 : pathname;
202 if (fnmatch(exclude, basename, 0) == 0)
203 return to_exclude;
205 else {
206 /* match with FNM_PATHNAME:
207 * exclude has base (baselen long) implicitly
208 * in front of it.
210 int baselen = x->baselen;
211 if (*exclude == '/')
212 exclude++;
214 if (pathlen < baselen ||
215 (baselen && pathname[baselen-1] != '/') ||
216 strncmp(pathname, x->base, baselen))
217 continue;
219 if (fnmatch(exclude, pathname+baselen,
220 FNM_PATHNAME) == 0)
221 return to_exclude;
225 return -1; /* undecided */
228 int excluded(struct dir_struct *dir, const char *pathname)
230 int pathlen = strlen(pathname);
231 int st;
233 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
234 switch (excluded_1(pathname, pathlen, &dir->exclude_list[st])) {
235 case 0:
236 return 0;
237 case 1:
238 return 1;
241 return 0;
244 static void add_name(struct dir_struct *dir, const char *pathname, int len)
246 struct dir_entry *ent;
248 if (cache_name_pos(pathname, len) >= 0)
249 return;
251 if (dir->nr == dir->alloc) {
252 int alloc = alloc_nr(dir->alloc);
253 dir->alloc = alloc;
254 dir->entries = xrealloc(dir->entries, alloc*sizeof(ent));
256 ent = xmalloc(sizeof(*ent) + len + 1);
257 ent->len = len;
258 memcpy(ent->name, pathname, len);
259 ent->name[len] = 0;
260 dir->entries[dir->nr++] = ent;
263 static int dir_exists(const char *dirname, int len)
265 int pos = cache_name_pos(dirname, len);
266 if (pos >= 0)
267 return 1;
268 pos = -pos-1;
269 if (pos >= active_nr) /* can't */
270 return 0;
271 return !strncmp(active_cache[pos]->name, dirname, len);
275 * Read a directory tree. We currently ignore anything but
276 * directories, regular files and symlinks. That's because git
277 * doesn't handle them at all yet. Maybe that will change some
278 * day.
280 * Also, we ignore the name ".git" (even if it is not a directory).
281 * That likely will not change.
283 static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen, int check_only)
285 DIR *fdir = opendir(path);
286 int contents = 0;
288 if (fdir) {
289 int exclude_stk;
290 struct dirent *de;
291 char fullname[PATH_MAX + 1];
292 memcpy(fullname, base, baselen);
294 exclude_stk = push_exclude_per_directory(dir, base, baselen);
296 while ((de = readdir(fdir)) != NULL) {
297 int len;
299 if ((de->d_name[0] == '.') &&
300 (de->d_name[1] == 0 ||
301 !strcmp(de->d_name + 1, ".") ||
302 !strcmp(de->d_name + 1, "git")))
303 continue;
304 len = strlen(de->d_name);
305 memcpy(fullname + baselen, de->d_name, len+1);
306 if (excluded(dir, fullname) != dir->show_ignored) {
307 if (!dir->show_ignored || DTYPE(de) != DT_DIR) {
308 continue;
312 switch (DTYPE(de)) {
313 struct stat st;
314 default:
315 continue;
316 case DT_UNKNOWN:
317 if (lstat(fullname, &st))
318 continue;
319 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
320 break;
321 if (!S_ISDIR(st.st_mode))
322 continue;
323 /* fallthrough */
324 case DT_DIR:
325 memcpy(fullname + baselen + len, "/", 2);
326 len++;
327 if (dir->show_other_directories &&
328 !dir_exists(fullname, baselen + len)) {
329 if (dir->hide_empty_directories &&
330 !read_directory_recursive(dir,
331 fullname, fullname,
332 baselen + len, 1))
333 continue;
334 break;
337 contents += read_directory_recursive(dir,
338 fullname, fullname, baselen + len, 0);
339 continue;
340 case DT_REG:
341 case DT_LNK:
342 break;
344 contents++;
345 if (check_only)
346 goto exit_early;
347 else
348 add_name(dir, fullname, baselen + len);
350 exit_early:
351 closedir(fdir);
353 pop_exclude_per_directory(dir, exclude_stk);
356 return contents;
359 static int cmp_name(const void *p1, const void *p2)
361 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
362 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
364 return cache_name_compare(e1->name, e1->len,
365 e2->name, e2->len);
368 int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen)
371 * Make sure to do the per-directory exclude for all the
372 * directories leading up to our base.
374 if (baselen) {
375 if (dir->exclude_per_dir) {
376 char *p, *pp = xmalloc(baselen+1);
377 memcpy(pp, base, baselen+1);
378 p = pp;
379 while (1) {
380 char save = *p;
381 *p = 0;
382 push_exclude_per_directory(dir, pp, p-pp);
383 *p++ = save;
384 if (!save)
385 break;
386 p = strchr(p, '/');
387 if (p)
388 p++;
389 else
390 p = pp + baselen;
392 free(pp);
396 read_directory_recursive(dir, path, base, baselen, 0);
397 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
398 return dir->nr;
402 file_exists(const char *f)
404 struct stat sb;
405 return stat(f, &sb) == 0;