t7409: make sure submodule is initialized and updated in more detail
[gitweb.git] / dir.c
diff --git a/dir.c b/dir.c
index bbf958ccfd817c033d1b42ef02394435f9bea858..a772c6dc6c2bf4dc40f46f0bb5d0899312f3ffd4 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -341,8 +341,7 @@ void add_exclude(const char *string, const char *base,
        x->flags = flags;
        if (!strchr(string, '/'))
                x->flags |= EXC_FLAG_NODIR;
-       if (no_wildcard(string))
-               x->flags |= EXC_FLAG_NOWILDCARD;
+       x->nowildcardlen = simple_length(string);
        if (*string == '*' && no_wildcard(string+1))
                x->flags |= EXC_FLAG_ENDSWITH;
        ALLOC_GROW(which->excludes, which->nr + 1, which->alloc);
@@ -518,8 +517,9 @@ int excluded_from_list(const char *pathname,
 
        for (i = el->nr - 1; 0 <= i; i--) {
                struct exclude *x = el->excludes[i];
-               const char *exclude = x->pattern;
+               const char *name, *exclude = x->pattern;
                int to_exclude = x->to_exclude;
+               int namelen, prefix = x->nowildcardlen;
 
                if (x->flags & EXC_FLAG_MUSTBEDIR) {
                        if (*dtype == DT_UNKNOWN)
@@ -530,7 +530,7 @@ int excluded_from_list(const char *pathname,
 
                if (x->flags & EXC_FLAG_NODIR) {
                        /* match basename */
-                       if (x->flags & EXC_FLAG_NOWILDCARD) {
+                       if (prefix == x->patternlen) {
                                if (!strcmp_icase(exclude, basename))
                                        return to_exclude;
                        } else if (x->flags & EXC_FLAG_ENDSWITH) {
@@ -544,31 +544,42 @@ int excluded_from_list(const char *pathname,
                        continue;
                }
 
-
                /* match with FNM_PATHNAME:
                 * exclude has base (baselen long) implicitly in front of it.
                 */
-               if (*exclude == '/')
+               if (*exclude == '/') {
                        exclude++;
+                       prefix--;
+               }
 
                if (pathlen < x->baselen ||
                    (x->baselen && pathname[x->baselen-1] != '/') ||
                    strncmp_icase(pathname, x->base, x->baselen))
                        continue;
 
-               if (x->flags & EXC_FLAG_NOWILDCARD) {
-                       if (!strcmp_icase(exclude, pathname + x->baselen))
-                               return to_exclude;
-               } else {
-                       if (fnmatch_icase(exclude, pathname+x->baselen,
-                                         FNM_PATHNAME) == 0)
-                               return to_exclude;
+               namelen = x->baselen ? pathlen - x->baselen : pathlen;
+               name = pathname + pathlen  - namelen;
+
+               /* if the non-wildcard part is longer than the
+                  remaining pathname, surely it cannot match */
+               if (prefix > namelen)
+                       continue;
+
+               if (prefix) {
+                       if (strncmp_icase(exclude, name, prefix))
+                               continue;
+                       exclude += prefix;
+                       name    += prefix;
+                       namelen -= prefix;
                }
+
+               if (!namelen || !fnmatch_icase(exclude, name, FNM_PATHNAME))
+                       return to_exclude;
        }
        return -1; /* undecided */
 }
 
-int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
+static int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
 {
        int pathlen = strlen(pathname);
        int st;
@@ -588,6 +599,64 @@ int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
        return 0;
 }
 
+void path_exclude_check_init(struct path_exclude_check *check,
+                            struct dir_struct *dir)
+{
+       check->dir = dir;
+       strbuf_init(&check->path, 256);
+}
+
+void path_exclude_check_clear(struct path_exclude_check *check)
+{
+       strbuf_release(&check->path);
+}
+
+/*
+ * Is this name excluded?  This is for a caller like show_files() that
+ * do not honor directory hierarchy and iterate through paths that are
+ * possibly in an ignored directory.
+ *
+ * A path to a directory known to be excluded is left in check->path to
+ * optimize for repeated checks for files in the same excluded directory.
+ */
+int path_excluded(struct path_exclude_check *check,
+                 const char *name, int namelen, int *dtype)
+{
+       int i;
+       struct strbuf *path = &check->path;
+
+       /*
+        * we allow the caller to pass namelen as an optimization; it
+        * must match the length of the name, as we eventually call
+        * excluded() on the whole name string.
+        */
+       if (namelen < 0)
+               namelen = strlen(name);
+
+       if (path->len &&
+           path->len <= namelen &&
+           !memcmp(name, path->buf, path->len) &&
+           (!name[path->len] || name[path->len] == '/'))
+               return 1;
+
+       strbuf_setlen(path, 0);
+       for (i = 0; name[i]; i++) {
+               int ch = name[i];
+
+               if (ch == '/') {
+                       int dt = DT_DIR;
+                       if (excluded(check->dir, path->buf, &dt))
+                               return 1;
+               }
+               strbuf_addch(path, ch);
+       }
+
+       /* An entry in the index; cannot be a directory with subentries */
+       strbuf_setlen(path, 0);
+
+       return excluded(check->dir, name, dtype);
+}
+
 static struct dir_entry *dir_entry_new(const char *pathname, int len)
 {
        struct dir_entry *ent;
@@ -1234,12 +1303,17 @@ int remove_dir_recursively(struct strbuf *path, int flag)
 void setup_standard_excludes(struct dir_struct *dir)
 {
        const char *path;
+       char *xdg_path;
 
        dir->exclude_per_dir = ".gitignore";
        path = git_path("info/exclude");
+       if (!excludes_file) {
+               home_config_paths(NULL, &xdg_path, "ignore");
+               excludes_file = xdg_path;
+       }
        if (!access(path, R_OK))
                add_excludes_from_file(dir, path);
-       if (excludes_file && !access(excludes_file, R_OK))
+       if (!access(excludes_file, R_OK))
                add_excludes_from_file(dir, excludes_file);
 }