@@ -1577,6 +1577,66 @@ static int backfill_tags(struct display_state *display_state,
15771577 return retcode ;
15781578}
15791579
1580+ static const char * strip_refshead (const char * name ){
1581+ skip_prefix (name , "refs/heads/" , & name );
1582+ return name ;
1583+ }
1584+
1585+ static int set_head (const struct ref * remote_refs )
1586+ {
1587+ int result = 0 ;
1588+ struct strbuf b_head = STRBUF_INIT , b_remote_head = STRBUF_INIT ;
1589+ const char * remote = gtransport -> remote -> name ;
1590+ char * head_name = NULL ;
1591+ struct ref * ref , * matches ;
1592+ struct ref * fetch_map = NULL , * * fetch_map_tail = & fetch_map ;
1593+ struct refspec_item refspec = {
1594+ .force = 0 ,
1595+ .pattern = 1 ,
1596+ .src = (char * ) "refs/heads/*" ,
1597+ .dst = (char * ) "refs/heads/*" ,
1598+ };
1599+ struct string_list heads = STRING_LIST_INIT_DUP ;
1600+ struct ref_store * refs = get_main_ref_store (the_repository );
1601+
1602+ get_fetch_map (remote_refs , & refspec , & fetch_map_tail , 0 );
1603+ matches = guess_remote_head (find_ref_by_name (remote_refs , "HEAD" ),
1604+ fetch_map , 1 );
1605+ for (ref = matches ; ref ; ref = ref -> next ) {
1606+ string_list_append (& heads , strip_refshead (ref -> name ));
1607+ }
1608+
1609+
1610+ if (!heads .nr )
1611+ result = 1 ;
1612+ else if (heads .nr > 1 )
1613+ result = 1 ;
1614+ else
1615+ head_name = xstrdup (heads .items [0 ].string );
1616+
1617+ if (!head_name )
1618+ goto cleanup ;
1619+ strbuf_addf (& b_head , "refs/remotes/%s/HEAD" , remote );
1620+ strbuf_addf (& b_remote_head , "refs/remotes/%s/%s" , remote , head_name );
1621+ /* make sure it's valid */
1622+ if (!refs_ref_exists (refs , b_remote_head .buf )) {
1623+ result = 1 ;
1624+ goto cleanup ;
1625+ }
1626+ if (refs_update_symref_extended (refs , b_head .buf , b_remote_head .buf ,
1627+ "fetch" , NULL , 1 ))
1628+ result = 1 ;
1629+
1630+ cleanup :
1631+ free (head_name );
1632+ free_refs (fetch_map );
1633+ free_refs (matches );
1634+ string_list_clear (& heads , 0 );
1635+ strbuf_release (& b_head );
1636+ strbuf_release (& b_remote_head );
1637+ return result ;
1638+ }
1639+
15801640static int do_fetch (struct transport * transport ,
15811641 struct refspec * rs ,
15821642 const struct fetch_config * config )
@@ -1646,6 +1706,8 @@ static int do_fetch(struct transport *transport,
16461706 "refs/tags/" );
16471707 }
16481708
1709+ strvec_push (& transport_ls_refs_options .ref_prefixes , "HEAD" );
1710+
16491711 if (must_list_refs ) {
16501712 trace2_region_enter ("fetch" , "remote_refs" , the_repository );
16511713 remote_refs = transport_get_remote_refs (transport ,
@@ -1790,6 +1852,12 @@ static int do_fetch(struct transport *transport,
17901852 "you need to specify exactly one branch with the --set-upstream option" ));
17911853 }
17921854 }
1855+ if (set_head (remote_refs ))
1856+ ;
1857+ /*
1858+ * Way too many cases where this can go wrong
1859+ * so let's just fail silently for now.
1860+ */
17931861
17941862cleanup :
17951863 if (retcode ) {
0 commit comments