Skip to content

Commit fc9261c

Browse files
committed
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>
1 parent ca02465 commit fc9261c

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

builtin/push.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ static const char *map_refspec(const char *ref,
5858
}
5959
}
6060

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+
6172
return ref;
6273
}
6374

t/t5516-fetch-push.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,7 @@ test_expect_success 'with no remote.$name.push, it is not used as refmap' '
11601160
git pull ../testrepo master &&
11611161
git branch next &&
11621162
git config remote.dst.url ../dst &&
1163+
git config push.default matching &&
11631164
git push dst master &&
11641165
git show-ref refs/heads/master >../dst/expect
11651166
) &&
@@ -1171,6 +1172,35 @@ test_expect_success 'with no remote.$name.push, it is not used as refmap' '
11711172
test_cmp dst/expect dst/actual
11721173
'
11731174

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+
11741204
test_expect_success 'push does not follow tags by default' '
11751205
mk_test testrepo heads/master &&
11761206
rm -fr src dst &&

0 commit comments

Comments
 (0)