2626 TAGS_SET = 2
2727};
2828
29- static int all , append , force , keep , multiple , update_head_ok , verbosity ;
29+ static int all , append , force , keep , multiple , prune , update_head_ok , verbosity ;
3030static int tags = TAGS_DEFAULT ;
3131static const char * depth ;
3232static const char * upload_pack ;
@@ -49,6 +49,8 @@ static struct option builtin_fetch_options[] = {
4949 "fetch all tags and associated objects" , TAGS_SET ),
5050 OPT_SET_INT ('n' , NULL , & tags ,
5151 "do not fetch all tags (--no-tags)" , TAGS_UNSET ),
52+ OPT_BOOLEAN ('p' , "prune" , & prune ,
53+ "prune tracking branches no longer on remote" ),
5254 OPT_BOOLEAN ('k' , "keep" , & keep , "keep downloaded pack" ),
5355 OPT_BOOLEAN ('u' , "update-head-ok" , & update_head_ok ,
5456 "allow updating of HEAD ref" ),
@@ -492,6 +494,28 @@ static int fetch_refs(struct transport *transport, struct ref *ref_map)
492494 return ret ;
493495}
494496
497+ static int prune_refs (struct transport * transport , struct ref * ref_map )
498+ {
499+ int result = 0 ;
500+ struct ref * ref , * stale_refs = get_stale_heads (transport -> remote , ref_map );
501+ const char * dangling_msg = dry_run
502+ ? " (%s will become dangling)\n"
503+ : " (%s has become dangling)\n" ;
504+
505+ for (ref = stale_refs ; ref ; ref = ref -> next ) {
506+ if (!dry_run )
507+ result |= delete_ref (ref -> name , NULL , 0 );
508+ if (verbosity >= 0 ) {
509+ fprintf (stderr , " x %-*s %-*s -> %s\n" ,
510+ SUMMARY_WIDTH , "[deleted]" ,
511+ REFCOL_WIDTH , "(none)" , prettify_refname (ref -> name ));
512+ warn_dangling_symref (stderr , dangling_msg , ref -> name );
513+ }
514+ }
515+ free_refs (stale_refs );
516+ return result ;
517+ }
518+
495519static int add_existing (const char * refname , const unsigned char * sha1 ,
496520 int flag , void * cbdata )
497521{
@@ -616,6 +640,8 @@ static int do_fetch(struct transport *transport,
616640 free_refs (ref_map );
617641 return 1 ;
618642 }
643+ if (prune )
644+ prune_refs (transport , ref_map );
619645 free_refs (ref_map );
620646
621647 /* if neither --no-tags nor --tags was specified, do automated tag
@@ -699,9 +725,11 @@ static int add_remote_or_group(const char *name, struct string_list *list)
699725static int fetch_multiple (struct string_list * list )
700726{
701727 int i , result = 0 ;
702- const char * argv [] = { "fetch" , NULL , NULL , NULL , NULL };
728+ const char * argv [] = { "fetch" , NULL , NULL , NULL , NULL , NULL };
703729 int argc = 1 ;
704730
731+ if (prune )
732+ argv [argc ++ ] = "--prune" ;
705733 if (verbosity >= 2 )
706734 argv [argc ++ ] = "-v" ;
707735 if (verbosity >= 1 )
0 commit comments