]> git.ipfire.org Git - thirdparty/git.git/commitdiff
read-cache: pass 'istate' to stat/mode helper functions
authorTian Yuchen <cat@malon.dev>
Sat, 30 May 2026 16:05:19 +0000 (00:05 +0800)
committerJunio C Hamano <gitster@pobox.com>
Sat, 30 May 2026 22:53:39 +0000 (07:53 +0900)
In the previous commit, the gloabl 'trust_executable_bit' was
migrated into 'repo_config_values', but low-level helpers in
read-cache.c still relied on 'the_repository' to access it.

Refactor the signatures of ce_mode_from_stat(), st_mode_from_ce(),
fake_lstat(), and check_removed() to accept a 'struct
index_state *istate'. This allows these functions to retrieve the
repository context via 'istate->repo'.

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Ayush Chandekar <ayu.chandekar@gmail.com>
Mentored-by: Olamide Caleb Bello <belkid98@gmail.com>
Signed-off-by: Tian Yuchen <cat@malon.dev>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply.c
builtin/update-index.c
diff-lib.c
read-cache-ll.h
read-cache.c
read-cache.h

diff --git a/apply.c b/apply.c
index 73ca9907f8c87ad13182d7780936933d6335161f..a81bb29a6f7fc4b5c76a703d15b57435c86444a3 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -3890,13 +3890,13 @@ static int check_preimage(struct apply_state *state,
        }
 
        if (!state->cached && !previous) {
-               struct repo_config_values *cfg = repo_config_values(the_repository);
+               struct repo_config_values *cfg = repo_config_values(state->repo);
 
                if (*ce && !(*ce)->ce_mode)
                        BUG("ce_mode == 0 for path '%s'", old_name);
 
                if (cfg->trust_executable_bit || !S_ISREG(st->st_mode))
-                       st_mode = ce_mode_from_stat(*ce, st->st_mode);
+                       st_mode = ce_mode_from_stat(state->repo->index, *ce, st->st_mode);
                else if (*ce)
                        st_mode = (*ce)->ce_mode;
                else
index 3d6646c318b98e806a0aba09c3f034b133dc582c..3af1641fbce30c42e6579a9cdb902e9f49cf4fbd 100644 (file)
@@ -294,7 +294,7 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
        ce->ce_flags = create_ce_flags(0);
        ce->ce_namelen = len;
        fill_stat_cache_info(the_repository->index, ce, st);
-       ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
+       ce->ce_mode = ce_mode_from_stat(the_repository->index, old, st->st_mode);
 
        if (index_path(the_repository->index, &ce->oid, path, st,
                       info_only ? 0 : INDEX_WRITE_OBJECT)) {
index ae91027a024eece23f0f1ede20467eeb425a2b3b..95fd3ba4b96ff560e00d8fff094bb715b70830c0 100644 (file)
  * exists for ce that is a submodule -- it is a submodule that is not
  * checked out).  Return negative for an error.
  */
-static int check_removed(const struct cache_entry *ce, struct stat *st)
+static int check_removed(struct index_state *istate, const struct cache_entry *ce, struct stat *st)
 {
        int stat_err;
 
        if (!(ce->ce_flags & CE_FSMONITOR_VALID))
                stat_err = lstat(ce->name, st);
        else
-               stat_err = fake_lstat(ce, st);
+               stat_err = fake_lstat(istate, ce, st);
        if (stat_err < 0) {
                if (!is_missing_file_error(errno))
                        return -1;
@@ -158,9 +158,9 @@ void run_diff_files(struct rev_info *revs, unsigned int option)
                        int num_compare_stages = 0;
                        struct stat st;
 
-                       changed = check_removed(ce, &st);
+                       changed = check_removed(revs->repo->index, ce, &st);
                        if (!changed)
-                               wt_mode = ce_mode_from_stat(ce, st.st_mode);
+                               wt_mode = ce_mode_from_stat(revs->repo->index, ce, st.st_mode);
                        else {
                                if (changed < 0) {
                                        perror(ce->name);
@@ -193,7 +193,7 @@ void run_diff_files(struct rev_info *revs, unsigned int option)
                                        num_compare_stages++;
                                        oidcpy(&dpath->parent[stage - 2].oid,
                                               &nce->oid);
-                                       dpath->parent[stage-2].mode = ce_mode_from_stat(nce, mode);
+                                       dpath->parent[stage-2].mode = ce_mode_from_stat(revs->repo->index, nce, mode);
                                        dpath->parent[stage-2].status =
                                                DIFF_STATUS_MODIFIED;
                                }
@@ -249,7 +249,7 @@ void run_diff_files(struct rev_info *revs, unsigned int option)
                } else {
                        struct stat st;
 
-                       changed = check_removed(ce, &st);
+                       changed = check_removed(revs->repo->index, ce, &st);
                        if (changed) {
                                if (changed < 0) {
                                        perror(ce->name);
@@ -262,7 +262,7 @@ void run_diff_files(struct rev_info *revs, unsigned int option)
                                continue;
                        } else if (revs->diffopt.ita_invisible_in_index &&
                                   ce_intent_to_add(ce)) {
-                               newmode = ce_mode_from_stat(ce, st.st_mode);
+                               newmode = ce_mode_from_stat(revs->repo->index, ce, st.st_mode);
                                diff_addremove(&revs->diffopt, '+', newmode,
                                               null_oid(the_hash_algo), 0, ce->name, 0);
                                continue;
@@ -270,7 +270,7 @@ void run_diff_files(struct rev_info *revs, unsigned int option)
 
                        changed = match_stat_with_submodule(&revs->diffopt, ce, &st,
                                                            ce_option, &dirty_submodule);
-                       newmode = ce_mode_from_stat(ce, st.st_mode);
+                       newmode = ce_mode_from_stat(revs->repo->index, ce, st.st_mode);
                }
 
                if (!changed && !dirty_submodule) {
@@ -324,7 +324,7 @@ static int get_stat_data(const struct cache_entry *ce,
        if (!cached && !ce_uptodate(ce)) {
                int changed;
                struct stat st;
-               changed = check_removed(ce, &st);
+               changed = check_removed(diffopt->repo->index, ce, &st);
                if (changed < 0)
                        return -1;
                else if (changed) {
@@ -338,7 +338,7 @@ static int get_stat_data(const struct cache_entry *ce,
                changed = match_stat_with_submodule(diffopt, ce, &st,
                                                    0, dirty_submodule);
                if (changed) {
-                       mode = ce_mode_from_stat(ce, st.st_mode);
+                       mode = ce_mode_from_stat(diffopt->repo->index, ce, st.st_mode);
                        oid = null_oid(the_hash_algo);
                }
        }
index 2c8b4b21b1c7e97f9c122390da9fd353c97be819..9fb9bedfbfd7eb6e22f99ec344c5a484d7ca21de 100644 (file)
@@ -442,7 +442,7 @@ void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, st
  * for lstat() for a tracked path that is known to be up-to-date via
  * some out-of-line means (like fsmonitor).
  */
-int fake_lstat(const struct cache_entry *ce, struct stat *st);
+int fake_lstat(struct index_state *istate, const struct cache_entry *ce, struct stat *st);
 
 #define REFRESH_REALLY                   (1 << 0) /* ignore_valid */
 #define REFRESH_UNMERGED                 (1 << 1) /* allow unmerged */
index 113dc20c970502eac614c1f03d2f1ccdd0121d13..267a8bb70727c042d179c186f4784b65d6f8dead 100644 (file)
@@ -203,9 +203,12 @@ void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, st
        }
 }
 
-unsigned int ce_mode_from_stat(const struct cache_entry *ce, unsigned int mode)
+unsigned int ce_mode_from_stat(struct index_state *istate,
+                              const struct cache_entry *ce,
+                              unsigned int mode)
 {
-       struct repo_config_values *cfg = repo_config_values(the_repository);
+       struct repository *repo = (istate && istate->repo) ? istate->repo : the_repository;
+       struct repo_config_values *cfg = repo_config_values(repo);
 
        if (!has_symlinks && S_ISREG(mode) &&
            ce && S_ISLNK(ce->ce_mode))
@@ -218,9 +221,10 @@ unsigned int ce_mode_from_stat(const struct cache_entry *ce, unsigned int mode)
        return create_ce_mode(mode);
 }
 
-static unsigned int st_mode_from_ce(const struct cache_entry *ce)
+static unsigned int st_mode_from_ce(struct index_state *istate, const struct cache_entry *ce)
 {
-       struct repo_config_values *cfg = repo_config_values(the_repository);
+       struct repository *repo = (istate && istate->repo) ? istate->repo : the_repository;
+       struct repo_config_values *cfg = repo_config_values(repo);
 
        switch (ce->ce_mode & S_IFMT) {
        case S_IFLNK:
@@ -236,10 +240,10 @@ static unsigned int st_mode_from_ce(const struct cache_entry *ce)
        }
 }
 
-int fake_lstat(const struct cache_entry *ce, struct stat *st)
+int fake_lstat(struct index_state *istate, const struct cache_entry *ce, struct stat *st)
 {
        fake_lstat_data(&ce->ce_stat_data, st);
-       st->st_mode = st_mode_from_ce(ce);
+       st->st_mode = st_mode_from_ce(istate, ce);
 
        /* always succeed as lstat() replacement */
        return 0;
@@ -323,10 +327,12 @@ static int ce_modified_check_fs(struct index_state *istate,
        return 0;
 }
 
-static int ce_match_stat_basic(const struct cache_entry *ce, struct stat *st)
+static int ce_match_stat_basic(struct index_state *istate,
+                              const struct cache_entry *ce, struct stat *st)
 {
        unsigned int changed = 0;
-       struct repo_config_values *cfg = repo_config_values(the_repository);
+       struct repository *repo = (istate && istate->repo) ? istate->repo : the_repository;
+       struct repo_config_values *cfg = repo_config_values(repo);
 
        if (ce->ce_flags & CE_REMOVE)
                return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED;
@@ -431,7 +437,7 @@ int ie_match_stat(struct index_state *istate,
        if (ce_intent_to_add(ce))
                return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED;
 
-       changed = ce_match_stat_basic(ce, st);
+       changed = ce_match_stat_basic(istate, ce, st);
 
        /*
         * Within 1 second of this sequence:
@@ -738,7 +744,8 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
                          (intent_only ? ADD_CACHE_NEW_ONLY : 0));
        unsigned hash_flags = pretend ? 0 : INDEX_WRITE_OBJECT;
 
-       struct repo_config_values *cfg = repo_config_values(the_repository);
+       struct repository *repo = (istate && istate->repo) ? istate->repo : the_repository;
+       struct repo_config_values *cfg = repo_config_values(repo);
 
        if (flags & ADD_CACHE_RENORMALIZE)
                hash_flags |= INDEX_RENORMALIZE;
@@ -770,7 +777,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
                int pos = index_name_pos_also_unmerged(istate, path, namelen);
 
                ent = (0 <= pos) ? istate->cache[pos] : NULL;
-               ce->ce_mode = ce_mode_from_stat(ent, st_mode);
+               ce->ce_mode = ce_mode_from_stat(istate, ent, st_mode);
        }
 
        /* When core.ignorecase=true, determine if a directory of the same name but differing
@@ -2593,7 +2600,7 @@ static void ce_smudge_racily_clean_entry(struct index_state *istate,
 
        if (lstat(ce->name, &st) < 0)
                return;
-       if (ce_match_stat_basic(ce, &st))
+       if (ce_match_stat_basic(istate, ce, &st))
                return;
        if (ce_modified_check_fs(istate, ce, &st)) {
                /* This is "racily clean"; smudge it.  Note that this
index 3c4af2faeb5f787cfbb7bc1f7801ce8f101e049a..61299ed95ba2f5dfd2978f16e81affa05bce671a 100644 (file)
@@ -5,7 +5,8 @@
 #include "object.h"
 #include "pathspec.h"
 
-unsigned int ce_mode_from_stat(const struct cache_entry *ce,
+unsigned int ce_mode_from_stat(struct index_state *istate,
+                               const struct cache_entry *ce,
                                unsigned int mode);
 
 static inline int ce_to_dtype(const struct cache_entry *ce)