]> git.ipfire.org Git - thirdparty/git.git/blobdiff - builtin-ls-files.c
GIT-VERSION-FILE: check ./version first.
[thirdparty/git.git] / builtin-ls-files.c
index 8dae9f70e2f9079d521fbc80e317addf41346d0f..ac89eb2f770d3b8dbc35ee2c24d9d6c82b8fb6fe 100644 (file)
@@ -5,29 +5,27 @@
  *
  * Copyright (C) Linus Torvalds, 2005
  */
-#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_stage = 0;
-static int show_unmerged = 0;
-static int show_modified = 0;
-static int show_killed = 0;
-static int show_valid_bit = 0;
+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 *tag_cached = "";
 static const char *tag_unmerged = "";
@@ -207,7 +205,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
        }
 }
 
-static void show_files(struct dir_struct *dir)
+static void show_files(struct dir_struct *dir, const char *prefix)
 {
        int i;
 
@@ -253,7 +251,7 @@ static void show_files(struct dir_struct *dir)
 /*
  * 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;
@@ -276,7 +274,7 @@ 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;
@@ -313,7 +311,7 @@ static void verify_pathspec(void)
                memcpy(real_prefix, prev, max);
                real_prefix[max] = 0;
        }
-       prefix = real_prefix;
+       return real_prefix;
 }
 
 static const char ls_files_usage[] =
@@ -322,14 +320,13 @@ static const char ls_files_usage[] =
        "[ --exclude-per-directory=<filename> ] [--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;
 
        memset(&dir, 0, sizeof(dir));
-       prefix = setup_git_directory();
        if (prefix)
                prefix_offset = strlen(prefix);
        git_config(git_default_config);
@@ -366,14 +363,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")) {
                        dir.show_ignored = 1;
+                       require_work_tree = 1;
                        continue;
                }
                if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
@@ -382,6 +382,7 @@ 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")) {
@@ -450,11 +451,15 @@ int cmd_ls_files(int argc, const char **argv, char** envp)
                break;
        }
 
+       if (require_work_tree &&
+                       (is_bare_repository() || 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 */
        if (pathspec)
-               verify_pathspec();
+               prefix = verify_pathspec(prefix);
 
        /* Treat unmatching pathspec elements as errors */
        if (pathspec && error_unmatch) {
@@ -477,8 +482,8 @@ int cmd_ls_files(int argc, const char **argv, char** envp)
 
        read_cache();
        if (prefix)
-               prune_cache();
-       show_files(&dir);
+               prune_cache(prefix);
+       show_files(&dir, prefix);
 
        if (ps_matched) {
                /* We need to make sure all pathspec matched otherwise
@@ -488,10 +493,14 @@ int cmd_ls_files(int argc, const char **argv, char** envp)
                for (num = 0; pathspec[num]; num++) {
                        if (ps_matched[num])
                                continue;
-                       error("pathspec '%s' did not match any.",
+                       error("pathspec '%s' did not match any file(s) known to git.",
                              pathspec[num] + prefix_offset);
                        errors++;
                }
+
+               if (errors)
+                       fprintf(stderr, "Did you forget to 'git add'?\n");
+
                return errors ? 1 : 0;
        }