]> asedeno.scripts.mit.edu Git - git.git/blobdiff - dir.c
git-svn log: handle unreachable revisions like "svn log"
[git.git] / dir.c
diff --git a/dir.c b/dir.c
index 4c17d3643eebf0d9071003065b5c120db130f9ab..225fdfb52c432b7af1af1cdf9aa9a31f8022e3ec 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -118,14 +118,32 @@ int match_pathspec(const char **pathspec, const char *name, int namelen, int pre
        return retval;
 }
 
+static int no_wildcard(const char *string)
+{
+       return string[strcspn(string, "*?[{")] == '\0';
+}
+
 void add_exclude(const char *string, const char *base,
                 int baselen, struct exclude_list *which)
 {
        struct exclude *x = xmalloc(sizeof (*x));
 
+       x->to_exclude = 1;
+       if (*string == '!') {
+               x->to_exclude = 0;
+               string++;
+       }
        x->pattern = string;
+       x->patternlen = strlen(string);
        x->base = base;
        x->baselen = baselen;
+       x->flags = 0;
+       if (!strchr(string, '/'))
+               x->flags |= EXC_FLAG_NODIR;
+       if (no_wildcard(string))
+               x->flags |= EXC_FLAG_NOWILDCARD;
+       if (*string == '*' && no_wildcard(string+1))
+               x->flags |= EXC_FLAG_ENDSWITH;
        if (which->nr == which->alloc) {
                which->alloc = alloc_nr(which->alloc);
                which->excludes = xrealloc(which->excludes,
@@ -209,7 +227,7 @@ void pop_exclude_per_directory(struct dir_struct *dir, int stk)
  * Return 1 for exclude, 0 for include and -1 for undecided.
  */
 static int excluded_1(const char *pathname,
-                     int pathlen,
+                     int pathlen, const char *basename,
                      struct exclude_list *el)
 {
        int i;
@@ -218,19 +236,21 @@ static int excluded_1(const char *pathname,
                for (i = el->nr - 1; 0 <= i; i--) {
                        struct exclude *x = el->excludes[i];
                        const char *exclude = x->pattern;
-                       int to_exclude = 1;
+                       int to_exclude = x->to_exclude;
 
-                       if (*exclude == '!') {
-                               to_exclude = 0;
-                               exclude++;
-                       }
-
-                       if (!strchr(exclude, '/')) {
+                       if (x->flags & EXC_FLAG_NODIR) {
                                /* match basename */
-                               const char *basename = strrchr(pathname, '/');
-                               basename = (basename) ? basename+1 : pathname;
-                               if (fnmatch(exclude, basename, 0) == 0)
-                                       return to_exclude;
+                               if (x->flags & EXC_FLAG_NOWILDCARD) {
+                                       if (!strcmp(exclude, basename))
+                                               return to_exclude;
+                               } else if (x->flags & EXC_FLAG_ENDSWITH) {
+                                       if (x->patternlen - 1 <= pathlen &&
+                                           !strcmp(exclude + 1, pathname + pathlen - x->patternlen + 1))
+                                               return to_exclude;
+                               } else {
+                                       if (fnmatch(exclude, basename, 0) == 0)
+                                               return to_exclude;
+                               }
                        }
                        else {
                                /* match with FNM_PATHNAME:
@@ -246,9 +266,14 @@ static int excluded_1(const char *pathname,
                                    strncmp(pathname, x->base, baselen))
                                    continue;
 
-                               if (fnmatch(exclude, pathname+baselen,
-                                           FNM_PATHNAME) == 0)
-                                       return to_exclude;
+                               if (x->flags & EXC_FLAG_NOWILDCARD) {
+                                       if (!strcmp(exclude, pathname + baselen))
+                                               return to_exclude;
+                               } else {
+                                       if (fnmatch(exclude, pathname+baselen,
+                                                   FNM_PATHNAME) == 0)
+                                           return to_exclude;
+                               }
                        }
                }
        }
@@ -259,9 +284,11 @@ int excluded(struct dir_struct *dir, const char *pathname)
 {
        int pathlen = strlen(pathname);
        int st;
+       const char *basename = strrchr(pathname, '/');
+       basename = (basename) ? basename+1 : pathname;
 
        for (st = EXC_CMDL; st <= EXC_FILE; st++) {
-               switch (excluded_1(pathname, pathlen, &dir->exclude_list[st])) {
+               switch (excluded_1(pathname, pathlen, basename, &dir->exclude_list[st])) {
                case 0:
                        return 0;
                case 1:
@@ -271,7 +298,8 @@ int excluded(struct dir_struct *dir, const char *pathname)
        return 0;
 }
 
-static struct dir_entry *dir_entry_new(const char *pathname, int len) {
+static struct dir_entry *dir_entry_new(const char *pathname, int len)
+{
        struct dir_entry *ent;
 
        ent = xmalloc(sizeof(*ent) + len + 1);
@@ -626,6 +654,7 @@ static void free_simplify(struct path_simplify *simplify)
 int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
 {
        struct path_simplify *simplify = create_simplify(pathspec);
+       char *pp = NULL;
 
        /*
         * Make sure to do the per-directory exclude for all the
@@ -633,7 +662,8 @@ int read_directory(struct dir_struct *dir, const char *path, const char *base, i
         */
        if (baselen) {
                if (dir->exclude_per_dir) {
-                       char *p, *pp = xmalloc(baselen+1);
+                       char *p;
+                       pp = xmalloc(baselen+1);
                        memcpy(pp, base, baselen+1);
                        p = pp;
                        while (1) {
@@ -649,12 +679,12 @@ int read_directory(struct dir_struct *dir, const char *path, const char *base, i
                                else
                                        p = pp + baselen;
                        }
-                       free(pp);
                }
        }
 
        read_directory_recursive(dir, path, base, baselen, 0, simplify);
        free_simplify(simplify);
+       free(pp);
        qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
        qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
        return dir->nr;
@@ -750,3 +780,15 @@ int remove_dir_recursively(struct strbuf *path, int only_empty)
                ret = rmdir(path->buf);
        return ret;
 }
+
+void setup_standard_excludes(struct dir_struct *dir)
+{
+       const char *path;
+
+       dir->exclude_per_dir = ".gitignore";
+       path = git_path("info/exclude");
+       if (!access(path, R_OK))
+               add_excludes_from_file(dir, path);
+       if (excludes_file && !access(excludes_file, R_OK))
+               add_excludes_from_file(dir, excludes_file);
+}