Fix error-prone fill_directory() API; make it only return matches
Traditionally, the expected calling convention for the dir.c API was: fill_directory(&dir, ..., pathspec) foreach entry in dir->entries: if (dir_path_match(entry, pathspec)) process_or_display(entry) This may have made sense once upon a time, because the fill_directory() call could use cheap checks to avoid doing full pathspec matching, and an external caller may have wanted to do other post-processing of the results anyway. However: * this structure makes it easy for users of the API to get it wrong * this structure actually makes it harder to understand fill_directory() and the functions it uses internally. It has tripped me up several times while trying to fix bugs and restructure things. * relying on post-filtering was already found to produce wrong results; pathspec matching had to be added internally for multiple cases in order to get the right results (see commits404ebceda0
(dir: also check directories for matching pathspecs, 2019-09-17) and89a1f4aaf7
(dir: if our pathspec might match files under a dir, recurse into it, 2019-09-17)) * it's bad for performance: fill_directory() already has to do lots of checks and knows the subset of cases where it still needs to do more checks. Forcing external callers to do full pathspec matching means they must re-check _every_ path. So, add the pathspec matching within the fill_directory() internals, and remove it from external callers. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
7f45ab2dca
commit
95c11ecc73
@ -691,8 +691,6 @@ static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
|
||||
|
||||
fill_directory(&dir, opt->repo->index, pathspec);
|
||||
for (i = 0; i < dir.nr; i++) {
|
||||
if (!dir_path_match(opt->repo->index, dir.entries[i], pathspec, 0, NULL))
|
||||
continue;
|
||||
hit |= grep_file(opt, dir.entries[i]->name);
|
||||
if (hit && opt->status_only)
|
||||
break;
|
||||
|
Reference in New Issue
Block a user