dir.c: make 'git-status --ignored' work within leading directories
[gitweb.git] / dir.c
diff --git a/dir.c b/dir.c
index 1e42b2b1509e2cf154a1d9722138fbeae437daf5..fd4aeae3e7aeeceda50b16f6707ac77d12d96ffb 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -1104,22 +1104,30 @@ static enum directory_treatment treat_directory(struct dir_struct *dir,
 
        /* This is the "show_other_directories" case */
 
+       /* might be a sub directory in an excluded directory */
+       if (!exclude) {
+               struct path_exclude_check check;
+               int dt = DT_DIR;
+               path_exclude_check_init(&check, dir);
+               exclude = is_path_excluded(&check, dirname, len, &dt);
+               path_exclude_check_clear(&check);
+       }
+
        /*
         * We are looking for ignored files and our directory is not ignored,
-        * check if it contains only ignored files
+        * check if it contains untracked files (i.e. is listed as untracked)
         */
        if ((dir->flags & DIR_SHOW_IGNORED) && !exclude) {
                int ignored;
                dir->flags &= ~DIR_SHOW_IGNORED;
-               dir->flags |= DIR_HIDE_EMPTY_DIRECTORIES;
                ignored = read_directory_recursive(dir, dirname, len, 1, simplify);
-               dir->flags &= ~DIR_HIDE_EMPTY_DIRECTORIES;
                dir->flags |= DIR_SHOW_IGNORED;
 
-               return ignored ? ignore_directory : show_directory;
+               if (ignored)
+                       return ignore_directory;
        }
-       if (!(dir->flags & DIR_SHOW_IGNORED) &&
-           !(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
+
+       if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
                return show_directory;
        if (!read_directory_recursive(dir, dirname, len, 1, simplify))
                return ignore_directory;
@@ -1144,16 +1152,13 @@ static int treat_file(struct dir_struct *dir, struct strbuf *path, int exclude,
        struct path_exclude_check check;
        int exclude_file = 0;
 
+       /* Always exclude indexed files */
+       if (index_name_exists(&the_index, path->buf, path->len, ignore_case))
+               return 1;
+
        if (exclude)
                exclude_file = !(dir->flags & DIR_SHOW_IGNORED);
        else if (dir->flags & DIR_SHOW_IGNORED) {
-               /* Always exclude indexed files */
-               struct cache_entry *ce = index_name_exists(&the_index,
-                   path->buf, path->len, ignore_case);
-
-               if (ce)
-                       return 1;
-
                path_exclude_check_init(&check, dir);
 
                if (!is_path_excluded(&check, path->buf, path->len, dtype))
@@ -1308,7 +1313,6 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,
                return path_ignored;
        case DT_DIR:
                strbuf_addch(path, '/');
-
                switch (treat_directory(dir, path->buf, path->len, exclude, simplify)) {
                case show_directory:
                        break;
@@ -1378,8 +1382,7 @@ static int read_directory_recursive(struct dir_struct *dir,
                switch (treat_path(dir, de, &path, baselen, simplify)) {
                case path_recurse:
                        contents += read_directory_recursive(dir, path.buf,
-                                                            path.len, 0,
-                                                            simplify);
+                               path.len, check_only, simplify);
                        continue;
                case path_ignored:
                        continue;
@@ -1444,12 +1447,14 @@ static int treat_leading_path(struct dir_struct *dir,
        struct strbuf sb = STRBUF_INIT;
        int baselen, rc = 0;
        const char *cp;
+       int old_flags = dir->flags;
 
        while (len && path[len - 1] == '/')
                len--;
        if (!len)
                return 1;
        baselen = 0;
+       dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;
        while (1) {
                cp = path + baselen + !!baselen;
                cp = memchr(cp, '/', path + len - cp);
@@ -1472,6 +1477,7 @@ static int treat_leading_path(struct dir_struct *dir,
                }
        }
        strbuf_release(&sb);
+       dir->flags = old_flags;
        return rc;
 }
 
@@ -1647,7 +1653,7 @@ int remove_path(const char *name)
 {
        char *slash;
 
-       if (unlink(name) && errno != ENOENT)
+       if (unlink(name) && errno != ENOENT && errno != ENOTDIR)
                return -1;
 
        slash = strrchr(name, '/');