convert report_path_error to take struct pathspec
[gitweb.git] / builtin / ls-files.c
index 22020729cbc35dc5fe11d2a3194fe5994fe67ff9..50e6edf07e9d706ed5208ced7d0740ab2310b84e 100644 (file)
@@ -13,6 +13,7 @@
 #include "parse-options.h"
 #include "resolve-undo.h"
 #include "string-list.h"
+#include "pathspec.h"
 
 static int abbrev;
 static int show_deleted;
@@ -30,7 +31,7 @@ static int debug_mode;
 static const char *prefix;
 static int max_prefix_len;
 static int prefix_len;
-static const char **pathspec;
+static struct pathspec pathspec;
 static int error_unmatch;
 static char *ps_matched;
 static const char *with_tree;
@@ -59,7 +60,7 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)
        if (len >= ent->len)
                die("git ls-files: internal error - directory entry not superset of prefix");
 
-       if (!match_pathspec(pathspec, ent->name, ent->len, len, ps_matched))
+       if (!match_pathspec_depth(&pathspec, ent->name, ent->len, len, ps_matched))
                return;
 
        fputs(tag, stdout);
@@ -134,7 +135,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 (!match_pathspec(pathspec, ce->name, ce_namelen(ce), len, ps_matched))
+       if (!match_pathspec_depth(&pathspec, ce->name, ce_namelen(ce), len, ps_matched))
                return;
 
        if (tag && *tag && show_valid_bit &&
@@ -165,11 +166,13 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
        }
        write_name(ce->name, ce_namelen(ce));
        if (debug_mode) {
-               printf("  ctime: %d:%d\n", ce->ce_ctime.sec, ce->ce_ctime.nsec);
-               printf("  mtime: %d:%d\n", ce->ce_mtime.sec, ce->ce_mtime.nsec);
-               printf("  dev: %d\tino: %d\n", ce->ce_dev, ce->ce_ino);
-               printf("  uid: %d\tgid: %d\n", ce->ce_uid, ce->ce_gid);
-               printf("  size: %d\tflags: %x\n", ce->ce_size, ce->ce_flags);
+               struct stat_data *sd = &ce->ce_stat_data;
+
+               printf("  ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
+               printf("  mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
+               printf("  dev: %d\tino: %d\n", sd->sd_dev, sd->sd_ino);
+               printf("  uid: %d\tgid: %d\n", sd->sd_uid, sd->sd_gid);
+               printf("  size: %d\tflags: %x\n", sd->sd_size, ce->ce_flags);
        }
 }
 
@@ -188,7 +191,7 @@ static void show_ru_info(void)
                len = strlen(path);
                if (len < max_prefix_len)
                        continue; /* outside of the prefix */
-               if (!match_pathspec(pathspec, path, len, max_prefix_len, ps_matched))
+               if (!match_pathspec_depth(&pathspec, path, len, max_prefix_len, ps_matched))
                        continue; /* uninterested */
                for (i = 0; i < 3; i++) {
                        if (!ui->mode[i])
@@ -213,13 +216,13 @@ static void show_files(struct dir_struct *dir)
 
        /* For cached/deleted files we don't need to even do the readdir */
        if (show_others || show_killed) {
-               fill_directory(dir, pathspec);
+               fill_directory(dir, pathspec.raw);
                if (show_others)
                        show_other_files(dir);
                if (show_killed)
                        show_killed_files(dir);
        }
-       if (show_cached | show_stage) {
+       if (show_cached || show_stage) {
                for (i = 0; i < active_nr; i++) {
                        struct cache_entry *ce = active_cache[i];
                        if ((dir->flags & DIR_SHOW_IGNORED) &&
@@ -233,7 +236,7 @@ static void show_files(struct dir_struct *dir)
                                (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce);
                }
        }
-       if (show_deleted | show_modified) {
+       if (show_deleted || show_modified) {
                for (i = 0; i < active_nr; i++) {
                        struct cache_entry *ce = active_cache[i];
                        struct stat st;
@@ -281,21 +284,6 @@ static void prune_cache(const char *prefix)
        active_nr = last;
 }
 
-static void strip_trailing_slash_from_submodules(void)
-{
-       const char **p;
-
-       for (p = pathspec; *p != NULL; p++) {
-               int len = strlen(*p), pos;
-
-               if (len < 1 || (*p)[len - 1] != '/')
-                       continue;
-               pos = cache_name_pos(*p, len - 1);
-               if (pos >= 0 && S_ISGITLINK(active_cache[pos]->ce_mode))
-                       *p = xstrndup(*p, len - 1);
-       }
-}
-
 /*
  * Read the tree specified with --with-tree option
  * (typically, HEAD) into stage #1 and then
@@ -358,15 +346,16 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
        }
 }
 
-int report_path_error(const char *ps_matched, const char **pathspec, const char *prefix)
+int report_path_error(const char *ps_matched,
+                     const struct pathspec *pathspec,
+                     const char *prefix)
 {
        /*
         * Make sure all pathspec matched; otherwise it is an error.
         */
        struct strbuf sb = STRBUF_INIT;
-       const char *name;
        int num, errors = 0;
-       for (num = 0; pathspec[num]; num++) {
+       for (num = 0; num < pathspec->nr; num++) {
                int other, found_dup;
 
                if (ps_matched[num])
@@ -374,13 +363,16 @@ int report_path_error(const char *ps_matched, const char **pathspec, const char
                /*
                 * The caller might have fed identical pathspec
                 * twice.  Do not barf on such a mistake.
+                * FIXME: parse_pathspec should have eliminated
+                * duplicate pathspec.
                 */
                for (found_dup = other = 0;
-                    !found_dup && pathspec[other];
+                    !found_dup && other < pathspec->nr;
                     other++) {
                        if (other == num || !ps_matched[other])
                                continue;
-                       if (!strcmp(pathspec[other], pathspec[num]))
+                       if (!strcmp(pathspec->items[other].original,
+                                   pathspec->items[num].original))
                                /*
                                 * Ok, we have a match already.
                                 */
@@ -389,9 +381,8 @@ int report_path_error(const char *ps_matched, const char **pathspec, const char
                if (found_dup)
                        continue;
 
-               name = quote_path_relative(pathspec[num], -1, &sb, prefix);
                error("pathspec '%s' did not match any file(s) known to git.",
-                     name);
+                     pathspec->items[num].original);
                errors++;
        }
        strbuf_release(&sb);
@@ -549,30 +540,25 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
        if (require_work_tree && !is_inside_work_tree())
                setup_work_tree();
 
-       pathspec = get_pathspec(prefix, argv);
-
-       /* be nice with submodule paths ending in a slash */
-       if (pathspec)
-               strip_trailing_slash_from_submodules();
+       parse_pathspec(&pathspec, 0,
+                      PATHSPEC_PREFER_CWD |
+                      PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
+                      prefix, argv);
 
        /* Find common prefix for all pathspec's */
-       max_prefix = common_prefix(pathspec);
+       max_prefix = common_prefix(pathspec.raw);
        max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
 
        /* Treat unmatching pathspec elements as errors */
-       if (pathspec && error_unmatch) {
-               int num;
-               for (num = 0; pathspec[num]; num++)
-                       ;
-               ps_matched = xcalloc(1, num);
-       }
+       if (pathspec.nr && error_unmatch)
+               ps_matched = xcalloc(1, pathspec.nr);
 
        if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
                die("ls-files --ignored needs some exclude pattern");
 
        /* With no flags, we default to showing the cached files */
-       if (!(show_stage | show_deleted | show_others | show_unmerged |
-             show_killed | show_modified | show_resolve_undo))
+       if (!(show_stage || show_deleted || show_others || show_unmerged ||
+             show_killed || show_modified || show_resolve_undo))
                show_cached = 1;
 
        if (max_prefix)
@@ -592,7 +578,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 
        if (ps_matched) {
                int bad;
-               bad = report_path_error(ps_matched, pathspec, prefix);
+               bad = report_path_error(ps_matched, &pathspec, prefix);
                if (bad)
                        fprintf(stderr, "Did you forget to 'git add'?\n");