Skip to content

Commit 8d28316

Browse files
committed
Merge branch 'atetubou/fscache_checkout_flush'
Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 697b3e0 + 8116278 commit 8d28316

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
@@ -372,6 +372,7 @@ static int checkout_paths(const struct checkout_opts *opts,
372372
state.istate = &the_index;
373373

374374
enable_delayed_checkout(&state);
375+
enable_fscache(1);
375376
for (pos = 0; pos < active_nr; pos++) {
376377
struct cache_entry *ce = active_cache[pos];
377378
if (ce->ce_flags & CE_MATCHED) {
@@ -390,6 +391,7 @@ static int checkout_paths(const struct checkout_opts *opts,
390391
pos = skip_same_name(ce, pos) - 1;
391392
}
392393
}
394+
enable_fscache(0);
393395
errs |= finish_delayed_checkout(&state, &nr_checkouts);
394396

395397
if (opts->count_checkout_paths) {

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
@@ -786,12 +786,55 @@ static int add_excludes(const char *fname, const char *base, int baselen,
786786
size_t size = 0;
787787
char *buf;
788788

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

670670
save_commit_buffer = 0;
671671

672+
enable_fscache(1);
672673
for (ref = *refs; ref; ref = ref->next) {
673674
struct object *o;
674675

@@ -689,6 +690,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
689690
cutoff = commit->date;
690691
}
691692
}
693+
enable_fscache(0);
692694

693695
if (!args->deepen) {
694696
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
@@ -1288,6 +1288,14 @@ static inline int is_missing_file_error(int errno_)
12881288
#define enable_fscache(x) /* noop */
12891289
#endif
12901290

1291+
#ifndef is_fscache_enabled
1292+
#define is_fscache_enabled(path) (0)
1293+
#endif
1294+
1295+
#ifndef flush_fscache
1296+
#define flush_fscache() /* noop */
1297+
#endif
1298+
12911299
extern int cmd_main(int, const char **);
12921300

12931301
/*

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)