branch_get_push: do not segfault when HEAD is detached

Move the detached HEAD check from branch_get_push_1() to branch_get_push() to avoid setting branch->push_tracking_ref when branch is NULL. Signed-off-by: Kyle Meyer <kyle@kyleam.com> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kyle Meyer committed Jan 6, 2017 at 20:12 UTC b10731f43dc21fa47c275052e7c074c686335cd3
2 files changed +9 -3
remote.c
+3 -3
@@ -1717,9 +1717,6 @@ static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)
1717 {
1718 struct remote *remote;
1719
1720 - if (!branch)
1721 - return error_buf(err, _("HEAD does not point to a branch"));
1722 -
1720 remote = remote_get(pushremote_for_branch(branch, NULL));
1721 if (!remote)
1722 return error_buf(err,
@@ -1779,6 +1776,9 @@ static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)
1776
1777 const char *branch_get_push(struct branch *branch, struct strbuf *err)
1778 {
1779 + if (!branch)
1780 + return error_buf(err, _("HEAD does not point to a branch"));
1781 +
1782 if (!branch->push_tracking_ref)
1783 branch->push_tracking_ref = branch_get_push_1(branch, err);
1784 return branch->push_tracking_ref;
t/t1514-rev-parse-push.sh
+6
@@ -60,4 +60,10 @@ test_expect_success '@{push} with push refspecs' '
60 resolve topic@{push} refs/remotes/origin/magic/topic
61 '
62
63 +test_expect_success 'resolving @{push} fails with a detached HEAD' '
64 + git checkout HEAD^0 &&
65 + test_when_finished "git checkout -" &&
66 + test_must_fail git rev-parse @{push}
67 +'
68 +
69 test_done