Skip to content

Commit

Permalink
remote: handle pushremote config in any order
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Feb 24, 2014
1 parent 7bbc4e8 commit 98b406f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static int branches_nr;

static struct branch *current_branch;
static const char *default_remote_name;
static const char *branch_pushremote_name;
static const char *pushremote_name;
static int explicit_default_remote_name;

Expand Down Expand Up @@ -352,7 +353,7 @@ static int handle_config(const char *key, const char *value, void *cb)
}
} else if (!strcmp(subkey, ".pushremote")) {
if (branch == current_branch)
if (git_config_string(&pushremote_name, key, value))
if (git_config_string(&branch_pushremote_name, key, value))
return -1;
} else if (!strcmp(subkey, ".merge")) {
if (!value)
Expand Down Expand Up @@ -492,6 +493,10 @@ static void read_config(void)
make_branch(head_ref + strlen("refs/heads/"), 0);
}
git_config(handle_config, NULL);
if (branch_pushremote_name) {
free((char *)pushremote_name);
pushremote_name = branch_pushremote_name;
}
alias_all_urls();
}

Expand Down
13 changes: 13 additions & 0 deletions t/t5516-fetch-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,19 @@ test_expect_success 'push with config branch.*.pushremote' '
check_push_result down_repo $the_commit heads/master
'

test_expect_success 'branch.*.pushremote config order is irrelevant' '
mk_test one_repo heads/master &&
mk_test two_repo heads/master &&
test_config remote.one.url one_repo &&
test_config remote.two.url two_repo &&
test_config branch.master.pushremote two_repo &&
test_config remote.pushdefault one_repo &&
test_config push.default matching &&
git push &&
check_push_result one_repo $the_first_commit heads/master &&
check_push_result two_repo $the_commit heads/master
'

test_expect_success 'push with dry-run' '
mk_test testrepo heads/master &&
Expand Down

0 comments on commit 98b406f

Please sign in to comment.