@@ -1579,10 +1579,47 @@ 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 set_head_advice_msg (const char * remote , const char * head_name )
15831583{
1584- int result = 0 , is_bare ;
1585- struct strbuf b_head = STRBUF_INIT , b_remote_head = STRBUF_INIT ;
1584+ const char message_advice_set_head [] =
1585+ N_ ("Run 'git remote set-head %s %s' to follow the change, or set\n"
1586+ "'remote.%s.followRemoteHEAD' configuration option to a different value\n"
1587+ "if you do not want to see this message. Specifically running\n"
1588+ "'git config set remote.%s.followRemoteHEAD %s' will disable the warning\n"
1589+ "until the remote changes HEAD to something else." );
1590+
1591+ advise_if_enabled (ADVICE_FETCH_SET_HEAD_WARN , _ (message_advice_set_head ),
1592+ remote , head_name , remote , remote , head_name );
1593+ }
1594+
1595+ static void report_set_head (const char * remote , const char * head_name ,
1596+ struct strbuf * buf_prev , int updateres ) {
1597+ struct strbuf buf_prefix = STRBUF_INIT ;
1598+ const char * prev_head = NULL ;
1599+
1600+ strbuf_addf (& buf_prefix , "refs/remotes/%s/" , remote );
1601+ skip_prefix (buf_prev -> buf , buf_prefix .buf , & prev_head );
1602+
1603+ if (prev_head && strcmp (prev_head , head_name )) {
1604+ printf ("'HEAD' at '%s' is '%s', but we have '%s' locally.\n" ,
1605+ remote , head_name , prev_head );
1606+ set_head_advice_msg (remote , head_name );
1607+ }
1608+ else if (updateres && buf_prev -> len ) {
1609+ printf ("'HEAD' at '%s' is '%s', "
1610+ "but we have a detached HEAD pointing to '%s' locally.\n" ,
1611+ remote , head_name , buf_prev -> buf );
1612+ set_head_advice_msg (remote , head_name );
1613+ }
1614+ strbuf_release (& buf_prefix );
1615+ }
1616+
1617+ static int set_head (const struct ref * remote_refs , int follow_remote_head ,
1618+ const char * no_warn_branch )
1619+ {
1620+ int result = 0 , create_only , is_bare , was_detached ;
1621+ struct strbuf b_head = STRBUF_INIT , b_remote_head = STRBUF_INIT ,
1622+ b_local_head = STRBUF_INIT ;
15861623 const char * remote = gtransport -> remote -> name ;
15871624 char * head_name = NULL ;
15881625 struct ref * ref , * matches ;
@@ -1603,6 +1640,8 @@ static int set_head(const struct ref *remote_refs)
16031640 string_list_append (& heads , strip_refshead (ref -> name ));
16041641 }
16051642
1643+ if (follow_remote_head == FOLLOW_REMOTE_NEVER )
1644+ goto cleanup ;
16061645
16071646 if (!heads .nr )
16081647 result = 1 ;
@@ -1614,6 +1653,7 @@ static int set_head(const struct ref *remote_refs)
16141653 if (!head_name )
16151654 goto cleanup ;
16161655 is_bare = is_bare_repository ();
1656+ create_only = follow_remote_head == FOLLOW_REMOTE_ALWAYS ? 0 : !is_bare ;
16171657 if (is_bare ) {
16181658 strbuf_addstr (& b_head , "HEAD" );
16191659 strbuf_addf (& b_remote_head , "refs/heads/%s" , head_name );
@@ -1626,16 +1666,24 @@ static int set_head(const struct ref *remote_refs)
16261666 result = 1 ;
16271667 goto cleanup ;
16281668 }
1629- if (refs_update_symref_extended (refs , b_head .buf , b_remote_head .buf ,
1630- "fetch" , NULL , !is_bare ))
1669+ was_detached = refs_update_symref_extended (refs , b_head .buf , b_remote_head .buf ,
1670+ "fetch" , & b_local_head , create_only );
1671+ if (was_detached == -1 ) {
16311672 result = 1 ;
1673+ goto cleanup ;
1674+ }
1675+ if (verbosity >= 0 &&
1676+ follow_remote_head == FOLLOW_REMOTE_WARN &&
1677+ (!no_warn_branch || strcmp (no_warn_branch , head_name )))
1678+ report_set_head (remote , head_name , & b_local_head , was_detached );
16321679
16331680cleanup :
16341681 free (head_name );
16351682 free_refs (fetch_map );
16361683 free_refs (matches );
16371684 string_list_clear (& heads , 0 );
16381685 strbuf_release (& b_head );
1686+ strbuf_release (& b_local_head );
16391687 strbuf_release (& b_remote_head );
16401688 return result ;
16411689}
@@ -1873,7 +1921,8 @@ static int do_fetch(struct transport *transport,
18731921 "you need to specify exactly one branch with the --set-upstream option" ));
18741922 }
18751923 }
1876- if (set_head (remote_refs ))
1924+ if (set_head (remote_refs , transport -> remote -> follow_remote_head ,
1925+ transport -> remote -> no_warn_branch ))
18771926 ;
18781927 /*
18791928 * Way too many cases where this can go wrong
0 commit comments