Skip to content

Commit 7cb4274

Browse files
committed
Merge branch 'vd/worktree-config-is-per-repository'
The value of config.worktree is per-repository, but has been kept in a singleton global variable per process. This has been OK as most Git operations interacted with a single repository at a time, but not right for operations like recursive "grep" that want to access multiple repositories from a single process without forking. The global variable has been eliminated and made into a member in the per-repository data structure. * vd/worktree-config-is-per-repository: repository: move 'repository_format_worktree_config' to repo scope config: pass 'repo' directly to 'config_with_options()' config: use gitdir to get worktree config
2 parents 9cd234e + 3867f6d commit 7cb4274

13 files changed

+102
-37
lines changed

builtin/config.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "color.h"
66
#include "editor.h"
77
#include "environment.h"
8+
#include "repository.h"
89
#include "gettext.h"
910
#include "ident.h"
1011
#include "parse-options.h"
@@ -375,7 +376,8 @@ static int get_value(const char *key_, const char *regex_, unsigned flags)
375376
}
376377

377378
config_with_options(collect_config, &values,
378-
&given_config_source, &config_options);
379+
&given_config_source, the_repository,
380+
&config_options);
379381

380382
if (!values.nr && default_value) {
381383
struct strbuf *item;
@@ -486,7 +488,8 @@ static void get_color(const char *var, const char *def_color)
486488
get_color_found = 0;
487489
parsed_color[0] = '\0';
488490
config_with_options(git_get_color_config, NULL,
489-
&given_config_source, &config_options);
491+
&given_config_source, the_repository,
492+
&config_options);
490493

491494
if (!get_color_found && def_color) {
492495
if (color_parse(def_color, parsed_color) < 0)
@@ -518,7 +521,8 @@ static int get_colorbool(const char *var, int print)
518521
get_diff_color_found = -1;
519522
get_color_ui_found = -1;
520523
config_with_options(git_get_colorbool_config, NULL,
521-
&given_config_source, &config_options);
524+
&given_config_source, the_repository,
525+
&config_options);
522526

523527
if (get_colorbool_found < 0) {
524528
if (!strcmp(get_colorbool_slot, "color.diff"))
@@ -607,7 +611,8 @@ static int get_urlmatch(const char *var, const char *url)
607611
}
608612

609613
config_with_options(urlmatch_config_entry, &config,
610-
&given_config_source, &config_options);
614+
&given_config_source, the_repository,
615+
&config_options);
611616

612617
ret = !values.nr;
613618

@@ -713,7 +718,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
713718
given_config_source.scope = CONFIG_SCOPE_LOCAL;
714719
} else if (use_worktree_config) {
715720
struct worktree **worktrees = get_worktrees();
716-
if (repository_format_worktree_config)
721+
if (the_repository->repository_format_worktree_config)
717722
given_config_source.file = git_pathdup("config.worktree");
718723
else if (worktrees[0] && worktrees[1])
719724
die(_("--worktree cannot be used with multiple "
@@ -827,7 +832,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
827832
if (actions == ACTION_LIST) {
828833
check_argc(argc, 0, 0);
829834
if (config_with_options(show_all_config, NULL,
830-
&given_config_source,
835+
&given_config_source, the_repository,
831836
&config_options) < 0) {
832837
if (given_config_source.file)
833838
die_errno(_("unable to read config file '%s'"),

builtin/worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ static int add_worktree(const char *path, const char *refname,
483483
* values from the current worktree into the new one, that way the
484484
* new worktree behaves the same as this one.
485485
*/
486-
if (repository_format_worktree_config)
486+
if (the_repository->repository_format_worktree_config)
487487
copy_filtered_worktree_config(sb_repo.buf);
488488

489489
strvec_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);

config.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ struct config_include_data {
199199
void *data;
200200
const struct config_options *opts;
201201
struct git_config_source *config_source;
202+
struct repository *repo;
202203
struct config_reader *config_reader;
203204

204205
/*
@@ -415,7 +416,8 @@ static void populate_remote_urls(struct config_include_data *inc)
415416

416417
inc->remote_urls = xmalloc(sizeof(*inc->remote_urls));
417418
string_list_init_dup(inc->remote_urls);
418-
config_with_options(add_remote_url, inc->remote_urls, inc->config_source, &opts);
419+
config_with_options(add_remote_url, inc->remote_urls,
420+
inc->config_source, inc->repo, &opts);
419421

420422
config_reader_set_scope(inc->config_reader, store_scope);
421423
}
@@ -2193,21 +2195,32 @@ int git_config_system(void)
21932195

21942196
static int do_git_config_sequence(struct config_reader *reader,
21952197
const struct config_options *opts,
2198+
const struct repository *repo,
21962199
config_fn_t fn, void *data)
21972200
{
21982201
int ret = 0;
21992202
char *system_config = git_system_config();
22002203
char *xdg_config = NULL;
22012204
char *user_config = NULL;
22022205
char *repo_config;
2206+
char *worktree_config;
22032207
enum config_scope prev_parsing_scope = reader->parsing_scope;
22042208

2205-
if (opts->commondir)
2209+
/*
2210+
* Ensure that either:
2211+
* - the git_dir and commondir are both set, or
2212+
* - the git_dir and commondir are both NULL
2213+
*/
2214+
if (!opts->git_dir != !opts->commondir)
2215+
BUG("only one of commondir and git_dir is non-NULL");
2216+
2217+
if (opts->commondir) {
22062218
repo_config = mkpathdup("%s/config", opts->commondir);
2207-
else if (opts->git_dir)
2208-
BUG("git_dir without commondir");
2209-
else
2219+
worktree_config = mkpathdup("%s/config.worktree", opts->git_dir);
2220+
} else {
22102221
repo_config = NULL;
2222+
worktree_config = NULL;
2223+
}
22112224

22122225
config_reader_set_scope(reader, CONFIG_SCOPE_SYSTEM);
22132226
if (git_config_system() && system_config &&
@@ -2230,11 +2243,10 @@ static int do_git_config_sequence(struct config_reader *reader,
22302243
ret += git_config_from_file(fn, repo_config, data);
22312244

22322245
config_reader_set_scope(reader, CONFIG_SCOPE_WORKTREE);
2233-
if (!opts->ignore_worktree && repository_format_worktree_config) {
2234-
char *path = git_pathdup("config.worktree");
2235-
if (!access_or_die(path, R_OK, 0))
2236-
ret += git_config_from_file(fn, path, data);
2237-
free(path);
2246+
if (!opts->ignore_worktree && worktree_config &&
2247+
repo && repo->repository_format_worktree_config &&
2248+
!access_or_die(worktree_config, R_OK, 0)) {
2249+
ret += git_config_from_file(fn, worktree_config, data);
22382250
}
22392251

22402252
config_reader_set_scope(reader, CONFIG_SCOPE_COMMAND);
@@ -2246,11 +2258,13 @@ static int do_git_config_sequence(struct config_reader *reader,
22462258
free(xdg_config);
22472259
free(user_config);
22482260
free(repo_config);
2261+
free(worktree_config);
22492262
return ret;
22502263
}
22512264

22522265
int config_with_options(config_fn_t fn, void *data,
22532266
struct git_config_source *config_source,
2267+
struct repository *repo,
22542268
const struct config_options *opts)
22552269
{
22562270
struct config_include_data inc = CONFIG_INCLUDE_INIT;
@@ -2261,6 +2275,7 @@ int config_with_options(config_fn_t fn, void *data,
22612275
inc.fn = fn;
22622276
inc.data = data;
22632277
inc.opts = opts;
2278+
inc.repo = repo;
22642279
inc.config_source = config_source;
22652280
inc.config_reader = &the_reader;
22662281
fn = git_config_include;
@@ -2279,12 +2294,10 @@ int config_with_options(config_fn_t fn, void *data,
22792294
} else if (config_source && config_source->file) {
22802295
ret = git_config_from_file(fn, config_source->file, data);
22812296
} else if (config_source && config_source->blob) {
2282-
struct repository *repo = config_source->repo ?
2283-
config_source->repo : the_repository;
22842297
ret = git_config_from_blob_ref(fn, repo, config_source->blob,
22852298
data);
22862299
} else {
2287-
ret = do_git_config_sequence(&the_reader, opts, fn, data);
2300+
ret = do_git_config_sequence(&the_reader, opts, repo, fn, data);
22882301
}
22892302

22902303
if (inc.remote_urls) {
@@ -2343,7 +2356,7 @@ void read_early_config(config_fn_t cb, void *data)
23432356
opts.git_dir = gitdir.buf;
23442357
}
23452358

2346-
config_with_options(cb, data, NULL, &opts);
2359+
config_with_options(cb, data, NULL, NULL, &opts);
23472360

23482361
strbuf_release(&commondir);
23492362
strbuf_release(&gitdir);
@@ -2363,7 +2376,7 @@ void read_very_early_config(config_fn_t cb, void *data)
23632376
opts.ignore_cmdline = 1;
23642377
opts.system_gently = 1;
23652378

2366-
config_with_options(cb, data, NULL, &opts);
2379+
config_with_options(cb, data, NULL, NULL, &opts);
23672380
}
23682381

23692382
RESULT_MUST_BE_USED
@@ -2671,7 +2684,7 @@ static void repo_read_config(struct repository *repo)
26712684
data.config_set = repo->config;
26722685
data.config_reader = &the_reader;
26732686

2674-
if (config_with_options(config_set_callback, &data, NULL, &opts) < 0)
2687+
if (config_with_options(config_set_callback, &data, NULL, repo, &opts) < 0)
26752688
/*
26762689
* config_with_options() normally returns only
26772690
* zero, as most errors are fatal, and
@@ -2815,7 +2828,7 @@ static void read_protected_config(void)
28152828
git_configset_init(&protected_config);
28162829
data.config_set = &protected_config;
28172830
data.config_reader = &the_reader;
2818-
config_with_options(config_set_callback, &data, NULL, &opts);
2831+
config_with_options(config_set_callback, &data, NULL, NULL, &opts);
28192832
}
28202833

28212834
void git_protected_config(config_fn_t fn, void *data)
@@ -3327,7 +3340,7 @@ int repo_config_set_worktree_gently(struct repository *r,
33273340
const char *key, const char *value)
33283341
{
33293342
/* Only use worktree-specific config if it is already enabled. */
3330-
if (repository_format_worktree_config) {
3343+
if (r->repository_format_worktree_config) {
33313344
char *file = repo_git_path(r, "config.worktree");
33323345
int ret = git_config_set_multivar_in_file_gently(
33333346
file, key, value, NULL, 0);

config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "hashmap.h"
55
#include "string-list.h"
6+
#include "repository.h"
67

78

89
/**
@@ -49,8 +50,6 @@ const char *config_scope_name(enum config_scope scope);
4950
struct git_config_source {
5051
unsigned int use_stdin:1;
5152
const char *file;
52-
/* The repository if blob is not NULL; leave blank for the_repository */
53-
struct repository *repo;
5453
const char *blob;
5554
enum config_scope scope;
5655
};
@@ -196,6 +195,7 @@ void git_config(config_fn_t fn, void *);
196195
*/
197196
int config_with_options(config_fn_t fn, void *,
198197
struct git_config_source *config_source,
198+
struct repository *repo,
199199
const struct config_options *opts);
200200

201201
/**

environment.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ int is_bare_repository_cfg = -1; /* unspecified */
4242
int warn_ambiguous_refs = 1;
4343
int warn_on_object_refname_ambiguity = 1;
4444
int repository_format_precious_objects;
45-
int repository_format_worktree_config;
4645
const char *git_commit_encoding;
4746
const char *git_log_output_encoding;
4847
char *apply_default_whitespace;

environment.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ extern char *notes_ref_name;
197197
extern int grafts_replace_parents;
198198

199199
extern int repository_format_precious_objects;
200-
extern int repository_format_worktree_config;
201200

202201
/*
203202
* Create a temporary file rooted in the object database directory, or

repository.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ int repo_init(struct repository *repo,
182182
goto error;
183183

184184
repo_set_hash_algo(repo, format.hash_algo);
185+
repo->repository_format_worktree_config = format.worktree_config;
185186

186187
/* take ownership of format.partial_clone */
187188
repo->repository_format_partial_clone = format.partial_clone;

repository.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ struct repository {
163163
struct promisor_remote_config *promisor_remote_config;
164164

165165
/* Configurations */
166+
int repository_format_worktree_config;
166167

167168
/* Indicate if a repository has a different 'commondir' from 'gitdir' */
168169
unsigned different_commondir:1;

setup.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,11 +650,10 @@ static int check_repository_format_gently(const char *gitdir, struct repository_
650650
}
651651

652652
repository_format_precious_objects = candidate->precious_objects;
653-
repository_format_worktree_config = candidate->worktree_config;
654653
string_list_clear(&candidate->unknown_extensions, 0);
655654
string_list_clear(&candidate->v1_only_extensions, 0);
656655

657-
if (repository_format_worktree_config) {
656+
if (candidate->worktree_config) {
658657
/*
659658
* pick up core.bare and core.worktree from per-worktree
660659
* config if present
@@ -1423,6 +1422,9 @@ int discover_git_directory(struct strbuf *commondir,
14231422
return -1;
14241423
}
14251424

1425+
the_repository->repository_format_worktree_config =
1426+
candidate.worktree_config;
1427+
14261428
/* take ownership of candidate.partial_clone */
14271429
the_repository->repository_format_partial_clone =
14281430
candidate.partial_clone;
@@ -1560,6 +1562,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
15601562
}
15611563
if (startup_info->have_repository) {
15621564
repo_set_hash_algo(the_repository, repo_fmt.hash_algo);
1565+
the_repository->repository_format_worktree_config =
1566+
repo_fmt.worktree_config;
15631567
/* take ownership of repo_fmt.partial_clone */
15641568
the_repository->repository_format_partial_clone =
15651569
repo_fmt.partial_clone;
@@ -1651,6 +1655,8 @@ void check_repository_format(struct repository_format *fmt)
16511655
check_repository_format_gently(get_git_dir(), fmt, NULL);
16521656
startup_info->have_repository = 1;
16531657
repo_set_hash_algo(the_repository, fmt->hash_algo);
1658+
the_repository->repository_format_worktree_config =
1659+
fmt->worktree_config;
16541660
the_repository->repository_format_partial_clone =
16551661
xstrdup_or_null(fmt->partial_clone);
16561662
clear_repository_format(&repo_fmt);

submodule-config.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,15 +659,14 @@ static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void
659659
config_source.file = file;
660660
} else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
661661
repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
662-
config_source.repo = repo;
663662
config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
664663
if (repo != the_repository)
665664
add_submodule_odb_by_path(repo->objects->odb->path);
666665
} else {
667666
goto out;
668667
}
669668

670-
config_with_options(fn, data, &config_source, &opts);
669+
config_with_options(fn, data, &config_source, repo, &opts);
671670

672671
out:
673672
free(oidstr);

0 commit comments

Comments
 (0)