@@ -1579,10 +1579,35 @@ static const char *strip_refshead(const char *name){
15791579 return name ;
15801580}
15811581
1582- static int set_head (const struct ref * remote_refs )
1582+ static void report_set_head (const char * remote , const char * head_name ,
1583+ struct strbuf * buf_prev , int updateres ) {
1584+ struct strbuf buf_prefix = STRBUF_INIT ;
1585+ const char * prev_head = NULL ;
1586+
1587+ strbuf_addf (& buf_prefix , "refs/remotes/%s/" , remote );
1588+ skip_prefix (buf_prev -> buf , buf_prefix .buf , & prev_head );
1589+
1590+ if (prev_head && strcmp (prev_head , head_name )) {
1591+ printf ("'HEAD' at '%s' is '%s', but we have '%s' locally.\n" ,
1592+ remote , head_name , prev_head );
1593+ printf ("Run 'git remote set-head %s %s' to follow the change.\n" ,
1594+ remote , head_name );
1595+ }
1596+ else if (updateres && buf_prev -> len ) {
1597+ printf ("'HEAD' at '%s' is '%s', "
1598+ "but we have a detached HEAD pointing to '%s' locally.\n" ,
1599+ remote , head_name , buf_prev -> buf );
1600+ printf ("Run 'git remote set-head %s %s' to follow the change.\n" ,
1601+ remote , head_name );
1602+ }
1603+ strbuf_release (& buf_prefix );
1604+ }
1605+
1606+ static int set_head (const struct ref * remote_refs , int follow_remote_head )
15831607{
1584- int result = 0 , is_bare ;
1585- struct strbuf b_head = STRBUF_INIT , b_remote_head = STRBUF_INIT ;
1608+ int result = 0 , create_only , is_bare , was_detached ;
1609+ struct strbuf b_head = STRBUF_INIT , b_remote_head = STRBUF_INIT ,
1610+ b_local_head = STRBUF_INIT ;
15861611 const char * remote = gtransport -> remote -> name ;
15871612 char * head_name = NULL ;
15881613 struct ref * ref , * matches ;
@@ -1603,6 +1628,8 @@ static int set_head(const struct ref *remote_refs)
16031628 string_list_append (& heads , strip_refshead (ref -> name ));
16041629 }
16051630
1631+ if (follow_remote_head < 0 )
1632+ goto cleanup ;
16061633
16071634 if (!heads .nr )
16081635 result = 1 ;
@@ -1614,6 +1641,7 @@ static int set_head(const struct ref *remote_refs)
16141641 if (!head_name )
16151642 goto cleanup ;
16161643 is_bare = repo_is_bare (the_repository );
1644+ create_only = follow_remote_head == 2 ? 0 : !is_bare ;
16171645 if (is_bare ) {
16181646 strbuf_addstr (& b_head , "HEAD" );
16191647 strbuf_addf (& b_remote_head , "refs/heads/%s" , head_name );
@@ -1626,16 +1654,22 @@ static int set_head(const struct ref *remote_refs)
16261654 result = 1 ;
16271655 goto cleanup ;
16281656 }
1629- if (refs_update_symref_extended (refs , b_head .buf , b_remote_head .buf ,
1630- "fetch" , NULL , !is_bare ))
1657+ was_detached = refs_update_symref_extended (refs , b_head .buf , b_remote_head .buf ,
1658+ "fetch" , & b_local_head , create_only );
1659+ if (was_detached == -1 ) {
16311660 result = 1 ;
1661+ goto cleanup ;
1662+ }
1663+ if (follow_remote_head == 1 && verbosity >= 0 )
1664+ report_set_head (remote , head_name , & b_local_head , was_detached );
16321665
16331666cleanup :
16341667 free (head_name );
16351668 free_refs (fetch_map );
16361669 free_refs (matches );
16371670 string_list_clear (& heads , 0 );
16381671 strbuf_release (& b_head );
1672+ strbuf_release (& b_local_head );
16391673 strbuf_release (& b_remote_head );
16401674 return result ;
16411675}
@@ -1855,7 +1889,7 @@ static int do_fetch(struct transport *transport,
18551889 "you need to specify exactly one branch with the --set-upstream option" ));
18561890 }
18571891 }
1858- if (set_head (remote_refs ))
1892+ if (set_head (remote_refs , transport -> remote -> follow_remote_head ))
18591893 ;
18601894 /*
18611895 * Way too many cases where this can go wrong
0 commit comments