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