untracked-cache: fix subdirectory handling

Previously, some calls lookup_untracked would pass a full path. But lookup_untracked assumes that the portion of the path up to and including to the untracked_cache_dir has been removed. So lookup_untracked would be looking in the untracked_cache for 'foo' for 'foo/bar' (instead of just looking for 'bar'). This would cause untracked cache corruption. Instead, treat_directory learns to track the base length of the parent directory, so that only the last path component is passed to lookup_untracked. Helped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: David Turner <dturner@twopensource.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Turner committed Aug 19, 2015 at 20:01 UTC 2e5910f27695b1b24e8a6870940bfac2a03ef0d7
2 files changed +79 -7
dir.c
+8 -6
@@ -1297,7 +1297,7 @@ static enum exist_status directory_exists_in_index(const char *dirname, int len)
1297 */
1298 static enum path_treatment treat_directory(struct dir_struct *dir,
1299 struct untracked_cache_dir *untracked,
1300 - const char *dirname, int len, int exclude,
1300 + const char *dirname, int len, int baselen, int exclude,
1301 const struct path_simplify *simplify)
1302 {
1303 /* The "len-1" is to strip the final '/' */
@@ -1324,7 +1324,8 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
1324 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
1325 return exclude ? path_excluded : path_untracked;
1326
1327 - untracked = lookup_untracked(dir->untracked, untracked, dirname, len);
1327 + untracked = lookup_untracked(dir->untracked, untracked,
1328 + dirname + baselen, len - baselen);
1329 return read_directory_recursive(dir, dirname, len,
1330 untracked, 1, simplify);
1331 }
@@ -1444,6 +1445,7 @@ static int get_dtype(struct dirent *de, const char *path, int len)
1445 static enum path_treatment treat_one_path(struct dir_struct *dir,
1446 struct untracked_cache_dir *untracked,
1447 struct strbuf *path,
1448 + int baselen,
1449 const struct path_simplify *simplify,
1450 int dtype, struct dirent *de)
1451 {
@@ -1495,8 +1497,8 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,
1497 return path_none;
1498 case DT_DIR:
1499 strbuf_addch(path, '/');
1498 - return treat_directory(dir, untracked, path->buf, path->len, exclude,
1499 - simplify);
1500 + return treat_directory(dir, untracked, path->buf, path->len,
1501 + baselen, exclude, simplify);
1502 case DT_REG:
1503 case DT_LNK:
1504 return exclude ? path_excluded : path_untracked;
@@ -1557,7 +1559,7 @@ static enum path_treatment treat_path(struct dir_struct *dir,
1559 return path_none;
1560
1561 dtype = DTYPE(de);
1560 - return treat_one_path(dir, untracked, path, simplify, dtype, de);
1562 + return treat_one_path(dir, untracked, path, baselen, simplify, dtype, de);
1563 }
1564
1565 static void add_untracked(struct untracked_cache_dir *dir, const char *name)
@@ -1827,7 +1829,7 @@ static int treat_leading_path(struct dir_struct *dir,
1829 break;
1830 if (simplify_away(sb.buf, sb.len, simplify))
1831 break;
1830 - if (treat_one_path(dir, NULL, &sb, simplify,
1832 + if (treat_one_path(dir, NULL, &sb, baselen, simplify,
1833 DT_DIR, NULL) == path_none)
1834 break; /* do not recurse into it */
1835 if (len <= baselen) {
t/t7063-status-untracked-cache.sh
+71 -1
@@ -408,7 +408,8 @@ test_expect_success 'set up sparse checkout' '
408 test_path_is_file done/one
409 '
410
411 -test_expect_success 'create files, some of which are gitignored' '
411 +test_expect_success 'create/modify files, some of which are gitignored' '
412 + echo two bis >done/two &&
413 echo three >done/three && # three is gitignored
414 echo four >done/four && # four is gitignored at a higher level
415 echo five >done/five # five is not gitignored
@@ -420,6 +421,7 @@ test_expect_success 'test sparse status with untracked cache' '
421 GIT_TRACE_UNTRACKED_STATS="$TRASH_DIRECTORY/trace" \
422 git status --porcelain >../status.actual &&
423 cat >../status.expect <<EOF &&
424 + M done/two
425 ?? .gitignore
426 ?? done/five
427 ?? dtwo/
@@ -459,6 +461,7 @@ test_expect_success 'test sparse status again with untracked cache' '
461 GIT_TRACE_UNTRACKED_STATS="$TRASH_DIRECTORY/trace" \
462 git status --porcelain >../status.actual &&
463 cat >../status.expect <<EOF &&
464 + M done/two
465 ?? .gitignore
466 ?? done/five
467 ?? dtwo/
@@ -473,4 +476,71 @@ EOF
476 test_cmp ../trace.expect ../trace
477 '
478
479 +test_expect_success 'set up for test of subdir and sparse checkouts' '
480 + mkdir done/sub &&
481 + mkdir done/sub/sub &&
482 + echo "sub" > done/sub/sub/file
483 +'
484 +
485 +test_expect_success 'test sparse status with untracked cache and subdir' '
486 + avoid_racy &&
487 + : >../trace &&
488 + GIT_TRACE_UNTRACKED_STATS="$TRASH_DIRECTORY/trace" \
489 + git status --porcelain >../status.actual &&
490 + cat >../status.expect <<EOF &&
491 + M done/two
492 +?? .gitignore
493 +?? done/five
494 +?? done/sub/
495 +?? dtwo/
496 +EOF
497 + test_cmp ../status.expect ../status.actual &&
498 + cat >../trace.expect <<EOF &&
499 +node creation: 2
500 +gitignore invalidation: 0
501 +directory invalidation: 1
502 +opendir: 3
503 +EOF
504 + test_cmp ../trace.expect ../trace
505 +'
506 +
507 +test_expect_success 'verify untracked cache dump (sparse/subdirs)' '
508 + test-dump-untracked-cache >../actual &&
509 + cat >../expect <<EOF &&
510 +info/exclude 13263c0978fb9fad16b2d580fb800b6d811c3ff0
511 +core.excludesfile 0000000000000000000000000000000000000000
512 +exclude_per_dir .gitignore
513 +flags 00000006
514 +/ e6fcc8f2ee31bae321d66afd183fcb7237afae6e recurse valid
515 +.gitignore
516 +dtwo/
517 +/done/ 1946f0437f90c5005533cbe1736a6451ca301714 recurse valid
518 +five
519 +sub/
520 +/done/sub/ 0000000000000000000000000000000000000000 recurse check_only valid
521 +sub/
522 +/done/sub/sub/ 0000000000000000000000000000000000000000 recurse check_only valid
523 +file
524 +/dthree/ 0000000000000000000000000000000000000000 recurse check_only valid
525 +/dtwo/ 0000000000000000000000000000000000000000 recurse check_only valid
526 +two
527 +EOF
528 + test_cmp ../expect ../actual
529 +'
530 +
531 +test_expect_success 'test sparse status again with untracked cache and subdir' '
532 + avoid_racy &&
533 + : >../trace &&
534 + GIT_TRACE_UNTRACKED_STATS="$TRASH_DIRECTORY/trace" \
535 + git status --porcelain >../status.actual &&
536 + test_cmp ../status.expect ../status.actual &&
537 + cat >../trace.expect <<EOF &&
538 +node creation: 0
539 +gitignore invalidation: 0
540 +directory invalidation: 0
541 +opendir: 0
542 +EOF
543 + test_cmp ../trace.expect ../trace
544 +'
545 +
546 test_done