Skip to content

Commit 777c55a

Browse files
committed
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 <[email protected]>
1 parent 282616c commit 777c55a

File tree

4 files changed

+44
-44
lines changed

4 files changed

+44
-44
lines changed

builtin/ls-files.c

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -354,49 +354,6 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
354354
}
355355
}
356356

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-
400357
static const char * const ls_files_usage[] = {
401358
N_("git ls-files [options] [<file>...]"),
402359
NULL

cache.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,6 @@ extern int ws_blank_line(const char *line, int len, unsigned ws_rule);
14111411
#define ws_tab_width(rule) ((rule) & WS_TAB_WIDTH_MASK)
14121412

14131413
/* ls-files */
1414-
int report_path_error(const char *ps_matched, const struct pathspec *pathspec, const char *prefix);
14151414
void overlay_tree_on_cache(const char *tree_name, const char *prefix);
14161415

14171416
char *alias_lookup(const char *alias);

dir.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,49 @@ int match_pathspec(const struct pathspec *ps,
377377
return negative ? 0 : positive;
378378
}
379379

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+
380423
/*
381424
* Return the length of the "simple" part of a path match limiter.
382425
*/

dir.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ extern char *common_prefix(const struct pathspec *pathspec);
135135
extern int match_pathspec(const struct pathspec *pathspec,
136136
const char *name, int namelen,
137137
int prefix, char *seen, int is_dir);
138+
extern int report_path_error(const char *ps_matched, const struct pathspec *pathspec, const char *prefix);
138139
extern int within_depth(const char *name, int namelen, int depth, int max_depth);
139140

140141
extern int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec);

0 commit comments

Comments
 (0)