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,
1643
1643
}
1644
1644
}
1645
1645
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
+
1646
1656
if (!strcmp (source_ref -> name , "HEAD" ) ||
1647
1657
starts_with (source_ref -> name , "refs/heads/" ))
1648
1658
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' '
91
91
check_config_missing other2
92
92
'
93
93
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
+
94
105
# tests for pull --set-upstream
95
106
96
107
test_expect_success ' setup bare parent pull' '
@@ -178,4 +189,15 @@ test_expect_success 'pull --set-upstream with valid URL and branch sets branch'
178
189
check_config_missing other2
179
190
'
180
191
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
+
181
203
test_done
You can’t perform that action at this time.
0 commit comments