Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 98b406f

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7bbc4e8 commit 98b406f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

remote.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static int branches_nr;
4949

5050
static struct branch *current_branch;
5151
static const char *default_remote_name;
52+
static const char *branch_pushremote_name;
5253
static const char *pushremote_name;
5354
static int explicit_default_remote_name;
5455

@@ -352,7 +353,7 @@ static int handle_config(const char *key, const char *value, void *cb)
352353
}
353354
} else if (!strcmp(subkey, ".pushremote")) {
354355
if (branch == current_branch)
355-
if (git_config_string(&pushremote_name, key, value))
356+
if (git_config_string(&branch_pushremote_name, key, value))
356357
return -1;
357358
} else if (!strcmp(subkey, ".merge")) {
358359
if (!value)
@@ -492,6 +493,10 @@ static void read_config(void)
492493
make_branch(head_ref + strlen("refs/heads/"), 0);
493494
}
494495
git_config(handle_config, NULL);
496+
if (branch_pushremote_name) {
497+
free((char *)pushremote_name);
498+
pushremote_name = branch_pushremote_name;
499+
}
495500
alias_all_urls();
496501
}
497502

t/t5516-fetch-push.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,19 @@ test_expect_success 'push with config branch.*.pushremote' '
536536
check_push_result down_repo $the_commit heads/master
537537
'
538538

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+
539552
test_expect_success 'push with dry-run' '
540553
541554
mk_test testrepo heads/master &&

0 commit comments

Comments
 (0)