diff --git a/apply.c b/apply.c index 249248d4f2..fbb907d3c0 100644 --- a/apply.c +++ b/apply.c @@ -3893,7 +3893,7 @@ static int check_preimage(struct apply_state *state, if (*ce && !(*ce)->ce_mode) BUG("ce_mode == 0 for path '%s'", old_name); - if (trust_executable_bit || !S_ISREG(st->st_mode)) + if (repo_trust_executable_bit(the_repository) || !S_ISREG(st->st_mode)) st_mode = ce_mode_from_stat(*ce, st->st_mode); else if (*ce) st_mode = (*ce)->ce_mode; diff --git a/environment.c b/environment.c index 6ee11e9fc8..fe30b3dcb9 100644 --- a/environment.c +++ b/environment.c @@ -41,7 +41,6 @@ static int pack_compression_seen; static int zlib_compression_seen; -int trust_executable_bit = 1; int has_symlinks = 1; int minimum_abbrev = 4, default_abbrev = -1; int ignore_case; @@ -142,6 +141,13 @@ int repo_protect_hfs(struct repository *repo) PROTECT_HFS_DEFAULT; } +int repo_trust_executable_bit(struct repository *repo) +{ + return repo->gitdir? + repo_config_values(repo)->trust_executable_bit : + 1; +} + int have_git_dir(void) { return startup_info->have_repository @@ -305,7 +311,7 @@ int git_default_core_config(const char *var, const char *value, /* This needs a better name */ if (!strcmp(var, "core.filemode")) { - trust_executable_bit = git_config_bool(var, value); + cfg->trust_executable_bit = git_config_bool(var, value); return 0; } if (!strcmp(var, "core.trustctime")) { @@ -726,6 +732,7 @@ void repo_config_values_init(struct repo_config_values *cfg) cfg->apply_sparse_checkout = 0; cfg->protect_hfs = PROTECT_HFS_DEFAULT; cfg->protect_ntfs = PROTECT_NTFS_DEFAULT; + cfg->trust_executable_bit = 1; cfg->branch_track = BRANCH_TRACK_REMOTE; cfg->trust_ctime = 1; cfg->check_stat = 1; diff --git a/environment.h b/environment.h index d188955f5b..915286a33c 100644 --- a/environment.h +++ b/environment.h @@ -100,6 +100,7 @@ struct repo_config_values { int warn_on_object_refname_ambiguity; int protect_hfs; int protect_ntfs; + int trust_executable_bit; /* section "sparse" config values */ int sparse_expect_files_outside_of_patterns; @@ -143,6 +144,13 @@ int git_default_core_config(const char *var, const char *value, int repo_protect_hfs(struct repository *repo); int repo_protect_ntfs(struct repository *repo); +/* + * Getter for the `trust_executable_bit` field of `struct repo_config_values`. + * It checks `repo->gitdir` to prevent calling repo_config_values() + * before the configuration is loaded or in bare environments. + */ +int repo_trust_executable_bit(struct repository *repo); + void repo_config_values_init(struct repo_config_values *cfg); int is_bare_repository(struct repository *repo); @@ -170,7 +178,6 @@ int is_bare_repository(struct repository *repo); int have_git_dir(void); /* Environment bits from configuration mechanism */ -extern int trust_executable_bit; extern int has_symlinks; extern int minimum_abbrev, default_abbrev; extern int ignore_case; diff --git a/read-cache.c b/read-cache.c index 2c6a60c756..fb40ce2048 100644 --- a/read-cache.c +++ b/read-cache.c @@ -203,15 +203,26 @@ 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) +{ + if (!has_symlinks && S_ISREG(mode) && + ce && S_ISLNK(ce->ce_mode)) + return ce->ce_mode; + if (!repo_trust_executable_bit(the_repository) && S_ISREG(mode)) { + if (ce && S_ISREG(ce->ce_mode)) + return ce->ce_mode; + return create_ce_mode(0666); + } + return create_ce_mode(mode); +} + 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; + return (ce->ce_mode & (repo_trust_executable_bit(the_repository) ? 0755 : 0644)) | S_IFREG; case S_IFGITLINK: return S_IFDIR | 0755; case S_IFDIR: @@ -321,7 +332,7 @@ static int ce_match_stat_basic(const struct cache_entry *ce, struct stat *st) /* We consider only the owner x bit to be relevant for * "mode changes" */ - if (trust_executable_bit && + if (repo_trust_executable_bit(the_repository) && (0100 & (ce->ce_mode ^ st->st_mode))) changed |= MODE_CHANGED; break; @@ -742,7 +753,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st, ce->ce_flags |= CE_INTENT_TO_ADD; - if (trust_executable_bit && has_symlinks) { + if (repo_trust_executable_bit(the_repository) && has_symlinks) { ce->ce_mode = create_ce_mode(st_mode); } else { /* If there is an existing entry, pick the mode bits and type diff --git a/read-cache.h b/read-cache.h index 043da1f1aa..9088a0724a 100644 --- a/read-cache.h +++ b/read-cache.h @@ -5,20 +5,15 @@ #include "object.h" #include "pathspec.h" -static inline unsigned int ce_mode_from_stat(const struct cache_entry *ce, - unsigned int mode) -{ - extern int trust_executable_bit, has_symlinks; - if (!has_symlinks && S_ISREG(mode) && - ce && S_ISLNK(ce->ce_mode)) - return ce->ce_mode; - if (!trust_executable_bit && S_ISREG(mode)) { - if (ce && S_ISREG(ce->ce_mode)) - return ce->ce_mode; - return create_ce_mode(0666); - } - return create_ce_mode(mode); -} +/* + * Determine the appropriate index mode for a file based on its stat() + * information and the existing cache entry (if any). + * + * This function handles degradation for filesystems that lack + * symlink support or reliable executable bits. + */ +unsigned int ce_mode_from_stat(const struct cache_entry *ce, + unsigned int mode); static inline int ce_to_dtype(const struct cache_entry *ce) {