remote: handle pushremote config in any order

The remote we push can be defined either by remote.pushdefault or by branch.*.pushremote for the current branch. The order in which they appear in the config file should not matter to precedence (which should be to prefer the branch-specific config). The current code parses the config linearly and uses a single string to store both values, overwriting any previous value. Thus, config like: [branch "master"] pushremote = foo [remote] pushdefault = bar erroneously ends up pushing to "bar" from the master branch. We can fix this by storing both values and resolving the correct value after all config is read. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 24, 2014 at 03:59 UTC 98b406f3ad6a6989a5b11c2a2582a9f539d66263
2 files changed +19 -1
remote.c
+6 -1
@@ -49,6 +49,7 @@ static int branches_nr;
49
50 static struct branch *current_branch;
51 static const char *default_remote_name;
52 +static const char *branch_pushremote_name;
53 static const char *pushremote_name;
54 static int explicit_default_remote_name;
55
@@ -352,7 +353,7 @@ static int handle_config(const char *key, const char *value, void *cb)
353 }
354 } else if (!strcmp(subkey, ".pushremote")) {
355 if (branch == current_branch)
355 - if (git_config_string(&pushremote_name, key, value))
356 + if (git_config_string(&branch_pushremote_name, key, value))
357 return -1;
358 } else if (!strcmp(subkey, ".merge")) {
359 if (!value)
@@ -492,6 +493,10 @@ static void read_config(void)
493 make_branch(head_ref + strlen("refs/heads/"), 0);
494 }
495 git_config(handle_config, NULL);
496 + if (branch_pushremote_name) {
497 + free((char *)pushremote_name);
498 + pushremote_name = branch_pushremote_name;
499 + }
500 alias_all_urls();
501 }
502
t/t5516-fetch-push.sh
+13
@@ -536,6 +536,19 @@ test_expect_success 'push with config branch.*.pushremote' '
536 check_push_result down_repo $the_commit heads/master
537 '
538
539 +test_expect_success 'branch.*.pushremote config order is irrelevant' '
540 + mk_test one_repo heads/master &&
541 + mk_test two_repo heads/master &&
542 + test_config remote.one.url one_repo &&
543 + test_config remote.two.url two_repo &&
544 + test_config branch.master.pushremote two_repo &&
545 + test_config remote.pushdefault one_repo &&
546 + test_config push.default matching &&
547 + git push &&
548 + check_push_result one_repo $the_first_commit heads/master &&
549 + check_push_result two_repo $the_commit heads/master
550 +'
551 +
552 test_expect_success 'push with dry-run' '
553
554 mk_test testrepo heads/master &&