summaryrefslogtreecommitdiff
path: root/read-cache.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2023-09-14 21:46:46 (GMT)
committerJunio C Hamano <gitster@pobox.com>2023-09-16 00:08:46 (GMT)
commitc33fa871a5c89091cfc89fd7b6ef504d2d48bef2 (patch)
treed8eefcf40275cd2453b3ec5a34685f28627dd095 /read-cache.c
parent43c8a30d150ecede9709c1f2527c8fba92c65f40 (diff)
downloadgit-c33fa871a5c89091cfc89fd7b6ef504d2d48bef2.zip
git-c33fa871a5c89091cfc89fd7b6ef504d2d48bef2.tar.gz
git-c33fa871a5c89091cfc89fd7b6ef504d2d48bef2.tar.bz2
cache: add fake_lstat()
At times, we may already know that a path represented by a cache_entry ce has no changes via some out-of-line means, like fsmonitor, and yet need the control to go through a codepath that requires us to have "struct stat" obtained by lstat() on the path, for various purposes (e.g. "ie_match_stat()" wants cached stat-info is still current wrt "struct stat", "diff" wants to know st_mode). The callers of lstat() on a tracked file, when its cache_entry knows it is up-to-date, can instead call this helper to pretend that it called lstat() by faking the "struct stat" information. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'read-cache.c')
-rw-r--r--read-cache.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/read-cache.c b/read-cache.c
index 080bd39..eb750e2 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -197,6 +197,33 @@ void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, st
}
}
+static unsigned int st_mode_from_ce(const struct cache_entry *ce)
+{
+ extern int trust_executable_bit, has_symlinks;
+
+ switch (ce->ce_mode & S_IFMT) {
+ case S_IFLNK:
+ return has_symlinks ? S_IFLNK : (S_IFREG | 0644);
+ case S_IFREG:
+ return (ce->ce_mode & (trust_executable_bit ? 0755 : 0644)) | S_IFREG;
+ case S_IFGITLINK:
+ return S_IFDIR | 0755;
+ case S_IFDIR:
+ return ce->ce_mode;
+ default:
+ BUG("unsupported ce_mode: %o", ce->ce_mode);
+ }
+}
+
+int fake_lstat(const struct cache_entry *ce, struct stat *st)
+{
+ fake_lstat_data(&ce->ce_stat_data, st);
+ st->st_mode = st_mode_from_ce(ce);
+
+ /* always succeed as lstat() replacement */
+ return 0;
+}
+
static int ce_compare_data(struct index_state *istate,
const struct cache_entry *ce,
struct stat *st)