git-tar-tree: no more void pointer arithmetic
[git.git] / dir.c
diff --git a/dir.c b/dir.c
index 3f41a5d..d778ecd 100644 (file)
--- a/dir.c
+++ b/dir.c
 #include "cache.h"
 #include "dir.h"
 
+int common_prefix(const char **pathspec)
+{
+       const char *path, *slash, *next;
+       int prefix;
+
+       if (!pathspec)
+               return 0;
+
+       path = *pathspec;
+       slash = strrchr(path, '/');
+       if (!slash)
+               return 0;
+
+       prefix = slash - path + 1;
+       while ((next = *++pathspec) != NULL) {
+               int len = strlen(next);
+               if (len >= prefix && !memcmp(path, next, len))
+                       continue;
+               for (;;) {
+                       if (!len)
+                               return 0;
+                       if (next[--len] != '/')
+                               continue;
+                       if (memcmp(path, next, len+1))
+                               continue;
+                       prefix = len + 1;
+                       break;
+               }
+       }
+       return prefix;
+}
+
+static int match_one(const char *match, const char *name, int namelen)
+{
+       int matchlen;
+
+       /* If the match was just the prefix, we matched */
+       matchlen = strlen(match);
+       if (!matchlen)
+               return 1;
+
+       /*
+        * If we don't match the matchstring exactly,
+        * we need to match by fnmatch
+        */
+       if (strncmp(match, name, matchlen))
+               return !fnmatch(match, name, 0);
+
+       /*
+        * If we did match the string exactly, we still
+        * need to make sure that it happened on a path
+        * component boundary (ie either the last character
+        * of the match was '/', or the next character of
+        * the name was '/' or the terminating NUL.
+        */
+       return  match[matchlen-1] == '/' ||
+               name[matchlen] == '/' ||
+               !name[matchlen];
+}
+
+int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
+{
+       int retval;
+       const char *match;
+
+       name += prefix;
+       namelen -= prefix;
+
+       for (retval = 0; (match = *pathspec++) != NULL; seen++) {
+               if (retval & *seen)
+                       continue;
+               match += prefix;
+               if (match_one(match, name, namelen)) {
+                       retval = 1;
+                       *seen = 1;
+               }
+       }
+       return retval;
+}
+
 void add_exclude(const char *string, const char *base,
                 int baselen, struct exclude_list *which)
 {
@@ -78,7 +158,7 @@ void add_excludes_from_file(struct dir_struct *dir, const char *fname)
                die("cannot use %s as an exclude file", fname);
 }
 
-int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
+static int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
 {
        char exclude_file[PATH_MAX];
        struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
@@ -289,6 +369,32 @@ static int cmp_name(const void *p1, const void *p2)
 
 int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen)
 {
+       /*
+        * Make sure to do the per-directory exclude for all the
+        * directories leading up to our base.
+        */
+       if (baselen) {
+               if (dir->exclude_per_dir) {
+                       char *p, *pp = xmalloc(baselen+1);
+                       memcpy(pp, base, baselen+1);
+                       p = pp;
+                       while (1) {
+                               char save = *p;
+                               *p = 0;
+                               push_exclude_per_directory(dir, pp, p-pp);
+                               *p++ = save;
+                               if (!save)
+                                       break;
+                               p = strchr(p, '/');
+                               if (p)
+                                       p++;
+                               else
+                                       p = pp + baselen;
+                       }
+                       free(pp);
+               }
+       }
+
        read_directory_recursive(dir, path, base, baselen);
        qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
        return dir->nr;