Reduce the number of connects when fetching
[gitweb.git] / builtin-ls-files.c
index 3a0c5f215003bda5cfa2109953cd556765f6ea2c..0f0ab2da167c57402efad9dc09e58964759c23cd 100644 (file)
@@ -5,32 +5,29 @@
  *
  * Copyright (C) Linus Torvalds, 2005
  */
-#include <dirent.h>
-#include <fnmatch.h>
-
 #include "cache.h"
 #include "quote.h"
+#include "dir.h"
 #include "builtin.h"
-
-static int abbrev = 0;
-static int show_deleted = 0;
-static int show_cached = 0;
-static int show_others = 0;
-static int show_ignored = 0;
-static int show_stage = 0;
-static int show_unmerged = 0;
-static int show_modified = 0;
-static int show_killed = 0;
-static int show_other_directories = 0;
-static int hide_empty_directories = 0;
-static int show_valid_bit = 0;
+#include "tree.h"
+
+static int abbrev;
+static int show_deleted;
+static int show_cached;
+static int show_others;
+static int show_stage;
+static int show_unmerged;
+static int show_modified;
+static int show_killed;
+static int show_valid_bit;
 static int line_terminator = '\n';
 
-static int prefix_len = 0, prefix_offset = 0;
-static const char *prefix = NULL;
-static const char **pathspec = NULL;
-static int error_unmatch = 0;
-static char *ps_matched = NULL;
+static int prefix_len;
+static int prefix_offset;
+static const char **pathspec;
+static int error_unmatch;
+static char *ps_matched;
+static const char *with_tree;
 
 static const char *tag_cached = "";
 static const char *tag_unmerged = "";
@@ -39,333 +36,30 @@ static const char *tag_other = "";
 static const char *tag_killed = "";
 static const char *tag_modified = "";
 
-static const char *exclude_per_dir = NULL;
-
-/* We maintain three exclude pattern lists:
- * EXC_CMDL lists patterns explicitly given on the command line.
- * EXC_DIRS lists patterns obtained from per-directory ignore files.
- * EXC_FILE lists patterns from fallback ignore files.
- */
-#define EXC_CMDL 0
-#define EXC_DIRS 1
-#define EXC_FILE 2
-static struct exclude_list {
-       int nr;
-       int alloc;
-       struct exclude {
-               const char *pattern;
-               const char *base;
-               int baselen;
-       } **excludes;
-} exclude_list[3];
-
-static void add_exclude(const char *string, const char *base,
-                       int baselen, struct exclude_list *which)
-{
-       struct exclude *x = xmalloc(sizeof (*x));
-
-       x->pattern = string;
-       x->base = base;
-       x->baselen = baselen;
-       if (which->nr == which->alloc) {
-               which->alloc = alloc_nr(which->alloc);
-               which->excludes = realloc(which->excludes,
-                                         which->alloc * sizeof(x));
-       }
-       which->excludes[which->nr++] = x;
-}
-
-static int add_excludes_from_file_1(const char *fname,
-                                   const char *base,
-                                   int baselen,
-                                   struct exclude_list *which)
-{
-       int fd, i;
-       long size;
-       char *buf, *entry;
-
-       fd = open(fname, O_RDONLY);
-       if (fd < 0)
-               goto err;
-       size = lseek(fd, 0, SEEK_END);
-       if (size < 0)
-               goto err;
-       lseek(fd, 0, SEEK_SET);
-       if (size == 0) {
-               close(fd);
-               return 0;
-       }
-       buf = xmalloc(size+1);
-       if (read(fd, buf, size) != size)
-               goto err;
-       close(fd);
-
-       buf[size++] = '\n';
-       entry = buf;
-       for (i = 0; i < size; i++) {
-               if (buf[i] == '\n') {
-                       if (entry != buf + i && entry[0] != '#') {
-                               buf[i - (i && buf[i-1] == '\r')] = 0;
-                               add_exclude(entry, base, baselen, which);
-                       }
-                       entry = buf + i + 1;
-               }
-       }
-       return 0;
-
- err:
-       if (0 <= fd)
-               close(fd);
-       return -1;
-}
-
-static void add_excludes_from_file(const char *fname)
-{
-       if (add_excludes_from_file_1(fname, "", 0,
-                                    &exclude_list[EXC_FILE]) < 0)
-               die("cannot use %s as an exclude file", fname);
-}
-
-static int push_exclude_per_directory(const char *base, int baselen)
-{
-       char exclude_file[PATH_MAX];
-       struct exclude_list *el = &exclude_list[EXC_DIRS];
-       int current_nr = el->nr;
-
-       if (exclude_per_dir) {
-               memcpy(exclude_file, base, baselen);
-               strcpy(exclude_file + baselen, exclude_per_dir);
-               add_excludes_from_file_1(exclude_file, base, baselen, el);
-       }
-       return current_nr;
-}
-
-static void pop_exclude_per_directory(int stk)
-{
-       struct exclude_list *el = &exclude_list[EXC_DIRS];
-
-       while (stk < el->nr)
-               free(el->excludes[--el->nr]);
-}
-
-/* Scan the list and let the last match determines the fate.
- * Return 1 for exclude, 0 for include and -1 for undecided.
- */
-static int excluded_1(const char *pathname,
-                     int pathlen,
-                     struct exclude_list *el)
-{
-       int i;
-
-       if (el->nr) {
-               for (i = el->nr - 1; 0 <= i; i--) {
-                       struct exclude *x = el->excludes[i];
-                       const char *exclude = x->pattern;
-                       int to_exclude = 1;
-
-                       if (*exclude == '!') {
-                               to_exclude = 0;
-                               exclude++;
-                       }
-
-                       if (!strchr(exclude, '/')) {
-                               /* match basename */
-                               const char *basename = strrchr(pathname, '/');
-                               basename = (basename) ? basename+1 : pathname;
-                               if (fnmatch(exclude, basename, 0) == 0)
-                                       return to_exclude;
-                       }
-                       else {
-                               /* match with FNM_PATHNAME:
-                                * exclude has base (baselen long) implicitly
-                                * in front of it.
-                                */
-                               int baselen = x->baselen;
-                               if (*exclude == '/')
-                                       exclude++;
-
-                               if (pathlen < baselen ||
-                                   (baselen && pathname[baselen-1] != '/') ||
-                                   strncmp(pathname, x->base, baselen))
-                                   continue;
-
-                               if (fnmatch(exclude, pathname+baselen,
-                                           FNM_PATHNAME) == 0)
-                                       return to_exclude;
-                       }
-               }
-       }
-       return -1; /* undecided */
-}
-
-static int excluded(const char *pathname)
-{
-       int pathlen = strlen(pathname);
-       int st;
-
-       for (st = EXC_CMDL; st <= EXC_FILE; st++) {
-               switch (excluded_1(pathname, pathlen, &exclude_list[st])) {
-               case 0:
-                       return 0;
-               case 1:
-                       return 1;
-               }
-       }
-       return 0;
-}
-
-struct nond_on_fs {
-       int len;
-       char name[FLEX_ARRAY]; /* more */
-};
-
-static struct nond_on_fs **dir;
-static int nr_dir;
-static int dir_alloc;
-
-static void add_name(const char *pathname, int len)
-{
-       struct nond_on_fs *ent;
-
-       if (cache_name_pos(pathname, len) >= 0)
-               return;
-
-       if (nr_dir == dir_alloc) {
-               dir_alloc = alloc_nr(dir_alloc);
-               dir = xrealloc(dir, dir_alloc*sizeof(ent));
-       }
-       ent = xmalloc(sizeof(*ent) + len + 1);
-       ent->len = len;
-       memcpy(ent->name, pathname, len);
-       ent->name[len] = 0;
-       dir[nr_dir++] = ent;
-}
-
-static int dir_exists(const char *dirname, int len)
-{
-       int pos = cache_name_pos(dirname, len);
-       if (pos >= 0)
-               return 1;
-       pos = -pos-1;
-       if (pos >= active_nr) /* can't */
-               return 0;
-       return !strncmp(active_cache[pos]->name, dirname, len);
-}
 
 /*
- * Read a directory tree. We currently ignore anything but
- * directories, regular files and symlinks. That's because git
- * doesn't handle them at all yet. Maybe that will change some
- * day.
- *
- * Also, we ignore the name ".git" (even if it is not a directory).
- * That likely will not change.
- */
-static int read_directory(const char *path, const char *base, int baselen)
-{
-       DIR *fdir = opendir(path);
-       int contents = 0;
-
-       if (fdir) {
-               int exclude_stk;
-               struct dirent *de;
-               char fullname[MAXPATHLEN + 1];
-               memcpy(fullname, base, baselen);
-
-               exclude_stk = push_exclude_per_directory(base, baselen);
-
-               while ((de = readdir(fdir)) != NULL) {
-                       int len;
-
-                       if ((de->d_name[0] == '.') &&
-                           (de->d_name[1] == 0 ||
-                            !strcmp(de->d_name + 1, ".") ||
-                            !strcmp(de->d_name + 1, "git")))
-                               continue;
-                       len = strlen(de->d_name);
-                       memcpy(fullname + baselen, de->d_name, len+1);
-                       if (excluded(fullname) != show_ignored) {
-                               if (!show_ignored || DTYPE(de) != DT_DIR) {
-                                       continue;
-                               }
-                       }
-
-                       switch (DTYPE(de)) {
-                       struct stat st;
-                       int subdir, rewind_base;
-                       default:
-                               continue;
-                       case DT_UNKNOWN:
-                               if (lstat(fullname, &st))
-                                       continue;
-                               if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
-                                       break;
-                               if (!S_ISDIR(st.st_mode))
-                                       continue;
-                               /* fallthrough */
-                       case DT_DIR:
-                               memcpy(fullname + baselen + len, "/", 2);
-                               len++;
-                               rewind_base = nr_dir;
-                               subdir = read_directory(fullname, fullname,
-                                                       baselen + len);
-                               if (show_other_directories &&
-                                   (subdir || !hide_empty_directories) &&
-                                   !dir_exists(fullname, baselen + len)) {
-                                       // Rewind the read subdirectory
-                                       while (nr_dir > rewind_base)
-                                               free(dir[--nr_dir]);
-                                       break;
-                               }
-                               contents += subdir;
-                               continue;
-                       case DT_REG:
-                       case DT_LNK:
-                               break;
-                       }
-                       add_name(fullname, baselen + len);
-                       contents++;
-               }
-               closedir(fdir);
-
-               pop_exclude_per_directory(exclude_stk);
-       }
-
-       return contents;
-}
-
-static int cmp_name(const void *p1, const void *p2)
-{
-       const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1;
-       const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2;
-
-       return cache_name_compare(e1->name, e1->len,
-                                 e2->name, e2->len);
-}
-
-/*
- * Match a pathspec against a filename. The first "len" characters
+ * Match a pathspec against a filename. The first "skiplen" characters
  * are the common prefix
  */
-static int match(const char **spec, char *ps_matched,
-                const char *filename, int len)
+int pathspec_match(const char **spec, char *ps_matched,
+                  const char *filename, int skiplen)
 {
        const char *m;
 
        while ((m = *spec++) != NULL) {
-               int matchlen = strlen(m + len);
+               int matchlen = strlen(m + skiplen);
 
                if (!matchlen)
                        goto matched;
-               if (!strncmp(m + len, filename + len, matchlen)) {
-                       if (m[len + matchlen - 1] == '/')
+               if (!strncmp(m + skiplen, filename + skiplen, matchlen)) {
+                       if (m[skiplen + matchlen - 1] == '/')
                                goto matched;
-                       switch (filename[len + matchlen]) {
+                       switch (filename[skiplen + matchlen]) {
                        case '/': case '\0':
                                goto matched;
                        }
                }
-               if (!fnmatch(m + len, filename + len, 0))
+               if (!fnmatch(m + skiplen, filename + skiplen, 0))
                        goto matched;
                if (ps_matched)
                        ps_matched++;
@@ -378,7 +72,7 @@ static int match(const char **spec, char *ps_matched,
        return 0;
 }
 
-static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
+static void show_dir_entry(const char *tag, struct dir_entry *ent)
 {
        int len = prefix_len;
        int offset = prefix_offset;
@@ -386,42 +80,59 @@ static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
        if (len >= ent->len)
                die("git-ls-files: internal error - directory entry not superset of prefix");
 
-       if (pathspec && !match(pathspec, ps_matched, ent->name, len))
+       if (pathspec && !pathspec_match(pathspec, ps_matched, ent->name, len))
                return;
 
        fputs(tag, stdout);
-       write_name_quoted("", 0, ent->name + offset, line_terminator, stdout);
-       putchar(line_terminator);
+       write_name_quoted(ent->name + offset, stdout, line_terminator);
 }
 
-static void show_other_files(void)
+static void show_other_files(struct dir_struct *dir)
 {
        int i;
-       for (i = 0; i < nr_dir; i++) {
-               /* We should not have a matching entry, but we
-                * may have an unmerged entry for this path.
-                */
-               struct nond_on_fs *ent = dir[i];
-               int pos = cache_name_pos(ent->name, ent->len);
+
+
+       /*
+        * Skip matching and unmerged entries for the paths,
+        * since we want just "others".
+        *
+        * (Matching entries are normally pruned during
+        * the directory tree walk, but will show up for
+        * gitlinks because we don't necessarily have
+        * dir->show_other_directories set to suppress
+        * them).
+        */
+       for (i = 0; i < dir->nr; i++) {
+               struct dir_entry *ent = dir->entries[i];
+               int len, pos;
                struct cache_entry *ce;
+
+               /*
+                * Remove the '/' at the end that directory
+                * walking adds for directory entries.
+                */
+               len = ent->len;
+               if (len && ent->name[len-1] == '/')
+                       len--;
+               pos = cache_name_pos(ent->name, len);
                if (0 <= pos)
-                       die("bug in show-other-files");
+                       continue;       /* exact match */
                pos = -pos - 1;
-               if (pos < active_nr) { 
+               if (pos < active_nr) {
                        ce = active_cache[pos];
-                       if (ce_namelen(ce) == ent->len &&
-                           !memcmp(ce->name, ent->name, ent->len))
+                       if (ce_namelen(ce) == len &&
+                           !memcmp(ce->name, ent->name, len))
                                continue; /* Yup, this one exists unmerged */
                }
                show_dir_entry(tag_other, ent);
        }
 }
 
-static void show_killed_files(void)
+static void show_killed_files(struct dir_struct *dir)
 {
        int i;
-       for (i = 0; i < nr_dir; i++) {
-               struct nond_on_fs *ent = dir[i];
+       for (i = 0; i < dir->nr; i++) {
+               struct dir_entry *ent = dir->entries[i];
                char *cp, *sp;
                int pos, len, killed = 0;
 
@@ -462,7 +173,7 @@ static void show_killed_files(void)
                        }
                }
                if (killed)
-                       show_dir_entry(tag_killed, dir[i]);
+                       show_dir_entry(tag_killed, dir->entries[i]);
        }
 }
 
@@ -474,7 +185,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
        if (len >= ce_namelen(ce))
                die("git-ls-files: internal error - cache entry not superset of prefix");
 
-       if (pathspec && !match(pathspec, ps_matched, ce->name, len))
+       if (pathspec && !pathspec_match(pathspec, ps_matched, ce->name, len))
                return;
 
        if (tag && *tag && show_valid_bit &&
@@ -496,24 +207,18 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
 
        if (!show_stage) {
                fputs(tag, stdout);
-               write_name_quoted("", 0, ce->name + offset,
-                                 line_terminator, stdout);
-               putchar(line_terminator);
-       }
-       else {
+       } else {
                printf("%s%06o %s %d\t",
                       tag,
                       ntohl(ce->ce_mode),
                       abbrev ? find_unique_abbrev(ce->sha1,abbrev)
                                : sha1_to_hex(ce->sha1),
                       ce_stage(ce));
-               write_name_quoted("", 0, ce->name + offset,
-                                 line_terminator, stdout);
-               putchar(line_terminator);
        }
+       write_name_quoted(ce->name + offset, stdout, line_terminator);
 }
 
-static void show_files(void)
+static void show_files(struct dir_struct *dir, const char *prefix)
 {
        int i;
 
@@ -522,42 +227,23 @@ static void show_files(void)
                const char *path = ".", *base = "";
                int baselen = prefix_len;
 
-               if (baselen) {
+               if (baselen)
                        path = base = prefix;
-                       if (exclude_per_dir) {
-                               char *p, *pp = xmalloc(baselen+1);
-                               memcpy(pp, prefix, baselen+1);
-                               p = pp;
-                               while (1) {
-                                       char save = *p;
-                                       *p = 0;
-                                       push_exclude_per_directory(pp, p-pp);
-                                       *p++ = save;
-                                       if (!save)
-                                               break;
-                                       p = strchr(p, '/');
-                                       if (p)
-                                               p++;
-                                       else
-                                               p = pp + baselen;
-                               }
-                               free(pp);
-                       }
-               }
-               read_directory(path, base, baselen);
-               qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
+               read_directory(dir, path, base, baselen, pathspec);
                if (show_others)
-                       show_other_files();
+                       show_other_files(dir);
                if (show_killed)
-                       show_killed_files();
+                       show_killed_files(dir);
        }
        if (show_cached | show_stage) {
                for (i = 0; i < active_nr; i++) {
                        struct cache_entry *ce = active_cache[i];
-                       if (excluded(ce->name) != show_ignored)
+                       if (excluded(dir, ce->name) != dir->show_ignored)
                                continue;
                        if (show_unmerged && !ce_stage(ce))
                                continue;
+                       if (ce->ce_flags & htons(CE_UPDATE))
+                               continue;
                        show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
                }
        }
@@ -566,7 +252,7 @@ static void show_files(void)
                        struct cache_entry *ce = active_cache[i];
                        struct stat st;
                        int err;
-                       if (excluded(ce->name) != show_ignored)
+                       if (excluded(dir, ce->name) != dir->show_ignored)
                                continue;
                        err = lstat(ce->name, &st);
                        if (show_deleted && err)
@@ -580,14 +266,15 @@ static void show_files(void)
 /*
  * Prune the index to only contain stuff starting with "prefix"
  */
-static void prune_cache(void)
+static void prune_cache(const char *prefix)
 {
        int pos = cache_name_pos(prefix, prefix_len);
        unsigned int first, last;
 
        if (pos < 0)
                pos = -pos-1;
-       active_cache += pos;
+       memmove(active_cache, active_cache + pos,
+               (active_nr - pos) * sizeof(struct cache_entry *));
        active_nr -= pos;
        first = 0;
        last = active_nr;
@@ -603,10 +290,9 @@ static void prune_cache(void)
        active_nr = last;
 }
 
-static void verify_pathspec(void)
+static const char *verify_pathspec(const char *prefix)
 {
        const char **p, *n, *prev;
-       char *real_prefix;
        unsigned long max;
 
        prev = NULL;
@@ -633,28 +319,120 @@ static void verify_pathspec(void)
        if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
                die("git-ls-files: cannot generate relative filenames containing '..'");
 
-       real_prefix = NULL;
        prefix_len = max;
-       if (max) {
-               real_prefix = xmalloc(max + 1);
-               memcpy(real_prefix, prev, max);
-               real_prefix[max] = 0;
+       return max ? xmemdupz(prev, max) : NULL;
+}
+
+/*
+ * Read the tree specified with --with-tree option
+ * (typically, HEAD) into stage #1 and then
+ * squash them down to stage #0.  This is used for
+ * --error-unmatch to list and check the path patterns
+ * that were given from the command line.  We are not
+ * going to write this index out.
+ */
+void overlay_tree_on_cache(const char *tree_name, const char *prefix)
+{
+       struct tree *tree;
+       unsigned char sha1[20];
+       const char **match;
+       struct cache_entry *last_stage0 = NULL;
+       int i;
+
+       if (get_sha1(tree_name, sha1))
+               die("tree-ish %s not found.", tree_name);
+       tree = parse_tree_indirect(sha1);
+       if (!tree)
+               die("bad tree-ish %s", tree_name);
+
+       /* Hoist the unmerged entries up to stage #3 to make room */
+       for (i = 0; i < active_nr; i++) {
+               struct cache_entry *ce = active_cache[i];
+               if (!ce_stage(ce))
+                       continue;
+               ce->ce_flags |= htons(CE_STAGEMASK);
+       }
+
+       if (prefix) {
+               static const char *(matchbuf[2]);
+               matchbuf[0] = prefix;
+               matchbuf [1] = NULL;
+               match = matchbuf;
+       } else
+               match = NULL;
+       if (read_tree(tree, 1, match))
+               die("unable to read tree entries %s", tree_name);
+
+       for (i = 0; i < active_nr; i++) {
+               struct cache_entry *ce = active_cache[i];
+               switch (ce_stage(ce)) {
+               case 0:
+                       last_stage0 = ce;
+                       /* fallthru */
+               default:
+                       continue;
+               case 1:
+                       /*
+                        * If there is stage #0 entry for this, we do not
+                        * need to show it.  We use CE_UPDATE bit to mark
+                        * such an entry.
+                        */
+                       if (last_stage0 &&
+                           !strcmp(last_stage0->name, ce->name))
+                               ce->ce_flags |= htons(CE_UPDATE);
+               }
+       }
+}
+
+int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset)
+{
+       /*
+        * Make sure all pathspec matched; otherwise it is an error.
+        */
+       int num, errors = 0;
+       for (num = 0; pathspec[num]; num++) {
+               int other, found_dup;
+
+               if (ps_matched[num])
+                       continue;
+               /*
+                * The caller might have fed identical pathspec
+                * twice.  Do not barf on such a mistake.
+                */
+               for (found_dup = other = 0;
+                    !found_dup && pathspec[other];
+                    other++) {
+                       if (other == num || !ps_matched[other])
+                               continue;
+                       if (!strcmp(pathspec[other], pathspec[num]))
+                               /*
+                                * Ok, we have a match already.
+                                */
+                               found_dup = 1;
+               }
+               if (found_dup)
+                       continue;
+
+               error("pathspec '%s' did not match any file(s) known to git.",
+                     pathspec[num] + prefix_offset);
+               errors++;
        }
-       prefix = real_prefix;
+       return errors;
 }
 
 static const char ls_files_usage[] =
        "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
        "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
-       "[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] "
-       "[--] [<file>]*";
+       "[ --exclude-per-directory=<filename> ] [--exclude-standard] "
+       "[--full-name] [--abbrev] [--] [<file>]*";
 
-int cmd_ls_files(int argc, const char **argv, char** envp)
+int cmd_ls_files(int argc, const char **argv, const char *prefix)
 {
        int i;
-       int exc_given = 0;
+       int exc_given = 0, require_work_tree = 0;
+       struct dir_struct dir;
 
-       prefix = setup_git_directory();
+       memset(&dir, 0, sizeof(dir));
        if (prefix)
                prefix_offset = strlen(prefix);
        git_config(git_default_config);
@@ -691,14 +469,17 @@ int cmd_ls_files(int argc, const char **argv, char** envp)
                }
                if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
                        show_modified = 1;
+                       require_work_tree = 1;
                        continue;
                }
                if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
                        show_others = 1;
+                       require_work_tree = 1;
                        continue;
                }
                if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
-                       show_ignored = 1;
+                       dir.show_ignored = 1;
+                       require_work_tree = 1;
                        continue;
                }
                if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
@@ -707,14 +488,15 @@ int cmd_ls_files(int argc, const char **argv, char** envp)
                }
                if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
                        show_killed = 1;
+                       require_work_tree = 1;
                        continue;
                }
                if (!strcmp(arg, "--directory")) {
-                       show_other_directories = 1;
+                       dir.show_other_directories = 1;
                        continue;
                }
                if (!strcmp(arg, "--no-empty-directory")) {
-                       hide_empty_directories = 1;
+                       dir.hide_empty_directories = 1;
                        continue;
                }
                if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
@@ -727,27 +509,32 @@ int cmd_ls_files(int argc, const char **argv, char** envp)
                }
                if (!strcmp(arg, "-x") && i+1 < argc) {
                        exc_given = 1;
-                       add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]);
+                       add_exclude(argv[++i], "", 0, &dir.exclude_list[EXC_CMDL]);
                        continue;
                }
-               if (!strncmp(arg, "--exclude=", 10)) {
+               if (!prefixcmp(arg, "--exclude=")) {
                        exc_given = 1;
-                       add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]);
+                       add_exclude(arg+10, "", 0, &dir.exclude_list[EXC_CMDL]);
                        continue;
                }
                if (!strcmp(arg, "-X") && i+1 < argc) {
                        exc_given = 1;
-                       add_excludes_from_file(argv[++i]);
+                       add_excludes_from_file(&dir, argv[++i]);
                        continue;
                }
-               if (!strncmp(arg, "--exclude-from=", 15)) {
+               if (!prefixcmp(arg, "--exclude-from=")) {
                        exc_given = 1;
-                       add_excludes_from_file(arg+15);
+                       add_excludes_from_file(&dir, arg+15);
                        continue;
                }
-               if (!strncmp(arg, "--exclude-per-directory=", 24)) {
+               if (!prefixcmp(arg, "--exclude-per-directory=")) {
                        exc_given = 1;
-                       exclude_per_dir = arg + 24;
+                       dir.exclude_per_dir = arg + 24;
+                       continue;
+               }
+               if (!strcmp(arg, "--exclude-standard")) {
+                       exc_given = 1;
+                       setup_standard_excludes(&dir);
                        continue;
                }
                if (!strcmp(arg, "--full-name")) {
@@ -758,7 +545,11 @@ int cmd_ls_files(int argc, const char **argv, char** envp)
                        error_unmatch = 1;
                        continue;
                }
-               if (!strncmp(arg, "--abbrev=", 9)) {
+               if (!prefixcmp(arg, "--with-tree=")) {
+                       with_tree = arg + 12;
+                       continue;
+               }
+               if (!prefixcmp(arg, "--abbrev=")) {
                        abbrev = strtoul(arg+9, NULL, 10);
                        if (abbrev && abbrev < MINIMUM_ABBREV)
                                abbrev = MINIMUM_ABBREV;
@@ -775,11 +566,14 @@ int cmd_ls_files(int argc, const char **argv, char** envp)
                break;
        }
 
+       if (require_work_tree && !is_inside_work_tree())
+               setup_work_tree();
+
        pathspec = get_pathspec(prefix, argv + i);
 
        /* Verify that the pathspec matches the prefix */
        if (pathspec)
-               verify_pathspec();
+               prefix = verify_pathspec(prefix);
 
        /* Treat unmatching pathspec elements as errors */
        if (pathspec && error_unmatch) {
@@ -789,7 +583,7 @@ int cmd_ls_files(int argc, const char **argv, char** envp)
                ps_matched = xcalloc(1, num);
        }
 
-       if (show_ignored && !exc_given) {
+       if (dir.show_ignored && !exc_given) {
                fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
                        argv[0]);
                exit(1);
@@ -802,22 +596,25 @@ int cmd_ls_files(int argc, const char **argv, char** envp)
 
        read_cache();
        if (prefix)
-               prune_cache();
-       show_files();
+               prune_cache(prefix);
+       if (with_tree) {
+               /*
+                * Basic sanity check; show-stages and show-unmerged
+                * would not make any sense with this option.
+                */
+               if (show_stage || show_unmerged)
+                       die("ls-files --with-tree is incompatible with -s or -u");
+               overlay_tree_on_cache(with_tree, prefix);
+       }
+       show_files(&dir, prefix);
 
        if (ps_matched) {
-               /* We need to make sure all pathspec matched otherwise
-                * it is an error.
-                */
-               int num, errors = 0;
-               for (num = 0; pathspec[num]; num++) {
-                       if (ps_matched[num])
-                               continue;
-                       error("pathspec '%s' did not match any.",
-                             pathspec[num] + prefix_offset);
-                       errors++;
-               }
-               return errors ? 1 : 0;
+               int bad;
+               bad = report_path_error(ps_matched, pathspec, prefix_offset);
+               if (bad)
+                       fprintf(stderr, "Did you forget to 'git add'?\n");
+
+               return bad ? 1 : 0;
        }
 
        return 0;