Skip to content

Commit 038226e

Browse files
committed
Merge branch 'dt/untracked-subdir'
The experimental untracked-cache feature were buggy when paths with a few levels of subdirectories are involved. * dt/untracked-subdir: untracked cache: fix entry invalidation untracked-cache: fix subdirectory handling
2 parents 3a9835b + 73f9145 commit 038226e

2 files changed

Lines changed: 162 additions & 20 deletions

File tree

dir.c

Lines changed: 64 additions & 18 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) {
@@ -2616,23 +2618,67 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
26162618
return uc;
26172619
}
26182620

2621+
static void invalidate_one_directory(struct untracked_cache *uc,
2622+
struct untracked_cache_dir *ucd)
2623+
{
2624+
uc->dir_invalidated++;
2625+
ucd->valid = 0;
2626+
ucd->untracked_nr = 0;
2627+
}
2628+
2629+
/*
2630+
* Normally when an entry is added or removed from a directory,
2631+
* invalidating that directory is enough. No need to touch its
2632+
* ancestors. When a directory is shown as "foo/bar/" in git-status
2633+
* however, deleting or adding an entry may have cascading effect.
2634+
*
2635+
* Say the "foo/bar/file" has become untracked, we need to tell the
2636+
* untracked_cache_dir of "foo" that "bar/" is not an untracked
2637+
* directory any more (because "bar" is managed by foo as an untracked
2638+
* "file").
2639+
*
2640+
* Similarly, if "foo/bar/file" moves from untracked to tracked and it
2641+
* was the last untracked entry in the entire "foo", we should show
2642+
* "foo/" instead. Which means we have to invalidate past "bar" up to
2643+
* "foo".
2644+
*
2645+
* This function traverses all directories from root to leaf. If there
2646+
* is a chance of one of the above cases happening, we invalidate back
2647+
* to root. Otherwise we just invalidate the leaf. There may be a more
2648+
* sophisticated way than checking for SHOW_OTHER_DIRECTORIES to
2649+
* detect these cases and avoid unnecessary invalidation, for example,
2650+
* checking for the untracked entry named "bar/" in "foo", but for now
2651+
* stick to something safe and simple.
2652+
*/
2653+
static int invalidate_one_component(struct untracked_cache *uc,
2654+
struct untracked_cache_dir *dir,
2655+
const char *path, int len)
2656+
{
2657+
const char *rest = strchr(path, '/');
2658+
2659+
if (rest) {
2660+
int component_len = rest - path;
2661+
struct untracked_cache_dir *d =
2662+
lookup_untracked(uc, dir, path, component_len);
2663+
int ret =
2664+
invalidate_one_component(uc, d, rest + 1,
2665+
len - (component_len + 1));
2666+
if (ret)
2667+
invalidate_one_directory(uc, dir);
2668+
return ret;
2669+
}
2670+
2671+
invalidate_one_directory(uc, dir);
2672+
return uc->dir_flags & DIR_SHOW_OTHER_DIRECTORIES;
2673+
}
2674+
26192675
void untracked_cache_invalidate_path(struct index_state *istate,
26202676
const char *path)
26212677
{
2622-
const char *sep;
2623-
struct untracked_cache_dir *d;
26242678
if (!istate->untracked || !istate->untracked->root)
26252679
return;
2626-
sep = strrchr(path, '/');
2627-
if (sep)
2628-
d = lookup_untracked(istate->untracked,
2629-
istate->untracked->root,
2630-
path, sep - path);
2631-
else
2632-
d = istate->untracked->root;
2633-
istate->untracked->dir_invalidated++;
2634-
d->valid = 0;
2635-
d->untracked_nr = 0;
2680+
invalidate_one_component(istate->untracked, istate->untracked->root,
2681+
path, strlen(path));
26362682
}
26372683

26382684
void untracked_cache_remove_from_index(struct index_state *istate,

t/t7063-status-untracked-cache.sh

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ EOF
375375
node creation: 0
376376
gitignore invalidation: 0
377377
directory invalidation: 0
378-
opendir: 1
378+
opendir: 2
379379
EOF
380380
test_cmp ../trace.expect ../trace
381381
'
@@ -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,12 +461,80 @@ 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
465+
?? .gitignore
466+
?? done/five
467+
?? dtwo/
468+
EOF
469+
test_cmp ../status.expect ../status.actual &&
470+
cat >../trace.expect <<EOF &&
471+
node creation: 0
472+
gitignore invalidation: 0
473+
directory invalidation: 0
474+
opendir: 0
475+
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
462492
?? .gitignore
463493
?? done/five
494+
?? done/sub/
464495
?? dtwo/
465496
EOF
466497
test_cmp ../status.expect ../status.actual &&
467498
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 &&
468538
node creation: 0
469539
gitignore invalidation: 0
470540
directory invalidation: 0
@@ -473,4 +543,30 @@ EOF
473543
test_cmp ../trace.expect ../trace
474544
'
475545

546+
test_expect_success 'move entry in subdir from untracked to cached' '
547+
git add dtwo/two &&
548+
git status --porcelain >../status.actual &&
549+
cat >../status.expect <<EOF &&
550+
M done/two
551+
A dtwo/two
552+
?? .gitignore
553+
?? done/five
554+
?? done/sub/
555+
EOF
556+
test_cmp ../status.expect ../status.actual
557+
'
558+
559+
test_expect_success 'move entry in subdir from cached to untracked' '
560+
git rm --cached dtwo/two &&
561+
git status --porcelain >../status.actual &&
562+
cat >../status.expect <<EOF &&
563+
M done/two
564+
?? .gitignore
565+
?? done/five
566+
?? done/sub/
567+
?? dtwo/
568+
EOF
569+
test_cmp ../status.expect ../status.actual
570+
'
571+
476572
test_done

0 commit comments

Comments
 (0)