The index does not store directories explicitly (except submodules) so
we have to figure them out from file list. The function
show_directories() deliberately generates duplicate directories and
expects the previous patch to remove duplicates.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclo...@gmail.com>
---
 builtin/ls-files.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index cd8e35c..7e50192 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -27,6 +27,7 @@ static int show_resolve_undo;
 static int show_modified;
 static int show_killed;
 static int show_valid_bit;
+static int show_dirs;
 static int line_terminator = '\n';
 static int debug_mode;
 static int use_color;
@@ -337,6 +338,43 @@ static void show_files(struct dir_struct *dir)
        }
 }
 
+static void show_directories(const struct cache_entry *ce)
+{
+       static const char *last_directory;
+       struct strbuf sb = STRBUF_INIT;
+       const char *p = ce->name + prefix_len;
+       const char *sep;
+
+       if (last_directory) {
+               int len = strlen(last_directory);
+               if (!strncmp(ce->name, last_directory, len) &&
+                   ce->name[len] == '/')
+                       p += len + 1;
+       }
+
+       while (*p && (sep = strchr(p, '/'))) {
+               struct strbuf sb2 = STRBUF_INIT;
+               strbuf_reset(&sb);
+               strbuf_add(&sb, ce->name, sep - ce->name);
+               p = sep + 1;
+               if (!match_pathspec(&pathspec, sb.buf, sb.len,
+                                   prefix_len, NULL, 1))
+                       continue;
+               write_name(&sb2, sb.buf);
+               if (want_color(use_color)) {
+                       struct strbuf sb3 = STRBUF_INIT;
+                       color_filename(&sb3, ce->name, sb2.buf, S_IFDIR, 1);
+                       strbuf_release(&sb2);
+                       sb2 = sb3;
+               }
+               if (show_tag)
+                       strbuf_insert(&sb2, 0, tag_cached, strlen(tag_cached));
+               last_directory = strbuf_detach(&sb, NULL);
+               strbuf_fputs(&sb2, last_directory, NULL);
+               strbuf_release(&sb2);
+       }
+}
+
 static void show_files_compact(struct dir_struct *dir)
 {
        int i;
@@ -357,6 +395,8 @@ static void show_files_compact(struct dir_struct *dir)
                const struct cache_entry *ce = active_cache[i];
                struct stat st;
                int err, shown = 0;
+               if (show_dirs)
+                       show_directories(ce);
                if ((dir->flags & DIR_SHOW_IGNORED) &&
                    !ce_excluded(dir, ce))
                        continue;
@@ -825,6 +865,7 @@ int cmd_ls(int argc, const char **argv, const char 
*cmd_prefix)
        use_color = -1;
        max_depth = 0;
        show_tag = -1;
+       show_dirs = 1;
 
        argc = parse_options(argc, argv, prefix, builtin_ls_options,
                             ls_usage, 0);
-- 
1.9.1.345.ga1a145c

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to