report_path_error(): move to dir.c
authorJunio C Hamano <gitster@pobox.com>
Tue, 24 Mar 2015 21:12:10 +0000 (14:12 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 24 Mar 2015 21:12:10 +0000 (14:12 -0700)
The expected call sequence is for the caller to use match_pathspec()
repeatedly on a set of pathspecs, accumulating the "hits" in a
separate array, and then call this function to diagnose a pathspec
that never matched anything, as that can indicate a typo from the
command line, e.g. "git commit Maekfile".

Many builtin commands use this function from builtin/ls-files.c,
which is not a very healthy arrangement. ls-files might have been
the first command to feel the need for such a helper, but the need
is shared by everybody who uses the "match and then report" pattern.

Move it to dir.c where match_pathspec() is defined.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/ls-files.c
cache.h
dir.c
dir.h
index 47c38808a26a4602f8bd2c7d87f67770a625c679..47d70b2b9be9ea757a65239065089ac603569ffa 100644 (file)
@@ -354,49 +354,6 @@ void overlay_tree_on_cache(const char *tree_name, 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;
-       int num, errors = 0;
-       for (num = 0; num < pathspec->nr; 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.
-                * FIXME: parse_pathspec should have eliminated
-                * duplicate pathspec.
-                */
-               for (found_dup = other = 0;
-                    !found_dup && other < pathspec->nr;
-                    other++) {
-                       if (other == num || !ps_matched[other])
-                               continue;
-                       if (!strcmp(pathspec->items[other].original,
-                                   pathspec->items[num].original))
-                               /*
-                                * 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->items[num].original);
-               errors++;
-       }
-       strbuf_release(&sb);
-       return errors;
-}
-
 static const char * const ls_files_usage[] = {
        N_("git ls-files [options] [<file>...]"),
        NULL
diff --git a/cache.h b/cache.h
index f23fdbee96fc9ad14e3a16e77d277293207dd6c3..8ec0b65ee5453b2e4841d34e96b9da680df847c0 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -1411,7 +1411,6 @@ extern int ws_blank_line(const char *line, int len, unsigned ws_rule);
 #define ws_tab_width(rule)     ((rule) & WS_TAB_WIDTH_MASK)
 
 /* ls-files */
-int report_path_error(const char *ps_matched, const struct pathspec *pathspec, const char *prefix);
 void overlay_tree_on_cache(const char *tree_name, const char *prefix);
 
 char *alias_lookup(const char *alias);
diff --git a/dir.c b/dir.c
index 797805d6a1d33ef66ccb61155135cdcd1a53d9d0..5d6e102a1949d2569ff69b5cf35e27ece5de2a96 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -377,6 +377,49 @@ int match_pathspec(const struct pathspec *ps,
        return negative ? 0 : positive;
 }
 
+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;
+       int num, errors = 0;
+       for (num = 0; num < pathspec->nr; 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.
+                * FIXME: parse_pathspec should have eliminated
+                * duplicate pathspec.
+                */
+               for (found_dup = other = 0;
+                    !found_dup && other < pathspec->nr;
+                    other++) {
+                       if (other == num || !ps_matched[other])
+                               continue;
+                       if (!strcmp(pathspec->items[other].original,
+                                   pathspec->items[num].original))
+                               /*
+                                * 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->items[num].original);
+               errors++;
+       }
+       strbuf_release(&sb);
+       return errors;
+}
+
 /*
  * Return the length of the "simple" part of a path match limiter.
  */
diff --git a/dir.h b/dir.h
index 55e53456afab4c9fb8441144ac7393458b553006..ed336add79282315d3c321dbf91a35127c05d42e 100644 (file)
--- a/dir.h
+++ b/dir.h
@@ -135,6 +135,7 @@ extern char *common_prefix(const struct pathspec *pathspec);
 extern int match_pathspec(const struct pathspec *pathspec,
                          const char *name, int namelen,
                          int prefix, char *seen, int is_dir);
+extern int report_path_error(const char *ps_matched, const struct pathspec *pathspec, const char *prefix);
 extern int within_depth(const char *name, int namelen, int depth, int max_depth);
 
 extern int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec);