Skip to content

Commit eb89546

Browse files
committed
Merge branch 'atetubou/fscache_checkout_flush'
Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 64ea2dd + 54c44c2 commit eb89546

File tree

8 files changed

+119
-7
lines changed

8 files changed

+119
-7
lines changed

builtin/checkout.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ static int checkout_worktree(const struct checkout_opts *opts)
346346
state.istate = &the_index;
347347

348348
enable_delayed_checkout(&state);
349+
enable_fscache(1);
349350
for (pos = 0; pos < active_nr; pos++) {
350351
struct cache_entry *ce = active_cache[pos];
351352
if (ce->ce_flags & CE_MATCHED) {
@@ -365,6 +366,7 @@ static int checkout_worktree(const struct checkout_opts *opts)
365366
pos = skip_same_name(ce, pos) - 1;
366367
}
367368
}
369+
enable_fscache(0);
368370
remove_marked_cache_entries(&the_index, 1);
369371
remove_scheduled_dirs();
370372
errs |= finish_delayed_checkout(&state, &nr_checkouts);

compat/win32/fscache.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ static void fscache_clear(void)
259259
/*
260260
* Checks if the cache is enabled for the given path.
261261
*/
262-
static inline int fscache_enabled(const char *path)
262+
int fscache_enabled(const char *path)
263263
{
264264
return enabled > 0 && !is_absolute_path(path);
265265
}
@@ -430,6 +430,18 @@ int fscache_enable(int enable)
430430
return result;
431431
}
432432

433+
/*
434+
* Flush cached stats result when fscache is enabled.
435+
*/
436+
void fscache_flush(void)
437+
{
438+
if (enabled) {
439+
EnterCriticalSection(&mutex);
440+
fscache_clear();
441+
LeaveCriticalSection(&mutex);
442+
}
443+
}
444+
433445
/*
434446
* Lstat replacement, uses the cache if enabled, otherwise redirects to
435447
* mingw_lstat.

compat/win32/fscache.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
int fscache_enable(int enable);
55
#define enable_fscache(x) fscache_enable(x)
66

7+
int fscache_enabled(const char *path);
8+
#define is_fscache_enabled(path) fscache_enabled(path)
9+
10+
void fscache_flush(void);
11+
#define flush_fscache() fscache_flush()
12+
713
DIR *fscache_opendir(const char *dir);
814
int fscache_lstat(const char *file_name, struct stat *buf);
915

dir.c

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -785,12 +785,55 @@ static int add_excludes(const char *fname, const char *base, int baselen,
785785
size_t size = 0;
786786
char *buf;
787787

788-
fd = open(fname, O_RDONLY);
789-
if (fd < 0 || fstat(fd, &st) < 0) {
790-
if (fd < 0)
791-
warn_on_fopen_errors(fname);
792-
else
793-
close(fd);
788+
/*
789+
* A performance optimization for status.
790+
*
791+
* During a status scan, git looks in each directory for a .gitignore
792+
* file before scanning the directory. Since .gitignore files are not
793+
* that common, we can waste a lot of time looking for files that are
794+
* not there. Fortunately, the fscache already knows if the directory
795+
* contains a .gitignore file, since it has already read the directory
796+
* and it already has the stat-data.
797+
*
798+
* If the fscache is enabled, use the fscache-lstat() interlude to see
799+
* if the file exists (in the fscache hash maps) before trying to open()
800+
* it.
801+
*
802+
* This causes problem when the .gitignore file is a symlink, because
803+
* we call lstat() rather than stat() on the symlnk and the resulting
804+
* stat-data is for the symlink itself rather than the target file.
805+
* We CANNOT use stat() here because the fscache DOES NOT install an
806+
* interlude for stat() and mingw_stat() always calls "open-fstat-close"
807+
* on the file and defeats the purpose of the optimization here. Since
808+
* symlinks are even more rare than .gitignore files, we force a fstat()
809+
* after our open() to get stat-data for the target file.
810+
*/
811+
if (is_fscache_enabled(fname)) {
812+
if (lstat(fname, &st) < 0) {
813+
fd = -1;
814+
} else {
815+
fd = open(fname, O_RDONLY);
816+
if (fd < 0)
817+
warn_on_fopen_errors(fname);
818+
else if (S_ISLNK(st.st_mode) && fstat(fd, &st) < 0) {
819+
warn_on_fopen_errors(fname);
820+
close(fd);
821+
fd = -1;
822+
}
823+
}
824+
} else {
825+
fd = open(fname, O_RDONLY);
826+
if (fd < 0 || fstat(fd, &st) < 0) {
827+
if (fd < 0)
828+
warn_on_fopen_errors(fname);
829+
else {
830+
close(fd);
831+
fd = -1;
832+
}
833+
}
834+
}
835+
836+
if (fd < 0) {
794837
if (!istate)
795838
return -1;
796839
r = read_skip_worktree_file_from_index(istate, fname,

entry.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ static int write_entry(struct cache_entry *ce,
367367
}
368368

369369
finish:
370+
/* Flush cached lstat in fscache after writing to disk. */
371+
flush_fscache();
372+
370373
if (state->refresh_cache) {
371374
assert(state->istate);
372375
if (!fstat_done)

fetch-pack.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
672672

673673
save_commit_buffer = 0;
674674

675+
enable_fscache(1);
675676
for (ref = *refs; ref; ref = ref->next) {
676677
struct object *o;
677678

@@ -692,6 +693,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
692693
cutoff = commit->date;
693694
}
694695
}
696+
enable_fscache(0);
695697

696698
if (!args->deepen) {
697699
for_each_ref(mark_complete_oid, NULL);

git-compat-util.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,14 @@ static inline int is_missing_file_error(int errno_)
12981298
#define enable_fscache(x) /* noop */
12991299
#endif
13001300

1301+
#ifndef is_fscache_enabled
1302+
#define is_fscache_enabled(path) (0)
1303+
#endif
1304+
1305+
#ifndef flush_fscache
1306+
#define flush_fscache() /* noop */
1307+
#endif
1308+
13011309
int cmd_main(int, const char **);
13021310

13031311
/*

t/t7201-co.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,42 @@ fill () {
3232
}
3333

3434

35+
test_expect_success MINGW 'fscache flush cache' '
36+
37+
git init fscache-test &&
38+
cd fscache-test &&
39+
git config core.fscache 1 &&
40+
echo A > test.txt &&
41+
git add test.txt &&
42+
git commit -m A &&
43+
echo B >> test.txt &&
44+
git checkout . &&
45+
test -z "$(git status -s)" &&
46+
echo A > expect.txt &&
47+
test_cmp expect.txt test.txt &&
48+
cd .. &&
49+
rm -rf fscache-test
50+
'
51+
52+
test_expect_success MINGW 'fscache flush cache dir' '
53+
54+
git init fscache-test &&
55+
cd fscache-test &&
56+
git config core.fscache 1 &&
57+
echo A > test.txt &&
58+
git add test.txt &&
59+
git commit -m A &&
60+
rm test.txt &&
61+
mkdir test.txt &&
62+
touch test.txt/test.txt &&
63+
git checkout . &&
64+
test -z "$(git status -s)" &&
65+
echo A > expect.txt &&
66+
test_cmp expect.txt test.txt &&
67+
cd .. &&
68+
rm -rf fscache-test
69+
'
70+
3571
test_expect_success setup '
3672
3773
fill x y z > same &&

0 commit comments

Comments
 (0)