dir: revert "dir: select directories correctly"

This reverts commit f6526728f950cacfd5b5e42bcc65f2c47f3da654. The change in f652672 (dir: select directories correctly, 2021-09-24) caused a regression in directory-based matches with non-cone-mode patterns, especially for .gitignore patterns. A test is included to prevent this regression in the future. The commit ed495847 (dir: fix pattern matching on dirs, 2021-09-24) was reverted in 5ceb663 (dir: fix directory-matching bug, 2021-11-02) for similar reasons. Neither commit changed tests, and tests added later in the series continue to pass when these commits are reverted. Reported-by: Danial Alihosseini <danial.alihosseini@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Nov 19, 2021 at 09:13 UTC 33c5d6c8456ecb1e89450483586f5f3f115ffa93
2 files changed +22 -49
dir.c
+5 -49
@@ -1303,44 +1303,6 @@ int match_pathname(const char *pathname, int pathlen,
1303 WM_PATHNAME) == 0;
1304 }
1305
1306 -static int path_matches_dir_pattern(const char *pathname,
1307 - int pathlen,
1308 - struct strbuf **path_parent,
1309 - int *dtype,
1310 - struct path_pattern *pattern,
1311 - struct index_state *istate)
1312 -{
1313 - if (!*path_parent) {
1314 - char *slash;
1315 - CALLOC_ARRAY(*path_parent, 1);
1316 - strbuf_add(*path_parent, pathname, pathlen);
1317 - slash = find_last_dir_sep((*path_parent)->buf);
1318 -
1319 - if (slash)
1320 - strbuf_setlen(*path_parent, slash - (*path_parent)->buf);
1321 - else
1322 - strbuf_setlen(*path_parent, 0);
1323 - }
1324 -
1325 - /*
1326 - * If the parent directory matches the pattern, then we do not
1327 - * need to check for dtype.
1328 - */
1329 - if ((*path_parent)->len &&
1330 - match_pathname((*path_parent)->buf, (*path_parent)->len,
1331 - pattern->base,
1332 - pattern->baselen ? pattern->baselen - 1 : 0,
1333 - pattern->pattern, pattern->nowildcardlen,
1334 - pattern->patternlen, pattern->flags))
1335 - return 1;
1336 -
1337 - *dtype = resolve_dtype(*dtype, istate, pathname, pathlen);
1338 - if (*dtype != DT_DIR)
1339 - return 0;
1340 -
1341 - return 1;
1342 -}
1343 -
1306 /*
1307 * Scan the given exclude list in reverse to see whether pathname
1308 * should be ignored. The first match (i.e. the last on the list), if
@@ -1356,7 +1318,6 @@ static struct path_pattern *last_matching_pattern_from_list(const char *pathname
1318 {
1319 struct path_pattern *res = NULL; /* undecided */
1320 int i;
1359 - struct strbuf *path_parent = NULL;
1321
1322 if (!pl->nr)
1323 return NULL; /* undefined */
@@ -1366,10 +1327,11 @@ static struct path_pattern *last_matching_pattern_from_list(const char *pathname
1327 const char *exclude = pattern->pattern;
1328 int prefix = pattern->nowildcardlen;
1329
1369 - if (pattern->flags & PATTERN_FLAG_MUSTBEDIR &&
1370 - !path_matches_dir_pattern(pathname, pathlen, &path_parent,
1371 - dtype, pattern, istate))
1372 - continue;
1330 + if (pattern->flags & PATTERN_FLAG_MUSTBEDIR) {
1331 + *dtype = resolve_dtype(*dtype, istate, pathname, pathlen);
1332 + if (*dtype != DT_DIR)
1333 + continue;
1334 + }
1335
1336 if (pattern->flags & PATTERN_FLAG_NODIR) {
1337 if (match_basename(basename,
@@ -1393,12 +1355,6 @@ static struct path_pattern *last_matching_pattern_from_list(const char *pathname
1355 break;
1356 }
1357 }
1396 -
1397 - if (path_parent) {
1398 - strbuf_release(path_parent);
1399 - free(path_parent);
1400 - }
1401 -
1358 return res;
1359 }
1360
t/t0008-ignores.sh
+17
@@ -828,6 +828,23 @@ test_expect_success 'exact prefix matching (without root)' '
828 test_cmp expect actual
829 '
830
831 +test_expect_success 'directories and ** matches' '
832 + cat >.gitignore <<-\EOF &&
833 + data/**
834 + !data/**/
835 + !data/**/*.txt
836 + EOF
837 + git check-ignore file \
838 + data/file data/data1/file1 data/data1/file1.txt \
839 + data/data2/file2 data/data2/file2.txt >actual &&
840 + cat >expect <<-\EOF &&
841 + data/file
842 + data/data1/file1
843 + data/data2/file2
844 + EOF
845 + test_cmp expect actual
846 +'
847 +
848 ############################################################################
849 #
850 # test whitespace handling