Skip to content

Commit dcaf17c

Browse files
committed
Merge branch 'ab/fetch-set-upstream-while-detached'
"git fetch --set-upstream" did not check if there is a current branch, leading to a segfault when it is run on a detached HEAD, which has been corrected. * ab/fetch-set-upstream-while-detached: pull, fetch: fix segfault in --set-upstream option
2 parents 597af31 + 17baeaf commit dcaf17c

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

builtin/fetch.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,16 @@ static int do_fetch(struct transport *transport,
16431643
}
16441644
}
16451645
if (source_ref) {
1646+
if (!branch) {
1647+
const char *shortname = source_ref->name;
1648+
skip_prefix(shortname, "refs/heads/", &shortname);
1649+
1650+
warning(_("could not set upstream of HEAD to '%s' from '%s' when "
1651+
"it does not point to any branch."),
1652+
shortname, transport->remote->name);
1653+
goto skip;
1654+
}
1655+
16461656
if (!strcmp(source_ref->name, "HEAD") ||
16471657
starts_with(source_ref->name, "refs/heads/"))
16481658
install_branch_config(0,

t/t5553-set-upstream.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ test_expect_success 'fetch --set-upstream with valid URL sets upstream to URL' '
9191
check_config_missing other2
9292
'
9393

94+
test_expect_success 'fetch --set-upstream with a detached HEAD' '
95+
git checkout HEAD^0 &&
96+
test_when_finished "git checkout -" &&
97+
cat >expect <<-\EOF &&
98+
warning: could not set upstream of HEAD to '"'"'main'"'"' from '"'"'upstream'"'"' when it does not point to any branch.
99+
EOF
100+
git fetch --set-upstream upstream main 2>actual.raw &&
101+
grep ^warning: actual.raw >actual &&
102+
test_cmp expect actual
103+
'
104+
94105
# tests for pull --set-upstream
95106

96107
test_expect_success 'setup bare parent pull' '
@@ -178,4 +189,15 @@ test_expect_success 'pull --set-upstream with valid URL and branch sets branch'
178189
check_config_missing other2
179190
'
180191

192+
test_expect_success 'pull --set-upstream with a detached HEAD' '
193+
git checkout HEAD^0 &&
194+
test_when_finished "git checkout -" &&
195+
cat >expect <<-\EOF &&
196+
warning: could not set upstream of HEAD to '"'"'main'"'"' from '"'"'upstream'"'"' when it does not point to any branch.
197+
EOF
198+
git pull --no-rebase --set-upstream upstream main 2>actual.raw &&
199+
grep ^warning: actual.raw >actual &&
200+
test_cmp expect actual
201+
'
202+
181203
test_done

0 commit comments

Comments
 (0)