Skip to content

Commit f951ed9

Browse files
malon7782gitster
authored andcommitted
environment: move trust_executable_bit into repo_config_values
Move the global 'trust_executable_bit' configuration into the repository-specific 'repo_config_values' struct. To ensure code readability, the getter function 'repo_trust_executable_bit()' has been introduced. For now, associated functions access this configuration by explicitly falling back to 'the_repository'. 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>
1 parent e5d7e4c commit f951ed9

4 files changed

Lines changed: 22 additions & 8 deletions

File tree

apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3893,7 +3893,7 @@ static int check_preimage(struct apply_state *state,
38933893
if (*ce && !(*ce)->ce_mode)
38943894
BUG("ce_mode == 0 for path '%s'", old_name);
38953895

3896-
if (trust_executable_bit || !S_ISREG(st->st_mode))
3896+
if (repo_trust_executable_bit(the_repository) || !S_ISREG(st->st_mode))
38973897
st_mode = ce_mode_from_stat(*ce, st->st_mode);
38983898
else if (*ce)
38993899
st_mode = (*ce)->ce_mode;

environment.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
static int pack_compression_seen;
4242
static int zlib_compression_seen;
4343

44-
int trust_executable_bit = 1;
4544
int trust_ctime = 1;
4645
int check_stat = 1;
4746
int has_symlinks = 1;
@@ -142,6 +141,13 @@ int is_bare_repository(void)
142141
return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
143142
}
144143

144+
int repo_trust_executable_bit(struct repository *repo)
145+
{
146+
return repo->gitdir?
147+
repo_config_values(repo)->trust_executable_bit :
148+
1;
149+
}
150+
145151
int have_git_dir(void)
146152
{
147153
return startup_info->have_repository
@@ -305,7 +311,7 @@ int git_default_core_config(const char *var, const char *value,
305311

306312
/* This needs a better name */
307313
if (!strcmp(var, "core.filemode")) {
308-
trust_executable_bit = git_config_bool(var, value);
314+
cfg->trust_executable_bit = git_config_bool(var, value);
309315
return 0;
310316
}
311317
if (!strcmp(var, "core.trustctime")) {
@@ -720,5 +726,6 @@ void repo_config_values_init(struct repo_config_values *cfg)
720726
{
721727
cfg->attributes_file = NULL;
722728
cfg->apply_sparse_checkout = 0;
729+
cfg->trust_executable_bit = 1;
723730
cfg->branch_track = BRANCH_TRACK_REMOTE;
724731
}

environment.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ struct repo_config_values {
9191
/* section "core" config values */
9292
char *attributes_file;
9393
int apply_sparse_checkout;
94+
int trust_executable_bit;
9495

9596
/* section "branch" config values */
9697
enum branch_track branch_track;
@@ -123,6 +124,13 @@ int git_default_config(const char *, const char *,
123124
int git_default_core_config(const char *var, const char *value,
124125
const struct config_context *ctx, void *cb);
125126

127+
/*
128+
* Getter for the `trust_executable_bit` field of `struct repo_config_values`.
129+
* It checks `repo->gitdir` to prevent calling repo_config_values()
130+
* before the configuration is loaded or in bare environments.
131+
*/
132+
int repo_trust_executable_bit(struct repository *repo);
133+
126134
void repo_config_values_init(struct repo_config_values *cfg);
127135

128136
/*
@@ -158,7 +166,6 @@ int is_bare_repository(void);
158166
extern char *git_work_tree_cfg;
159167

160168
/* Environment bits from configuration mechanism */
161-
extern int trust_executable_bit;
162169
extern int trust_ctime;
163170
extern int check_stat;
164171
extern int has_symlinks;

read-cache.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ unsigned int ce_mode_from_stat(const struct cache_entry *ce, unsigned int mode)
208208
if (!has_symlinks && S_ISREG(mode) &&
209209
ce && S_ISLNK(ce->ce_mode))
210210
return ce->ce_mode;
211-
if (!trust_executable_bit && S_ISREG(mode)) {
211+
if (!repo_trust_executable_bit(the_repository) && S_ISREG(mode)) {
212212
if (ce && S_ISREG(ce->ce_mode))
213213
return ce->ce_mode;
214214
return create_ce_mode(0666);
@@ -222,7 +222,7 @@ static unsigned int st_mode_from_ce(const struct cache_entry *ce)
222222
case S_IFLNK:
223223
return has_symlinks ? S_IFLNK : (S_IFREG | 0644);
224224
case S_IFREG:
225-
return (ce->ce_mode & (trust_executable_bit ? 0755 : 0644)) | S_IFREG;
225+
return (ce->ce_mode & (repo_trust_executable_bit(the_repository) ? 0755 : 0644)) | S_IFREG;
226226
case S_IFGITLINK:
227227
return S_IFDIR | 0755;
228228
case S_IFDIR:
@@ -332,7 +332,7 @@ static int ce_match_stat_basic(const struct cache_entry *ce, struct stat *st)
332332
/* We consider only the owner x bit to be relevant for
333333
* "mode changes"
334334
*/
335-
if (trust_executable_bit &&
335+
if (repo_trust_executable_bit(the_repository) &&
336336
(0100 & (ce->ce_mode ^ st->st_mode)))
337337
changed |= MODE_CHANGED;
338338
break;
@@ -753,7 +753,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
753753
ce->ce_flags |= CE_INTENT_TO_ADD;
754754

755755

756-
if (trust_executable_bit && has_symlinks) {
756+
if (repo_trust_executable_bit(the_repository) && has_symlinks) {
757757
ce->ce_mode = create_ce_mode(st_mode);
758758
} else {
759759
/* If there is an existing entry, pick the mode bits and type

0 commit comments

Comments
 (0)