Skip to content

Commit

Permalink
pass "git -c foo=bar" params through environment
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Aug 24, 2010
1 parent 64fdc08 commit 2b64fc8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ static inline enum object_type object_type(unsigned int mode)
#define GRAFT_ENVIRONMENT "GIT_GRAFT_FILE"
#define TEMPLATE_DIR_ENVIRONMENT "GIT_TEMPLATE_DIR"
#define CONFIG_ENVIRONMENT "GIT_CONFIG"
#define CONFIG_DATA_ENVIRONMENT "GIT_CONFIG_PARAMETERS"
#define EXEC_PATH_ENVIRONMENT "GIT_EXEC_PATH"
#define CEILING_DIRECTORIES_ENVIRONMENT "GIT_CEILING_DIRECTORIES"
#define NO_REPLACE_OBJECTS_ENVIRONMENT "GIT_NO_REPLACE_OBJECTS"
Expand Down Expand Up @@ -970,7 +971,9 @@ extern int update_server_info(int);
typedef int (*config_fn_t)(const char *, const char *, void *);
extern int git_default_config(const char *, const char *, void *);
extern int git_config_from_file(config_fn_t fn, const char *, void *);
extern void git_config_push_parameter(const char *text);
extern int git_config_parse_parameter(const char *text);
extern int git_config_parse_environment(void);
extern int git_config_from_parameters(config_fn_t fn, void *data);
extern int git_config(config_fn_t fn, void *);
extern int git_parse_ulong(const char *, unsigned long *);
Expand Down
57 changes: 54 additions & 3 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "cache.h"
#include "exec_cmd.h"
#include "strbuf.h"
#include "quote.h"

#define MAXNAME (256)

Expand All @@ -34,6 +35,19 @@ static void lowercase(char *p)
*p = tolower(*p);
}

void git_config_push_parameter(const char *text)
{
struct strbuf env = STRBUF_INIT;
const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
if (old) {
strbuf_addstr(&env, old);
strbuf_addch(&env, ' ');
}
sq_quote_buf(&env, text);
setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1);
strbuf_release(&env);
}

int git_config_parse_parameter(const char *text)
{
struct config_item *ct;
Expand Down Expand Up @@ -61,6 +75,37 @@ int git_config_parse_parameter(const char *text)
return 0;
}

int git_config_parse_environment(void) {
const char *env = getenv(CONFIG_DATA_ENVIRONMENT);
char *envw;
const char **argv = NULL;
int nr = 0, alloc = 0;
int i;

if (!env)
return 0;
/* sq_dequote will write over it */
envw = xstrdup(env);

if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) {
free(envw);
return error("bogus format in " CONFIG_DATA_ENVIRONMENT);
}

for (i = 0; i < nr; i++) {
if (git_config_parse_parameter(argv[i]) < 0) {
error("bogus config parameter: %s", argv[i]);
free(argv);
free(envw);
return -1;
}
}

free(argv);
free(envw);
return 0;
}

static int get_next_char(void)
{
int c;
Expand Down Expand Up @@ -773,7 +818,14 @@ int git_config_global(void)

int git_config_from_parameters(config_fn_t fn, void *data)
{
static int loaded_environment;
const struct config_item *ct;

if (!loaded_environment) {
if (git_config_parse_environment() < 0)
return -1;
loaded_environment = 1;
}
for (ct = config_parameters; ct; ct = ct->next)
if (fn(ct->name, ct->value, data) < 0)
return -1;
Expand Down Expand Up @@ -812,10 +864,9 @@ int git_config(config_fn_t fn, void *data)
}
free(repo_config);

if (config_parameters) {
ret += git_config_from_parameters(fn, data);
ret += git_config_from_parameters(fn, data);
if (config_parameters)
found += 1;
}

if (found == 0)
return -1;
Expand Down
2 changes: 1 addition & 1 deletion git.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
fprintf(stderr, "-c expects a configuration string\n" );
usage(git_usage_string);
}
git_config_parse_parameter((*argv)[1]);
git_config_push_parameter((*argv)[1]);
(*argv)++;
(*argc)--;
} else {
Expand Down

0 comments on commit 2b64fc8

Please sign in to comment.