environment: move push_default into repo_config_values

The global variable 'push_default' specifies the default behavior of 'git push' when no explicit refspec is provided. Move 'push_default' into 'struct repo_config_values' to continue the libification effort. While 'enum push_default_type' ideally belongs in 'remote.h', moving it there introduces a circular dependency chain: remote.h -> hash.h -> repository.h -> environment.h. Therefore, the enum definition is kept in 'environment.h' just above 'struct repo_config_values' with a NEEDSWORK comment for future cleanup. Modify the configuration parsing in environment.c to update the per-repository structure directly, and update caller across the codebase to access the value via 'repo_config_values()'. 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>

Tian Yuchen committed Jul 14, 2026 at 11:25 UTC 1a6c84e98dffe06fbe1acdf03817dffa10e180ef
4 files changed +32 -22
builtin/push.c
+6 -4
@@ -73,6 +73,7 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
73 struct remote *remote, struct ref *matched)
74 {
75 const char *branch_name;
76 + struct repo_config_values *cfg = repo_config_values(the_repository);
77
78 if (remote->push.nr) {
79 struct refspec_item query = {
@@ -88,7 +89,7 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
89 }
90 }
91
91 - if (push_default == PUSH_DEFAULT_UPSTREAM &&
92 + if (cfg->push_default == PUSH_DEFAULT_UPSTREAM &&
93 skip_prefix(matched->name, "refs/heads/", &branch_name)) {
94 struct branch *branch = branch_get(branch_name);
95 if (branch->merge_nr == 1 && branch->merge[0]->src) {
@@ -160,7 +161,7 @@ static NORETURN void die_push_simple(struct branch *branch,
161 * Don't show advice for people who explicitly set
162 * push.default.
163 */
163 - if (push_default == PUSH_DEFAULT_UNSPECIFIED)
164 + if (cfg->push_default == PUSH_DEFAULT_UNSPECIFIED)
165 advice_pushdefault_maybe = _("\n"
166 "To choose either option permanently, "
167 "see push.default in 'git help config'.\n");
@@ -231,8 +232,9 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
232 struct branch *branch;
233 const char *dst;
234 int same_remote;
235 + struct repo_config_values *cfg = repo_config_values(the_repository);
236
235 - switch (push_default) {
237 + switch (cfg->push_default) {
238 case PUSH_DEFAULT_MATCHING:
239 refspec_append(&rs, ":");
240 return;
@@ -252,7 +254,7 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
254 dst = branch->refname;
255 same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL));
256
255 - switch (push_default) {
257 + switch (cfg->push_default) {
258 default:
259 case PUSH_DEFAULT_UNSPECIFIED:
260 case PUSH_DEFAULT_SIMPLE:
environment.c
+9 -7
@@ -58,7 +58,6 @@ enum eol core_eol = EOL_UNSET;
58 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
59 char *check_roundtrip_encoding;
60 enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
61 -enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
61 #ifndef OBJECT_CREATION_MODE
62 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
63 #endif
@@ -620,21 +619,23 @@ static int git_default_branch_config(const char *var, const char *value)
619
620 static int git_default_push_config(const char *var, const char *value)
621 {
622 + struct repo_config_values *cfg = repo_config_values(the_repository);
623 +
624 if (!strcmp(var, "push.default")) {
625 if (!value)
626 return config_error_nonbool(var);
627 else if (!strcmp(value, "nothing"))
627 - push_default = PUSH_DEFAULT_NOTHING;
628 + cfg->push_default = PUSH_DEFAULT_NOTHING;
629 else if (!strcmp(value, "matching"))
629 - push_default = PUSH_DEFAULT_MATCHING;
630 + cfg->push_default = PUSH_DEFAULT_MATCHING;
631 else if (!strcmp(value, "simple"))
631 - push_default = PUSH_DEFAULT_SIMPLE;
632 + cfg->push_default = PUSH_DEFAULT_SIMPLE;
633 else if (!strcmp(value, "upstream"))
633 - push_default = PUSH_DEFAULT_UPSTREAM;
634 + cfg->push_default = PUSH_DEFAULT_UPSTREAM;
635 else if (!strcmp(value, "tracking")) /* deprecated */
635 - push_default = PUSH_DEFAULT_UPSTREAM;
636 + cfg->push_default = PUSH_DEFAULT_UPSTREAM;
637 else if (!strcmp(value, "current"))
637 - push_default = PUSH_DEFAULT_CURRENT;
638 + cfg->push_default = PUSH_DEFAULT_CURRENT;
639 else {
640 error(_("malformed value for %s: %s"), var, value);
641 return error(_("must be one of nothing, matching, simple, "
@@ -726,6 +727,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
727 cfg->askpass_program = NULL;
728 cfg->apply_default_whitespace = NULL;
729 cfg->apply_default_ignorewhitespace = NULL;
730 + cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
731 cfg->apply_sparse_checkout = 0;
732 cfg->branch_track = BRANCH_TRACK_REMOTE;
733 cfg->trust_ctime = 1;
environment.h
+16 -10
@@ -87,6 +87,21 @@ extern const char * const local_repo_env[];
87 struct strvec;
88
89 struct repository;
90 +
91 +/*
92 + * NEEDSWORK: It would be better if these definitions could be moved to
93 + * other more specific files, but care is needed to avoid circular
94 + * inclusion issues.
95 + */
96 +enum push_default_type {
97 + PUSH_DEFAULT_NOTHING = 0,
98 + PUSH_DEFAULT_MATCHING,
99 + PUSH_DEFAULT_SIMPLE,
100 + PUSH_DEFAULT_UPSTREAM,
101 + PUSH_DEFAULT_CURRENT,
102 + PUSH_DEFAULT_UNSPECIFIED
103 +};
104 +
105 struct repo_config_values {
106 /* section "core" config values */
107 char *attributes_file;
@@ -96,6 +111,7 @@ struct repo_config_values {
111 char *askpass_program;
112 char *apply_default_whitespace;
113 char *apply_default_ignorewhitespace;
114 + enum push_default_type push_default;
115 int apply_sparse_checkout;
116 int trust_ctime;
117 int check_stat;
@@ -197,16 +213,6 @@ enum rebase_setup_type {
213 };
214 extern enum rebase_setup_type autorebase;
215
200 -enum push_default_type {
201 - PUSH_DEFAULT_NOTHING = 0,
202 - PUSH_DEFAULT_MATCHING,
203 - PUSH_DEFAULT_SIMPLE,
204 - PUSH_DEFAULT_UPSTREAM,
205 - PUSH_DEFAULT_CURRENT,
206 - PUSH_DEFAULT_UNSPECIFIED
207 -};
208 -extern enum push_default_type push_default;
209 -
216 enum object_creation_mode {
217 OBJECT_CREATION_USES_HARDLINKS = 0,
218 OBJECT_CREATION_USES_RENAMES = 1
remote.c
+1 -1
@@ -1933,7 +1933,7 @@ static char *branch_get_push_1(struct repository *repo,
1933 if (remote->mirror)
1934 return tracking_for_push_dest(remote, branch->refname, err);
1935
1936 - switch (push_default) {
1936 + switch (repo_config_values(repo)->push_default) {
1937 case PUSH_DEFAULT_NOTHING:
1938 return error_buf(err, _("push has no destination (push.default is 'nothing')"));
1939