@@ -244,7 +244,8 @@ static int add(int argc, const char **argv, const char *prefix)
244244 strbuf_reset (& buf2 );
245245 strbuf_addf (& buf2 , "refs/remotes/%s/%s" , name , master );
246246
247- if (refs_update_symref (get_main_ref_store (the_repository ), buf .buf , buf2 .buf , "remote add" ))
247+ if (refs_update_symref (get_main_ref_store (the_repository ), buf .buf , buf2 .buf ,
248+ "remote add" , NULL , 0 ))
248249 result = error (_ ("Could not setup master '%s'" ), master );
249250 }
250251
@@ -864,7 +865,8 @@ static int mv(int argc, const char **argv, const char *prefix)
864865 strbuf_reset (& buf3 );
865866 strbuf_addf (& buf3 , "remote: renamed %s to %s" ,
866867 item -> string , buf .buf );
867- if (refs_update_symref (get_main_ref_store (the_repository ), buf .buf , buf2 .buf , buf3 .buf ))
868+ if (refs_update_symref (get_main_ref_store (the_repository ), buf .buf , buf2 .buf ,
869+ buf3 .buf , NULL , 0 ))
868870 die (_ ("creating '%s' failed" ), buf .buf );
869871 display_progress (progress , ++ refs_renamed_nr );
870872 }
@@ -1399,11 +1401,36 @@ static int show(int argc, const char **argv, const char *prefix)
13991401 return result ;
14001402}
14011403
1404+ static void report_auto (const char * remote , const char * head_name ,
1405+ struct strbuf * buf_prev ) {
1406+ struct strbuf buf_prefix = STRBUF_INIT ;
1407+ const char * prev_head = NULL ;
1408+
1409+ strbuf_addf (& buf_prefix , "refs/remotes/%s/" , remote );
1410+ skip_prefix (buf_prev -> buf , buf_prefix .buf , & prev_head );
1411+
1412+ if (prev_head && !strcmp (prev_head , head_name ))
1413+ printf ("'%s/HEAD' is unchanged and points to '%s'\n" ,
1414+ remote , head_name );
1415+ else if (prev_head )
1416+ printf ("'%s/HEAD' has changed from '%s' and now points to '%s'\n" ,
1417+ remote , prev_head , head_name );
1418+ else if (buf_prev -> len == 0 )
1419+ printf ("'%s/HEAD' is now created and points to '%s'\n" ,
1420+ remote , head_name );
1421+ else
1422+ printf ("'%s/HEAD' used to point to '%s' "
1423+ "(which is unusual), but now points to '%s'\n" ,
1424+ remote , buf_prev -> buf , head_name );
1425+ }
1426+
14021427static int set_head (int argc , const char * * argv , const char * prefix )
14031428{
14041429 int i , opt_a = 0 , opt_d = 0 , result = 0 ;
1405- struct strbuf buf = STRBUF_INIT , buf2 = STRBUF_INIT ;
1430+ struct strbuf buf = STRBUF_INIT , buf2 = STRBUF_INIT ,
1431+ buf_prev = STRBUF_INIT ;
14061432 char * head_name = NULL ;
1433+ struct ref_store * refs = get_main_ref_store (the_repository );
14071434
14081435 struct option options [] = {
14091436 OPT_BOOL ('a' , "auto" , & opt_a ,
@@ -1434,25 +1461,28 @@ static int set_head(int argc, const char **argv, const char *prefix)
14341461 head_name = xstrdup (states .heads .items [0 ].string );
14351462 free_remote_ref_states (& states );
14361463 } else if (opt_d && !opt_a && argc == 1 ) {
1437- if (refs_delete_ref (get_main_ref_store ( the_repository ) , NULL , buf .buf , NULL , REF_NO_DEREF ))
1464+ if (refs_delete_ref (refs , NULL , buf .buf , NULL , REF_NO_DEREF ))
14381465 result |= error (_ ("Could not delete %s" ), buf .buf );
14391466 } else
14401467 usage_with_options (builtin_remote_sethead_usage , options );
14411468
14421469 if (head_name ) {
14431470 strbuf_addf (& buf2 , "refs/remotes/%s/%s" , argv [0 ], head_name );
14441471 /* make sure it's valid */
1445- if (!refs_ref_exists (get_main_ref_store ( the_repository ) , buf2 .buf ))
1472+ if (!refs_ref_exists (refs , buf2 .buf ))
14461473 result |= error (_ ("Not a valid ref: %s" ), buf2 .buf );
1447- else if (refs_update_symref (get_main_ref_store (the_repository ), buf .buf , buf2 .buf , "remote set-head" ))
1474+ else if (refs_update_symref (refs , buf .buf , buf2 .buf ,
1475+ "remote set-head" , & buf_prev , 0 ))
14481476 result |= error (_ ("Could not setup %s" ), buf .buf );
1449- else if (opt_a )
1450- printf ("%s/HEAD set to %s\n" , argv [0 ], head_name );
1477+ else if (opt_a ) {
1478+ report_auto (argv [0 ], head_name , & buf_prev );
1479+ }
14511480 free (head_name );
14521481 }
14531482
14541483 strbuf_release (& buf );
14551484 strbuf_release (& buf2 );
1485+ strbuf_release (& buf_prev );
14561486 return result ;
14571487}
14581488
0 commit comments