File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff 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
96107test_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+
181203test_done
You can’t perform that action at this time.
0 commit comments