report_path_error(): move to dir.c
[gitweb.git] / dir.c
diff --git a/dir.c b/dir.c
index eb6f581270f81a2e844e3396f966e67d06d27f41..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.
  */
@@ -508,21 +551,25 @@ void clear_exclude_list(struct exclude_list *el)
 
 static void trim_trailing_spaces(char *buf)
 {
-       int i, last_space = -1, nr_spaces, len = strlen(buf);
-       for (i = 0; i < len; i++)
-               if (buf[i] == '\\')
-                       i++;
-               else if (buf[i] == ' ') {
-                       if (last_space == -1) {
-                               last_space = i;
-                               nr_spaces = 1;
-                       } else
-                               nr_spaces++;
-               } else
-                       last_space = -1;
-
-       if (last_space != -1 && last_space + nr_spaces == len)
-               buf[last_space] = '\0';
+       char *p, *last_space = NULL;
+
+       for (p = buf; *p; p++)
+               switch (*p) {
+               case ' ':
+                       if (!last_space)
+                               last_space = p;
+                       break;
+               case '\\':
+                       p++;
+                       if (!*p)
+                               return;
+                       /* fallthrough */
+               default:
+                       last_space = NULL;
+               }
+
+       if (last_space)
+               *last_space = '\0';
 }
 
 int add_excludes_from_file_to_list(const char *fname,