Skip to content

Commit 2e5910f

Browse files
dturner-twgitster
authored andcommitted
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 <[email protected]> Signed-off-by: David Turner <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f178136 commit 2e5910f

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

dir.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ static enum exist_status directory_exists_in_index(const char *dirname, int len)
12971297
*/
12981298
static enum path_treatment treat_directory(struct dir_struct *dir,
12991299
struct untracked_cache_dir *untracked,
1300-
const char *dirname, int len, int exclude,
1300+
const char *dirname, int len, int baselen, int exclude,
13011301
const struct path_simplify *simplify)
13021302
{
13031303
/* The "len-1" is to strip the final '/' */
@@ -1324,7 +1324,8 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
13241324
if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
13251325
return exclude ? path_excluded : path_untracked;
13261326

1327-
untracked = lookup_untracked(dir->untracked, untracked, dirname, len);
1327+
untracked = lookup_untracked(dir->untracked, untracked,
1328+
dirname + baselen, len - baselen);
13281329
return read_directory_recursive(dir, dirname, len,
13291330
untracked, 1, simplify);
13301331
}
@@ -1444,6 +1445,7 @@ static int get_dtype(struct dirent *de, const char *path, int len)
14441445
static enum path_treatment treat_one_path(struct dir_struct *dir,
14451446
struct untracked_cache_dir *untracked,
14461447
struct strbuf *path,
1448+
int baselen,
14471449
const struct path_simplify *simplify,
14481450
int dtype, struct dirent *de)
14491451
{
@@ -1495,8 +1497,8 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,
14951497
return path_none;
14961498
case DT_DIR:
14971499
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);
15001502
case DT_REG:
15011503
case DT_LNK:
15021504
return exclude ? path_excluded : path_untracked;
@@ -1557,7 +1559,7 @@ static enum path_treatment treat_path(struct dir_struct *dir,
15571559
return path_none;
15581560

15591561
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);
15611563
}
15621564

15631565
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,
18271829
break;
18281830
if (simplify_away(sb.buf, sb.len, simplify))
18291831
break;
1830-
if (treat_one_path(dir, NULL, &sb, simplify,
1832+
if (treat_one_path(dir, NULL, &sb, baselen, simplify,
18311833
DT_DIR, NULL) == path_none)
18321834
break; /* do not recurse into it */
18331835
if (len <= baselen) {

t/t7063-status-untracked-cache.sh

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ test_expect_success 'set up sparse checkout' '
408408
test_path_is_file done/one
409409
'
410410

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 &&
412413
echo three >done/three && # three is gitignored
413414
echo four >done/four && # four is gitignored at a higher level
414415
echo five >done/five # five is not gitignored
@@ -420,6 +421,7 @@ test_expect_success 'test sparse status with untracked cache' '
420421
GIT_TRACE_UNTRACKED_STATS="$TRASH_DIRECTORY/trace" \
421422
git status --porcelain >../status.actual &&
422423
cat >../status.expect <<EOF &&
424+
M done/two
423425
?? .gitignore
424426
?? done/five
425427
?? dtwo/
@@ -459,6 +461,7 @@ test_expect_success 'test sparse status again with untracked cache' '
459461
GIT_TRACE_UNTRACKED_STATS="$TRASH_DIRECTORY/trace" \
460462
git status --porcelain >../status.actual &&
461463
cat >../status.expect <<EOF &&
464+
M done/two
462465
?? .gitignore
463466
?? done/five
464467
?? dtwo/
@@ -473,4 +476,71 @@ EOF
473476
test_cmp ../trace.expect ../trace
474477
'
475478

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+
476546
test_done

0 commit comments

Comments
 (0)