Fix setting config variables with an alternative GIT_CONFIG
When setting a config variable, git_config_set() ignored the variables GIT_CONFIG and GIT_CONFIG_LOCAL. Now, when GIT_CONFIG_LOCAL is set, it will write to that file. If not, GIT_CONFIG is checked, and only as a fallback, the change is written to $GIT_DIR/config. Add a test for it, and also future-proof the test for the upcoming $HOME/.gitconfig support. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
Jun 20, 2006 at 00:51 UTC
9c3796fc0474ac6fc77da4886a246a37a7fbe856
3 files changed
+37
-4
config.c
+12
-3
@@ -500,10 +500,19 @@ int git_config_set_multivar(const char* key, const char* value,
500
int i, dot;
501
int fd = -1, in_fd;
502
int ret;
503
- char* config_filename = strdup(git_path("config"));
504
- char* lock_file = strdup(git_path("config.lock"));
503
+ char* config_filename;
504
+ char* lock_file;
505
const char* last_dot = strrchr(key, '.');
506
507
+ config_filename = getenv("GIT_CONFIG");
508
+ if (!config_filename) {
509
+ config_filename = getenv("GIT_CONFIG_LOCAL");
510
+ if (!config_filename)
511
+ config_filename = git_path("config");
512
+ }
513
+ config_filename = strdup(config_filename);
514
+ lock_file = strdup(mkpath("%s.lock", config_filename));
515
+
516
/*
517
* Since "key" actually contains the section name and the real
518
* key name separated by a dot, we have to know where the dot is.
@@ -610,7 +619,7 @@ int git_config_set_multivar(const char* key, const char* value,
619
* As a side effect, we make sure to transform only a valid
620
* existing config file.
621
*/
613
- if (git_config(store_aux)) {
622
+ if (git_config_from_file(store_aux, config_filename)) {
623
fprintf(stderr, "invalid config file\n");
624
free(store.key);
625
if (store.value_regex != NULL) {
t/Makefile
+1
-1
@@ -19,7 +19,7 @@ endif
19
all: $(T) clean
20
21
$(T):
22
- @echo "*** $@ ***"; '$(SHELL_PATH_SQ)' $@ $(GIT_TEST_OPTS)
22
+ @echo "*** $@ ***"; GIT_CONFIG=.git/config '$(SHELL_PATH_SQ)' $@ $(GIT_TEST_OPTS)
23
24
clean:
25
rm -fr trash
t/t1300-repo-config.sh
+24
@@ -309,5 +309,29 @@ EOF
309
310
test_expect_success 'new variable inserts into proper section' 'cmp .git/config expect'
311
312
+cat > other-config << EOF
313
+[ein]
314
+ bahn = strasse
315
+EOF
316
+
317
+cat > expect << EOF
318
+ein.bahn=strasse
319
+EOF
320
+
321
+GIT_CONFIG=other-config git-repo-config -l > output
322
+
323
+test_expect_success 'alternative GIT_CONFIG' 'cmp output expect'
324
+
325
+GIT_CONFIG=other-config git-repo-config anwohner.park ausweis
326
+
327
+cat > expect << EOF
328
+[ein]
329
+ bahn = strasse
330
+[anwohner]
331
+ park = ausweis
332
+EOF
333
+
334
+test_expect_success '--set in alternative GIT_CONFIG' 'cmp other-config expect'
335
+
336
test_done
337