@matheusbsbs / matheus-bagliano / commits / 2b64fc894d
Fork 0

pass "git -c foo=bar" params through environment

Git uses the "-c foo=bar" parameters to set a config variable for a single git invocation. We currently do this by making a list in the current process and consulting that list in git_config. This works fine for built-ins, but the config changes are silently ignored by subprocesses, including dashed externals and invocations to "git config" from shell scripts. This patch instead puts them in an environment variable which we consult when looking at config (both internally and via calls "git config"). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 23, 2010 at 15:16 UTC 2b64fc894dada0916558fd0bfd19c50631f978cb
3 files changed +58 -4
cache.h
+3
@@ -379,6 +379,7 @@ static inline enum object_type object_type(unsigned int mode)
379 #define GRAFT_ENVIRONMENT "GIT_GRAFT_FILE"
380 #define TEMPLATE_DIR_ENVIRONMENT "GIT_TEMPLATE_DIR"
381 #define CONFIG_ENVIRONMENT "GIT_CONFIG"
382 +#define CONFIG_DATA_ENVIRONMENT "GIT_CONFIG_PARAMETERS"
383 #define EXEC_PATH_ENVIRONMENT "GIT_EXEC_PATH"
384 #define CEILING_DIRECTORIES_ENVIRONMENT "GIT_CEILING_DIRECTORIES"
385 #define NO_REPLACE_OBJECTS_ENVIRONMENT "GIT_NO_REPLACE_OBJECTS"
@@ -970,7 +971,9 @@ extern int update_server_info(int);
971 typedef int (*config_fn_t)(const char *, const char *, void *);
972 extern int git_default_config(const char *, const char *, void *);
973 extern int git_config_from_file(config_fn_t fn, const char *, void *);
974 +extern void git_config_push_parameter(const char *text);
975 extern int git_config_parse_parameter(const char *text);
976 +extern int git_config_parse_environment(void);
977 extern int git_config_from_parameters(config_fn_t fn, void *data);
978 extern int git_config(config_fn_t fn, void *);
979 extern int git_parse_ulong(const char *, unsigned long *);
config.c
+54 -3
@@ -8,6 +8,7 @@
8 #include "cache.h"
9 #include "exec_cmd.h"
10 #include "strbuf.h"
11 +#include "quote.h"
12
13 #define MAXNAME (256)
14
@@ -34,6 +35,19 @@ static void lowercase(char *p)
35 *p = tolower(*p);
36 }
37
38 +void git_config_push_parameter(const char *text)
39 +{
40 + struct strbuf env = STRBUF_INIT;
41 + const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
42 + if (old) {
43 + strbuf_addstr(&env, old);
44 + strbuf_addch(&env, ' ');
45 + }
46 + sq_quote_buf(&env, text);
47 + setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1);
48 + strbuf_release(&env);
49 +}
50 +
51 int git_config_parse_parameter(const char *text)
52 {
53 struct config_item *ct;
@@ -61,6 +75,37 @@ int git_config_parse_parameter(const char *text)
75 return 0;
76 }
77
78 +int git_config_parse_environment(void) {
79 + const char *env = getenv(CONFIG_DATA_ENVIRONMENT);
80 + char *envw;
81 + const char **argv = NULL;
82 + int nr = 0, alloc = 0;
83 + int i;
84 +
85 + if (!env)
86 + return 0;
87 + /* sq_dequote will write over it */
88 + envw = xstrdup(env);
89 +
90 + if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) {
91 + free(envw);
92 + return error("bogus format in " CONFIG_DATA_ENVIRONMENT);
93 + }
94 +
95 + for (i = 0; i < nr; i++) {
96 + if (git_config_parse_parameter(argv[i]) < 0) {
97 + error("bogus config parameter: %s", argv[i]);
98 + free(argv);
99 + free(envw);
100 + return -1;
101 + }
102 + }
103 +
104 + free(argv);
105 + free(envw);
106 + return 0;
107 +}
108 +
109 static int get_next_char(void)
110 {
111 int c;
@@ -773,7 +818,14 @@ int git_config_global(void)
818
819 int git_config_from_parameters(config_fn_t fn, void *data)
820 {
821 + static int loaded_environment;
822 const struct config_item *ct;
823 +
824 + if (!loaded_environment) {
825 + if (git_config_parse_environment() < 0)
826 + return -1;
827 + loaded_environment = 1;
828 + }
829 for (ct = config_parameters; ct; ct = ct->next)
830 if (fn(ct->name, ct->value, data) < 0)
831 return -1;
@@ -812,10 +864,9 @@ int git_config(config_fn_t fn, void *data)
864 }
865 free(repo_config);
866
815 - if (config_parameters) {
816 - ret += git_config_from_parameters(fn, data);
867 + ret += git_config_from_parameters(fn, data);
868 + if (config_parameters)
869 found += 1;
818 - }
870
871 if (found == 0)
872 return -1;
git.c
+1 -1
@@ -136,7 +136,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
136 fprintf(stderr, "-c expects a configuration string\n" );
137 usage(git_usage_string);
138 }
139 - git_config_parse_parameter((*argv)[1]);
139 + git_config_push_parameter((*argv)[1]);
140 (*argv)++;
141 (*argc)--;
142 } else {