Skip to content

Commit c89fa34

Browse files
committed
Merge branch 'atetubou/fscache_checkout_flush'
Signed-off-by: Johannes Schindelin <[email protected]>
2 parents efa1996 + 30c14ae commit c89fa34

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

369369
enable_delayed_checkout(&state);
370+
enable_fscache(1);
370371
for (pos = 0; pos < active_nr; pos++) {
371372
struct cache_entry *ce = active_cache[pos];
372373
if (ce->ce_flags & CE_MATCHED) {
@@ -381,6 +382,7 @@ static int checkout_paths(const struct checkout_opts *opts,
381382
pos = skip_same_name(ce, pos) - 1;
382383
}
383384
}
385+
enable_fscache(0);
384386
errs |= finish_delayed_checkout(&state);
385387

386388
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))

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
@@ -824,12 +824,55 @@ static int add_excludes(const char *fname, const char *base, int baselen,
824824
size_t size = 0;
825825
char *buf;
826826

827-
fd = open(fname, O_RDONLY);
828-
if (fd < 0 || fstat(fd, &st) < 0) {
829-
if (fd < 0)
830-
warn_on_fopen_errors(fname);
831-
else
832-
close(fd);
827+
/*
828+
* A performance optimization for status.
829+
*
830+
* During a status scan, git looks in each directory for a .gitignore
831+
* file before scanning the directory. Since .gitignore files are not
832+
* that common, we can waste a lot of time looking for files that are
833+
* not there. Fortunately, the fscache already knows if the directory
834+
* contains a .gitignore file, since it has already read the directory
835+
* and it already has the stat-data.
836+
*
837+
* If the fscache is enabled, use the fscache-lstat() interlude to see
838+
* if the file exists (in the fscache hash maps) before trying to open()
839+
* it.
840+
*
841+
* This causes problem when the .gitignore file is a symlink, because
842+
* we call lstat() rather than stat() on the symlnk and the resulting
843+
* stat-data is for the symlink itself rather than the target file.
844+
* We CANNOT use stat() here because the fscache DOES NOT install an
845+
* interlude for stat() and mingw_stat() always calls "open-fstat-close"
846+
* on the file and defeats the purpose of the optimization here. Since
847+
* symlinks are even more rare than .gitignore files, we force a fstat()
848+
* after our open() to get stat-data for the target file.
849+
*/
850+
if (is_fscache_enabled(fname)) {
851+
if (lstat(fname, &st) < 0) {
852+
fd = -1;
853+
} else {
854+
fd = open(fname, O_RDONLY);
855+
if (fd < 0)
856+
warn_on_fopen_errors(fname);
857+
else if (S_ISLNK(st.st_mode) && fstat(fd, &st) < 0) {
858+
warn_on_fopen_errors(fname);
859+
close(fd);
860+
fd = -1;
861+
}
862+
}
863+
} else {
864+
fd = open(fname, O_RDONLY);
865+
if (fd < 0 || fstat(fd, &st) < 0) {
866+
if (fd < 0)
867+
warn_on_fopen_errors(fname);
868+
else {
869+
close(fd);
870+
fd = -1;
871+
}
872+
}
873+
}
874+
875+
if (fd < 0) {
833876
if (!istate)
834877
return -1;
835878
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
@@ -680,6 +680,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
680680

681681
save_commit_buffer = 0;
682682

683+
enable_fscache(1);
683684
for (ref = *refs; ref; ref = ref->next) {
684685
struct object *o;
685686
unsigned int flags = OBJECT_INFO_QUICK;
@@ -709,6 +710,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
709710
cutoff = commit->date;
710711
}
711712
}
713+
enable_fscache(0);
712714

713715
oidset_clear(&loose_oid_set);
714716

git-compat-util.h

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

1264+
#ifndef is_fscache_enabled
1265+
#define is_fscache_enabled(path) (0)
1266+
#endif
1267+
1268+
#ifndef flush_fscache
1269+
#define flush_fscache() /* noop */
1270+
#endif
1271+
12641272
extern int cmd_main(int, const char **);
12651273

12661274
/*

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)