Code

tree_entry_interesting(): support depth limit
[git.git] / dir.c
diff --git a/dir.c b/dir.c
index 852e60f2a8d4784bf73d044f89fe822538121286..c3bddb60c81fbf362614f081a5b62f0160a01c63 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -87,6 +87,21 @@ int fill_directory(struct dir_struct *dir, const char **pathspec)
        return len;
 }
 
+int within_depth(const char *name, int namelen,
+                       int depth, int max_depth)
+{
+       const char *cp = name, *cpe = name + namelen;
+
+       while (cp < cpe) {
+               if (*cp++ != '/')
+                       continue;
+               depth++;
+               if (depth > max_depth)
+                       return 0;
+       }
+       return 1;
+}
+
 /*
  * Does 'match' match the given name?
  * A match is found if
@@ -253,6 +268,18 @@ static void *read_skip_worktree_file_from_index(const char *path, size_t *size)
        return data;
 }
 
+void free_excludes(struct exclude_list *el)
+{
+       int i;
+
+       for (i = 0; i < el->nr; i++)
+               free(el->excludes[i]);
+       free(el->excludes);
+
+       el->nr = 0;
+       el->excludes = NULL;
+}
+
 int add_excludes_from_file_to_list(const char *fname,
                                   const char *base,
                                   int baselen,
@@ -389,13 +416,6 @@ int excluded_from_list(const char *pathname,
                        int to_exclude = x->to_exclude;
 
                        if (x->flags & EXC_FLAG_MUSTBEDIR) {
-                               if (!dtype) {
-                                       if (!prefixcmp(pathname, exclude) &&
-                                           pathname[x->patternlen] == '/')
-                                               return to_exclude;
-                                       else
-                                               continue;
-                               }
                                if (*dtype == DT_UNKNOWN)
                                        *dtype = get_dtype(NULL, pathname, pathlen);
                                if (*dtype != DT_DIR)
@@ -1033,6 +1053,12 @@ char *get_relative_cwd(char *buffer, int size, const char *dir)
        case '/':
                return cwd + 1;
        default:
+               /*
+                * dir can end with a path separator when it's root
+                * directory. Return proper prefix in that case.
+                */
+               if (dir[-1] == '/')
+                       return cwd;
                return NULL;
        }
 }
@@ -1140,3 +1166,34 @@ int remove_path(const char *name)
        return 0;
 }
 
+int init_pathspec(struct pathspec *pathspec, const char **paths)
+{
+       const char **p = paths;
+       int i;
+
+       memset(pathspec, 0, sizeof(*pathspec));
+       if (!p)
+               return 0;
+       while (*p)
+               p++;
+       pathspec->raw = paths;
+       pathspec->nr = p - paths;
+       if (!pathspec->nr)
+               return 0;
+
+       pathspec->items = xmalloc(sizeof(struct pathspec_item)*pathspec->nr);
+       for (i = 0; i < pathspec->nr; i++) {
+               struct pathspec_item *item = pathspec->items+i;
+               const char *path = paths[i];
+
+               item->match = path;
+               item->len = strlen(path);
+       }
+       return 0;
+}
+
+void free_pathspec(struct pathspec *pathspec)
+{
+       free(pathspec->items);
+       pathspec->items = NULL;
+}