Skip to content

Commit 1c86c72

Browse files
committed
environment: move "branch.autoSetupMerge" into struct config_values
The config value `brach.autoSetupMerge` is parsed in `git_default_branch_config()` and stored in the global variable `git_branch_track`. This global variable can cause unexpected behaviours when multiple Git repos run in the the same process. Move this value into `struct config_values` which can be accessed per repo via `git_default_config()`. This would mean we do not have to remove code from `git_default_branch_config()`, thereby retaining the same behaviour. Suggested-by: Phillip Wood <phillip.wood123@gmail.com> Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Usman Akinyemi <usmanakinyemi202@gmail.com> Signed-off-by: Olamide Caleb Bello <belkid98@gmail.com>
1 parent f25afc6 commit 1c86c72

File tree

8 files changed

+19
-9
lines changed

8 files changed

+19
-9
lines changed

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ int cmd_branch(int argc,
795795
if (!sorting_options.nr)
796796
string_list_append(&sorting_options, "refname");
797797

798-
track = git_branch_track;
798+
track = the_repository->cfg_values->git_branch_track;
799799

800800
head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD",
801801
0, &head_oid, NULL);

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1631,7 +1631,7 @@ static int checkout_branch(struct checkout_opts *opts,
16311631
if (opts->track != BRANCH_TRACK_UNSPECIFIED)
16321632
die(_("'%s' cannot be used with '%s'"), "--detach", "-t");
16331633
} else if (opts->track == BRANCH_TRACK_UNSPECIFIED)
1634-
opts->track = git_branch_track;
1634+
opts->track = the_repository->cfg_values->git_branch_track;
16351635

16361636
if (new_branch_info->name && !new_branch_info->commit)
16371637
die(_("Cannot switch branch to a non-commit '%s'"),

builtin/push.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static NORETURN void die_push_simple(struct branch *branch,
162162
advice_pushdefault_maybe = _("\n"
163163
"To choose either option permanently, "
164164
"see push.default in 'git help config'.\n");
165-
if (git_branch_track != BRANCH_TRACK_SIMPLE)
165+
if (the_repository->cfg_values->git_branch_track != BRANCH_TRACK_SIMPLE)
166166
advice_automergesimple_maybe = _("\n"
167167
"To avoid automatically configuring "
168168
"an upstream branch when its name\n"

builtin/submodule--helper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3128,7 +3128,7 @@ static int module_create_branch(int argc, const char **argv, const char *prefix,
31283128
};
31293129

31303130
repo_config(the_repository, git_default_config, NULL);
3131-
track = git_branch_track;
3131+
track = the_repository->cfg_values->git_branch_track;
31323132
argc = parse_options(argc, argv, prefix, options, usage, 0);
31333133

31343134
if (argc != 3)

config.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,10 @@ void config_values_clear(struct config_values *cfg)
17661766
free(cfg->attributes_file_path);
17671767
}
17681768

1769+
void config_values_init_defaults(struct config_values *cfg) {
1770+
cfg->git_branch_track = BRANCH_TRACK_REMOTE;
1771+
}
1772+
17691773
void git_configset_init(struct config_set *set)
17701774
{
17711775
hashmap_init(&set->config_hash, config_set_element_cmp, NULL, 0);

config.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "string-list.h"
66
#include "repository.h"
77
#include "parse.h"
8+
#include "branch.h"
89

910
/**
1011
* The config API gives callers a way to access Git configuration files
@@ -142,6 +143,9 @@ struct config_values {
142143
char *attributes_file_path;
143144
int sparse_checkout;
144145

146+
/* branch config values */
147+
enum branch_track git_branch_track;
148+
145149
};
146150
#define CONFIG_CONTEXT_INIT { 0 }
147151

@@ -195,6 +199,7 @@ int git_config_from_blob_oid(config_fn_t fn, const char *name,
195199
void git_config_push_parameter(const char *text);
196200
void git_config_push_env(const char *spec);
197201
int git_config_from_parameters(config_fn_t fn, void *data);
202+
void config_values_init_defaults(struct config_values *cfg);
198203
void config_values_clear(struct config_values *cfg);
199204

200205
/*

environment.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
6666
enum eol core_eol = EOL_UNSET;
6767
int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
6868
char *check_roundtrip_encoding;
69-
enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
7069
enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
7170
enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
7271
#ifndef OBJECT_CREATION_MODE
@@ -607,18 +606,19 @@ static int git_default_i18n_config(const char *var, const char *value)
607606

608607
static int git_default_branch_config(const char *var, const char *value)
609608
{
609+
struct config_values *cfg = the_repository->cfg_values;
610610
if (!strcmp(var, "branch.autosetupmerge")) {
611611
if (value && !strcmp(value, "always")) {
612-
git_branch_track = BRANCH_TRACK_ALWAYS;
612+
cfg->git_branch_track = BRANCH_TRACK_ALWAYS;
613613
return 0;
614614
} else if (value && !strcmp(value, "inherit")) {
615-
git_branch_track = BRANCH_TRACK_INHERIT;
615+
cfg->git_branch_track = BRANCH_TRACK_INHERIT;
616616
return 0;
617617
} else if (value && !strcmp(value, "simple")) {
618-
git_branch_track = BRANCH_TRACK_SIMPLE;
618+
cfg->git_branch_track = BRANCH_TRACK_SIMPLE;
619619
return 0;
620620
}
621-
git_branch_track = git_config_bool(var, value);
621+
cfg->git_branch_track = git_config_bool(var, value);
622622
return 0;
623623
}
624624
if (!strcmp(var, "branch.autosetuprebase")) {

repository.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void initialize_repository(struct repository *repo)
5656
repo->parsed_objects = parsed_object_pool_new(repo);
5757
ALLOC_ARRAY(repo->index, 1);
5858
CALLOC_ARRAY(repo->cfg_values, 1);
59+
config_values_init_defaults(repo->cfg_values);
5960
index_state_init(repo->index, repo);
6061
repo->check_deprecated_config = true;
6162

0 commit comments

Comments
 (0)