push: also use "upstream" mapping when pushing a single ref

When the user is using the 'upstream' mode, these commands: $ git push $ git push origin would find the 'upstream' branch for the current branch, and then push the current branch to update it. However, pushing a single branch explicitly, i.e. $ git push origin $(git symbolic-ref --short HEAD) would not go through the same ref mapping process, and ends up updating the branch at 'origin' of the same name, which may not necessarily be the upstream of the branch being pushed. In the spirit similar to the previous one, map a colon-less refspec using the upstream mapping logic. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Dec 3, 2013 at 16:23 UTC fc9261ca611080b1dae76b86b3bf5f36d592042f
2 files changed +41
builtin/push.c
+11
@@ -58,6 +58,17 @@ static const char *map_refspec(const char *ref,
58 }
59 }
60
61 + if (push_default == PUSH_DEFAULT_UPSTREAM &&
62 + !prefixcmp(matched->name, "refs/heads/")) {
63 + struct branch *branch = branch_get(matched->name + 11);
64 + if (branch->merge_nr == 1 && branch->merge[0]->src) {
65 + struct strbuf buf = STRBUF_INIT;
66 + strbuf_addf(&buf, "%s:%s",
67 + ref, branch->merge[0]->src);
68 + return strbuf_detach(&buf, NULL);
69 + }
70 + }
71 +
72 return ref;
73 }
74
t/t5516-fetch-push.sh
+30
@@ -1160,6 +1160,7 @@ test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1160 git pull ../testrepo master &&
1161 git branch next &&
1162 git config remote.dst.url ../dst &&
1163 + git config push.default matching &&
1164 git push dst master &&
1165 git show-ref refs/heads/master >../dst/expect
1166 ) &&
@@ -1171,6 +1172,35 @@ test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1172 test_cmp dst/expect dst/actual
1173 '
1174
1175 +test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1176 + mk_test testrepo heads/master &&
1177 + rm -fr src dst &&
1178 + git init src &&
1179 + git init --bare dst &&
1180 + (
1181 + cd src &&
1182 + git pull ../testrepo master &&
1183 + git branch next &&
1184 + git config remote.dst.url ../dst &&
1185 + git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1186 + git config push.default upstream &&
1187 +
1188 + git config branch.master.merge refs/heads/trunk &&
1189 + git config branch.master.remote dst &&
1190 +
1191 + git push dst master &&
1192 + git show-ref refs/heads/master |
1193 + sed -e "s|refs/heads/master|refs/heads/trunk|" >../dst/expect
1194 + ) &&
1195 + (
1196 + cd dst &&
1197 + test_must_fail git show-ref refs/heads/master &&
1198 + test_must_fail git show-ref refs/heads/next &&
1199 + git show-ref refs/heads/trunk >actual
1200 + ) &&
1201 + test_cmp dst/expect dst/actual
1202 +'
1203 +
1204 test_expect_success 'push does not follow tags by default' '
1205 mk_test testrepo heads/master &&
1206 rm -fr src dst &&