Skip to content

Commit b10731f

Browse files
kyleamgitster
authored andcommitted
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 <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0b65a8d commit b10731f

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

remote.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,9 +1717,6 @@ static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)
17171717
{
17181718
struct remote *remote;
17191719

1720-
if (!branch)
1721-
return error_buf(err, _("HEAD does not point to a branch"));
1722-
17231720
remote = remote_get(pushremote_for_branch(branch, NULL));
17241721
if (!remote)
17251722
return error_buf(err,
@@ -1779,6 +1776,9 @@ static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)
17791776

17801777
const char *branch_get_push(struct branch *branch, struct strbuf *err)
17811778
{
1779+
if (!branch)
1780+
return error_buf(err, _("HEAD does not point to a branch"));
1781+
17821782
if (!branch->push_tracking_ref)
17831783
branch->push_tracking_ref = branch_get_push_1(branch, err);
17841784
return branch->push_tracking_ref;

t/t1514-rev-parse-push.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,10 @@ test_expect_success '@{push} with push refspecs' '
6060
resolve topic@{push} refs/remotes/origin/magic/topic
6161
'
6262

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+
6369
test_done

0 commit comments

Comments
 (0)