filter-branch: always export GIT_DIR if it is set
[gitweb.git] / builtin-ls-files.c
index 21c2a6e2d9b68c0c3bcfac824e85c7b392c72486..48a313516db12dbb74d1f0293d3a55edc0cb3f2c 100644 (file)
@@ -89,20 +89,38 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)
 static void show_other_files(struct dir_struct *dir)
 {
        int i;
+
+
+       /*
+        * 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++) {
-               /* We should not have a matching entry, but we
-                * may have an unmerged entry for this path.
-                */
                struct dir_entry *ent = dir->entries[i];
-               int pos = cache_name_pos(ent->name, ent->len);
+               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) { 
                        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);
@@ -216,7 +234,7 @@ static void show_files(struct dir_struct *dir, const char *prefix)
 
                if (baselen)
                        path = base = prefix;
-               read_directory(dir, path, base, baselen);
+               read_directory(dir, path, base, baselen, pathspec);
                if (show_others)
                        show_other_files(dir);
                if (show_killed)
@@ -323,7 +341,7 @@ static const char ls_files_usage[] =
 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;
 
        memset(&dir, 0, sizeof(dir));
@@ -363,14 +381,17 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
                }
                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")) {
                        dir.show_ignored = 1;
+                       require_work_tree = 1;
                        continue;
                }
                if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
@@ -379,6 +400,7 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
                }
                if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
                        show_killed = 1;
+                       require_work_tree = 1;
                        continue;
                }
                if (!strcmp(arg, "--directory")) {
@@ -402,7 +424,7 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
                        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, &dir.exclude_list[EXC_CMDL]);
                        continue;
@@ -412,12 +434,12 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
                        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(&dir, arg+15);
                        continue;
                }
-               if (!strncmp(arg, "--exclude-per-directory=", 24)) {
+               if (!prefixcmp(arg, "--exclude-per-directory=")) {
                        exc_given = 1;
                        dir.exclude_per_dir = arg + 24;
                        continue;
@@ -430,7 +452,7 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
                        error_unmatch = 1;
                        continue;
                }
-               if (!strncmp(arg, "--abbrev=", 9)) {
+               if (!prefixcmp(arg, "--abbrev=")) {
                        abbrev = strtoul(arg+9, NULL, 10);
                        if (abbrev && abbrev < MINIMUM_ABBREV)
                                abbrev = MINIMUM_ABBREV;
@@ -447,6 +469,10 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
                break;
        }
 
+       if (require_work_tree &&
+                       (!is_inside_work_tree() || is_inside_git_dir()))
+               die("This operation must be run in a work tree");
+
        pathspec = get_pathspec(prefix, argv + i);
 
        /* Verify that the pathspec matches the prefix */