report_path_error(): move to dir.c

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>

Junio C Hamano committed Mar 24, 2015 at 14:12 UTC 777c55a61615837d4391facd75cf334b96635801
4 files changed +44 -44
builtin/ls-files.c
-43
@@ -354,49 +354,6 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
354 }
355 }
356
357 -int report_path_error(const char *ps_matched,
358 - const struct pathspec *pathspec,
359 - const char *prefix)
360 -{
361 - /*
362 - * Make sure all pathspec matched; otherwise it is an error.
363 - */
364 - struct strbuf sb = STRBUF_INIT;
365 - int num, errors = 0;
366 - for (num = 0; num < pathspec->nr; num++) {
367 - int other, found_dup;
368 -
369 - if (ps_matched[num])
370 - continue;
371 - /*
372 - * The caller might have fed identical pathspec
373 - * twice. Do not barf on such a mistake.
374 - * FIXME: parse_pathspec should have eliminated
375 - * duplicate pathspec.
376 - */
377 - for (found_dup = other = 0;
378 - !found_dup && other < pathspec->nr;
379 - other++) {
380 - if (other == num || !ps_matched[other])
381 - continue;
382 - if (!strcmp(pathspec->items[other].original,
383 - pathspec->items[num].original))
384 - /*
385 - * Ok, we have a match already.
386 - */
387 - found_dup = 1;
388 - }
389 - if (found_dup)
390 - continue;
391 -
392 - error("pathspec '%s' did not match any file(s) known to git.",
393 - pathspec->items[num].original);
394 - errors++;
395 - }
396 - strbuf_release(&sb);
397 - return errors;
398 -}
399 -
357 static const char * const ls_files_usage[] = {
358 N_("git ls-files [options] [<file>...]"),
359 NULL
cache.h
-1
@@ -1411,7 +1411,6 @@ extern int ws_blank_line(const char *line, int len, unsigned ws_rule);
1411 #define ws_tab_width(rule) ((rule) & WS_TAB_WIDTH_MASK)
1412
1413 /* ls-files */
1414 -int report_path_error(const char *ps_matched, const struct pathspec *pathspec, const char *prefix);
1414 void overlay_tree_on_cache(const char *tree_name, const char *prefix);
1415
1416 char *alias_lookup(const char *alias);
dir.c
+43
@@ -377,6 +377,49 @@ int match_pathspec(const struct pathspec *ps,
377 return negative ? 0 : positive;
378 }
379
380 +int report_path_error(const char *ps_matched,
381 + const struct pathspec *pathspec,
382 + const char *prefix)
383 +{
384 + /*
385 + * Make sure all pathspec matched; otherwise it is an error.
386 + */
387 + struct strbuf sb = STRBUF_INIT;
388 + int num, errors = 0;
389 + for (num = 0; num < pathspec->nr; num++) {
390 + int other, found_dup;
391 +
392 + if (ps_matched[num])
393 + continue;
394 + /*
395 + * The caller might have fed identical pathspec
396 + * twice. Do not barf on such a mistake.
397 + * FIXME: parse_pathspec should have eliminated
398 + * duplicate pathspec.
399 + */
400 + for (found_dup = other = 0;
401 + !found_dup && other < pathspec->nr;
402 + other++) {
403 + if (other == num || !ps_matched[other])
404 + continue;
405 + if (!strcmp(pathspec->items[other].original,
406 + pathspec->items[num].original))
407 + /*
408 + * Ok, we have a match already.
409 + */
410 + found_dup = 1;
411 + }
412 + if (found_dup)
413 + continue;
414 +
415 + error("pathspec '%s' did not match any file(s) known to git.",
416 + pathspec->items[num].original);
417 + errors++;
418 + }
419 + strbuf_release(&sb);
420 + return errors;
421 +}
422 +
423 /*
424 * Return the length of the "simple" part of a path match limiter.
425 */
dir.h
+1
@@ -135,6 +135,7 @@ extern char *common_prefix(const struct pathspec *pathspec);
135 extern int match_pathspec(const struct pathspec *pathspec,
136 const char *name, int namelen,
137 int prefix, char *seen, int is_dir);
138 +extern int report_path_error(const char *ps_matched, const struct pathspec *pathspec, const char *prefix);
139 extern int within_depth(const char *name, int namelen, int depth, int max_depth);
140
141 extern int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec);